{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-fill-scroll",
  "type": "registry:component",
  "title": "Text Fill Scroll",
  "description": "A paragraph that fills with color word by word, left to right, as it scrolls through the viewport.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "text",
    "scroll",
    "fill",
    "progress",
    "manifesto",
    "reading"
  ],
  "docs": "https://hyperblur.dev/effects/text-fill-scroll",
  "files": [
    {
      "path": "registry/hyperblur/text-fill-scroll.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-fill-scroll.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextFillScrollProps extends React.HTMLAttributes<HTMLParagraphElement> {\n  text: string;\n  fillColor?: string;\n  baseColor?: string;\n  startAt?: number;\n  endAt?: number;\n  idleLoop?: boolean;\n  idleDuration?: number;\n  resumeDelay?: number;\n}\n\nconst css = `\n.hb-qda { margin: 0; font-family: inherit; box-sizing: border-box; }\n.hb-qda-w {\n  --hb-qda-f: 0;\n  color: var(--hb-qda-base);\n  background-image: linear-gradient(\n    90deg,\n    var(--hb-qda-fill) 0,\n    var(--hb-qda-fill) calc(var(--hb-qda-f) * 112% - 12%),\n    var(--hb-qda-base) calc(var(--hb-qda-f) * 112%),\n    var(--hb-qda-base) 100%\n  );\n  background-repeat: no-repeat;\n  -webkit-background-clip: text;\n  background-clip: text;\n  -webkit-text-fill-color: transparent;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qda-w { transition: none; }\n}\n`;\n\nfunction clamp(v: number) {\n  return v < 0 ? 0 : v > 1 ? 1 : v;\n}\n\nexport function TextFillScroll({\n  text,\n  fillColor = \"#ededed\",\n  baseColor = \"#3b3b3b\",\n  startAt = 0.85,\n  endAt = 0.4,\n  idleLoop = false,\n  idleDuration = 5,\n  resumeDelay = 1.6,\n  className = \"\",\n  style,\n  ...rest\n}: TextFillScrollProps) {\n  const ref = React.useRef<HTMLParagraphElement>(null);\n  const words = React.useMemo(() => text.split(/\\s+/).filter(Boolean), [text]);\n\n  React.useEffect(() => {\n    const root = ref.current;\n    if (!root) return;\n    const nodes = Array.from(root.querySelectorAll<HTMLElement>(\"[data-w]\"));\n    const n = nodes.length;\n    if (n === 0) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const painted = nodes.map(() => -1);\n    const holdSeconds = 1.2;\n    const t0 = performance.now();\n    let lastScroll = -1e9;\n    let lastFrame = t0;\n    let shown = 0;\n    let visible = false;\n    let raf = 0;\n\n    const measure = () => {\n      const box = root.getBoundingClientRect();\n      const vh = window.innerHeight || 1;\n      const span = box.height + vh * (startAt - endAt);\n      return clamp((vh * startAt - box.top) / Math.max(1, span));\n    };\n\n    const paint = (p: number) => {\n      for (let i = 0; i < n; i++) {\n        const f = clamp(p * n - i);\n        if (Math.abs(f - painted[i]) > 0.002) {\n          painted[i] = f;\n          nodes[i].style.setProperty(\"--hb-qda-f\", f.toFixed(3));\n        }\n      }\n    };\n\n    const tick = (now: number) => {\n      raf = 0;\n      if (!visible) return;\n      const dt = Math.min(0.1, (now - lastFrame) / 1000);\n      lastFrame = now;\n      const scrolling = now - lastScroll < resumeDelay * 1000;\n      let target: number;\n      if (idleLoop && !scrolling && !reduce) {\n        const cycle = idleDuration + holdSeconds;\n        const phase = (((now - t0) / 1000) % cycle) / idleDuration;\n        target = clamp(phase);\n      } else {\n        target = measure();\n      }\n      shown += (target - shown) * (reduce ? 1 : 1 - Math.exp(-dt * 7));\n      paint(shown);\n      if (idleLoop || Math.abs(target - shown) > 0.0008) raf = requestAnimationFrame(tick);\n    };\n\n    const kick = () => {\n      if (!raf && visible) {\n        lastFrame = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    };\n    const onScroll = () => {\n      lastScroll = performance.now();\n      kick();\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        visible = entry.isIntersecting;\n        kick();\n      },\n      { rootMargin: \"120px\" },\n    );\n    io.observe(root);\n    window.addEventListener(\"scroll\", onScroll, { passive: true, capture: true });\n    window.addEventListener(\"resize\", onScroll, { passive: true });\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      window.removeEventListener(\"scroll\", onScroll, true);\n      window.removeEventListener(\"resize\", onScroll);\n    };\n  }, [words, startAt, endAt, idleLoop, idleDuration, resumeDelay]);\n\n  const vars = {\n    \"--hb-qda-fill\": fillColor,\n    \"--hb-qda-base\": baseColor,\n    ...style,\n  } as React.CSSProperties;\n\n  return (\n    <p ref={ref} className={`hb-qda ${className}`} style={vars} {...rest}>\n      <style href=\"hb-text-fill-scroll\" precedence=\"default\">{css}</style>\n      {words.map((w, i) => (\n        <React.Fragment key={`${i}-${w}`}>\n          <span className=\"hb-qda-w\" data-w=\"\">\n            {w}\n          </span>\n          {i < words.length - 1 ? \" \" : null}\n        </React.Fragment>\n      ))}\n    </p>\n  );\n}\n"
    }
  ]
}