Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create global vuetify snackbar
  • Loading branch information
stephannv committed Oct 9, 2019
1 parent c8fd742 commit e3a0593
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
28 changes: 28 additions & 0 deletions components/Snackbar.vue
@@ -0,0 +1,28 @@
<template>
<v-snackbar v-model="show" :color="color">
{{ message }}
<v-btn text @click="show = false">Close</v-btn>
</v-snackbar>
</template>

<script>
export default {
data () {
return {
show: false,
message: '',
color: ''
}
},
created () {
this.$store.subscribe((mutation, state) => {
if (mutation.type === 'snackbar/showMessage') {
this.message = state.snackbar.content
this.color = state.snackbar.color
this.show = true
}
})
}
}
</script>
7 changes: 7 additions & 0 deletions layouts/default.vue
Expand Up @@ -85,11 +85,18 @@
>
<span>&copy; 2019</span>
</v-footer>
<Snackbar></Snackbar>
</v-app>
</template>

<script>
import Snackbar from '~/components/Snackbar'
export default {
components: {
Snackbar
},
data () {
return {
clipped: false,
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.js
Expand Up @@ -30,6 +30,7 @@ export default {
** Plugins to load before mounting the App
*/
plugins: [
'~/plugins/notifier.js'
],
/*
** Nuxt.js dev-modules
Expand Down
9 changes: 9 additions & 0 deletions pages/index.vue
Expand Up @@ -73,6 +73,9 @@
>
Continue
</v-btn>
<v-btn color="primary" @click="showSnackbar">
Show snackbar
</v-btn>
</v-card-actions>
</v-card>
</v-flex>
Expand All @@ -88,5 +91,11 @@ export default {
Logo,
VuetifyLogo
},
methods: {
showSnackbar () {
this.$notifier.showMessage({ content: 'Hello, snackbar', color: 'info' })
}
}
}
</script>
7 changes: 7 additions & 0 deletions plugins/notifier.js
@@ -0,0 +1,7 @@
export default ({ app, store }, inject) => {
inject('notifier', {
showMessage ({ content = '', color = '' }) {
store.commit('snackbar/showMessage', { content, color })
}
})
}
11 changes: 11 additions & 0 deletions store/snackbar.js
@@ -0,0 +1,11 @@
export const state = () => ({
content: '',
color: ''
})

export const mutations = {
showMessage (state, payload) {
state.content = payload.content
state.color = payload.color
}
}

0 comments on commit e3a0593

Please sign in to comment.