Skip to content

Instantly share code, notes, and snippets.

@rebz
Created April 13, 2019 20:28
Show Gist options
  • Save rebz/08077eb88bf1aa1918e28b5228bc26c8 to your computer and use it in GitHub Desktop.
Save rebz/08077eb88bf1aa1918e28b5228bc26c8 to your computer and use it in GitHub Desktop.
Custom directive to drop in a project for Modal-esque elements.
/*
* Add `v-click-outside="function"` to your element.
* Does not take into account 'touchstart'
*/
export default {
bind: function (el, binding, vnode) {
el.event = function (event) {
if (!(el == event.target || el.contains(event.target))) {
vnode.context[binding.expression](event);
}
}
document.body.addEventListener('click', el.event)
},
unbind: function (el) {
document.body.removeEventListener('click', el.event)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment