Skip to content

MdDialogRef.afterClosed() emits before dialog refocuses on an underlying element #3890

Closed

Description

@benelliott

Bug, feature request, or proposal:

The MdDialogRef.afterClosed() observable emits an event before the MdDialogContainer has restored focus to an underlying element. This can cause problems if subscriptions to MdDialogRef.afterClosed() involve triggering focus events themselves.

This happens because MdDialogContainer waits for the NgZone's onMicrotaskEmpty to emit before executing its refocus operation.

What is the expected behavior?

If this is the intended behaviour then I believe it should be made explicit in the documentation. Otherwise, the MdDialogRef.afterClosed() observable should only emit once all dialog cleanup has been completed.

Currently, the documentation says:

afterClosed - Gets an observable that is notified when the dialog is finished closing.

What is the current behavior?

See above

What are the steps to reproduce?

See this plunker. AppComponent wants to focus its input element after the dialog has closed but this event is overwritten by MdDialogContainer, which restores focus to the button element.

What is the use-case or motivation for changing an existing behavior?

The contract with MdDialogRef.afterClosed() should be clear.

I am using MdDialog within the context of an application that refocuses elements in a non-trivial way so it would not be possible to control this behaviour through tabindex attributes.

Which versions of Angular, Material, OS, browsers are affected?

Tested on Angular 4.0.1/Material head f40296e/Windows 7/Chrome 57

Is there anything else we should know?

The workaround I am using is to perform the focus event inside another NgZone.onMicrotaskEmpty subscription:

this.modal.afterClosed().subscribe(() => {
    this.ngZone.onMicrotaskEmpty.first().subscribe(() => {
        // Focus operation here
    });
});

Activity

  1. changed the title [-]`MdDialogRef.afterClosed()` emits before dialog is fully closed[/-] [+]`MdDialogRef.afterClosed()` emits before dialog refocuses on an underlying element[/+] on Apr 4, 2017
  2. self-assigned this
    on Apr 4, 2017
  3. crisbeto commented on Apr 25, 2017

    @crisbeto
    Member

    Sounds weird @dbandstra, I'll take a look. Also, if you want to close all your open dialogs, you can use MdDialog.closeAll.

  4. crisbeto commented on Apr 29, 2017

    @crisbeto
    Member

    I wasn't able to reproduce your issue @dbandstra. Here's what I tried:

    openDialogs() {
      this.dialogRef = this.dialog.open(JazzDialog, {
        data: { message: 'dialog 1' }
      });
    
      this.otherDialogRef = this.dialog.open(JazzDialog, {
        data: { message: 'dialog 2' }
      });
    
      this.otherDialogRef.afterClosed().subscribe(() => {
        this.dialogRef.close();
      });
    }

    Can you post a Plunkr that shows it? Also I'll close this issue since it should've been fixed by #3875.

  5. crisbeto commented on May 4, 2017

    @crisbeto
    Member

    Thanks, that's definitely a bug. I'll look into it.

  6. crisbeto commented on May 8, 2017

    @crisbeto
    Member

    The issue seems to be with the animations module @dbandstra. You can work around it either by using closeAll to close all of the dialogs, or wrapping the final dialogRef.close call in a setTimeout.

  7. angular-automatic-lock-bot commented on Sep 5, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions