Created
March 6, 2019 20:42
-
-
Save hawkeye64/c538a2bcfd6e7ecab3677219beb3c151 to your computer and use it in GitHub Desktop.
Vue directive (untested)
This file contains hidden or 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', { | |
bind: function (el, binding, vnode) { | |
el.clickOutsideEvent = function (event) { | |
// here I check that click was outside the el and his childrens | |
if (!(el == event.target || el.contains(event.target))) { | |
// and if it did, call method provided in attribute value | |
vnode.context[binding.expression](event); | |
} | |
}; | |
document.body.addEventListener('click', el.clickOutsideEvent) | |
}, | |
unbind: function (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