Skip to content

Commit

Permalink
chore: listen for click out only post blur (#1061)
Browse files Browse the repository at this point in the history
Co-authored-by: Lee Chase <[email protected]>
  • Loading branch information
lee-chase and lee-chase authored Nov 18, 2020
1 parent 1f31f36 commit 1323ff8
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions packages/core/src/directives/clickout.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
// This directive determines calls the associated method if a click happens outside of el

const handlerMap = new WeakMap();

export default {
bind(el, binding, vnode) {
el.clickOutHandler = function(event) {
// neither element or child of
if (!(el == event.target || el.contains(event.target))) {
// call method
vnode.context[binding.expression](event);
}
};
document.body.addEventListener('click', el.clickOutHandler);
if (!el.clickOutsideBlur) {
el.clickOutsideBlur = function(blurEv) {
// neither element or child of
if (!handlerMap.has(el)) {
handlerMap.set(el, clickoutEv => {
// neither element or child of
if (!(el == clickoutEv.target || el.contains(clickoutEv.target))) {
// call method
vnode.context[binding.expression](clickoutEv);
}
});
document.body.addEventListener('click', handlerMap.get(el));
}
};

el.addEventListener('focusout', el.clickOutsideBlur);
}
},
unbind(el) {
document.body.removeEventListener('click', el.clickOutHandler);
handlerMap.delete(el);
el.removeEventListener('focusout', el.clickOutsideBlur);
},
};

0 comments on commit 1323ff8

Please sign in to comment.