Skip to content

Commit

Permalink
fixup! fix(material/dialog): don't wait for animation before moving f…
Browse files Browse the repository at this point in the history
…ocus
  • Loading branch information
crisbeto committed Mar 4, 2022
1 parent 69f9c63 commit 94d6f88
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/material/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class MatDialogConfig<D = any> {
*/
restoreFocus?: boolean = true;

/** Whether to wait for the opening animation to finish before trapping focus. */
delayFocusTrap?: boolean = true;

/** Scroll strategy to be used for the dialog. */
scrollStrategy?: ScrollStrategy;

Expand Down
14 changes: 12 additions & 2 deletions src/material/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
if (this._document) {
this._elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom();
}

this._trapFocus();
}

/**
Expand Down Expand Up @@ -327,6 +325,10 @@ export class MatDialogContainer extends _MatDialogContainerBase {
_onAnimationDone({toState, totalTime}: AnimationEvent) {
if (toState === 'enter') {
this._animationStateChanged.next({state: 'opened', totalTime});

if (this._config.delayFocusTrap) {
this._trapFocus();
}
} else if (toState === 'exit') {
this._restoreFocus();
this._animationStateChanged.next({state: 'closed', totalTime});
Expand All @@ -350,4 +352,12 @@ export class MatDialogContainer extends _MatDialogContainerBase {
// view container is using OnPush change detection.
this._changeDetectorRef.markForCheck();
}

override _initializeWithAttachedContent() {
super._initializeWithAttachedContent();

if (!this._config.delayFocusTrap) {
this._trapFocus();
}
}
}
4 changes: 2 additions & 2 deletions src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ describe('MatDialog', () => {

tick(500);
viewContainerFixture.detectChanges();
expect(lastFocusOrigin).toBe(null);
expect(lastFocusOrigin!).toBe('program');

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

Expand Down Expand Up @@ -1421,7 +1421,7 @@ describe('MatDialog', () => {

tick(500);
viewContainerFixture.detectChanges();
expect(lastFocusOrigin).toBe(null);
expect(lastFocusOrigin!).toBe('program');

const backdrop = overlayContainerElement.querySelector(
'.cdk-overlay-backdrop',
Expand Down
3 changes: 3 additions & 0 deletions tools/public_api_guard/material/dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class MatDialogConfig<D = any> {
closeOnNavigation?: boolean;
componentFactoryResolver?: ComponentFactoryResolver;
data?: D | null;
delayFocusTrap?: boolean;
direction?: Direction;
disableClose?: boolean;
hasBackdrop?: boolean;
Expand All @@ -185,6 +186,8 @@ export class MatDialogConfig<D = any> {

// @public
export class MatDialogContainer extends _MatDialogContainerBase {
// (undocumented)
_initializeWithAttachedContent(): void;
_onAnimationDone({ toState, totalTime }: AnimationEvent_2): void;
_onAnimationStart({ toState, totalTime }: AnimationEvent_2): void;
_startExitAnimation(): void;
Expand Down

0 comments on commit 94d6f88

Please sign in to comment.