{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"scroll-linked-paragraph-words-brighten-as-you-read","type":"registry:block","title":"Scroll Linked Words Brighten as You Read","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/scroll-linked-paragraph-words-brighten-as-you-read.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\nconst PASSAGE =\n  \"We print forty copies of every book, sewn by hand at a long table under the north window. The paper comes from a mill two valleys over, still damp when it arrives. Nobody here is in a hurry. A book takes the time it takes, and then it goes out into the world to be read slowly, in good light, by someone who chose it on purpose.\";\nconst AUTO_PX_PER_SEC = 22; // speed of the gentle demo scroll before anyone touches it\n\nexport default function ScrollLinkedParagraphWordsBrightenAsYouRead() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const boxRef = useRef<HTMLDivElement>(null);\n\n  // Fallback for browsers without scroll-driven animations: light each word once it passes the reading line.\n  useEffect(() => {\n    const box = boxRef.current;\n    if (!box || reduce || CSS.supports(\"animation-timeline: view()\")) return;\n    box.setAttribute(\"data-fallback\", \"\");\n    const io = new IntersectionObserver(\n      (entries) => entries.forEach((e) => e.target.classList.toggle(\"swb-lit\", e.isIntersecting || e.boundingClientRect.top < (e.rootBounds?.top ?? 0))),\n      { root: box, rootMargin: \"0px 0px -45% 0px\" },\n    );\n    box.querySelectorAll(\".swb-w\").forEach((w) => io.observe(w));\n    return () => io.disconnect();\n  }, [reduce]);\n\n  // A slow demo scroll so the effect is visible without input; any touch, wheel or key hands control back.\n  useEffect(() => {\n    const box = boxRef.current;\n    const root = rootRef.current;\n    if (!box || !root || reduce) return;\n    let frame = 0;\n    let last = 0;\n    let pos = box.scrollTop;\n    let onScreen = true;\n    let stopped = false;\n    const step = (now: number) => {\n      const dt = last ? Math.min(now - last, 50) : 0;\n      last = now;\n      pos += (AUTO_PX_PER_SEC * dt) / 1000;\n      if (pos >= box.scrollHeight - box.clientHeight) pos = 0;\n      box.scrollTop = pos;\n      frame = requestAnimationFrame(step);\n    };\n    const start = () => { if (!stopped && onScreen && !frame) { last = 0; frame = requestAnimationFrame(step); } };\n    const stop = () => { cancelAnimationFrame(frame); frame = 0; };\n    const handOver = () => { stopped = true; stop(); };\n    const io = new IntersectionObserver(([e]) => { onScreen = e.isIntersecting; if (onScreen) start(); else stop(); });\n    io.observe(root);\n    const events = [\"wheel\", \"touchstart\", \"pointerdown\", \"keydown\"] as const;\n    events.forEach((ev) => box.addEventListener(ev, handOver, { passive: true }));\n    start();\n    return () => {\n      stop();\n      io.disconnect();\n      events.forEach((ev) => box.removeEventListener(ev, handOver));\n    };\n  }, [reduce]);\n\n  return (\n    <section ref={rootRef} className=\"swb-root px-5 py-16 sm:px-10 sm:py-20\">\n      <style>{`\n        .swb-root { background: #f3ede1; color: #2a2118; }\n        .swb-box { height: 360px; overflow-y: auto; scrollbar-width: thin; scrollbar-color: #c8bba3 transparent;\n          -webkit-mask-image: linear-gradient(transparent, #000 14%, #000 80%, transparent);\n          mask-image: linear-gradient(transparent, #000 14%, #000 80%, transparent); }\n        .swb-box:focus-visible { outline: 2px solid #9a5b2e; outline-offset: 6px; border-radius: 8px; }\n        .swb-text { font-family: Georgia, \"Iowan Old Style\", \"Times New Roman\", serif; font-size: clamp(1.45rem, 3.4vw, 2.25rem);\n          line-height: 1.45; letter-spacing: -.005em; padding: 120px 0 260px; margin: 0; }\n        .swb-w { opacity: .2; }\n        @supports (animation-timeline: view()) {\n          .swb-w { animation: swb-lit linear both; animation-timeline: view(); animation-range: cover 18% cover 48%; }\n        }\n        @keyframes swb-lit { from { opacity: .2 } to { opacity: 1 } }\n        .swb-box[data-fallback] .swb-w { animation: none; transition: opacity .6s ease; }\n        .swb-box[data-fallback] .swb-w.swb-lit { opacity: 1; }\n        @media (prefers-reduced-motion: reduce) { .swb-w, .swb-box[data-fallback] .swb-w { animation: none; opacity: 1; transition: none; } }\n      `}</style>\n      <div className=\"mx-auto grid max-w-5xl gap-10 md:grid-cols-[220px_1fr]\">\n        <div>\n          <p className=\"text-xs font-semibold uppercase tracking-[0.22em] text-[#8a4f26]\">Saltmarsh Press</p>\n          <h2 className=\"mt-3 text-2xl font-semibold leading-snug\">A note from the bindery</h2>\n          <p className=\"mt-3 text-sm leading-relaxed text-[#5b4d3d]\">Scroll the passage. Each word brightens as it reaches your reading line.</p>\n          <a href=\"#\" className=\"mt-6 inline-flex min-h-[44px] items-center rounded-md bg-[#2a2118] px-5 text-sm font-semibold text-[#f3ede1] transition hover:bg-[#3b2f23] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#9a5b2e] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f3ede1]\">\n            See the autumn list\n          </a>\n        </div>\n        <div ref={boxRef} className=\"swb-box\" tabIndex={0} role=\"region\" aria-label=\"A note from the bindery, scrollable\">\n          <p className=\"swb-text\">\n            {PASSAGE.split(\" \").map((w, i) => (\n              <span key={i}>\n                <span className=\"swb-w\">{w}</span>{\" \"}\n              </span>\n            ))}\n          </p>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
