import { arrow, FloatingArrow, FloatingPortal, offset, useFloating, useHover, useInteractions, useTransitionStyles, } from '@floating-ui/react'; import React, { useRef, useState } from 'react'; interface ITooltip { /** Element to display the tooltip around. */ children: React.ReactElement> /** Text to display in the tooltip. */ text: string } /** * Tooltip */ const Tooltip: React.FC = (props) => { const { children, text } = props; const [isOpen, setIsOpen] = useState(false); const arrowRef = useRef(null); const { x, y, strategy, refs, context } = useFloating({ open: isOpen, onOpenChange: setIsOpen, placement: 'top', middleware: [ offset(6), arrow({ element: arrowRef, }), ], }); const hover = useHover(context); const { isMounted, styles } = useTransitionStyles(context, { initial: { opacity: 0, transform: 'scale(0.8)', }, duration: { open: 200, close: 200, }, }); const { getReferenceProps, getFloatingProps } = useInteractions([ hover, ]); return ( <> {React.cloneElement(children, { ref: refs.setReference, ...getReferenceProps(), })} {(isMounted) && (
{text}
)} ); }; export default Tooltip;