All files / atom/tooltip/src index.js

68.75% Statements 11/16
15% Branches 3/20
50% Functions 2/4
73.33% Lines 11/15

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                1x   1x                                   1x 1x                     1x 1x 1x     1x 1x                                                         1x                                                             1x                  
import {forwardRef, useCallback, useMemo, useRef} from 'react'
import useIntersection from 'react-use/lib/useIntersection'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
import UAParser from 'ua-parser-js'
 
import loadable from '@loadable/component'
 
import useControlledState from '@s-ui/react-hooks/lib/useControlledState'
 
import {
  BASE_CLASS,
  CLASS_ARROW,
  CLASS_INNER,
  CLASS_TARGET,
  COLORS,
  DEFAULT_OFFSET,
  PLACEMENTS,
  PREFIX_PLACEMENT,
  TRIGGERS
} from './config.js'
import TooltipExtendChildren from './TooltipExtendChildren.js'
 
const Tooltip = loadable(() => import('reactstrap/lib/Tooltip'), {ssr: true})
 
const AtomTooltip = forwardRef(
  (
    {
      children,
      color,
      content,
      defaultIsVisible,
      delay,
      isArrowed = true,
      isVisible,
      onToggle,
      onOpen,
      onClose,
      placement,
      trigger
    },
    forwardedRef
  ) => {
    const [isVisibleState, setIsVisibleState] = useControlledState(isVisible, defaultIsVisible)
    const toggleHandler = useCallback(
      event => {
        const newState = !isVisibleState
        newState
          ? typeof onOpen === 'function' && onOpen(event, {isVisible: newState})
          : typeof onClose === 'function' && onClose(event, {isVisible: newState})
        setIsVisibleState(newState)
        typeof onToggle === 'function' && onToggle(event, {isVisible: newState})
      },
      [setIsVisibleState, isVisibleState, onOpen, onClose, onToggle]
    )
    const childrenRef = useRef()
    const deviceType = useMemo(
      () => trigger !== undefined && new UAParser(navigator.userAgent).getDevice().type,
      [trigger]
    )
    const intersection = useIntersection(childrenRef, {threshold: 0})
    return (
      <>
        <TooltipExtendChildren ref={childrenRef} className={CLASS_TARGET}>
          {children}
        </TooltipExtendChildren>
        {intersection && intersection.isIntersecting && (
          <Tooltip
            arrowClassName={CLASS_ARROW}
            className={cx(BASE_CLASS, color && `${BASE_CLASS}--${color}Color`)}
            delay={delay}
            hideArrow={!isArrowed}
            innerClassName={CLASS_INNER}
            innerRef={forwardedRef}
            isOpen={isVisibleState}
            offset={DEFAULT_OFFSET}
            placement={placement}
            placementPrefix={PREFIX_PLACEMENT}
            target={childrenRef}
            toggle={toggleHandler}
            trigger={trigger || (['mobile', 'tablet'].includes(deviceType) ? 'legacy' : 'hover')}
          >
            {content}
          </Tooltip>
        )}
      </>
    )
  }
)
 
AtomTooltip.propTypes = {
  /** inner element wrapped by the tooltip **/
  children: PropTypes.node,
 
  /**
   * Color of tooltip: 'primary', 'accent', 'neutral', 'success', 'alert', 'error'
   */
  color: PropTypes.oneOf(Object.values(COLORS)),
 
  /** the tooltip content **/
  content: PropTypes.node,
 
  delay: PropTypes.oneOfType([PropTypes.shape({show: PropTypes.number, hide: PropTypes.number}), PropTypes.number]),
  /** Uncontrolled (initial) value for the show pop over */
  defaultIsVisible: PropTypes.bool,
  /** shows the arrow if true (default true) **/
  isArrowed: PropTypes.bool,
  /** Controlled value for the show pop over */
  isVisible: PropTypes.bool,
  /** handler fired everytime the tooltip is opened or closed **/
  onToggle: PropTypes.func,
  /** handler fired everytime the tooltip is opened **/
  onOpen: PropTypes.func,
  /** handler fired everytime the tooltip is closed **/
  onClose: PropTypes.func,
  /** the placement where the tooltip appears **/
  placement: PropTypes.oneOf(Object.values(PLACEMENTS)),
  /** defined the behavior of the tooltip **/
  trigger: PropTypes.oneOf(Object.values(TRIGGERS))
}
 
AtomTooltip.displayName = 'AtomTooltip'
 
export {AtomTooltip}
 
export {COLORS as AtomTooltipColors}
export {TRIGGERS as AtomTooltipTriggers}
export {PLACEMENTS as AtomTooltipPlacements}
 
export default AtomTooltip