Skip to content

Commit 94d6f88

Browse files
committed
fixup! fix(material/dialog): don't wait for animation before moving focus
1 parent 69f9c63 commit 94d6f88

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

src/material/dialog/dialog-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export class MatDialogConfig<D = any> {
110110
*/
111111
restoreFocus?: boolean = true;
112112

113+
/** Whether to wait for the opening animation to finish before trapping focus. */
114+
delayFocusTrap?: boolean = true;
115+
113116
/** Scroll strategy to be used for the dialog. */
114117
scrollStrategy?: ScrollStrategy;
115118

src/material/dialog/dialog-container.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
117117
if (this._document) {
118118
this._elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom();
119119
}
120-
121-
this._trapFocus();
122120
}
123121

124122
/**
@@ -327,6 +325,10 @@ export class MatDialogContainer extends _MatDialogContainerBase {
327325
_onAnimationDone({toState, totalTime}: AnimationEvent) {
328326
if (toState === 'enter') {
329327
this._animationStateChanged.next({state: 'opened', totalTime});
328+
329+
if (this._config.delayFocusTrap) {
330+
this._trapFocus();
331+
}
330332
} else if (toState === 'exit') {
331333
this._restoreFocus();
332334
this._animationStateChanged.next({state: 'closed', totalTime});
@@ -350,4 +352,12 @@ export class MatDialogContainer extends _MatDialogContainerBase {
350352
// view container is using OnPush change detection.
351353
this._changeDetectorRef.markForCheck();
352354
}
355+
356+
override _initializeWithAttachedContent() {
357+
super._initializeWithAttachedContent();
358+
359+
if (!this._config.delayFocusTrap) {
360+
this._trapFocus();
361+
}
362+
}
353363
}

src/material/dialog/dialog.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ describe('MatDialog', () => {
13881388

13891389
tick(500);
13901390
viewContainerFixture.detectChanges();
1391-
expect(lastFocusOrigin).toBe(null);
1391+
expect(lastFocusOrigin!).toBe('program');
13921392

13931393
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
13941394

@@ -1421,7 +1421,7 @@ describe('MatDialog', () => {
14211421

14221422
tick(500);
14231423
viewContainerFixture.detectChanges();
1424-
expect(lastFocusOrigin).toBe(null);
1424+
expect(lastFocusOrigin!).toBe('program');
14251425

14261426
const backdrop = overlayContainerElement.querySelector(
14271427
'.cdk-overlay-backdrop',

tools/public_api_guard/material/dialog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export class MatDialogConfig<D = any> {
165165
closeOnNavigation?: boolean;
166166
componentFactoryResolver?: ComponentFactoryResolver;
167167
data?: D | null;
168+
delayFocusTrap?: boolean;
168169
direction?: Direction;
169170
disableClose?: boolean;
170171
hasBackdrop?: boolean;
@@ -185,6 +186,8 @@ export class MatDialogConfig<D = any> {
185186

186187
// @public
187188
export class MatDialogContainer extends _MatDialogContainerBase {
189+
// (undocumented)
190+
_initializeWithAttachedContent(): void;
188191
_onAnimationDone({ toState, totalTime }: AnimationEvent_2): void;
189192
_onAnimationStart({ toState, totalTime }: AnimationEvent_2): void;
190193
_startExitAnimation(): void;

0 commit comments

Comments
 (0)