Skip to content

Commit 98120fa

Browse files
committed
Add timeout to snackbar
1 parent 8728edc commit 98120fa

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-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/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| `viewContainerRef: ViewContainerRef` | The view container ref to attach the snack bar to. |
1616
| `role: AriaLivePoliteness = 'assertive'` | The politeness level to announce the snack bar with. |
1717
| `announcementMessage: string` | The message to announce with the snack bar. |
18+
| `dismiss: number` | The length of time in milliseconds to wait before autodismissing the snack bar. |
1819

1920

2021
### Example

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+
dismiss?: number = 0;
1619
}

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

Lines changed: 20 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,25 @@ describe('MdSnackBar', () => {
287288

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

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

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

Lines changed: 6 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,12 @@ export class MdSnackBar {
6462
} else {
6563
snackBarRef.containerInstance.enter();
6664
}
65+
66+
// TODO(josephperrott): Set dismiss setTimeout after the snackbar finishes entering the view.
67+
if (config.dismiss > 0) {
68+
setTimeout(() => snackBarRef.dismiss(), config.dismiss);
69+
}
70+
6771
this._live.announce(config.announcementMessage, config.politeness);
6872
this._snackBarRef = snackBarRef;
6973
return this._snackBarRef;

0 commit comments

Comments
 (0)