{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"type-and-delete-phrase-loop-with-natural-typing-rhythm","type":"registry:component","title":"Type and Delete Loop with Natural Rhythm","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/type-and-delete-phrase-loop-with-natural-typing-rhythm.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The rotating endings. The longest one sets the reserved box, so nothing below moves.\nconst PHRASES = [\"a north-facing window.\", \"the shelf the cat ignores.\", \"people who forget to water.\"];\nconst HOLD_MS = 1700; // how long a finished phrase stays\nconst BASE_MS = 48; // typical gap between letters\n\n// Human rhythm: a little jitter per letter, longer after spaces and punctuation.\nfunction gapAfter(ch: string) {\n  const jitter = BASE_MS + Math.random() * 70;\n  if (ch === \",\" || ch === \";\") return jitter + 260;\n  if (ch === \"-\") return jitter + 120;\n  if (ch === \" \") return jitter + (Math.random() < 0.18 ? 240 : 40);\n  return jitter;\n}\n\nexport default function TypeAndDeletePhraseLoop() {\n  const rootRef = useRef<HTMLElement>(null);\n  const [text, setText] = useState(PHRASES[0]);\n  const [typing, setTyping] = useState(false);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) {\n      setText(PHRASES[0]);\n      return;\n    }\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\n    (async () => {\n      let index = 0;\n      await wait(900);\n      while (alive) {\n        const current = PHRASES[index];\n        setTyping(true);\n        for (let n = current.length; n >= 0 && alive; n--) {\n          setText(current.slice(0, n));\n          await wait(n > current.length - 4 ? 70 : 26);\n        }\n        index = (index + 1) % PHRASES.length;\n        const next = PHRASES[index];\n        await wait(380);\n        for (let n = 1; n <= next.length && alive; n++) {\n          setText(next.slice(0, n));\n          await wait(gapAfter(next[n - 1]));\n        }\n        if (!alive) break;\n        setTyping(false);\n        await wait(HOLD_MS);\n      }\n    })();\n\n    return () => {\n      alive = false;\n      window.clearTimeout(timer);\n      io.disconnect();\n    };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"tdl-root relative overflow-hidden px-4 py-20 sm:py-28\">\n      <style>{`\n        .tdl-root { background: radial-gradient(120% 90% at 20% 0%, #1f3a2c 0%, #142a20 45%, #0e1f17 100%); }\n        .tdl-h { font-family: \"Iowan Old Style\", Palatino, Georgia, serif; font-weight: 500; font-size: clamp(2.1rem, 6vw, 4rem); line-height: 1.12; color: #f2ecdc; letter-spacing: -0.01em; }\n        .tdl-stack { display: grid; }\n        .tdl-stack > * { grid-area: 1 / 1; }\n        .tdl-ghost { visibility: hidden; }\n        .tdl-typed { color: #c9dcb8; font-style: italic; }\n        .tdl-caret { display: inline-block; width: 3px; height: 0.95em; margin-left: 3px; vertical-align: -0.08em; background: #c9dcb8; border-radius: 1px; animation: tdl-blink 1.05s steps(1) infinite; }\n        [data-typing=\"true\"] .tdl-caret { animation: none; }\n        @keyframes tdl-blink { 0%, 55% { opacity: 1; } 56%, 100% { opacity: 0; } }\n        @media (prefers-reduced-motion: reduce) { .tdl-caret { animation: none; } }\n      `}</style>\n      <div className=\"mx-auto max-w-3xl\" data-typing={typing ? \"true\" : \"false\"}>\n        <p className=\"text-sm font-medium tracking-wide text-[#b9cdb0]\">Greenhouse No. 4, Ashby Road</p>\n        <h1 className=\"tdl-h mt-5\">\n          <span className=\"sr-only\">Plants for {PHRASES.join(\", for \")}</span>\n          <span aria-hidden=\"true\">\n            <span className=\"block\">Plants for</span>\n            <span className=\"tdl-stack\">\n              {PHRASES.map((p) => (\n                <span key={p} className=\"tdl-ghost tdl-typed\">{p}</span>\n              ))}\n              <span>\n                <span className=\"tdl-typed\">{text}</span>\n                <span className=\"tdl-caret\" />\n              </span>\n            </span>\n          </span>\n        </h1>\n        <p className=\"mt-6 max-w-md text-base leading-relaxed text-[#cfd8c8]\">Tell us about the light in your room and we will pot up something that will still be thriving next spring.</p>\n        <a href=\"#\" className=\"mt-8 inline-flex h-11 items-center rounded-full bg-[#e9e2cc] px-6 text-sm font-semibold text-[#142a20] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#c9dcb8] focus-visible:ring-offset-2 focus-visible:ring-offset-[#142a20]\">Find my plant</a>\n      </div>\n    </section>\n  );\n}\n"}]}
