/
clickout.js
31 lines (28 loc) 路 907 Bytes
/
clickout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/** @typedef {import('vue').DirectiveHook} DirectiveHook */
/** @type {DirectiveHook} */
function mounted(el, binding) {
// TODO: add escape handler?
el._vtsClickout = {
stop: (e) => e.stopPropagation(),
// esc: e => e.key === 'Escape' && el.data.event()
};
document.body.addEventListener('click', binding.value);
// document.body.addEventListener('keydown', el._vtsClickout.esc)
el.addEventListener('click', el._vtsClickout.stop);
}
/** @type {DirectiveHook} */
function unmounted(el, binding) {
document.body.removeEventListener('click', binding.value);
// document.body.removeEventListener('keydown', el._vtsClickout.esc)
el.removeEventListener('click', el._vtsClickout.stop);
}
/**
* @type {import('vue').Directive & { bind: DirectiveHook, unbind: DirectiveHook }}
*/
export default {
mounted,
unmounted,
// TODO: Drop Vue 2
bind: mounted,
unbind: unmounted,
};