Skip to content

Commit

Permalink
Begin dismiss timeout after open animation completes.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott committed Nov 17, 2016
1 parent 1aab808 commit 52aa060
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/demo-app/snack-bar/snack-bar-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class SnackBarDemo {

open() {
let config = new MdSnackBarConfig();
config.autoHide = this.autoHide;
config.duration = this.autoHide;
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
}
}
2 changes: 1 addition & 1 deletion src/lib/snack-bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| `viewContainerRef: ViewContainerRef` | The view container ref to attach the snack bar to. |
| `role: AriaLivePoliteness = 'assertive'` | The politeness level to announce the snack bar with. |
| `announcementMessage: string` | The message to announce with the snack bar. |
| `dismiss: number` | The length of time in milliseconds to wait before autodismissing the snack bar. |
| `duration: number` | The length of time in milliseconds to wait before autodismissing the snack bar. |


### Example
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snack-bar/snack-bar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export class MdSnackBarConfig {
viewContainerRef?: ViewContainerRef = null;

/** The length of time in milliseconds to wait before automatically dismissing the snack bar. */
dismiss?: number = 0;
duration?: number = 0;
}
28 changes: 21 additions & 7 deletions src/lib/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
host: {
'role': 'alert',
'[@state]': 'animationState',
'(@state.done)': 'markAsExited($event)'
'(@state.done)': 'onAnimationEnd($event)'
},
animations: [
trigger('state', [
Expand All @@ -58,7 +58,10 @@ export class MdSnackBarContainer extends BasePortalHost {
@ViewChild(PortalHostDirective) _portalHost: PortalHostDirective;

/** Subject for notifying that the snack bar has exited from view. */
private _onExit: Subject<any> = new Subject();
private onExit: Subject<any> = new Subject();

/** Subject for notifying that the snack bar has finished entering the view. */
private onEnter: Subject<any> = new Subject();

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

/** Mark snack bar as exited from the view. */
markAsExited(event: AnimationTransitionEvent) {
/** Handle end of animations, updating the state of the snackbar. */
onAnimationEnd(event: AnimationTransitionEvent) {
if (event.toState === 'void' || event.toState === 'complete') {
this._ngZone.run(() => {
this._onExit.next();
this._onExit.complete();
this.onExit.next();
this.onExit.complete();
});
}
if (event.toState === 'visible') {
this._ngZone.run(() => {
this.onEnter.next();
this.onEnter.complete();
});
}
}
Expand All @@ -104,4 +113,9 @@ export class MdSnackBarContainer extends BasePortalHost {
enter(): void {
this.animationState = 'visible';
}

/** Returns an observable resolving when the enter animation completes. */
_onEnter(): Observable<void> {
return this.onEnter.asObservable();
}
}
16 changes: 16 additions & 0 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class MdSnackBarRef<T> {
/** Subject for notifying the user that the snack bar has closed. */
private _afterClosed: Subject<any> = new Subject();

/** Subject for notifying the user that the snack bar has opened and appeared. */
private _afterOpened: Subject<any>;

/** Subject for notifying the user that the snack bar action was called. */
private _onAction: Subject<any> = new Subject();

Expand Down Expand Up @@ -51,11 +54,24 @@ export class MdSnackBarRef<T> {
}
}

/** Marks the snackbar as opened */
_open(): void {
if (!this._afterOpened.closed) {
this._afterOpened.next();
this._afterOpened.complete();
}
}

/** Gets an observable that is notified when the snack bar is finished closing. */
afterDismissed(): Observable<void> {
return this._afterClosed.asObservable();
}

/** Gets an observable that is notified when the snack bar has opened and appeared. */
afterOpened(): Observable<void> {
return this.containerInstance._onEnter();
}

/** Gets an observable that is notified when the snack bar action is called. */
onAction(): Observable<void> {
return this._onAction.asObservable();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('MdSnackBar', () => {
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
let dismissObservableCompleted = false;
let config = new MdSnackBarConfig();
config.dismiss = 250;
config.duration = 250;
let snackBarRef = snackBar.open('content', 'test', config);
snackBarRef.afterDismissed().subscribe(null, null, () => {
dismissObservableCompleted = true;
Expand Down
8 changes: 5 additions & 3 deletions src/lib/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ export class MdSnackBar {
snackBarRef.containerInstance.enter();
}

// TODO(josephperrott): Set dismiss setTimeout after the snackbar finishes entering the view.
if (config.dismiss > 0) {
setTimeout(() => snackBarRef.dismiss(), config.dismiss);
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
if (config.duration > 0) {
snackBarRef.afterOpened().subscribe(() => {
setTimeout(() => snackBarRef.dismiss(), config.duration);
});
}

this._live.announce(config.announcementMessage, config.politeness);
Expand Down

0 comments on commit 52aa060

Please sign in to comment.