Skip to content

Instantly share code, notes, and snippets.

@Bachana123
Created June 13, 2020 20:38
Show Gist options
  • Save Bachana123/683100f0a951c55bb60089d6c30bff33 to your computer and use it in GitHub Desktop.
Save Bachana123/683100f0a951c55bb60089d6c30bff33 to your computer and use it in GitHub Desktop.
mousedown, click outside
import Vue from 'vue'
Vue.directive('mouse-down', {
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('mousedown', el.clickOutsideEvent)
},
unbind: function (el) {
document.body.removeEventListener('mousedown', el.clickOutsideEvent)
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment