Skip to content

Commit

Permalink
Create hover.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorehouse authored Feb 15, 2020
1 parent 0bbb69d commit 9220ae9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/directives/hover/hover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// v-b-hover directive
import { isBrowser } from '../../utils/env'
import { EVENT_OPTIONS_NO_CAPTURE, eventOn, eventOff } from '../../utils/events'
import { isFunction } from '../../utils/inspect'

// --- Constants ---

const PROP = '__BV_hover_handler__'
const MOUSEENTER = 'mouseenter'
const MOUSELEAVE = 'mouseleave'

// --- Directive bind/unbind/update handler ---

const directive = (el, { value: callback }) {
if (isBrowser && isFunction(el[PROP] && el[PROP] !== callback) {
eventOff(el, MOUSEENTER, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
eventOff(el, MOUSELEAVE, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
}
if (isBrowser && isFunction(callback) && isBrowser) {
el[PROP] = evt => {
callback(evt.type === 'mouseenter')
}
eventOn(el, MOUSEENTER, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
eventOn(el, MOUSELEAVE, el[PROP], EVENT_OPTIONS_NO_CAPTURE)
}
}

// VBHover directive

export const VBHover = {
bind: directive,
componentUpdated: directive,
unbind(el) {
directive(el, { value: null }
}
}

0 comments on commit 9220ae9

Please sign in to comment.