{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"typed-headline-that-selects-and-retypes-one-word","type":"registry:component","title":"Headline That Retypes One Word","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/typed-headline-that-selects-and-retypes-one-word.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The fixed start of the headline and the words that get swapped in.\n// A word with a \"typo\" is typed wrong first, then corrected with the backspace key.\nconst LEAD = \"Sourdough, baked before\";\nconst WORDS = [\n  { word: \"sunrise.\", typo: \"sunrsie\" },\n  { word: \"the school run.\", typo: \"\" },\n  { word: \"you wake up.\", typo: \"\" },\n];\n\nexport default function RetypedWordHeadline() {\n  const rootRef = useRef<HTMLElement>(null);\n  const [word, setWord] = useState(WORDS[WORDS.length - 1].word);\n  const [selected, setSelected] = useState(false);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) { setWord(WORDS[WORDS.length - 1].word); setSelected(false); return; }\n    let alive = true;\n    let visible = true;\n    let timer = 0;\n    let resume: null | (() => void) = null;\n    const wait = (ms: number) =>\n      new Promise<void>((done) => {\n        const go = () => { timer = window.setTimeout(done, ms); };\n        if (visible) go();\n        else resume = go;\n      });\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && resume) { const r = resume; resume = null; r(); }\n    });\n    io.observe(el);\n    const typeOut = async (from: string, to: string) => {\n      for (let n = from.length + 1; n <= to.length && alive; n++) {\n        setWord(to.slice(0, n));\n        await wait(60 + Math.random() * 70);\n      }\n    };\n    (async () => {\n      let i = WORDS.length - 1;\n      await wait(700);\n      while (alive) {\n        setSelected(true);\n        await wait(520);\n        setSelected(false);\n        setWord(\"\");\n        await wait(180);\n        i = (i + 1) % WORDS.length;\n        const { word: w, typo } = WORDS[i];\n        if (typo) {\n          await typeOut(\"\", typo);\n          await wait(420);\n          let keep = 0;\n          while (keep < typo.length && typo[keep] === w[keep]) keep++;\n          for (let n = typo.length - 1; n >= keep && alive; n--) { setWord(typo.slice(0, n)); await wait(90); }\n          await wait(160);\n          await typeOut(w.slice(0, keep), w);\n        } else {\n          await typeOut(\"\", w);\n        }\n        await wait(1900);\n      }\n    })();\n    return () => { alive = false; window.clearTimeout(timer); io.disconnect(); };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"rwh-root px-4 py-20 sm:py-28\">\n      <style>{`\n        .rwh-root { background: radial-gradient(130% 100% at 80% 0%, #f6eddc 0%, #efe3cc 55%, #e6d6ba 100%); }\n        .rwh-h { font-family: \"Iowan Old Style\", Palatino, Georgia, serif; font-weight: 600; font-size: clamp(2.2rem, 6.4vw, 4.6rem); line-height: 1.06; letter-spacing: -0.02em; color: #2d241a; }\n        .rwh-stack { display: grid; }\n        .rwh-stack > * { grid-area: 1 / 1; }\n        .rwh-ghost { visibility: hidden; }\n        .rwh-word { color: #8a4a1f; border-radius: 4px; transition: background-color 120ms; }\n        .rwh-word[data-sel=\"true\"] { background: #e3c39c; }\n        .rwh-caret { display: inline-block; width: 3px; height: 0.9em; margin-left: 2px; vertical-align: -0.06em; background: #8a4a1f; animation: rwh-blink 1.05s steps(1) infinite; }\n        @keyframes rwh-blink { 0%, 55% { opacity: 1; } 56%, 100% { opacity: 0; } }\n        @media (prefers-reduced-motion: reduce) { .rwh-caret { animation: none; } .rwh-word { transition: none; } }\n      `}</style>\n      <div className=\"mx-auto max-w-4xl\">\n        <p className=\"text-sm font-semibold uppercase tracking-[0.18em] text-[#6b4a2a]\">Crumb &amp; Kiln</p>\n        <h1 className=\"rwh-h mt-5\">\n          <span className=\"sr-only\">{LEAD} sunrise, before the school run, before you wake up.</span>\n          <span aria-hidden=\"true\" className=\"rwh-stack\">\n            {WORDS.map((w) => (\n              <span key={w.word} className=\"rwh-ghost\">{LEAD} <span>{w.word}</span>&nbsp;</span>\n            ))}\n            <span>\n              {LEAD} <span className=\"rwh-word\" data-sel={selected ? \"true\" : \"false\"}>{word}</span>\n              <span className=\"rwh-caret\" />\n            </span>\n          </span>\n        </h1>\n        <p className=\"mt-6 max-w-lg text-base leading-relaxed text-[#4f3d2b]\">Long-fermented loaves from a wood-fired oven on Mill Row. Collect warm from seven, or have them left on your step.</p>\n        <a href=\"#\" className=\"mt-8 inline-flex h-11 items-center rounded-lg bg-[#2d241a] px-6 text-sm font-semibold text-[#f6eddc] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#8a4a1f] focus-visible:ring-offset-2\">Reserve a loaf</a>\n      </div>\n    </section>\n  );\n}\n"}]}
