Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 828edff

Browse files
committed
refactor(snackbar): Implement two-way binding for open prop (#83)
BREAKING CHANGES: Public component methods for open / close no longer exist. Please change your templates to use v-model instead.
1 parent d55aeb5 commit 828edff

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

components/snackbar/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,40 @@
33
### Markup
44

55
```html
6-
<m-snackbar ref="snackbar" />
6+
<m-button
7+
raised
8+
interactive
9+
@click="isSnackbarOpen=true">open</m-button>
10+
<m-snackbar
11+
:options="options"
12+
v-model="isSnackbarOpen"/>
713
```
814

915
### Script
1016

1117
```javascript
12-
let options = {
13-
message: 'snackbar message'
18+
data () {
19+
return {
20+
isSnackbarOpen: false,
21+
options: {
22+
message: 'I am a snackbar!',
23+
timeout: 3000,
24+
actionHandler: () => console.log('snackbar action'),
25+
actionText: 'ok',
26+
multiline: false,
27+
actionOnBottom: false
28+
}
29+
}
1430
}
15-
16-
this.$refs.snackbar.show(options)
1731
```
1832

19-
### Props & methods
33+
### Props
2034

2135
| Prop | Type | Default | Required | Description |
2236
|------|------|---------|----------|-------------|
23-
| alignStart | Boolean | - | false | start-aligned snackbar |
24-
| dismissesOnAction | Boolean | true | false | keep snackbar when action button is pressed |
25-
26-
| Method | Description |
27-
|--------|-------------|
28-
| show | show the snackbar with given options |
29-
30-
The show() function takes a object as parameter to configure the snackbar. The benefit is that you don't have to add more than one snackbar in your template to show different snackbars. Have a look at the [reference](https://github.com/material-components/material-components-web/blob/master/packages/mdc-snackbar/README.md#showing-a-message-and-action) for details of the options object.
37+
| alignStart | Boolean | false | - | start-aligned snackbar |
38+
| dismissesOnAction | Boolean | true | - | keep snackbar when action button is pressed |
39+
| options | Object | - | true | snackbar options (see script section)|
3140

3241
### Reference
3342

components/snackbar/Snackbar.vue

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<template>
22
<div
33
class="mdc-snackbar"
4-
:class="classes">
4+
:class="classes"
5+
aria-live="assertive"
6+
aria-atomic="true"
7+
aria-hidden="true"
8+
@MDCSnackbar:hide="model = false">
59
<div class="mdc-snackbar__text" />
610
<div class="mdc-snackbar__action-wrapper">
711
<button
812
type="button"
9-
class="mdc-button mdc-snackbar__action-button" />
13+
class="mdc-snackbar__action-button"/>
1014
</div>
1115
</div>
1216
</template>
@@ -18,14 +22,26 @@ import themeClassMixin from '../base/themeClassMixin.js'
1822
1923
export default {
2024
mixins: [themeClassMixin],
25+
model: {
26+
prop: 'open',
27+
event: 'change'
28+
},
2129
props: {
2230
alignStart: {
2331
type: Boolean,
24-
required: false
32+
default: false
2533
},
2634
dismissesOnAction: {
2735
type: Boolean,
2836
default: true
37+
},
38+
open :{
39+
type: Boolean,
40+
default: false
41+
},
42+
options: {
43+
type: Object,
44+
required: true
2945
}
3046
},
3147
data () {
@@ -38,18 +54,28 @@ export default {
3854
return {
3955
'mdc-snackbar--align-start': this.alignStart
4056
}
57+
},
58+
model: {
59+
get () {
60+
return this.open
61+
},
62+
set (value) {
63+
this.$emit('change', value)
64+
}
4165
}
4266
},
4367
mounted () {
4468
this.mdcSnackbar = MDCSnackbar.attachTo(this.$el)
45-
this.mdcSnackbar.dismissesOnAction = this.dismissesOnAction
4669
},
4770
beforeDestroy () {
4871
this.mdcSnackbar.destroy()
4972
},
50-
methods: {
51-
show (options) {
52-
this.mdcSnackbar.show(options)
73+
watch: {
74+
dismissesOnAction () {
75+
this.mdcSnackbar.dismissesOnAction = this.dismissesOnAction
76+
},
77+
open () {
78+
if (this.open) this.mdcSnackbar.show(this.options)
5379
}
5480
}
5581
}

components/snackbar/styles.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@import "@material/snackbar/mdc-snackbar";
2-
@import "@material/button/mdc-button";
3-
41
.mdc-snackbar {
52
transform: translateY(100%);
6-
}
3+
}
4+
5+
@import "@material/snackbar/mdc-snackbar";
6+

0 commit comments

Comments
 (0)