Skip to content

Commit 199f728

Browse files
authored
fix(dialog): backdrop not detaching if container view is destroyed (#20232)
* fix(dialog): backdrop not detaching if container view is destroyed We recently refactored the Material dialog so that it can be used as base class for the MDC based dialog. For this, we slightly changed how animations are announced to the corresponding dialog ref. This was done because the MDC dialog does not use Angular animations. It looks like we slightly regressed in cases where the dialog container view is destroyed immediately upon dialog close. This is because we accidentally no longer notify the dialog ref if the dialog animation state goes from `*` to `void`. Currently we only handle `*` to `exit` while we should also handle the `void` state. This issue has not surfaced in any of our tests, in the dev-app but one screenshot test inside g3 seems flaky after the dialog refactor. * fixup! fix(dialog): backdrop not detaching if container view is destroyed Add same test to MDC dialog
1 parent c8f03c7 commit 199f728

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/material-experimental/mdc-dialog/dialog.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,21 @@ describe('MDC-based MatDialog', () => {
14761476
expect(container.hasAttribute('aria-labelledby')).toBe(false);
14771477
}));
14781478
});
1479+
1480+
it('should dispose backdrop if containing dialog view is destroyed', fakeAsync(() => {
1481+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
1482+
viewContainerFixture.detectChanges();
1483+
flush();
1484+
1485+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeDefined();
1486+
1487+
dialogRef.close();
1488+
viewContainerFixture.componentInstance.showChildView = false;
1489+
viewContainerFixture.detectChanges();
1490+
flush();
1491+
1492+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBe(null);
1493+
}));
14791494
});
14801495

14811496
describe('MDC-based MatDialog with a parent MatDialog', () => {
@@ -1744,9 +1759,11 @@ class ComponentWithOnPushViewContainer {
17441759

17451760
@Component({
17461761
selector: 'arbitrary-component',
1747-
template: `<dir-with-view-container></dir-with-view-container>`,
1762+
template: `<dir-with-view-container *ngIf="showChildView"></dir-with-view-container>`,
17481763
})
17491764
class ComponentWithChildViewContainer {
1765+
showChildView = true;
1766+
17501767
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;
17511768

17521769
get childViewContainer() {

src/material/dialog/dialog-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export class MatDialogContainer extends _MatDialogContainerBase {
280280
_onAnimationStart({toState, totalTime}: AnimationEvent) {
281281
if (toState === 'enter') {
282282
this._animationStateChanged.next({state: 'opening', totalTime});
283-
} else if (toState === 'exit') {
283+
} else if (toState === 'exit' || toState === 'void') {
284284
this._animationStateChanged.next({state: 'closing', totalTime});
285285
}
286286
}

src/material/dialog/dialog.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,20 @@ describe('MatDialog', () => {
15671567
}));
15681568
});
15691569

1570+
it('should dispose backdrop if containing dialog view is destroyed', fakeAsync(() => {
1571+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
1572+
viewContainerFixture.detectChanges();
1573+
flush();
1574+
1575+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeDefined();
1576+
1577+
dialogRef.close();
1578+
viewContainerFixture.componentInstance.showChildView = false;
1579+
viewContainerFixture.detectChanges();
1580+
flush();
1581+
1582+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBe(null);
1583+
}));
15701584
});
15711585

15721586
describe('MatDialog with a parent MatDialog', () => {
@@ -1821,9 +1835,11 @@ class ComponentWithOnPushViewContainer {
18211835

18221836
@Component({
18231837
selector: 'arbitrary-component',
1824-
template: `<dir-with-view-container></dir-with-view-container>`,
1838+
template: `<dir-with-view-container *ngIf="showChildView"></dir-with-view-container>`,
18251839
})
18261840
class ComponentWithChildViewContainer {
1841+
showChildView = true;
1842+
18271843
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;
18281844

18291845
get childViewContainer() {

0 commit comments

Comments
 (0)