diff --git a/src/material/dialog/dialog-container.ts b/src/material/dialog/dialog-container.ts index 5b4ef223e287..b8d37b98c038 100644 --- a/src/material/dialog/dialog-container.ts +++ b/src/material/dialog/dialog-container.ts @@ -94,6 +94,8 @@ export class MatDialogContainer extends CdkDialogContainer impl /** Current timer for dialog animations. */ private _animationTimer: ReturnType | null = null; + private _isDestroyed = false; + constructor( elementRef: ElementRef, focusTrapFactory: FocusTrapFactory, @@ -262,6 +264,10 @@ export class MatDialogContainer extends CdkDialogContainer impl * be called by sub-classes that use different animation implementations. */ protected _openAnimationDone(totalTime: number) { + if (this._isDestroyed) { + return; + } + if (this._config.delayFocusTrap) { this._trapFocus(); } @@ -275,6 +281,8 @@ export class MatDialogContainer extends CdkDialogContainer impl if (this._animationTimer !== null) { clearTimeout(this._animationTimer); } + + this._isDestroyed = true; } override attachComponentPortal(portal: ComponentPortal): ComponentRef { diff --git a/src/material/dialog/dialog.spec.ts b/src/material/dialog/dialog.spec.ts index e0bb260b1506..a35639bd42fa 100644 --- a/src/material/dialog/dialog.spec.ts +++ b/src/material/dialog/dialog.spec.ts @@ -40,6 +40,7 @@ import { inject, TestBed, tick, + waitForAsync, } from '@angular/core/testing'; import {By} from '@angular/platform-browser'; import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations'; @@ -965,7 +966,8 @@ describe('MDC-based MatDialog', () => { dialog.open(PizzaMsg, {disableClose: true, viewContainerRef: testViewContainerRef}); viewContainerFixture.detectChanges(); - flushMicrotasks(); + flush(); + viewContainerFixture.detectChanges(); let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement; let input = overlayContainerElement.querySelector('input') as HTMLInputElement; @@ -994,7 +996,8 @@ describe('MDC-based MatDialog', () => { }); viewContainerFixture.detectChanges(); - flushMicrotasks(); + flush(); + viewContainerFixture.detectChanges(); let backdrop = overlayContainerElement.querySelector( '.cdk-overlay-backdrop', @@ -1179,7 +1182,8 @@ describe('MDC-based MatDialog', () => { dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef}); viewContainerFixture.detectChanges(); - flushMicrotasks(); + flush(); + viewContainerFixture.detectChanges(); expect(document.activeElement!.tagName) .withContext('Expected first tabbable element (input) in the dialog to be focused.') @@ -1505,7 +1509,8 @@ describe('MDC-based MatDialog', () => { dialog.open(DialogWithoutFocusableElements); viewContainerFixture.detectChanges(); - flushMicrotasks(); + flush(); + viewContainerFixture.detectChanges(); expect(document.activeElement!.tagName) .withContext('Expected dialog container to be focused.')