Skip to content

Commit 2e047b4

Browse files
crisbetommalerba
authored andcommitted
fix(dialog): not moving focus to container if autoFocus is disabled and focus was moved from a different component (#16221)
Given an example where we have a `mat-menu` that opens a dialog with `autoFocus = false`, the user's focus will end up on the menu's trigger, rather than the dialog container. This is due to the fact that we move focus to the dialog container immediately when the opening sequence starts and we assume that it's going to stay there. This isn't a problem when `autoFocus` is enabled, because we also try to move focus once the animation is done. These changes add an extra call after the animation finishes to ensure that the container has focus. Fixes #16215.
1 parent cfc3373 commit 2e047b4

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/material/dialog/dialog-container.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ export class MatDialogContainer extends BasePortalOutlet {
141141
// wait for the microtask queue to be empty.
142142
if (this._config.autoFocus) {
143143
this._focusTrap.focusInitialElementWhenReady();
144+
} else {
145+
// Otherwise ensure that focus is on the dialog container. It's possible that a different
146+
// component tried to move focus while the open animation was running. See:
147+
// https://github.com/angular/components/issues/16215
148+
this._elementRef.nativeElement.focus();
144149
}
145150
}
146151

0 commit comments

Comments
 (0)