-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: listen for click out only post blur (#1061)
Co-authored-by: Lee Chase <[email protected]>
- Loading branch information
Showing
1 changed file
with
21 additions
and
9 deletions.
There are no files selected for viewing
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
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); | ||
}, | ||
}; |