Skip to content

Instantly share code, notes, and snippets.

@ColtHands
Last active August 16, 2020 18:23
Show Gist options
  • Save ColtHands/fc0f3702d68e26eb0c041d45b8de7e99 to your computer and use it in GitHub Desktop.
Save ColtHands/fc0f3702d68e26eb0c041d45b8de7e99 to your computer and use it in GitHub Desktop.
Vue click outside component
Vue.directive('click-outside-component', {
bind(el, binding, vnode) {
el.clickOutsideEvent = function(event) {
if (!(vnode.context.$el == event.target || vnode.context.$el.contains(event.target))) {
vnode.context[binding.expression](event)
}
}
document.body.addEventListener('click', el.clickOutsideEvent)
},
unbind(el) {
document.body.removeEventListener('click', el.clickOutsideEvent)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment