Skip to content

feat(snackbar): Auto Hide for Snackbar #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/demo-app/snack-bar/snack-bar-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ <h1>SnackBar demo</h1>
[(ngModel)]="actionButtonLabel"></md-input>
</md-checkbox>
</div>
<div>
<md-checkbox [(ngModel)]="autoHide">
<p *ngIf="!autoHide">Auto hide after duration</p>
<md-input type="number" class="demo-button-label-input"
*ngIf="autoHide"
placeholder="Auto Hide Duration in ms"
[(ngModel)]="autoHideMs"></md-input>
</md-checkbox>
</div>
</div>

<button md-raised-button (click)="open()">OPEN</button>
8 changes: 7 additions & 1 deletion src/demo-app/snack-bar/snack-bar-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ export class SnackBarDemo {
message: string = 'Snack Bar opened.';
actionButtonLabel: string = 'Retry';
action: boolean = false;
autoHide: boolean = false;
autoHideMs: number | boolean;

constructor(
public snackBar: MdSnackBar,
public viewContainerRef: ViewContainerRef) { }

open() {
let config = new MdSnackBarConfig(this.viewContainerRef);
let autoHide: number | boolean = false;
if (this.autoHide) {
autoHide = this.autoHideMs;
}
let config = new MdSnackBarConfig(this.viewContainerRef, autoHide);
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
}
}
5 changes: 3 additions & 2 deletions 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. |

| `autoHideDuration: number | boolean` | The number of milliseconds to wait before automatically dismissing. If no value is specified the snackbar will dismiss normally. |

### Example
The service can be injected in a component.
Expand All @@ -30,7 +30,8 @@ export class MyComponent {
viewContainerRef: ViewContainerRef) {}

failedAttempt() {
config = new MdSnackBarConfig(this.viewContainerRef);
// This snackbar will dissmiss after 3s (3000ms)
config = new MdSnackBarConfig(this.viewContainerRef, 3000);
this.snackBar.open('It didn\'t quite work!', 'Try Again', config);
}

Expand Down
11 changes: 10 additions & 1 deletion src/lib/snack-bar/snack-bar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ export class MdSnackBarConfig {
/** The view container to place the overlay for the snack bar into. */
viewContainerRef: ViewContainerRef;

constructor(viewContainerRef: ViewContainerRef) {
/*
* The number of milliseconds to wait before automatically dismissing.
* If no value is specified the snackbar will dismiss normally.
* If a value is provided the snackbar can still be dismissed normally.
* If a snackbar is dismissed before the timer expires, the timer will be cleared.
*/
autoHideDuration: number | boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call this duration, where the value is null if it is persistent.


constructor(viewContainerRef: ViewContainerRef, autoHideDuration: number | boolean = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than being a constructor argument, this should just be a property that is set to the config with a default. We will shortly be giving the snackbar config the same treatment as the dialog config (#1679).

this.viewContainerRef = viewContainerRef;
this.autoHideDuration = autoHideDuration;
}
}
6 changes: 6 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,8 @@ export class MdSnackBarRef<T> {
/** Subject for notifying the user that the snack bar has closed. */
private _afterClosed: Subject<any> = new Subject();

autoHide: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_autoDismissTimeoutId


constructor(instance: T,
containerInstance: MdSnackBarContainer,
private _overlayRef: OverlayRef) {
Expand All @@ -36,6 +38,10 @@ export class MdSnackBarRef<T> {
this._afterClosed.complete();
});
}
// Clear autohide interval
if (this.autoHide) {
clearInterval(this.autoHide);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use setTimeout, the corresponding cancel function is cancelTimeout

}
}

/** Gets an observable that is notified when the snack bar is finished closing. */
Expand Down
8 changes: 6 additions & 2 deletions src/lib/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {MdSnackBarRef} from './snack-bar-ref';
import {MdSnackBarContainer} from './snack-bar-container';
import {SimpleSnackBar} from './simple-snack-bar';

// TODO(josephperrott): Automate dismiss after timeout.


/**
* Service to dispatch Material Design snack bar messages.
Expand Down Expand Up @@ -56,6 +54,12 @@ export class MdSnackBar {
} else {
mdSnackBarRef.containerInstance.enter();
}
// Auto dismiss after timeout.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment like

// If the snackbar has a specified duration, set up a timeout to automatically dismiss it.

if (config.autoHideDuration) {
mdSnackBarRef.autoHide = setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the braces:

mdSnackBarRef._autoDismissTimeoutId = 
    setTimeout(() => mdSnackBarRef.dismiss(), config.duration);

mdSnackBarRef.dismiss();
}, config.autoHideDuration);
}
this._live.announce(config.announcementMessage, config.politeness);
this._snackBarRef = mdSnackBarRef;
return this._snackBarRef;
Expand Down