{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"code-snippet-typing-animation-with-syntax-colours","type":"registry:component","title":"Code Snippet Typing with Syntax Colours","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/code-snippet-typing-animation-with-syntax-colours.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The snippet as coloured tokens: c comment, k keyword, f function, v name, s string, p punctuation, n new line, t indent.\nconst TOKENS: [string, string][] = [[\"c\", \"// Opening hours for the shop footer\"], [\"n\", \"\\n\"], [\"k\", \"export function \"], [\"f\", \"hoursFor\"], [\"p\", \"(day) {\"], [\"n\", \"\\n\"], [\"t\", \"  \"], [\"k\", \"const \"], [\"v\", \"week\"], [\"p\", \" = { \"], [\"v\", \"sat\"], [\"p\", \": \"], [\"s\", \"\\\"9-4\\\"\"], [\"p\", \", \"], [\"v\", \"sun\"], [\"p\", \": \"], [\"s\", \"\\\"shut\\\"\"], [\"p\", \" };\"], [\"n\", \"\\n\"], [\"t\", \"  \"], [\"k\", \"return \"], [\"v\", \"week\"], [\"p\", \"[day] ?? \"], [\"s\", \"\\\"8-6\\\"\"], [\"p\", \";\"], [\"n\", \"\\n\"], [\"p\", \"}\"], [\"n\", \"\\n\"], [\"n\", \"\\n\"], [\"v\", \"footer\"], [\"p\", \".\"], [\"f\", \"textContent\"], [\"p\", \" = \"], [\"f\", \"hoursFor\"], [\"p\", \"(\"], [\"s\", \"\\\"sat\\\"\"], [\"p\", \");\"]];\nconst FULL = TOKENS.map(([, t]) => t).join(\"\");\nconst LINES = FULL.split(\"\\n\").length;\nconst START_AT = Math.floor(FULL.length * 0.55); // begins part-way so the editor is never empty\n\n// Cut the token list after n characters, keeping each piece's colour.\nfunction slice(n: number) {\n  const out: [string, string][] = [];\n  let left = n;\n  for (const [kind, text] of TOKENS) {\n    if (left <= 0) break;\n    out.push([kind, text.slice(0, left)]);\n    left -= text.length;\n  }\n  return out;\n}\n\nfunction gapAfter(ch: string) {\n  if (ch === \"\\n\") return 260;\n  if (ch === \" \") return 40 + Math.random() * 50;\n  if (\"({;\".includes(ch)) return 120 + Math.random() * 80;\n  return 22 + Math.random() * 30;\n}\n\nexport default function CodeSnippetTyping() {\n  const rootRef = useRef<HTMLElement>(null);\n  const [count, setCount] = useState(START_AT);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el || reduce) { setCount(FULL.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 < FULL.length && alive) { await wait(gapAfter(FULL[n - 1] ?? \"\")); n++; setCount(n); }\n        await wait(3200);\n        n = 0;\n        setCount(0);\n        await wait(600);\n      }\n    })();\n    return () => { alive = false; window.clearTimeout(timer); io.disconnect(); };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"cst-root px-4 py-16 sm:py-24\">\n      <style>{`\n        .cst-root { background: linear-gradient(180deg, #0d1219 0%, #111a24 100%); }\n        .cst-win { max-width: 44rem; margin: 0 auto; border-radius: 12px; background: #151c25; border: 1px solid #243040; box-shadow: 0 30px 70px -30px rgba(0,0,0,0.7); overflow: hidden; }\n        .cst-tab { display: flex; align-items: center; gap: 10px; height: 40px; padding: 0 16px; background: #10161e; border-bottom: 1px solid #243040; font-size: 12px; color: #9aa7b6; }\n        .cst-tab b { font-weight: 500; color: #d7dde5; }\n        .cst-code { display: grid; grid-template-columns: auto 1fr; gap: 0 16px; padding: 20px 16px 24px; font-family: ui-monospace, \"SF Mono\", Menlo, Consolas, monospace; font-size: clamp(10.5px, 2.8vw, 15px); line-height: 1.75; }\n        .cst-nums { color: #5d6b7c; text-align: right; user-select: none; white-space: pre; }\n        .cst-stack { display: grid; min-width: 0; overflow-x: auto; }\n        .cst-stack > * { grid-area: 1 / 1; white-space: pre; }\n        .cst-ghost { visibility: hidden; }\n        .cst-c { color: #8996a5; font-style: italic; } .cst-k { color: #d8a26a; } .cst-f { color: #85b6dc; } .cst-v { color: #e2d6bf; } .cst-s { color: #a3c79a; } .cst-p { color: #b6c0cc; } .cst-t { color: inherit; }\n        .cst-caret { display: inline-block; width: 2px; height: 1.2em; vertical-align: -0.25em; background: #e8b77a; animation: cst-blink 1s steps(1) infinite; }\n        @keyframes cst-blink { 0%, 55% { opacity: 1; } 56%, 100% { opacity: 0; } }\n        .cst-note { max-width: 44rem; margin: 18px auto 0; text-align: center; font-size: 14px; color: #a9b4c1; }\n        @media (prefers-reduced-motion: reduce) { .cst-caret { animation: none; } }\n      `}</style>\n      <h2 className=\"sr-only\">Code example: opening hours function</h2>\n      <pre className=\"sr-only\">{FULL}</pre>\n      <div className=\"cst-win\" aria-hidden=\"true\">\n        <div className=\"cst-tab\"><b>hours.js</b><span>footer helpers</span></div>\n        <div className=\"cst-code\">\n          <div className=\"cst-nums\">{Array.from({ length: LINES }, (_, i) => i + 1).join(\"\\n\")}</div>\n          <div className=\"cst-stack\">\n            <div className=\"cst-ghost\">{FULL}</div>\n            <div>\n              {slice(count).map(([kind, text], i) => (\n                <span key={i} className={`cst-${kind}`}>{text}</span>\n              ))}\n              <span className=\"cst-caret\" />\n            </div>\n          </div>\n        </div>\n      </div>\n      <p className=\"cst-note\">Each token keeps its colour while it is being typed.</p>\n    </section>\n  );\n}\n"}]}
