{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shop-support-reply-typed-out-word-by-word","type":"registry:component","title":"Shop Support Reply Typed Word by Word","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/shop-support-reply-typed-out-word-by-word.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The customer's question and the shop's answer, which arrives word by word.\nconst QUESTION = \"Hi, will the speckled mugs be back before the 20th? It\\'s for my sister\\'s birthday.\";\nconst REPLY = \"Good news: a fresh batch of speckled mugs comes out of the kiln on Thursday. I can set two aside for you now and post them Friday morning, so they arrive in plenty of time.\";\nconst WORDS = REPLY.split(\" \");\nconst START_AT = 11; // words already shown on load, so the preview is never empty\n\nexport default function ShopSupportReplyTyped() {\n  const rootRef = useRef<HTMLElement>(null);\n  const [shown, setShown] = useState(START_AT);\n  const [thinking, setThinking] = useState(false);\n  const [still, setStill] = useState(START_AT); // words that appear without the fade-in\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) { setShown(WORDS.length); setStill(WORDS.length); setThinking(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    (async () => {\n      let n = START_AT;\n      while (alive) {\n        while (n < WORDS.length && alive) {\n          const prev = WORDS[n - 1] ?? \"\";\n          await wait(/[.,:]$/.test(prev) ? 360 : 70 + Math.random() * 110);\n          n++;\n          setShown(n);\n        }\n        await wait(4200);\n        n = 0;\n        setShown(0);\n        setStill(0);\n        setThinking(true);\n        await wait(1500);\n        setThinking(false);\n      }\n    })();\n    return () => { alive = false; window.clearTimeout(timer); io.disconnect(); };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"ssr-root px-4 py-16 sm:py-20\">\n      <style>{`\n        .ssr-root { background: linear-gradient(180deg, #f4f1ec 0%, #ebe6de 100%); }\n        .ssr-card { max-width: 30rem; margin: 0 auto; border-radius: 20px; background: #ffffff; border: 1px solid #e2dbd0; box-shadow: 0 24px 50px -28px rgba(60,45,30,0.35); overflow: hidden; }\n        .ssr-top { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid #eee8df; }\n        .ssr-av { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: #9b5a3c; color: #fff; font-size: 13px; font-weight: 600; }\n        .ssr-name { font-size: 14px; font-weight: 600; color: #2b2520; }\n        .ssr-status { font-size: 12px; color: #5f6b5a; }\n        .ssr-thread { display: flex; flex-direction: column; gap: 12px; padding: 20px 18px 24px; }\n        .ssr-msg { max-width: 85%; padding: 11px 14px; border-radius: 16px; font-size: 15px; line-height: 1.5; }\n        .ssr-me { align-self: flex-end; background: #2f3b33; color: #f3f1ec; border-bottom-right-radius: 5px; }\n        .ssr-them { align-self: flex-start; background: #f3ede4; color: #2b2520; border-bottom-left-radius: 5px; }\n        .ssr-stack { display: grid; }\n        .ssr-stack > * { grid-area: 1 / 1; }\n        .ssr-ghost { visibility: hidden; }\n        .ssr-w { display: inline; animation: ssr-in 260ms ease-out both; }\n        @keyframes ssr-in { from { opacity: 0; filter: blur(2px); } to { opacity: 1; filter: blur(0); } }\n        .ssr-dots { display: flex; gap: 5px; align-items: center; height: 1.5em; }\n        .ssr-dots i { width: 7px; height: 7px; border-radius: 50%; background: #9b8a78; animation: ssr-bob 1.1s ease-in-out infinite; }\n        .ssr-dots i:nth-child(2) { animation-delay: 0.15s; } .ssr-dots i:nth-child(3) { animation-delay: 0.3s; }\n        @keyframes ssr-bob { 0%, 60%, 100% { transform: translateY(0); opacity: 0.55; } 30% { transform: translateY(-4px); opacity: 1; } }\n        .ssr-time { font-size: 11px; color: #6b6158; }\n        @media (prefers-reduced-motion: reduce) { .ssr-w, .ssr-dots i { animation: none; } }\n      `}</style>\n      <div className=\"ssr-card\">\n        <div className=\"ssr-top\">\n          <span className=\"ssr-av\" aria-hidden=\"true\">MO</span>\n          <div>\n            <h2 className=\"ssr-name\">Maya at Kiln &amp; Clay</h2>\n            <p className=\"ssr-status\">Usually replies within the hour</p>\n          </div>\n        </div>\n        <div className=\"ssr-thread\">\n          <p className=\"ssr-msg ssr-me\"><span className=\"sr-only\">You said: </span>{QUESTION}</p>\n          <div className=\"ssr-msg ssr-them\">\n            <p className=\"sr-only\">Maya replied: {REPLY}</p>\n            <div className=\"ssr-stack\" aria-hidden=\"true\">\n              <span className=\"ssr-ghost\">{REPLY}</span>\n              {thinking || shown === 0 ? (\n                <span className=\"ssr-dots\"><i /><i /><i /></span>\n              ) : (\n                <span>\n                  {WORDS.slice(0, shown).map((w, i) => (\n                    <span key={i} className=\"ssr-w\" style={i < still ? { animation: \"none\" } : undefined}>{w} </span>\n                  ))}\n                </span>\n              )}\n            </div>\n          </div>\n          <span className=\"ssr-time\" aria-hidden=\"true\">Seen 2:14 pm</span>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
