Skip to content

Commit 81aa654

Browse files
committed
fix(dialog): don't move focus if it was moved during close animation
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 473d4c6 commit 81aa654

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/e2e-app/dialog/dialog-e2e.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
<button id="disabled" (click)="openDisabled()">DISABLED</button>
33
<button id="template" (click)="openTemplate()">TEMPLATE</button>
44

5-
<ng-template><div class="my-template-dialog">my template dialog</div></ng-template>
5+
<ng-template>
6+
<div class="my-template-dialog">my template dialog</div>
7+
<button>Dummy button</button>
8+
</ng-template>

src/material/dialog/dialog-container.ts

Lines changed: 12 additions & 4 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
/**
@@ -160,9 +163,14 @@ export class MatDialogContainer extends BasePortalOutlet {
160163
/** Restores focus to the element that was focused before the dialog opened. */
161164
private _restoreFocus() {
162165
const toFocus = this._elementFocusedBeforeDialogWasOpened;
163-
164-
// We need the extra check, because IE can set the `activeElement` to null in some cases.
165-
if (this._config.restoreFocus && toFocus && typeof toFocus.focus === 'function') {
166+
const activeElement = this._document.activeElement;
167+
168+
// Make sure that focus is still inside the dialog before moving it. It's possible that the
169+
// consumer moved it themselves before the animation was done, in which case we shouldn't
170+
// do anything. Also note that we need the extra check, because IE can set the `activeElement`
171+
// to null in some cases.
172+
if ((!activeElement || this._elementRef.nativeElement.contains(activeElement)) &&
173+
this._config.restoreFocus && toFocus && typeof toFocus.focus === 'function') {
166174
toFocus.focus();
167175
}
168176

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)