Last active
August 16, 2020 18:23
-
-
Save ColtHands/fc0f3702d68e26eb0c041d45b8de7e99 to your computer and use it in GitHub Desktop.
Vue click outside component
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
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