Skip to content

Commit 89f9b55

Browse files
committed
Begin dismiss timeout after open animation completes.
1 parent 1aab808 commit 89f9b55

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class SnackBarDemo {
1818

1919
open() {
2020
let config = new MdSnackBarConfig();
21-
config.autoHide = this.autoHide;
21+
config.dismiss = this.autoHide;
2222
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
2323
}
2424
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
4141
host: {
4242
'role': 'alert',
4343
'[@state]': 'animationState',
44-
'(@state.done)': 'markAsExited($event)'
44+
'(@state.done)': 'onAnimationEnd($event)'
4545
},
4646
animations: [
4747
trigger('state', [
@@ -58,7 +58,10 @@ export class MdSnackBarContainer extends BasePortalHost {
5858
@ViewChild(PortalHostDirective) _portalHost: PortalHostDirective;
5959

6060
/** Subject for notifying that the snack bar has exited from view. */
61-
private _onExit: Subject<any> = new Subject();
61+
private onExit: Subject<any> = new Subject();
62+
63+
/** Subject for notifying that the snack bar has finished entering the view. */
64+
private onEnter: Subject<any> = new Subject();
6265

6366
/** The state of the snack bar animations. */
6467
animationState: SnackBarState = 'initial';
@@ -87,15 +90,21 @@ export class MdSnackBarContainer extends BasePortalHost {
8790
/** Begin animation of the snack bar exiting from view. */
8891
exit(): Observable<void> {
8992
this.animationState = 'complete';
90-
return this._onExit.asObservable();
93+
return this.onExit.asObservable();
9194
}
9295

93-
/** Mark snack bar as exited from the view. */
94-
markAsExited(event: AnimationTransitionEvent) {
96+
/** Handle end of animations, updating the state of the snackbar. */
97+
onAnimationEnd(event: AnimationTransitionEvent) {
9598
if (event.toState === 'void' || event.toState === 'complete') {
9699
this._ngZone.run(() => {
97-
this._onExit.next();
98-
this._onExit.complete();
100+
this.onExit.next();
101+
this.onExit.complete();
102+
});
103+
}
104+
if (event.toState === 'visible') {
105+
this._ngZone.run(() => {
106+
this.onEnter.next();
107+
this.onEnter.complete();
99108
});
100109
}
101110
}
@@ -104,4 +113,9 @@ export class MdSnackBarContainer extends BasePortalHost {
104113
enter(): void {
105114
this.animationState = 'visible';
106115
}
116+
117+
/** Returns an observable resolving when the enter animation completes. */
118+
_onEnter(): Observable<void> {
119+
return this.onEnter.asObservable();
120+
}
107121
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export class MdSnackBarRef<T> {
1919
/** Subject for notifying the user that the snack bar has closed. */
2020
private _afterClosed: Subject<any> = new Subject();
2121

22+
/** Subject for notifying the user that the snack bar has opened and appeared. */
23+
private _afterOpened: Subject<any>;
24+
2225
/** Subject for notifying the user that the snack bar action was called. */
2326
private _onAction: Subject<any> = new Subject();
2427

@@ -51,11 +54,24 @@ export class MdSnackBarRef<T> {
5154
}
5255
}
5356

57+
/** Marks the snackbar as opened */
58+
_open(): void {
59+
if (!this._afterOpened.closed) {
60+
this._afterOpened.next();
61+
this._afterOpened.complete();
62+
}
63+
}
64+
5465
/** Gets an observable that is notified when the snack bar is finished closing. */
5566
afterDismissed(): Observable<void> {
5667
return this._afterClosed.asObservable();
5768
}
5869

70+
/** Gets an observable that is notified when the snack bar has opened and appeared. */
71+
afterOpened(): Observable<void> {
72+
return this.containerInstance._onEnter();
73+
}
74+
5975
/** Gets an observable that is notified when the snack bar action is called. */
6076
onAction(): Observable<void> {
6177
return this._onAction.asObservable();

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ export class MdSnackBar {
6363
snackBarRef.containerInstance.enter();
6464
}
6565

66-
// TODO(josephperrott): Set dismiss setTimeout after the snackbar finishes entering the view.
66+
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
6767
if (config.dismiss > 0) {
68-
setTimeout(() => snackBarRef.dismiss(), config.dismiss);
68+
snackBarRef.afterOpened().subscribe(() => {
69+
setTimeout(() => snackBarRef.dismiss(), config.dismiss);
70+
});
6971
}
7072

7173
this._live.announce(config.announcementMessage, config.politeness);

0 commit comments

Comments
 (0)