{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-velocity",
  "title": "Scroll velocity",
  "description": "Rows of large text drift in opposite directions and speed up or flip with your scroll speed and direction.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/text/scroll-velocity.tsx",
      "content": "\"use client\"\r\n\r\nimport * as React from \"react\"\r\nimport {\r\n  motion,\r\n  useInView,\r\n  useMotionValue,\r\n  useReducedMotion,\r\n  useScroll,\r\n  useSpring,\r\n  useTransform,\r\n  useVelocity,\r\n} from \"motion/react\"\r\nimport { cn } from \"@/lib/utils\"\r\n\r\nfunction wrap(min: number, max: number, v: number) {\r\n  const range = max - min\r\n  return ((((v - min) % range) + range) % range) + min\r\n}\r\n\r\nexport type ScrollVelocityRowProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> & {\r\n  /** Content of one repeat; it is copied as many times as needed to fill the row. */\r\n  children: React.ReactNode\r\n  /** Pixels per second at rest. Negative moves to the right. */\r\n  baseVelocity?: number\r\n  /** Scrollable element to track. Defaults to the window. */\r\n  scrollContainer?: React.RefObject<HTMLElement | null>\r\n  /** How strongly scroll speed boosts the row. 0 turns the effect off. */\r\n  velocityFactor?: number\r\n}\r\n\r\n/**\r\n * One endlessly looping row. It drifts at `baseVelocity`, speeds up with scroll speed and flips direction with\r\n * scroll direction. Stops while off-screen and stays still for reduced motion.\r\n */\r\nexport function ScrollVelocityRow({\r\n  children,\r\n  baseVelocity = 40,\r\n  scrollContainer,\r\n  velocityFactor = 1,\r\n  className,\r\n  ...props\r\n}: ScrollVelocityRowProps) {\r\n  const reduce = useReducedMotion() ?? false\r\n  const rootRef = React.useRef<HTMLDivElement>(null)\r\n  const itemRef = React.useRef<HTMLDivElement>(null)\r\n  const itemWidth = React.useRef(0)\r\n  const [copies, setCopies] = React.useState(2)\r\n  const inView = useInView(rootRef)\r\n\r\n  const { scrollY } = useScroll(scrollContainer ? { container: scrollContainer } : undefined)\r\n  const velocity = useVelocity(scrollY)\r\n  const smooth = useSpring(velocity, { damping: 50, stiffness: 400 })\r\n  const boost = useTransform(smooth, [0, 1000], [0, 5], { clamp: false })\r\n  const x = useMotionValue(0)\r\n  const direction = React.useRef(1)\r\n\r\n  // Repeat the content enough times to cover the row, plus one copy to wrap into.\r\n  React.useEffect(() => {\r\n    const root = rootRef.current\r\n    const item = itemRef.current\r\n    if (!root || !item) return\r\n    const update = () => {\r\n      const w = item.offsetWidth\r\n      itemWidth.current = w\r\n      if (w > 0) setCopies(Math.max(2, Math.ceil(root.offsetWidth / w) + 1))\r\n    }\r\n    update()\r\n    const ro = new ResizeObserver(update)\r\n    ro.observe(root)\r\n    ro.observe(item)\r\n    return () => ro.disconnect()\r\n  }, [])\r\n\r\n  // The frame loop exists only while the row is on screen: a row far down the page must not keep every frame busy.\r\n  React.useEffect(() => {\r\n    if (reduce || !inView) return\r\n    let last = performance.now()\r\n    let id = requestAnimationFrame(function tick(now) {\r\n      id = requestAnimationFrame(tick)\r\n      const w = itemWidth.current\r\n      const dt = Math.min(now - last, 50) / 1000\r\n      last = now\r\n      if (!w || document.hidden) return\r\n      const b = boost.get() * velocityFactor\r\n      if (b < 0) direction.current = -1\r\n      else if (b > 0) direction.current = 1\r\n      const move = direction.current * baseVelocity * dt * (1 + Math.abs(b))\r\n      x.set(wrap(-w, 0, x.get() - move))\r\n    })\r\n    return () => cancelAnimationFrame(id)\r\n  }, [reduce, inView, baseVelocity, velocityFactor, boost, x])\r\n\r\n  return (\r\n    <div ref={rootRef} className={cn(\"flex overflow-hidden whitespace-nowrap\", className)} {...props}>\r\n      <motion.div className=\"flex shrink-0\" style={{ x }}>\r\n        {Array.from({ length: copies }, (_, i) => (\r\n          <div key={i} ref={i === 0 ? itemRef : undefined} aria-hidden={i > 0 ? true : undefined} className=\"flex shrink-0 items-center\">\r\n            {children}\r\n          </div>\r\n        ))}\r\n      </motion.div>\r\n    </div>\r\n  )\r\n}\r\n\r\nexport type ScrollVelocityProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> & {\r\n  /** One string per row. Rows alternate direction. */\r\n  items: string[]\r\n  /** Pixels per second at rest. */\r\n  baseVelocity?: number\r\n  /** Scrollable element to track. Defaults to the window. */\r\n  scrollContainer?: React.RefObject<HTMLElement | null>\r\n  /** How strongly scroll speed boosts the rows. 0 turns the effect off. */\r\n  velocityFactor?: number\r\n  /** Shown between repeats of a row. */\r\n  separator?: React.ReactNode\r\n  /** Classes for every row, e.g. the font size and weight. */\r\n  rowClassName?: string\r\n}\r\n\r\n/** Stacked rows of large text that drift in alternating directions and react to scroll speed. */\r\nexport function ScrollVelocity({\r\n  items,\r\n  baseVelocity = 40,\r\n  scrollContainer,\r\n  velocityFactor = 1,\r\n  separator = \"·\",\r\n  rowClassName,\r\n  className,\r\n  ...props\r\n}: ScrollVelocityProps) {\r\n  return (\r\n    <div className={cn(\"flex flex-col gap-2\", className)} {...props}>\r\n      <p className=\"sr-only\">{items.join(\". \")}</p>\r\n      {items.map((text, i) => (\r\n        <ScrollVelocityRow\r\n          key={i}\r\n          aria-hidden=\"true\"\r\n          baseVelocity={i % 2 === 0 ? baseVelocity : -baseVelocity}\r\n          scrollContainer={scrollContainer}\r\n          velocityFactor={velocityFactor}\r\n          className={rowClassName}\r\n        >\r\n          <span className=\"px-[0.3em]\">{text}</span>\r\n          <span className=\"px-[0.3em] text-muted-foreground/60\">{separator}</span>\r\n        </ScrollVelocityRow>\r\n      ))}\r\n    </div>\r\n  )\r\n}\r\n",
      "type": "registry:component",
      "target": "components/goodui/scroll-velocity.tsx"
    }
  ],
  "categories": [
    "components",
    "text"
  ],
  "type": "registry:component"
}