{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"react-tooltip-with-hover-delay-and-escape-to-close","type":"registry:component","title":"React Tooltip With Hover Delay And Escape To Close","description":"","dependencies":[],"files":[{"path":"components/we-inc/react-tooltip-with-hover-delay-and-escape-to-close.tsx","type":"registry:component","content":"import { useEffect, useId, useRef, useState, type ReactNode } from \"react\";\n\n// A reusable React tooltip: opens after a short hover delay or at once on keyboard focus,\n// stays open while the pointer is over it, and closes on Escape.\nconst OPEN_DELAY = 350;\nconst CLOSE_DELAY = 120;\n\nfunction Tooltip({ text, children, startOpen = false }: { text: string; children: (props: { \"aria-describedby\": string }) => ReactNode; startOpen?: boolean }) {\n  const id = useId();\n  const [open, setOpen] = useState(startOpen);\n  const timer = useRef<number>();\n  const later = (v: boolean, ms: number) => { window.clearTimeout(timer.current); timer.current = window.setTimeout(() => setOpen(v), ms); };\n  useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  return (\n    <span\n      className=\"relative inline-flex\"\n      onPointerEnter={() => later(true, OPEN_DELAY)}\n      onPointerLeave={() => later(false, CLOSE_DELAY)}\n      onFocus={() => { window.clearTimeout(timer.current); setOpen(true); }}\n      onBlur={() => later(false, 0)}\n      onKeyDown={(e) => { if (e.key === \"Escape\") { window.clearTimeout(timer.current); setOpen(false); } }}\n    >\n      {children({ \"aria-describedby\": id })}\n      <span\n        role=\"tooltip\"\n        id={id}\n        className={`absolute bottom-full left-1/2 z-10 mb-2 w-max max-w-[14rem] -translate-x-1/2 rounded-lg bg-emerald-950 px-3 py-2 text-xs leading-snug text-emerald-50 shadow-lg transition duration-150 motion-reduce:transition-none ${open ? \"opacity-100\" : \"pointer-events-none translate-y-1 opacity-0\"}`}\n      >\n        {text}\n        <span aria-hidden=\"true\" className=\"absolute left-1/2 top-full -mt-1 h-2 w-2 -translate-x-1/2 rotate-45 bg-emerald-950\" />\n      </span>\n    </span>\n  );\n}\n\nconst posts = [\n  { title: \"Planting garlic in October\", status: \"Published\", tip: \"Live on the site since 12 October.\", tone: \"bg-emerald-100 text-emerald-900\" },\n  { title: \"What to sow under glass\", status: \"Scheduled\", tip: \"Goes live on Friday at 07:00 for subscribers first.\", tone: \"bg-amber-100 text-amber-900\" },\n  { title: \"Saving tomato seed\", status: \"Draft\", tip: \"Only you and editors can see this post.\", tone: \"bg-slate-100 text-slate-800\" },\n];\n\nexport default function ReactTooltipWithHoverDelayAndEscapeToClose() {\n\n  // Keep every tooltip inside the screen: measure on mount and resize, shift sideways if it would overflow.\n  const fitRoot = useRef<HTMLElement>(null);\n  useEffect(() => {\n    const fit = () => {\n      const w = document.documentElement.clientWidth;\n      fitRoot.current?.querySelectorAll<HTMLElement>(\"[role=tooltip]\").forEach((t) => {\n        t.style.translate = \"0px 0px\";\n        const r = t.getBoundingClientRect();\n        let dx = 0;\n        if (r.right > w - 8) dx = w - 8 - r.right;\n        if (r.left + dx < 8) dx = 8 - r.left;\n        t.style.translate = `${dx}px 0px`;\n        const arrow = t.querySelector<HTMLElement>(\"[aria-hidden]\");\n        if (arrow) arrow.style.translate = `${-dx}px 0px`;\n      });\n    };\n    fit();\n    window.addEventListener(\"resize\", fit);\n    return () => window.removeEventListener(\"resize\", fit);\n  }, []);\n  return (\n    <section ref={fitRoot} className=\"bg-lime-50 px-5 pb-12 pt-16\">\n      <div className=\"mx-auto max-w-xl rounded-2xl border border-lime-200 bg-white\">\n        <h2 className=\"border-b border-lime-100 px-5 py-4 text-base font-semibold text-slate-900\">Allotment Notes: posts</h2>\n        <ul className=\"divide-y divide-lime-100\">\n          {posts.map((p, i) => (\n            <li key={p.title} className=\"flex items-center justify-between gap-4 px-5 py-4 pt-6 first:pt-12\">\n              <span className=\"text-sm font-medium text-slate-800\">{p.title}</span>\n              <Tooltip text={p.tip} startOpen={i === 0}>\n                {(a) => (\n                  <button type=\"button\" {...a} className={`min-h-[32px] rounded-full px-3 text-xs font-semibold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-700 focus-visible:ring-offset-2 ${p.tone}`}>\n                    {p.status}\n                  </button>\n                )}\n              </Tooltip>\n            </li>\n          ))}\n        </ul>\n      </div>\n    </section>\n  );\n}\n"}]}
