Skip to content

Instantly share code, notes, and snippets.

@acacha
Last active November 13, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acacha/11281c4815168c29f1f8be8728f0720a to your computer and use it in GitHub Desktop.
Save acacha/11281c4815168c29f1f8be8728f0720a to your computer and use it in GitHub Desktop.
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