Skip to content

Commit ffa4a06

Browse files
crisbetovivian-hu-zz
authored andcommitted
fix(bottom-sheet): dismiss bottom sheet on destroy (#13120)
Dismisses the open bottom sheet if the service is destroyed.
1 parent bef9a17 commit ffa4a06

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/lib/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ describe('MatBottomSheet', () => {
274274
expect(overlayContainerElement.childElementCount).toBe(0);
275275
}));
276276

277+
it('should dismiss the bottom sheet when the service is destroyed', fakeAsync(() => {
278+
bottomSheet.open(PizzaMsg);
279+
viewContainerFixture.detectChanges();
280+
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
281+
282+
bottomSheet.ngOnDestroy();
283+
viewContainerFixture.detectChanges();
284+
flush();
285+
286+
expect(overlayContainerElement.childElementCount).toBe(0);
287+
}));
288+
277289
it('should open a new bottom sheet after dismissing a previous sheet', fakeAsync(() => {
278290
let config: MatBottomSheetConfig = {viewContainerRef: testViewContainerRef};
279291
let bottomSheetRef: MatBottomSheetRef<any> = bottomSheet.open(PizzaMsg, config);
@@ -614,6 +626,23 @@ describe('MatBottomSheet with parent MatBottomSheet', () => {
614626
expect(overlayContainerElement.textContent)
615627
.toContain('Taco', 'Expected child bottom sheet to be dismissed by opening from parent');
616628
}));
629+
630+
it('should not close parent bottom sheet when child is destroyed', fakeAsync(() => {
631+
parentBottomSheet.open(PizzaMsg);
632+
fixture.detectChanges();
633+
tick(1000);
634+
635+
expect(overlayContainerElement.textContent)
636+
.toContain('Pizza', 'Expected a bottom sheet to be opened');
637+
638+
childBottomSheet.ngOnDestroy();
639+
fixture.detectChanges();
640+
tick(1000);
641+
642+
expect(overlayContainerElement.textContent)
643+
.toContain('Pizza', 'Expected a bottom sheet to stay open');
644+
}));
645+
617646
});
618647

619648
describe('MatBottomSheet with default options', () => {

src/lib/bottom-sheet/bottom-sheet.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
TemplateRef,
1919
InjectionToken,
2020
Inject,
21+
OnDestroy,
2122
} from '@angular/core';
2223
import {Location} from '@angular/common';
2324
import {of as observableOf} from 'rxjs';
@@ -35,7 +36,7 @@ export const MAT_BOTTOM_SHEET_DEFAULT_OPTIONS =
3536
* Service to trigger Material Design bottom sheets.
3637
*/
3738
@Injectable({providedIn: MatBottomSheetModule})
38-
export class MatBottomSheet {
39+
export class MatBottomSheet implements OnDestroy {
3940
private _bottomSheetRefAtThisLevel: MatBottomSheetRef<any> | null = null;
4041

4142
/** Reference to the currently opened bottom sheet. */
@@ -118,6 +119,12 @@ export class MatBottomSheet {
118119
}
119120
}
120121

122+
ngOnDestroy() {
123+
if (this._bottomSheetRefAtThisLevel) {
124+
this._bottomSheetRefAtThisLevel.dismiss();
125+
}
126+
}
127+
121128
/**
122129
* Attaches the bottom sheet container component to the overlay.
123130
*/

0 commit comments

Comments
 (0)