Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CanBeCanceledByEscKey Vue Mixin
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