Skip to content

Commit 83b5842

Browse files
crisbetommalerba
authored andcommitted
feat(snack-bar): add dismiss method to MdSnackBar service (#3069)
Adds a `dismiss` method to the snack bar service, which dismisses the currently-open instance, mostly for convenience since the spec only allows one snack bar to open at a time.
1 parent bd6feb3 commit 83b5842

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ describe('MdSnackBar', () => {
150150
});
151151
}));
152152

153+
it('should be able to get dismissed through the service', async(() => {
154+
snackBar.open(simpleMessage);
155+
viewContainerFixture.detectChanges();
156+
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
157+
158+
snackBar.dismiss();
159+
viewContainerFixture.detectChanges();
160+
161+
viewContainerFixture.whenStable().then(() => {
162+
expect(overlayContainerElement.childElementCount).toBe(0);
163+
});
164+
}));
165+
153166
it('should clean itself up when the view container gets destroyed', async(() => {
154167
snackBar.open(simpleMessage, null, { viewContainerRef: testViewContainerRef });
155168
viewContainerFixture.detectChanges();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ export class MdSnackBar {
105105
return simpleSnackBarRef;
106106
}
107107

108+
/**
109+
* Dismisses the currently-visible snack bar.
110+
*/
111+
dismiss(): void {
112+
if (this._openedSnackBarRef) {
113+
this._openedSnackBarRef.dismiss();
114+
}
115+
}
116+
108117
/**
109118
* Attaches the snack bar container component to the overlay.
110119
*/

0 commit comments

Comments
 (0)