Skip to content

Commit

Permalink
Merge pull request #356 from material-components/snackbar-early-open
Browse files Browse the repository at this point in the history
Allow <mwc-button> open method to be called before firstUpdated
  • Loading branch information
dfreedm authored Aug 9, 2019
2 parents e86bd75 + 89b3707 commit b42cac9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Buttons slotted into `<mwc-snackbar>` now render with correct default styles.
Add `--mdc-snackbar-action-color` CSS custom property to override default
action button color.
- Fix bug where `<mwc-snackbar>` `open` method threw if called immediately
after construction (before `firstUpdated`).

## [0.6.0] - 2019-06-05
- Upgrade lerna to 3.x
Expand Down
26 changes: 24 additions & 2 deletions packages/snackbar/src/mwc-snackbar-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class SnackbarBase extends BaseElement {

@property({type: Boolean}) leading = false;

/**
* We can't open the snackbar until the foundation is initialized, but that
* doesn't happen until firstUpdated. Keep track of early calls to open() and
* do so after we have a foundation.
*/
private _earlyOpen: boolean|undefined;

render() {
const classes = {
'mdc-snackbar--stacked': this.stacked,
Expand Down Expand Up @@ -107,11 +114,26 @@ export class SnackbarBase extends BaseElement {
}

open() {
this.mdcFoundation.open();
if (this.mdcFoundation !== undefined) {
this.mdcFoundation.open();
} else {
this._earlyOpen = true;
}
}

close(reason = '') {
this.mdcFoundation.close(reason);
if (this.mdcFoundation !== undefined) {
this.mdcFoundation.close(reason);
} else if (this._earlyOpen === true) {
this._earlyOpen = false;
}
}

firstUpdated() {
super.firstUpdated();
if (this._earlyOpen === true) {
this.mdcFoundation.open();
}
}

_handleKeydown(e: KeyboardEvent) {
Expand Down

0 comments on commit b42cac9

Please sign in to comment.