I am trying to combine the use of a dialog and a snackbar with VueJS. The problem is the following:
Expected Behaviour:
I should be able to close the snackbar without closing the dialog
What happens now:
The dialog is being closed when the snackbar is clicked
Here is a JSFiddle to reproduce: https://jsfiddle.net/q6m2j4ae/5/
Here is the markup for the problem:
<v-container>
<v-dialog v-model="displayDialog" max-width="300px">
<v-card flat>
This is the dialog content
</v-card>
</v-dialog>
<v-snackbar
v-model="displaySnackbar"
:top="true"
:right="true"
:vertical="true"
color="success"
>
Some Content
<v-btn flat @click.stop="displaySnackbar = false">Close</v-btn>
</v-snackbar>
</v-container>
As you can see, the v-snackbar
is at the same level of the dialog. I am not allowed to nest the snackbar into the dialog. But even if I try the snackbar is not even displayed.
What I tried:
stop
modifier on the click event @click.stop="displaySnackbar = false"
would be enough to not close the dialog.z-index
applied to the elements. The snackbar has a z-index: 1000
and the dialog has a z-index:200
. So I'm not able to adjust that value.Is it a bug? How could I solve the problem on my hand?
We can create snackbars in Vuetify using the v-snackbar component. In the code below, we create a "Close" button in the action slot of the snackbar. When this button is clicked, the snackbar variable is set to false to hide the snackbar. snackbar is false by default, so at first we can only see the button that opens the snackbar:
A workaround (if the "dismiss on clicking outside the dialog" function is not needed) is to add the property persistent to the dialog. The click outside the dialog (when clicking the close in the snackbar) is the reason your dialog gets dismissed
A snackbar helps to display quick messages. We can use it to notify users of certain events that occur in an app (for example, deleting an item from a list). They might also contain actions related to the information shown that users can take. In this article, we’re going to learn how to create snackbars using the Vuetify framework.
Apply different styles to the snackbar using props such as text, shaped, outlined, and more. The vertical property allows you to stack the content of your v-snackbar. Ready for more? Continue your learning with related content selected by the Team or move between pages by using the navigation links below.
A workaround (if the "dismiss on clicking outside the dialog" function is not needed) is to add the property persistent
to the dialog.
The click outside the dialog (when clicking the close in the snackbar) is the reason your dialog gets dismissed
For anyone still looking for a good solution: add <div class="v-menu__content--active" style="display:none; z-index:1000;"></div>
as a child of your v-snackbar
. This tricks v-dialog
to think it was not the active component when click outside happened and prevents closing.
I had the same problem. I have produced a solution :
https://codepen.io/alignnc/pen/OdWvJd
<template>
<div id="app">
<v-app id="inspire">
<v-layout row justify-center>
<v-btn
color="primary"
dark
@click.native.stop="dialog = true">
Open Dialog
</v-btn>
<!-- dialog -->
<v-dialog
v-model="dialog"
max-width="290"
:transition="false"
:persistent="snack">
<v-card>
<v-card-text>
Click "agree" to set <br>
this.dialog to false,<br>
and this.snack to true
</v-card-text>
<v-btn
@click.prevent ="snack = true">
Agree
</v-btn>
</v-card>
</v-dialog>
<v-snackbar
v-model='snack'
color='error'
:top='true'>
Hello
<v-btn
color="accent"
flat
@click="snack = false">
Close
</v-btn>
</v-snackbar>
</v-layout>
</v-app>
</div>
</template>
<script>
new Vue({
el: '#app',
data () {
return {
dialog: false,
snack: false
}
}
})
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With