Skip to content

Commit 3476f51

Browse files
crisbetommalerba
authored andcommitted
fix(dialog): don't move focus if it was moved during close ani… (#17300)
Currently we restore focus to the previously-focused element once the dialog's exit animation has completed, however this can override any focus management the consumer might have done while the animation was running. These changes add extra check so that we only move focus if it was inside the dialog at the end of the animation. Fixes #17296.
1 parent 34e848f commit 3476f51

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/material/dialog/dialog-container.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export function throwMatDialogContentAlreadyAttachedError() {
7171
},
7272
})
7373
export class MatDialogContainer extends BasePortalOutlet {
74+
private _document: Document;
75+
7476
/** The portal outlet inside of this container into which the dialog content will be loaded. */
7577
@ViewChild(CdkPortalOutlet, {static: true}) _portalOutlet: CdkPortalOutlet;
7678

@@ -96,12 +98,13 @@ export class MatDialogContainer extends BasePortalOutlet {
9698
private _elementRef: ElementRef,
9799
private _focusTrapFactory: FocusTrapFactory,
98100
private _changeDetectorRef: ChangeDetectorRef,
99-
@Optional() @Inject(DOCUMENT) private _document: any,
101+
@Optional() @Inject(DOCUMENT) _document: any,
100102
/** The dialog configuration. */
101103
public _config: MatDialogConfig) {
102104

103105
super();
104106
this._ariaLabelledBy = _config.ariaLabelledBy || null;
107+
this._document = _document;
105108
}
106109

107110
/**
@@ -163,7 +166,17 @@ export class MatDialogContainer extends BasePortalOutlet {
163166

164167
// We need the extra check, because IE can set the `activeElement` to null in some cases.
165168
if (this._config.restoreFocus && toFocus && typeof toFocus.focus === 'function') {
166-
toFocus.focus();
169+
const activeElement = this._document.activeElement;
170+
const element = this._elementRef.nativeElement;
171+
172+
// Make sure that focus is still inside the dialog or is on the body (usually because a
173+
// non-focusable element like the backdrop was clicked) before moving it. It's possible that
174+
// the consumer moved it themselves before the animation was done, in which case we shouldn't
175+
// do anything.
176+
if (!activeElement || activeElement === this._document.body || activeElement === element ||
177+
element.contains(activeElement)) {
178+
toFocus.focus();
179+
}
167180
}
168181

169182
if (this._focusTrap) {

src/material/dialog/dialog.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,43 @@ describe('MatDialog', () => {
11481148
document.body.removeChild(button);
11491149
}));
11501150

1151+
it('should not move focus if it was moved outside the dialog while animating', fakeAsync(() => {
1152+
// Create a element that has focus before the dialog is opened.
1153+
const button = document.createElement('button');
1154+
const otherButton = document.createElement('button');
1155+
const body = document.body;
1156+
button.id = 'dialog-trigger';
1157+
otherButton.id = 'other-button';
1158+
body.appendChild(button);
1159+
body.appendChild(otherButton);
1160+
button.focus();
1161+
1162+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef });
1163+
1164+
flushMicrotasks();
1165+
viewContainerFixture.detectChanges();
1166+
flushMicrotasks();
1167+
1168+
expect(document.activeElement!.id)
1169+
.not.toBe('dialog-trigger', 'Expected the focus to change when dialog was opened.');
1170+
1171+
// Start the closing sequence and move focus out of dialog.
1172+
dialogRef.close();
1173+
otherButton.focus();
1174+
1175+
expect(document.activeElement!.id)
1176+
.toBe('other-button', 'Expected focus to be on the alternate button.');
1177+
1178+
flushMicrotasks();
1179+
viewContainerFixture.detectChanges();
1180+
flush();
1181+
1182+
expect(document.activeElement!.id)
1183+
.toBe('other-button', 'Expected focus to stay on the alternate button.');
1184+
1185+
body.removeChild(button);
1186+
body.removeChild(otherButton);
1187+
}));
11511188

11521189
});
11531190

0 commit comments

Comments
 (0)