Last active
November 13, 2020 18:14
-
-
Save acacha/11281c4815168c29f1f8be8728f0720a to your computer and use it in GitHub Desktop.
CanBeCanceledByEscKey Vue Mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
// SSR mount and Before mount are not executed in SSR | |
beforeMount() { | |
const escapeHandler = (e) => { | |
if (e.key === 'Escape') { | |
this.$emit('cancel') | |
} | |
} | |
document.addEventListener('keydown', escapeHandler) | |
this.$once('hook:destroyed', () => { | |
document.removeEventListener('keydown', escapeHandler) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment