{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"inline-definition-tooltips-with-help-icon","type":"registry:component","title":"Inline Definition Tooltips With Help Icon","description":"","dependencies":[],"files":[{"path":"components/we-inc/inline-definition-tooltips-with-help-icon.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Glossary terms inside running text: dotted underline plus a small help icon, each a focusable button with a definition tooltip.\nconst terms: Record<string, string> = {\n  ltv: \"Loan to value: the loan as a share of the home's price. A 150,000 loan on a 200,000 home is 75%.\",\n  fixed: \"Your rate stays the same for the whole fixed period, whatever the central bank does.\",\n  erc: \"Early repayment charge: a fee for paying the loan off or switching before the deal ends.\",\n};\n\nfunction Term({ id, children, open, onEsc }: { id: string; children: string; open: boolean; onEsc: () => void }) {\n  return (\n    <span className=\"group relative inline-block\">\n      <button\n        type=\"button\"\n        aria-describedby={`tt5-${id}`}\n        onKeyDown={(e) => e.key === \"Escape\" && onEsc()}\n        className=\"inline-flex items-baseline gap-1 rounded-sm font-medium text-teal-900 underline decoration-teal-600 decoration-dotted decoration-2 underline-offset-4 hover:decoration-solid focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-teal-600\"\n      >\n        {children}\n        <svg viewBox=\"0 0 16 16\" width=\"14\" height=\"14\" aria-hidden=\"true\" className=\"translate-y-0.5 fill-teal-600\"><path d=\"M8 1a7 7 0 1 1 0 14A7 7 0 0 1 8 1zm-.8 9.8v1.6h1.6v-1.6zM8 3.6a2.6 2.6 0 0 0-2.6 2.6H7a1 1 0 1 1 1.4.9c-.7.3-1.2 1-1.2 1.8v.4h1.6v-.3c0-.2.1-.4.3-.5A2.6 2.6 0 0 0 8 3.6z\" /></svg>\n      </button>\n      <span\n        role=\"tooltip\"\n        id={`tt5-${id}`}\n        className={`pointer-events-none absolute bottom-full left-0 z-10 mb-2 w-60 rounded-lg bg-teal-950 p-3 text-left text-xs font-normal leading-relaxed text-teal-50 shadow-lg transition-opacity duration-150 group-hover:opacity-100 group-focus-within:opacity-100 motion-reduce:transition-none sm:left-1/2 sm:-translate-x-1/2 ${open ? \"!opacity-100\" : \"opacity-0\"}`}\n      >\n        {terms[id]}\n      </span>\n    </span>\n  );\n}\n\nexport default function InlineDefinitionTooltipsWithHelpIcon() {\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  const [demo, setDemo] = useState(true);\n  const [esc, setEsc] = useState<string | null>(null);\n  const stop = () => demo && setDemo(false);\n  const props = (id: string) => ({ id, open: demo && id === \"ltv\" && esc !== id, onEsc: () => { setDemo(false); setEsc(id); } });\n  return (\n    <section ref={fitRoot} className=\"overflow-x-clip bg-teal-50 px-5 pb-14 pt-24\" onPointerMove={stop}>\n      <article className=\"mx-auto max-w-xl rounded-2xl bg-white p-6 sm:p-8\">\n        <h2 className=\"text-xl font-semibold text-slate-900\">Choosing your first mortgage</h2>\n        <p className=\"mt-4 text-base leading-8 text-slate-700\">\n          Lenders price deals by your <Term {...props(\"ltv\")}>LTV</Term>, so a bigger deposit usually means a lower rate. A <Term {...props(\"fixed\")}>fixed rate</Term> makes budgeting simple, but check the <Term {...props(\"erc\")}>ERC</Term> before you sign in case you move within five years.\n        </p>\n      </article>\n    </section>\n  );\n}\n"}]}
