{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-scroll-text-marquee",
  "title": "Scroll text marquee",
  "description": "A large line of text that loops sideways and speeds up with the scroll.",
  "dependencies": [
    "motion",
    "react-use-measure"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/scroll-text-marquee.tsx",
      "content": "'use client';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  motion,\n  useAnimationFrame,\n  useInView,\n  useMotionValue,\n  useScroll,\n  useSpring,\n  useTransform,\n  useVelocity,\n  wrap,\n  type MotionValue,\n} from 'motion/react';\nimport { useRef, type RefObject } from 'react';\nimport useMeasure from 'react-use-measure';\n\nexport type ScrollTextMarqueeDirection = 'left' | 'right';\n\ntype DirectionSign = 1 | -1;\n\ntype MeasureRef = ReturnType<typeof useMeasure>[0];\n\nconst VELOCITY_SPRING = { damping: 50, stiffness: 400 };\n\n/** At 1000px of scroll per second, the text runs this many times faster than at rest. */\nconst BOOST_PER_1000_PX = 4;\n\nfunction signOf(direction: ScrollTextMarqueeDirection): DirectionSign {\n  if (direction === 'right') {\n    return 1;\n  }\n  return -1;\n}\n\nfunction oppositeOf(sign: DirectionSign): DirectionSign {\n  if (sign === 1) {\n    return -1;\n  }\n  return 1;\n}\n\nfunction translateOf(offset: number, copyWidth: number) {\n  if (copyWidth === 0) {\n    return 'translateX(0px)';\n  }\n  const wrappedOffset = wrap(-copyWidth, 0, offset);\n  return `translateX(${wrappedOffset}px)`;\n}\n\nfunction hiddenFromReadersOf(index: number) {\n  if (index > 0) {\n    return true;\n  }\n  return undefined;\n}\n\nfunction measuredRefOf(index: number, measureRef: MeasureRef) {\n  if (index === 0) {\n    return measureRef;\n  }\n  return undefined;\n}\n\ntype MarqueeDriverProps = {\n  offset: MotionValue<number>;\n  boost: MotionValue<number>;\n  speed: number;\n  baseSign: DirectionSign;\n  bidirectional: boolean;\n  signRef: RefObject<DirectionSign>;\n};\n\n/**\n * The frame loop of the animation library runs for as long as one `useAnimationFrame` is mounted,\n * even when its callback does nothing, and each of those frames costs the whole page. So the loop\n * lives in this leaf, which is mounted only while the text moves.\n */\nfunction MarqueeDriver({\n  offset,\n  boost,\n  speed,\n  baseSign,\n  bidirectional,\n  signRef,\n}: MarqueeDriverProps) {\n  useAnimationFrame((_, delta) => {\n    const currentBoost = boost.get();\n    const isScrollingUp = currentBoost < 0;\n    const isScrollingDown = currentBoost > 0;\n    if (!bidirectional) {\n      signRef.current = baseSign;\n    }\n    const followsScrollUp = bidirectional && isScrollingUp;\n    const followsScrollDown = bidirectional && isScrollingDown;\n    if (followsScrollUp) {\n      signRef.current = oppositeOf(baseSign);\n    }\n    if (followsScrollDown) {\n      signRef.current = baseSign;\n    }\n    const seconds = delta / 1000;\n    const distanceAtRest = speed * seconds;\n    const boostSize = Math.abs(currentBoost);\n    const multiplier = 1 + boostSize;\n    const distance = distanceAtRest * multiplier;\n    const step = signRef.current * distance;\n    const nextOffset = offset.get() + step;\n    offset.set(nextOffset);\n  });\n\n  return null;\n}\n\nexport type ScrollTextMarqueeProps = {\n  children: React.ReactNode;\n  speed?: number;\n  direction?: ScrollTextMarqueeDirection;\n  bidirectional?: boolean;\n  repeat?: number;\n  containerRef?: RefObject<HTMLElement | null>;\n  className?: string;\n};\n\nexport function ScrollTextMarquee({\n  children,\n  speed = 60,\n  direction = 'left',\n  bidirectional = false,\n  repeat = 4,\n  containerRef,\n  className,\n}: ScrollTextMarqueeProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const [copyRef, { width: copyWidth }] = useMeasure();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const isInView = useInView(rootRef);\n  const baseSign = signOf(direction);\n  const signRef = useRef<DirectionSign>(baseSign);\n  const offset = useMotionValue(0);\n  const { scrollY } = useScroll({ container: containerRef });\n  const scrollVelocity = useVelocity(scrollY);\n  const smoothVelocity = useSpring(scrollVelocity, VELOCITY_SPRING);\n  const boost = useTransform(\n    smoothVelocity,\n    [0, 1000],\n    [0, BOOST_PER_1000_PX],\n    { clamp: false }\n  );\n  const transform = useTransform(offset, (latest) =>\n    translateOf(latest, copyWidth)\n  );\n  const copies = Array.from({ length: repeat }, (_, index) => index);\n  const isStill = shouldReduceMotion || !isInView;\n  const isMoving = !isStill;\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\n        'flex overflow-hidden text-5xl leading-tight font-semibold whitespace-nowrap sm:text-7xl',\n        className\n      )}\n    >\n      {isMoving && (\n        <MarqueeDriver\n          offset={offset}\n          boost={boost}\n          speed={speed}\n          baseSign={baseSign}\n          bidirectional={bidirectional}\n          signRef={signRef}\n        />\n      )}\n      <motion.div className='flex w-max' style={{ transform }}>\n        {copies.map((index) => (\n          <span\n            key={index}\n            ref={measuredRefOf(index, copyRef)}\n            aria-hidden={hiddenFromReadersOf(index)}\n            className='shrink-0 pr-[0.4em]'\n          >\n            {children}\n          </span>\n        ))}\n      </motion.div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/scroll-text-marquee.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}