import clsx from 'clsx' import type { ComponentProps, ReactElement, ReactNode } from 'react' import { cloneElement, useState } from 'react' import { usePopper } from 'react-popper' export interface TooltipProps extends ComponentProps<'div'> { label: ReactNode children: ReactElement } export const Tooltip = ({ label, children, ...props }: TooltipProps) => { const [referenceElement, setReferenceElement] = useState(null) const [popperElement, setPopperElement] = useState(null) const [show, setShow] = useState(false) const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: 'top', }) return ( <> {/* children with attached ref and mouse events */} {cloneElement(children, { ...children.props, ref: setReferenceElement, onMouseOver: () => setShow(true), onMouseOut: () => setShow(false), })} {/* popper element */} {show && (
{label}
)} ) }