Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify, closing Snackbar without closing dialog

Tags:

vuetify.js

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:

  • I thought that the stop modifier on the click event @click.stop="displaySnackbar = false" would be enough to not close the dialog.
  • I checked the 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?

like image 939
Hammerbot Avatar asked Apr 03 '18 10:04

Hammerbot


People also ask

How do I create a snackbar in vuetify?

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:

How to dismiss dialog when closing 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

What is a snackbar and how to use it?

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.

How do I apply different styles to the snackbar?

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.


3 Answers

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

like image 126
Sprigg Avatar answered Oct 22 '22 14:10

Sprigg


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.

like image 6
Michael Baranov Avatar answered Oct 22 '22 13:10

Michael Baranov


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>
like image 1
alignnc Avatar answered Oct 22 '22 13:10

alignnc