Skip to content

fix(bottom-sheet): DOM nodes not cleaned up if view container is destroyed mid-animation #16349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/material/bottom-sheet/bottom-sheet-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class MatBottomSheetRef<T = any, R = any> {
/** Result to be passed down to the `afterDismissed` stream. */
private _result: R | undefined;

/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
private _closeFallbackTimeout: number;

constructor(
containerInstance: MatBottomSheetContainer,
private _overlayRef: OverlayRef,
Expand All @@ -61,6 +64,7 @@ export class MatBottomSheetRef<T = any, R = any> {
containerInstance._animationStateChanged
.pipe(filter(event => event.phaseName === 'done' && event.toState === 'hidden'), take(1))
.subscribe(() => {
clearTimeout(this._closeFallbackTimeout);
_overlayRef.dispose();
});

Expand Down Expand Up @@ -91,7 +95,18 @@ export class MatBottomSheetRef<T = any, R = any> {
this.containerInstance._animationStateChanged.pipe(
filter(event => event.phaseName === 'start'),
take(1)
).subscribe(() => this._overlayRef.detachBackdrop());
).subscribe(event => {
// The logic that disposes of the overlay depends on the exit animation completing, however
// it isn't guaranteed if the parent view is destroyed while it's running. Add a fallback
// timeout which will clean everything up if the animation hasn't fired within the specified
// amount of time plus 100ms. We don't need to run this outside the NgZone, because for the
// vast majority of cases the timeout will have been cleared before it has fired.
this._closeFallbackTimeout = setTimeout(() => {
this._overlayRef.dispose();
}, event.totalTime + 100);

this._overlayRef.detachBackdrop();
});

this._result = result;
this.containerInstance.exit();
Expand Down
12 changes: 12 additions & 0 deletions src/material/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ describe('MatBottomSheet', () => {
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));

it('should dispose of bottom sheet if view container is destroyed while animating',
fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});

bottomSheetRef.dismiss();
viewContainerFixture.detectChanges();
viewContainerFixture.destroy();
flush();

expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));

it('should emit the backdropClick stream when clicking on the overlay backdrop', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('backdropClick spy');
Expand Down