Skip to content

Commit 9220ae9

Browse files
authored
Create hover.js
1 parent 0bbb69d commit 9220ae9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/directives/hover/hover.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// v-b-hover directive
2+
import { isBrowser } from '../../utils/env'
3+
import { EVENT_OPTIONS_NO_CAPTURE, eventOn, eventOff } from '../../utils/events'
4+
import { isFunction } from '../../utils/inspect'
5+
6+
// --- Constants ---
7+
8+
const PROP = '__BV_hover_handler__'
9+
const MOUSEENTER = 'mouseenter'
10+
const MOUSELEAVE = 'mouseleave'
11+
12+
// --- Directive bind/unbind/update handler ---
13+
14+
const directive = (el, { value: callback }) {
15+
if (isBrowser && isFunction(el[PROP] && el[PROP] !== callback) {
16+
eventOff(el, MOUSEENTER, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
17+
eventOff(el, MOUSELEAVE, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
18+
}
19+
if (isBrowser && isFunction(callback) && isBrowser) {
20+
el[PROP] = evt => {
21+
callback(evt.type === 'mouseenter')
22+
}
23+
eventOn(el, MOUSEENTER, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
24+
eventOn(el, MOUSELEAVE, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
25+
}
26+
}
27+
28+
// VBHover directive
29+
30+
export const VBHover = {
31+
bind: directive,
32+
componentUpdated: directive,
33+
unbind(el) {
34+
directive(el, { value: null }
35+
}
36+
}

0 commit comments

Comments
 (0)