|
| 1 | +import React, { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { usePopper } from "react-popper"; |
| 3 | +import PT from "prop-types"; |
| 4 | +import cn from "classnames"; |
| 5 | +import compStyles from "./styles.module.scss"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Displays a tooltip |
| 9 | + * |
| 10 | + * @param {Object} props component properties |
| 11 | + * @param {any} props.children tooltip target |
| 12 | + * @param {string} [props.className] class name to be added to root element |
| 13 | + * @param {any} props.content tooltip content |
| 14 | + * @param {number} [props.delay] postpone showing the tooltip after this delay |
| 15 | + * @param {boolean} [props.isDisabled] whether the tooltip is disabled |
| 16 | + * @param {import('@popperjs/core').Placement} [props.placement] tooltip's |
| 17 | + * preferred placement as defined in PopperJS documentation |
| 18 | + * @param {'absolute'|'fixed'} [props.strategy] tooltip positioning strategy |
| 19 | + * as defined in PopperJS documentation |
| 20 | + * @param {string} [props.targetClassName] class name to be added to element |
| 21 | + * wrapping around component's children |
| 22 | + * @param {string} [props.tooltipClassName] class name to be added to tooltip |
| 23 | + * element itself |
| 24 | + * @returns {JSX.Element} |
| 25 | + */ |
| 26 | +const Tooltip = ({ |
| 27 | + children, |
| 28 | + className, |
| 29 | + content, |
| 30 | + delay = 150, |
| 31 | + isDisabled = false, |
| 32 | + placement = "top", |
| 33 | + strategy = "absolute", |
| 34 | + targetClassName, |
| 35 | + tooltipClassName, |
| 36 | +}) => { |
| 37 | + const containerRef = useRef(null); |
| 38 | + const timeoutIdRef = useRef(0); |
| 39 | + const [isTooltipShown, setIsTooltipShown] = useState(false); |
| 40 | + const [referenceElement, setReferenceElement] = useState(null); |
| 41 | + const [popperElement, setPopperElement] = useState(null); |
| 42 | + const [arrowElement, setArrowElement] = useState(null); |
| 43 | + const { styles, attributes, update } = usePopper( |
| 44 | + referenceElement, |
| 45 | + popperElement, |
| 46 | + { |
| 47 | + placement, |
| 48 | + strategy, |
| 49 | + modifiers: [ |
| 50 | + { name: "arrow", options: { element: arrowElement, padding: 10 } }, |
| 51 | + { name: "offset", options: { offset: [0, 10] } }, |
| 52 | + { name: "preventOverflow", options: { padding: 15 } }, |
| 53 | + ], |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + const onMouseEnter = useCallback(() => { |
| 58 | + timeoutIdRef.current = window.setTimeout(() => { |
| 59 | + timeoutIdRef.current = 0; |
| 60 | + setIsTooltipShown(true); |
| 61 | + }, delay); |
| 62 | + }, [delay]); |
| 63 | + |
| 64 | + const onMouseLeave = useCallback(() => { |
| 65 | + if (timeoutIdRef.current) { |
| 66 | + clearTimeout(timeoutIdRef.current); |
| 67 | + } |
| 68 | + setIsTooltipShown(false); |
| 69 | + }, []); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + let observer = null; |
| 73 | + if (isTooltipShown && popperElement && update) { |
| 74 | + observer = new ResizeObserver(update); |
| 75 | + observer.observe(popperElement); |
| 76 | + } |
| 77 | + return () => { |
| 78 | + if (observer) { |
| 79 | + observer.unobserve(popperElement); |
| 80 | + } |
| 81 | + }; |
| 82 | + }, [isTooltipShown, popperElement, update]); |
| 83 | + |
| 84 | + return ( |
| 85 | + <div |
| 86 | + className={cn(compStyles.container, className)} |
| 87 | + ref={containerRef} |
| 88 | + onMouseEnter={isDisabled ? null : onMouseEnter} |
| 89 | + onMouseLeave={isDisabled ? null : onMouseLeave} |
| 90 | + > |
| 91 | + <span |
| 92 | + className={cn(compStyles.target, targetClassName)} |
| 93 | + ref={setReferenceElement} |
| 94 | + > |
| 95 | + {children} |
| 96 | + </span> |
| 97 | + {!isDisabled && isTooltipShown && ( |
| 98 | + <div |
| 99 | + ref={setPopperElement} |
| 100 | + className={cn(compStyles.tooltip, tooltipClassName)} |
| 101 | + style={styles.popper} |
| 102 | + {...attributes.popper} |
| 103 | + > |
| 104 | + {content} |
| 105 | + <div |
| 106 | + className={compStyles.tooltipArrow} |
| 107 | + ref={setArrowElement} |
| 108 | + style={styles.arrow} |
| 109 | + /> |
| 110 | + </div> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +Tooltip.propTypes = { |
| 117 | + children: PT.node, |
| 118 | + className: PT.string, |
| 119 | + content: PT.node, |
| 120 | + delay: PT.number, |
| 121 | + isDisabled: PT.bool, |
| 122 | + placement: PT.string, |
| 123 | + strategy: PT.oneOf(["absolute", "fixed"]), |
| 124 | + targetClassName: PT.string, |
| 125 | + tooltipClassName: PT.string, |
| 126 | +}; |
| 127 | + |
| 128 | +export default Tooltip; |
0 commit comments