Skip to content

Commit 80b38f7

Browse files
Sven Reglitzkisv2dev
authored andcommitted
fix(snack-bar): Clear duration timeout on dismiss
When a duration is passed to MdSnackBar.prototype.openFromComponent(), a timeout is created. This timeout is now cleared when dismissing the snackbar. Fixes #4859
1 parent 12d6e96 commit 80b38f7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export class MdSnackBarRef<T> {
3131
/** Subject for notifying the user that the snack bar action was called. */
3232
private _onAction: Subject<any> = new Subject();
3333

34+
/** Timeout handle for the dismiss timeout cleanup. */
35+
private _dismissTimeoutHandle: number;
36+
3437
constructor(instance: T,
3538
containerInstance: MdSnackBarContainer,
3639
private _overlayRef: OverlayRef) {
@@ -47,6 +50,12 @@ export class MdSnackBarRef<T> {
4750
if (!this._afterClosed.closed) {
4851
this.containerInstance.exit();
4952
}
53+
clearTimeout(this._dismissTimeoutHandle);
54+
}
55+
56+
/** Dismisses the snack bar after some duration */
57+
dismissAfter(duration: number): void {
58+
this._dismissTimeoutHandle = setTimeout(() => this.dismiss(), duration);
5059
}
5160

5261
/** Marks the snackbar action clicked. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class MdSnackBar {
8181
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
8282
if (config.duration > 0) {
8383
snackBarRef.afterOpened().subscribe(() => {
84-
setTimeout(() => snackBarRef.dismiss(), config.duration);
84+
snackBarRef.dismissAfter(config.duration);
8585
});
8686
}
8787

0 commit comments

Comments
 (0)