{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rotating-headline-scrambles-between-three-phrases","type":"registry:block","title":"Rotating Headline Scrambles Between Three Phrases","description":"","dependencies":[],"files":[{"path":"components/we-inc/rotating-headline-scrambles-between-three-phrases.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\nconst PHRASES = [\"heritage wheat.\", \"whole spelt.\", \"dark rye.\"]; // keep them short; the longest sets the width\nconst LABELS = [\"Wheat\", \"Spelt\", \"Rye\"];\nconst GLYPHS = \"abcdefghkmnprstuvwxyz#/+=\";\nconst STEP = 42; // ms between one letter settling and the next\nconst LEAD = 180; // ms of noise before the first letter settles\nconst HOLD = 3600; // ms each phrase rests before the next one\nconst W = Math.max(...PHRASES.map((p) => p.length));\nconst pad = (s: string) => s.padEnd(W, \" \");\n\nexport default function RotatingHeadlineScramblesBetweenThreePhrases() {\n  const root = useRef<HTMLElement>(null);\n  const out = useRef<HTMLSpanElement>(null);\n  const goRef = useRef<(n: number, manual?: boolean) => void>(() => {});\n  const [cur, setCur] = useState(0);\n\n  useEffect(() => {\n    const el = root.current;\n    const o = out.current;\n    if (!el || !o) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let n = 0;\n    let f = 0;\n    let timer = 0;\n    let auto = true;\n    let on = false;\n    const go = (next: number, manual = false) => {\n      if (manual) auto = false;\n      n = next;\n      setCur(next);\n      cancelAnimationFrame(f);\n      clearTimeout(timer);\n      if (reduce) { o.textContent = PHRASES[next]; return; }\n      const to = pad(PHRASES[next]);\n      const from = pad(o.textContent || \"\");\n      const t0 = performance.now();\n      const frame = (now: number) => {\n        const t = now - t0;\n        let s = \"\";\n        let done = true;\n        for (let i = 0; i < W; i++) {\n          if (t > LEAD + i * STEP) s += to[i];\n          else {\n            done = false;\n            s += to[i] === \" \" && from[i] === \" \" ? \" \" : GLYPHS[(Math.random() * GLYPHS.length) | 0];\n          }\n        }\n        o.textContent = s;\n        f = done ? 0 : requestAnimationFrame(frame);\n        if (done && auto && on) timer = window.setTimeout(() => go((n + 1) % PHRASES.length), HOLD);\n      };\n      frame(t0);\n    };\n    goRef.current = go;\n    if (reduce) return;\n    const h = el.querySelector(\"h2\");\n    const hover = () => { if (!f) go(n); };\n    h?.addEventListener(\"pointerenter\", hover);\n    const io = new IntersectionObserver(([e]) => {\n      on = e.isIntersecting;\n      clearTimeout(timer);\n      if (on && auto) go(n);\n    });\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(f);\n      clearTimeout(timer);\n      h?.removeEventListener(\"pointerenter\", hover);\n    };\n  }, []);\n\n  return (\n    <section ref={root} className=\"bg-[#1c2a21] px-5 py-[72px] text-[#f1ead9]\">\n      <style>{`\n        .w3rph-word { font: 600 .82em/1.15 ui-monospace, \"SF Mono\", Menlo, Consolas, monospace; letter-spacing: -.03em; white-space: pre; }\n        .w3rph-pick:focus-visible { outline: 2px solid #e6b860; outline-offset: 3px; }\n      `}</style>\n      <div className=\"mx-auto grid max-w-[68rem] items-center gap-10 min-[860px]:grid-cols-[1fr_380px]\">\n        <div>\n          <p className=\"text-xs font-semibold uppercase tracking-[0.24em] text-[#a9bba5]\">Fennick Mill</p>\n          <h2 className=\"mt-[18px] font-serif text-[clamp(36px,5.6vw,64px)] font-normal leading-[1.08] tracking-[-0.02em]\">\n            <span className=\"sr-only\">We stone-mill heritage wheat, whole spelt and dark rye.</span>\n            <span aria-hidden=\"true\" className=\"block\">We stone-mill</span>\n            <span ref={out} aria-hidden=\"true\" className=\"w3rph-word mt-1.5 block text-[#e6b860]\">{pad(PHRASES[0])}</span>\n          </h2>\n          <p className=\"mt-6 max-w-[32rem] text-[17px] leading-relaxed text-[#c9cdbd]\">\n            Grain from three farms within a morning's drive, ground slowly between French burr stones and bagged the same week.\n          </p>\n          <div className=\"mt-8 flex gap-2\" role=\"group\" aria-label=\"Show a flour\">\n            {LABELS.map((l, i) => (\n              <button\n                key={l}\n                type=\"button\"\n                aria-pressed={cur === i}\n                onClick={() => goRef.current(i, true)}\n                className={`w3rph-pick min-h-[44px] rounded-full border px-[18px] text-sm font-medium ${\n                  cur === i ? \"border-[#f1ead9] bg-[#f1ead9] text-[#1c2a21]\" : \"border-[#3d5244] hover:border-[#6d8670]\"\n                }`}\n              >\n                {l}\n              </button>\n            ))}\n          </div>\n        </div>\n        <img\n          src=\"https://images.pexels.com/photos/6294375/pexels-photo-6294375.jpeg?auto=compress&cs=tinysrgb&w=900\"\n          alt=\"A steel scoop resting in an open paper sack of fresh white flour\"\n          width={900}\n          height={1125}\n          loading=\"lazy\"\n          className=\"block aspect-[4/5] h-auto w-full max-w-[380px] rounded-[18px] object-cover shadow-[0_30px_60px_-30px_rgba(0,0,0,0.6)]\"\n        />\n      </div>\n    </section>\n  );\n}\n"}]}
