import { useRef, useState, useSyncExternalStore } from "react"; import { createPortal } from "react-dom"; import { hintsEnabled, subscribeHints } from "../lib/hints"; type Side = "top" | "bottom"; type Coords = { left: number; top: number; placement: Side }; /** Wrap any element to show a short glass hint caption on hover — but only while the * app-wide hints toggle (Settings → Appearance) is on. Rendered in a portal with * fixed positioning so it is never clipped by an overflow/stacking ancestor. */ export default function Tooltip({ hint, side = "top", children, }: { hint: string; side?: Side; children: React.ReactNode; }) { const enabled = useSyncExternalStore(subscribeHints, hintsEnabled, hintsEnabled); const ref = useRef(null); const [coords, setCoords] = useState(null); function show() { const el = ref.current; if (!el) return; const r = el.getBoundingClientRect(); // Prefer the requested side; flip to bottom if there's no room above. const placement: Side = side === "bottom" || r.top < 90 ? "bottom" : "top"; setCoords({ left: Math.min(Math.max(r.left + r.width / 2, 92), window.innerWidth - 92), top: placement === "top" ? r.top - 8 : r.bottom + 8, placement, }); } function hide() { setCoords(null); } if (!enabled || !hint) return <>{children}; return ( {children} {coords && createPortal(
{hint}
, document.body )}
); }