({ children, id, anchorEl, text, delay }: Props)
| 12 | } |
| 13 | |
| 14 | export default function Tooltip({ children, id, anchorEl, text, delay }: Props): JSX.Element { |
| 15 | const [open, setOpen] = useState(false) |
| 16 | const [referenceElement, setReferenceElement] = useState<HTMLElement | null>(null) |
| 17 | const [popperElement, setPopperElement] = useState<HTMLElement | null>(null) |
| 18 | const [arrowElement, setArrowElement] = useState<HTMLElement | null>(null) |
| 19 | const [container, setContainer] = useState<Element | null>(null) |
| 20 | const { styles: popperStyles, attributes } = usePopper(referenceElement, popperElement, { |
| 21 | modifiers: [ |
| 22 | { |
| 23 | name: 'arrow', |
| 24 | options: { |
| 25 | element: arrowElement, |
| 26 | }, |
| 27 | }, |
| 28 | { |
| 29 | name: 'offset', |
| 30 | options: { |
| 31 | offset: [0, 8], |
| 32 | }, |
| 33 | }, |
| 34 | ], |
| 35 | }) |
| 36 | |
| 37 | const timeout = useRef<number | null>(null) |
| 38 | const tooltipId = `${id}_tooltip` |
| 39 | |
| 40 | useEffect(() => { |
| 41 | if (anchorEl) { |
| 42 | if (typeof anchorEl === 'string') { |
| 43 | setContainer(document.querySelector(anchorEl)) |
| 44 | } else { |
| 45 | setContainer(anchorEl) |
| 46 | } |
| 47 | } else { |
| 48 | setContainer(document.body) |
| 49 | } |
| 50 | }, [container, anchorEl]) |
| 51 | |
| 52 | useEffect(() => { |
| 53 | const showEvents = ['mouseenter', 'focus'] |
| 54 | const hideEvents = ['mouseleave', 'blur'] |
| 55 | |
| 56 | const handleOpen = () => { |
| 57 | // There is no point in displaying an empty tooltip. |
| 58 | if (text === '') { |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | // Remove the title ahead of time to avoid displaying |
| 63 | // two tooltips at the same time (native + this one). |
| 64 | referenceElement?.removeAttribute('title') |
| 65 | |
| 66 | timeout.current = window.setTimeout(() => { |
| 67 | setOpen(true) |
| 68 | }, delay || 300) |
| 69 | } |
| 70 | |
| 71 | const handleClose = () => { |
nothing calls this directly
no outgoing calls
no test coverage detected