All files tippy.js

100% Statements 36/36
95% Branches 19/20
100% Functions 8/8
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                          4x     82x 4x 4x       82x 99x 1x       81x           81x 1x     81x 81x   81x       88x 88x     81x         1x 1x   1x               4x 4x         66x 4x 2x 2x   4x 1x           4x         4x 5x 2x 2x 1x       4x 4x    
import { version } from '../../package.json'
import { Defaults, setDefaults } from './defaults'
import createTippy from './createTippy'
import bindGlobalEventListeners from './bindGlobalEventListeners'
import {
  isPlainObject,
  polyfillVirtualReferenceProps,
  getArrayOfElements,
  toArray,
  hideAllPoppers,
  isBrowser
} from './utils'
 
let eventListenersBound = false
 
export default function tippy(targets, options, one) {
  if (!eventListenersBound) {
    bindGlobalEventListeners()
    eventListenersBound = true
  }
 
  // Throw an error if the user supplied an invalid option
  for (const key in options || {}) {
    if (!(key in Defaults)) {
      throw Error(`[tippy]: ${key} is not a valid option`)
    }
  }
 
  const props = { ...Defaults, ...options }
 
  /**
   * If they are specifying a virtual positioning reference, we need to polyfill
   * some native DOM props
   */
  if (isPlainObject(targets)) {
    polyfillVirtualReferenceProps(targets)
  }
 
  const references = getArrayOfElements(targets)
  const firstReference = references[0]
 
  const instances = (one && firstReference
    ? [firstReference]
    : references
  ).reduce((acc, reference) => {
    const tip = reference && createTippy(reference, props)
    return tip ? acc.concat(tip) : acc
  }, [])
 
  return {
    targets,
    props,
    instances,
    destroyAll() {
      this.instances.forEach(instance => {
        instance.destroy()
      })
      this.instances = []
    }
  }
}
 
/**
 * Static props
 */
tippy.version = version
tippy.defaults = Defaults
 
/**
 * Static methods
 */
tippy.one = (targets, options) => tippy(targets, options, true).instances[0]
tippy.setDefaults = partialDefaults => {
  setDefaults(partialDefaults)
  tippy.defaults = Defaults
}
tippy.disableAnimations = () => {
  tippy.setDefaults({
    duration: 0,
    updateDuration: 0,
    animateFill: false
  })
}
tippy.hideAllPoppers = hideAllPoppers
 
/**
 * Auto-init tooltips for elements with a `data-tippy="..."` attribute
 */
export const autoInit = () => {
  toArray(document.querySelectorAll('[data-tippy]')).forEach(el => {
    const content = el.getAttribute('data-tippy')
    if (content) {
      tippy(el, { content })
    }
  })
}
Eif (isBrowser) {
  setTimeout(autoInit)
}