{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"typewritten-letter-on-paper-with-uneven-ink-and-caret","type":"registry:component","title":"Typewritten Letter on Paper","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/typewritten-letter-on-paper-with-uneven-ink-and-caret.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The letter. Line breaks are kept; each new line gets a carriage-return pause.\nconst LETTER = \"Dear Mrs. Okafor,\\n\\nYour armchair is ready. We kept the original springs and covered it in the moss wool you chose in March.\\n\\nCollect it any weekday after ten, or we can bring it round on Saturday.\\n\\nWith thanks,\\nRuth Hale\";\nconst START_AT = Math.floor(LETTER.length * 0.5); // begins part-way so the page is never blank\n\n// Same seed every time, so the ink looks uneven but never flickers between renders.\nfunction ink(i: number) {\n  const a = Math.sin(i * 12.9898) * 43758.5453;\n  const b = Math.sin(i * 78.233) * 12543.1;\n  const h1 = a - Math.floor(a);\n  const h2 = b - Math.floor(b);\n  return { opacity: 0.72 + h1 * 0.28, top: `${((h2 - 0.5) * 0.9).toFixed(2)}px` };\n}\n\nfunction gapAfter(ch: string) {\n  if (ch === \"\\n\") return 430;\n  if (ch === \".\" || ch === \",\") return 210 + Math.random() * 120;\n  if (ch === \" \") return 70 + Math.random() * 60;\n  return 30 + Math.random() * 40;\n}\n\nexport default function TypewrittenLetter() {\n  const rootRef = useRef<HTMLElement>(null);\n  const [count, setCount] = useState(START_AT);\n  const [fade, setFade] = useState(false);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) { setCount(LETTER.length); 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    (async () => {\n      let n = START_AT;\n      while (alive) {\n        while (n < LETTER.length && alive) {\n          await wait(gapAfter(LETTER[n - 1] ?? \"\"));\n          n++;\n          setCount(n);\n        }\n        await wait(3600);\n        setFade(true);\n        await wait(700);\n        n = 0;\n        setCount(0);\n        setFade(false);\n        await wait(500);\n      }\n    })();\n    return () => { alive = false; window.clearTimeout(timer); io.disconnect(); };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"twl-root px-4 py-16 sm:py-20\">\n      <style>{`\n        .twl-root { background: radial-gradient(120% 80% at 50% 0%, #46525a 0%, #2f383e 60%, #262d32 100%); }\n        .twl-paper { position: relative; max-width: 36rem; margin: 0 auto; padding: 44px clamp(22px, 6vw, 56px) 52px; border-radius: 3px; color: #1f1b17;\n          background: linear-gradient(180deg, rgba(0,0,0,0) 48.6%, rgba(90,70,40,0.07) 49.8%, rgba(255,255,255,0.35) 50.4%, rgba(0,0,0,0) 51.5%), linear-gradient(170deg, #fbf6ea 0%, #f4ecdb 100%);\n          box-shadow: 0 1px 1px rgba(0,0,0,0.2), 0 24px 50px -20px rgba(0,0,0,0.6), 0 8px 18px -8px rgba(0,0,0,0.35); }\n        .twl-head { font-family: Georgia, serif; font-size: 13px; letter-spacing: 0.16em; text-transform: uppercase; color: #5a4632; border-bottom: 1px solid rgba(90,70,50,0.25); padding-bottom: 14px; margin: 0 0 26px; }\n        .twl-type { font-family: \"American Typewriter\", \"Courier Prime\", \"Courier New\", Courier, monospace; font-size: clamp(14px, 3.6vw, 17px); line-height: 1.7; white-space: pre-wrap; overflow-wrap: break-word; }\n        .twl-stack { display: grid; }\n        .twl-stack > * { grid-area: 1 / 1; }\n        .twl-ghost { visibility: hidden; }\n        .twl-live { transition: opacity 600ms ease; }\n        .twl-live[data-fade=\"true\"] { opacity: 0; }\n        .twl-ch { position: relative; text-shadow: 0 0 0.6px rgba(31,27,23,0.55); }\n        .twl-caret { display: inline-block; width: 0.55em; height: 2px; vertical-align: -0.2em; background: #1f1b17; opacity: 0.8; animation: twl-blink 0.9s steps(1) infinite; }\n        @keyframes twl-blink { 0%, 55% { opacity: 0.8; } 56%, 100% { opacity: 0; } }\n        @media (prefers-reduced-motion: reduce) { .twl-caret { animation: none; } .twl-live { transition: none; } }\n      `}</style>\n      <article className=\"twl-paper\">\n        <h2 className=\"twl-head\">Hale &amp; Daughter Upholstery</h2>\n        <p className=\"sr-only\">{LETTER.replace(/\\n+/g, \" \")}</p>\n        <div className=\"twl-type twl-stack\" aria-hidden=\"true\">\n          <div className=\"twl-ghost\">{LETTER}</div>\n          <div className=\"twl-live\" data-fade={fade ? \"true\" : \"false\"}>\n            {[...LETTER.slice(0, count)].map((ch, i) => (\n              <span key={i} className=\"twl-ch\" style={ink(i)}>{ch}</span>\n            ))}\n            <span className=\"twl-caret\" />\n          </div>\n        </div>\n      </article>\n    </section>\n  );\n}\n"}]}
