35

How can i close a vuetify's dialog opened without an activator, when the user press the ESC key on the keyboard?

Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
Benno Eggnauer
  • 853
  • 1
  • 7
  • 19

7 Answers7

54

Add @keydown.esc="dialog = false" to v-dialog component

<v-dialog v-model="dialog" @keydown.esc="dialog = false"></v-dialog>

data: () => ({
  dialog: false
}),

Working example: https://codepen.io/anon/pen/BJOOOQ


Additionally, if using dialog as custom component then possibly we need to emit input event:

<template>
  <v-dialog :value="value" @keydown.esc="$emit('input')">
  ...
Traxo
  • 18,464
  • 4
  • 75
  • 87
  • 1
    Thanks, with keyup it doesn't work, but with keydown ... interesting, a possible bug? @Tom works. – Benno Eggnauer Jan 16 '18 at 13:38
  • @benno I believe it's not a bug, just `keydown` was supported implemented for this case. – Traxo Jan 16 '18 at 13:39
  • can you explain a little bit further, what is the difference between keydown and keyup in this case? But aniway, it works! – Benno Eggnauer Jan 16 '18 at 13:43
  • @benno as far as I understand, there is no difference in events per se. It's just that vuetify devs added `keydown` listener for `v-dialog` and they **didn't** add `keyup` listener. That's all. – Traxo Jan 16 '18 at 13:47
  • 2
    This only works, if you have selected anything inside the modal. If there is no selection, the event will be triggered on the body and it will not work. Hence the solution of @Dazzle is sadly the better. – Nils-o-mat Apr 24 '20 at 10:05
15

This is the only way I was able to get it to work

mounted() {
    // Close modal with 'esc' key
    document.addEventListener("keydown", (e) => {
        if (e.keyCode == 27) {
            this.$emit('close');
        }
    });
},
Dazzle
  • 2,880
  • 3
  • 25
  • 52
7

this code maybe can help you

mounted() {
        let self = this;
        window.addEventListener('keyup', function(event) {
            // If  ESC key was pressed...
            if (event.keyCode === 27) {
                // try close your dialog
                self.advanced_search = false;
            }
        });
    },
kamiyar
  • 177
  • 1
  • 12
1

the same principle as adding keypress binding to any vue component should work - add the following code to the v-dialog component :

 @keydown.esc="dialog = false"

working example ( note the close button click event handler as well )

https://codepen.io/yordangeorgiev/pen/WBeMGq

Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
0

What you want to use is a Key Modifier. You can use v-on:keyup.esc directive on your dialog to detect if the escape key is detected.

Read this for more information about Key Modifiers

tony19
  • 125,647
  • 18
  • 229
  • 307
dovahkiin
  • 43
  • 8
0

While the solutions others have mentioned work, there still are conflicts with the bounce animation, making it not work after playing it by clicking outside the dialog, etc.

Setting the no-click-animation property also fixes the animation when closing as otherwise it plays both the close and bounce animation:

<v-dialog v-model="dialog" @keydown.esc="dialog=false" no-click-animation></v-dialog>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

@keydown.esc not working with my project. As far as I see it's good for Vue2 projects.

Working script:

mounted() {
    document.addEventListener("keydown", (e) => {
        if (e.key === 'Escape') {
            this.$emit('close');
            // Or any other way you want to close your modal
        }
    })
}
Michael Stachura
  • 1,100
  • 13
  • 18