{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"scroll-reveal","type":"registry:component","title":"Scroll reveal","description":"Words light up one by one as you scroll through them.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"components/scroll-reveal.tsx","type":"registry:component","content":"\"use client\";\n\nimport { Fragment, useCallback, useEffect, useRef } from \"react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\n// Short thumbs are hard to grab, so it never shrinks below this.\nconst MIN_THUMB = 32;\n\n// Each word lights up over this share of the scroll, so several are always\n// mid-fade and the edge between read and unread text stays soft.\nconst WINDOW = 0.14;\n\n// Only where the timeline can run, and only with motion allowed, do words\n// start dimmed. Everywhere else the text is simply readable. Linear, because\n// the scroll position is already the clock.\nconst CSS = `\n@supports (animation-timeline: scroll()) {\n  @media (prefers-reduced-motion: no-preference) {\n    .scroll-reveal {\n      scroll-timeline: --scroll-reveal y;\n    }\n    .scroll-reveal-word {\n      animation: scroll-reveal-word linear both;\n      animation-timeline: --scroll-reveal;\n      animation-range: var(--start) var(--end);\n    }\n  }\n}\n@keyframes scroll-reveal-word {\n  from {\n    color: var(--muted-foreground);\n    opacity: 0.5;\n    filter: blur(2px);\n  }\n  to {\n    color: var(--foreground);\n    opacity: 1;\n    filter: blur(0);\n  }\n}\n`;\n\nexport function ScrollReveal({\n  text,\n  label,\n  className,\n}: {\n  text: string;\n  label: string;\n  className?: string;\n}) {\n  const words = text.split(/\\s+/).filter(Boolean);\n  const last = Math.max(words.length - 1, 1);\n  const reduceMotion = useReducedMotion();\n  const scrollerRef = useRef<HTMLDivElement>(null);\n  const trackRef = useRef<HTMLDivElement>(null);\n  const thumbRef = useRef<HTMLDivElement>(null);\n  const drag = useRef<{ startY: number; startScroll: number }>(null);\n\n  // Written straight to the thumb on every scroll, so it tracks without\n  // re-rendering React.\n  const sync = useCallback(() => {\n    const scroller = scrollerRef.current;\n    const track = trackRef.current;\n    const thumb = thumbRef.current;\n    if (!scroller || !track || !thumb) return;\n    const height = Math.max(\n      (scroller.clientHeight / scroller.scrollHeight) * track.clientHeight,\n      MIN_THUMB,\n    );\n    const max = scroller.scrollHeight - scroller.clientHeight;\n    const progress = max > 0 ? scroller.scrollTop / max : 0;\n    thumb.style.height = `${height}px`;\n    thumb.style.transform = `translateY(${progress * (track.clientHeight - height)}px)`;\n  }, []);\n\n  useEffect(() => {\n    const scroller = scrollerRef.current;\n    if (!scroller) return;\n    sync();\n    const observer = new ResizeObserver(sync);\n    observer.observe(scroller);\n    return () => observer.disconnect();\n  }, [sync]);\n\n  // How many px of scroll one px of thumb travel is worth.\n  const ratio = () => {\n    const scroller = scrollerRef.current!;\n    const track = trackRef.current!;\n    const thumb = thumbRef.current!;\n    return (\n      (scroller.scrollHeight - scroller.clientHeight) /\n      Math.max(track.clientHeight - thumb.offsetHeight, 1)\n    );\n  };\n\n  return (\n    // The surface and focus ring live on the wrapper so the edge mask below\n    // fades only the text, not the box or its outline.\n    <div\n      className={cn(\n        \"relative h-[360px] w-[480px] max-w-full rounded-3xl bg-muted shadow-raised has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-solid has-[:focus-visible]:outline-offset-2 has-[:focus-visible]:outline-foreground\",\n        className,\n      )}\n    >\n      <style href=\"scroll-reveal\" precedence=\"default\">\n        {CSS}\n      </style>\n      {/* 44px fades hint that the text continues past each edge. */}\n      <div\n        ref={scrollerRef}\n        onScroll={sync}\n        tabIndex={0}\n        role=\"region\"\n        aria-label={label}\n        className=\"scroll-reveal h-full overflow-y-auto overscroll-contain rounded-3xl outline-hidden [mask-image:linear-gradient(to_bottom,transparent,black_44px,black_calc(100%-44px),transparent)] [scrollbar-width:none]\"\n      >\n        {/* The padding adds up to the box height (136 + 224 = 360), so the\n            reveal edge holds still about 40% down while the text moves\n            through it, from the first line to the last. */}\n        <p className=\"px-9 pt-[136px] pb-[224px] text-2xl leading-8 font-medium tracking-tight text-pretty text-foreground\">\n          {words.map((word, i) => {\n            const start = (i / last) * (1 - WINDOW);\n            return (\n              <Fragment key={i}>\n                <span\n                  className=\"scroll-reveal-word\"\n                  style={\n                    {\n                      \"--start\": `${start * 100}%`,\n                      \"--end\": `${(start + WINDOW) * 100}%`,\n                    } as React.CSSProperties\n                  }\n                >\n                  {word}\n                </span>{\" \"}\n              </Fragment>\n            );\n          })}\n        </p>\n      </div>\n      {/* Outside the scroller, so the edge mask never fades it. The native\n          bar is hidden; this one can be dragged to scrub through the reveal. */}\n      <div\n        ref={trackRef}\n        aria-hidden\n        className=\"group/bar absolute top-5 right-2 bottom-5 flex w-3.5 cursor-pointer touch-none justify-center\"\n        onPointerDown={(e) => {\n          const scroller = scrollerRef.current;\n          const thumb = thumbRef.current;\n          if (!scroller || !thumb || e.button !== 0) return;\n          if (e.target === thumb) {\n            drag.current = { startY: e.clientY, startScroll: scroller.scrollTop };\n            e.currentTarget.setPointerCapture(e.pointerId);\n            return;\n          }\n          // A click on the rail glides there, centring the thumb on the click.\n          const box = e.currentTarget.getBoundingClientRect();\n          const offset = e.clientY - box.top - thumb.offsetHeight / 2;\n          scroller.scrollTo({\n            top: offset * ratio(),\n            behavior: reduceMotion ? \"auto\" : \"smooth\",\n          });\n        }}\n        onPointerMove={(e) => {\n          const scroller = scrollerRef.current;\n          if (!drag.current || !scroller) return;\n          scroller.scrollTop =\n            drag.current.startScroll + (e.clientY - drag.current.startY) * ratio();\n        }}\n        onPointerUp={() => {\n          drag.current = null;\n        }}\n        onPointerCancel={() => {\n          drag.current = null;\n        }}\n      >\n        <div\n          ref={thumbRef}\n          className=\"w-1.5 rounded-full bg-foreground/20 transition-[width,background-color] duration-150 ease-out group-hover/bar:w-2 group-hover/bar:bg-foreground/35 group-active/bar:bg-foreground/50\"\n        />\n      </div>\n    </div>\n  );\n}\n\nexport default function ScrollRevealDemo() {\n  return (\n    <ScrollReveal\n      label=\"About motion\"\n      text=\"Good motion is felt before it is noticed. It tells you where something came from, what just changed, and what will happen when you let go. When every duration and curve is chosen on purpose, an interface stops feeling like a screen and starts feeling like a material.\"\n    />\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["text"],"docs":"From ui lab: https://lab.xevrion.dev/lab/scroll-reveal"}