Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close dialog when ESC key is pressed

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

like image 856
Benno Eggnauer Avatar asked Sep 18 '17 14:09

Benno Eggnauer


People also ask

How do I close dialog with ESC?

to listen for esc key presses with the @keydown. esc directive. And if the event is triggered, we set dialog to false to close the dialog.

How do I stop pop ups on escape?

We can use Esc key to close dialogue boxes or any custom popup modals. Here we will discuss even handling on ESC key press in which we can fire any event we want.

How do I close windows with ESC?

Some windows can be closed by pressing the esc key, like the Preference window in any app or the 'Safari help' window or the 'Fonts' window in TextExit for example. Other windows can not be closed by pressing the esc key, like the Safari browser window or the 'Special Characters' window.


4 Answers

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')">   ... 
like image 197
Traxo Avatar answered Sep 19 '22 08:09

Traxo


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');         }     }); }, 
like image 40
Dazzle Avatar answered Sep 20 '22 08:09

Dazzle


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;
            }
        });
    },
like image 25
kamiyar Avatar answered Sep 19 '22 08:09

kamiyar


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

like image 21
Yordan Georgiev Avatar answered Sep 17 '22 08:09

Yordan Georgiev