From fda44273d250681c96935bba14570242406e5157 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 12 Feb 2017 17:31:11 +0100 Subject: [PATCH] feat(snack-bar): add `dismiss` method to `MdSnackBar` service 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. --- src/lib/snack-bar/snack-bar.spec.ts | 13 +++++++++++++ src/lib/snack-bar/snack-bar.ts | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index d7c13c01a4b9..84904665aca4 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -150,6 +150,19 @@ describe('MdSnackBar', () => { }); })); + it('should be able to get dismissed through the service', async(() => { + snackBar.open(simpleMessage); + viewContainerFixture.detectChanges(); + expect(overlayContainerElement.childElementCount).toBeGreaterThan(0); + + snackBar.dismiss(); + viewContainerFixture.detectChanges(); + + viewContainerFixture.whenStable().then(() => { + expect(overlayContainerElement.childElementCount).toBe(0); + }); + })); + it('should clean itself up when the view container gets destroyed', async(() => { snackBar.open(simpleMessage, null, { viewContainerRef: testViewContainerRef }); viewContainerFixture.detectChanges(); diff --git a/src/lib/snack-bar/snack-bar.ts b/src/lib/snack-bar/snack-bar.ts index 4adeded7193e..4572069083d2 100644 --- a/src/lib/snack-bar/snack-bar.ts +++ b/src/lib/snack-bar/snack-bar.ts @@ -105,6 +105,15 @@ export class MdSnackBar { return simpleSnackBarRef; } + /** + * Dismisses the currently-visible snack bar. + */ + dismiss(): void { + if (this._openedSnackBarRef) { + this._openedSnackBarRef.dismiss(); + } + } + /** * Attaches the snack bar container component to the overlay. */