Skip to content

Commit 50be24b

Browse files
crisbetojelbourn
authored andcommitted
fix(snack-bar): dismiss snack bar on destroy (#13042)
Dismisses the currently-opened snack bar in `ngOnDestroy`.
1 parent f468321 commit 50be24b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ describe('MatSnackBar', () => {
452452
.toContain('custom-class', 'Expected class applied through the defaults to be applied.');
453453
}));
454454

455+
it('should dismiss the open snack bar on destroy', fakeAsync(() => {
456+
snackBar.open(simpleMessage);
457+
viewContainerFixture.detectChanges();
458+
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
459+
460+
snackBar.ngOnDestroy();
461+
viewContainerFixture.detectChanges();
462+
flush();
463+
464+
expect(overlayContainerElement.childElementCount).toBe(0);
465+
}));
466+
455467
describe('with custom component', () => {
456468
it('should open a custom component', () => {
457469
const snackBarRef = snackBar.openFromComponent(BurritosNotification);
@@ -603,6 +615,18 @@ describe('MatSnackBar with parent MatSnackBar', () => {
603615
expect(overlayContainerElement.textContent)
604616
.toContain('Taco', 'Expected child snackbar msg to be dismissed by opening from parent');
605617
}));
618+
619+
it('should not dismiss parent snack bar if child is destroyed', fakeAsync(() => {
620+
parentSnackBar.open('Pizza');
621+
fixture.detectChanges();
622+
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
623+
624+
childSnackBar.ngOnDestroy();
625+
fixture.detectChanges();
626+
flush();
627+
628+
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
629+
}));
606630
});
607631

608632
describe('MatSnackBar Positioning', () => {

src/lib/snack-bar/snack-bar.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Optional,
2121
SkipSelf,
2222
TemplateRef,
23+
OnDestroy,
2324
} from '@angular/core';
2425
import {take, takeUntil} from 'rxjs/operators';
2526
import {SimpleSnackBar} from './simple-snack-bar';
@@ -45,7 +46,7 @@ export function MAT_SNACK_BAR_DEFAULT_OPTIONS_FACTORY(): MatSnackBarConfig {
4546
* Service to dispatch Material Design snack bar messages.
4647
*/
4748
@Injectable({providedIn: MatSnackBarModule})
48-
export class MatSnackBar {
49+
export class MatSnackBar implements OnDestroy {
4950
/**
5051
* Reference to the current snack bar in the view *at this level* (in the Angular injector tree).
5152
* If there is a parent snack-bar service, all operations should delegate to that parent
@@ -129,6 +130,13 @@ export class MatSnackBar {
129130
}
130131
}
131132

133+
ngOnDestroy() {
134+
// Only dismiss the snack bar at the current level on destroy.
135+
if (this._snackBarRefAtThisLevel) {
136+
this._snackBarRefAtThisLevel.dismiss();
137+
}
138+
}
139+
132140
/**
133141
* Attaches the snack bar container component to the overlay.
134142
*/

0 commit comments

Comments
 (0)