import { type ReactNode, useRef, useState } from "react"; import { createPortal } from "react-dom"; interface TooltipProps { content: string; children: ReactNode; className?: string; } export function Tooltip({ content, children, className, }: Readonly) { const ref = useRef(null); const [pos, setPos] = useState<{ top: number; left: number } | null>(null); function show() { const el = ref.current; if (!el) return; const rect = el.getBoundingClientRect(); setPos({ top: rect.top - 4, left: rect.left + rect.width / 2, }); } function hide() { setPos(null); } return ( <> {children} {pos !== null && createPortal(
{content}
, document.body, )} ); }