Skip to content

Commit a78fd14

Browse files
committed
feat: add CClickaway directive
1 parent 1b43afa commit a78fd14

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/directives/CClickaway.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const HANDLER = 'c_clickaway_handler'
2+
3+
function setListeners (el, binding, vnode) {
4+
const callback = binding.value
5+
if (typeof callback !== 'function') {
6+
return
7+
}
8+
el[HANDLER] = (e) => el.contains(e.target) || callback.call(vnode.context, e)
9+
document.documentElement.addEventListener(binding.arg || 'click', el[HANDLER])
10+
}
11+
12+
function unsetListeners (el, binding) {
13+
document.documentElement.removeEventListener(binding.arg || 'click', el[HANDLER])
14+
delete el[HANDLER]
15+
}
16+
17+
export default {
18+
bind: setListeners,
19+
update: (el, binding, vnode) => {
20+
if (binding.value !== binding.oldValue) {
21+
unsetListeners(el, binding)
22+
setListeners(el, binding, vnode)
23+
}
24+
},
25+
unbind: unsetListeners
26+
}

src/directives/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import CClickaway from './CClickaway'
2+
import CEmitRootEvent from './CEmitRootEvent'
13
import CPopover from './CPopover'
24
import CTooltip from './CTooltip'
3-
import CEmitRootEvent from './CEmitRootEvent'
45

5-
export{
6+
export {
7+
CClickaway,
8+
CEmitRootEvent,
69
CPopover,
7-
CTooltip,
8-
CEmitRootEvent
10+
CTooltip
911
}

0 commit comments

Comments
 (0)