Skip to content

Commit 5d1abd9

Browse files
committed
Add timeout to snackbar
1 parent 8728edc commit 5d1abd9

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

src/demo-app/snack-bar/snack-bar-demo.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ <h1>SnackBar demo</h1>
1010
[(ngModel)]="actionButtonLabel"></md-input>
1111
</md-checkbox>
1212
</div>
13+
<div>
14+
<md-checkbox [(ngModel)]="setAutoHide">
15+
<p *ngIf="!setAutoHide">Auto hide after duration</p>
16+
<md-input type="number" class="demo-button-label-input"
17+
*ngIf="setAutoHide"
18+
placeholder="Auto Hide Duration in ms"
19+
[(ngModel)]="autoHide"></md-input>
20+
</md-checkbox>
21+
</div>
1322
</div>
1423

1524
<button md-raised-button (click)="open()">OPEN</button>

src/demo-app/snack-bar/snack-bar-demo.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component} from '@angular/core';
2-
import {MdSnackBar} from '@angular/material';
2+
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';
33

44
@Component({
55
moduleId: module.id,
@@ -10,11 +10,15 @@ export class SnackBarDemo {
1010
message: string = 'Snack Bar opened.';
1111
actionButtonLabel: string = 'Retry';
1212
action: boolean = false;
13+
setAutoHide: boolean = true;
14+
autoHide: number = 0;
1315

1416
constructor(
1517
public snackBar: MdSnackBar) { }
1618

1719
open() {
18-
this.snackBar.open(this.message, this.action && this.actionButtonLabel);
20+
let config = new MdSnackBarConfig();
21+
config.autoHide = this.autoHide;
22+
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
1923
}
2024
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ export class MdSnackBarConfig {
1313

1414
/** The view container to place the overlay for the snack bar into. */
1515
viewContainerRef?: ViewContainerRef = null;
16+
17+
/** The length of time in milliseconds to wait before automatically dismissing the snack bar. */
18+
autoHide?: number = 0;
1619
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@angular/core/testing';
1010
import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core';
1111
import {MdSnackBar, MdSnackBarModule} from './snack-bar';
12+
import {MdSnackBarConfig} from './snack-bar-config';
1213
import {OverlayContainer, MdLiveAnnouncer} from '../core';
1314
import {SimpleSnackBar} from './simple-snack-bar';
1415

@@ -287,6 +288,27 @@ describe('MdSnackBar', () => {
287288

288289
tick(500);
289290
}));
291+
292+
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
293+
let dismissObservableCompleted = false;
294+
let config = new MdSnackBarConfig();
295+
config.autoHide = 250;
296+
let snackBarRef = snackBar.open('content', 'test', config);
297+
snackBarRef.afterDismissed().subscribe(null, null, () => {
298+
dismissObservableCompleted = true;
299+
});
300+
301+
viewContainerFixture.detectChanges();
302+
viewContainerFixture.whenStable().then(() => {
303+
expect(dismissObservableCompleted).toBeFalsy('Expected the snack bar not to be dismissed');
304+
});
305+
306+
tick(1000);
307+
viewContainerFixture.detectChanges();
308+
viewContainerFixture.whenStable().then(() => {
309+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
310+
});
311+
}));
290312
});
291313

292314
@Directive({selector: 'dir-with-view-container'})

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {MdSnackBarContainer} from './snack-bar-container';
2222
import {SimpleSnackBar} from './simple-snack-bar';
2323
import {extendObject} from '../core/util/object-extend';
2424

25-
// TODO(josephperrott): Automate dismiss after timeout.
26-
2725

2826
/**
2927
* Service to dispatch Material Design snack bar messages.
@@ -64,6 +62,10 @@ export class MdSnackBar {
6462
} else {
6563
snackBarRef.containerInstance.enter();
6664
}
65+
if (config.autoHide > 0) {
66+
setTimeout(() => snackBarRef.dismiss(), config.autoHide + 225);
67+
}
68+
6769
this._live.announce(config.announcementMessage, config.politeness);
6870
this._snackBarRef = snackBarRef;
6971
return this._snackBarRef;

0 commit comments

Comments
 (0)