{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-effect-06",
  "title": "Rolling letters",
  "description": "Letters that roll forward like small cubes one after another, a wave that runs through the word and starts again with no seam, paused off screen and still under reduced motion.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/segments.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-agent-run.json",
    "https://ui.ndekocode.dev/r/use-autoplay.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/text-effect/text-effect-06.tsx",
      "content": "'use client';\n\nimport { useEffect, useRef, useState } from 'react';\n\nimport {\n  useAutoplay,\n  type AutoplayStep,\n} from '@/registry/ndeko/hooks/use-autoplay';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Segments, type Segment } from '@/registry/ndeko/ui/segments';\n\nimport { useHover } from './_parts/use-hover';\nimport { usePlayWhileSeen } from './_parts/use-play-while-seen';\n\nconst DEFAULT_STEP_MS = 90;\n\nconst DEFAULT_PAUSE_MS = 1400;\n\nconst ROLL_MS = 520;\n\nconst ROLL_EASING = 'cubic-bezier(0.65, 0, 0.35, 1)';\n\n/** The axis sits half a line behind the letter, so the face that rolls away and the one below it that rolls up are two sides of one cube. */\nconst AXIS = '50% 50% -0.5lh';\n\nconst PERSPECTIVE = '16em';\n\nconst SPACE = ' ';\n\nconst BOTTOM_FACE: React.CSSProperties = {\n  transform: 'rotateX(-90deg)',\n  transformOrigin: '50% 0%',\n};\n\nfunction periodOf(letterCount: number, stepMs: number, pauseMs: number) {\n  const waveMs = Math.max(0, letterCount - 1) * stepMs;\n  return waveMs + ROLL_MS + pauseMs;\n}\n\n/**\n * A letter rests, then rolls a quarter turn that ends on a face identical to the one it started\n * on. The next round starts there, so no round has a seam, whatever the speed.\n */\nfunction rollKeyframesOf(period: number): Keyframe[] {\n  const restShare = 1 - ROLL_MS / period;\n  return [\n    { offset: 0, transform: 'rotateX(0deg)' },\n    { offset: restShare, transform: 'rotateX(0deg)', easing: ROLL_EASING },\n    { offset: 1, transform: 'rotateX(90deg)' },\n  ];\n}\n\nfunction cubesIn(root: HTMLElement) {\n  const cubes = root.querySelectorAll<HTMLElement>('[data-cube]');\n  return Array.from(cubes);\n}\n\ntype CubeProps = {\n  letter: string;\n  position: number;\n};\n\nfunction Cube({ letter, position }: CubeProps) {\n  if (letter === SPACE) {\n    return <span className='inline-block'>{letter}</span>;\n  }\n  return (\n    <span\n      data-cube=''\n      data-position={position}\n      style={{ transformOrigin: AXIS }}\n      className='relative inline-block transform-3d'\n    >\n      <span className='block backface-hidden'>{letter}</span>\n      <span\n        aria-hidden='true'\n        style={BOTTOM_FACE}\n        className='absolute inset-x-0 top-full block select-none backface-hidden'\n      >\n        {letter}\n      </span>\n    </span>\n  );\n}\n\nexport type RollingLettersProps = {\n  text: string;\n  /** The gap between one letter starting to roll and the next, in milliseconds, at normal speed. */\n  stepMs?: number;\n  /** How long the letters rest between two waves, in milliseconds, at normal speed. */\n  pauseMs?: number;\n  /** How fast the wave runs, as a multiple of normal speed. A change of speed never makes a letter jump. */\n  speed?: number;\n  className?: string;\n};\n\n/**\n * Letters that roll forward like small cubes, one after another, in a wave that runs through the\n * word and starts again with no seam. Each letter is two faces of one cube; the text stays in the\n * page for screen readers. The wave plays only while the word is on screen and the tab is shown,\n * and under reduced motion the word rests still.\n */\nexport function RollingLetters({\n  text,\n  stepMs = DEFAULT_STEP_MS,\n  pauseMs = DEFAULT_PAUSE_MS,\n  speed = 1,\n  className,\n}: RollingLettersProps) {\n  const { ref, isPlaying } = usePlayWhileSeen<HTMLSpanElement>();\n  const rolls = useRef<Animation[]>([]);\n  const latestSpeed = useRef(speed);\n  const letters = Array.from(text);\n\n  useEffect(() => {\n    latestSpeed.current = speed;\n  });\n\n  useEffect(() => {\n    const root = ref.current;\n    if (!root || !isPlaying) {\n      return;\n    }\n    const letterCount = Array.from(text).length;\n    const period = periodOf(letterCount, stepMs, pauseMs);\n    const keyframes = rollKeyframesOf(period);\n    const started = cubesIn(root).map((cube) => {\n      const position = Number(cube.dataset.position);\n      const roll = cube.animate(keyframes, {\n        duration: period,\n        delay: position * stepMs,\n        iterations: Number.POSITIVE_INFINITY,\n      });\n      roll.playbackRate = latestSpeed.current;\n      return roll;\n    });\n    rolls.current = started;\n    return () => {\n      for (const roll of started) {\n        roll.cancel();\n      }\n      rolls.current = [];\n    };\n  }, [ref, isPlaying, text, stepMs, pauseMs]);\n\n  useEffect(() => {\n    for (const roll of rolls.current) {\n      roll.updatePlaybackRate(speed);\n    }\n  }, [speed]);\n\n  return (\n    <span\n      ref={ref}\n      className={cn(\n        'relative -mx-[0.12em] inline-block overflow-x-clip px-[0.12em]',\n        className\n      )}\n    >\n      <span className='sr-only'>{text}</span>\n      <span\n        aria-hidden='true'\n        style={{ perspective: PERSPECTIVE }}\n        className='inline-flex whitespace-pre'\n      >\n        {letters.map((letter, position) => (\n          <Cube key={position} letter={letter} position={position} />\n        ))}\n      </span>\n    </span>\n  );\n}\n\ntype Pace = 'calm' | 'quick';\n\nconst SPEEDS: Record<Pace, number> = {\n  calm: 1,\n  quick: 2.4,\n};\n\nconst PACES: Segment<Pace>[] = [\n  { value: 'calm', label: 'Calm' },\n  { value: 'quick', label: 'Quick' },\n];\n\n/** Enough rounds at each pace for the difference to be seen before the demonstration goes round again. */\nconst CALM_SHOWN_MS = 9000;\n\nconst QUICK_SHOWN_MS = 6000;\n\nexport default function TextEffect06() {\n  const [pace, setPace] = useState<Pace>('calm');\n  const { isHovered, hoverHandlers } = useHover();\n  const steps: AutoplayStep[] = [\n    { after: CALM_SHOWN_MS, run: () => setPace('quick') },\n    { after: QUICK_SHOWN_MS, run: () => setPace('calm') },\n  ];\n  const { ref } = useAutoplay(steps, {\n    loopAfter: 0,\n    onReset: () => setPace('calm'),\n    isEnabled: !isHovered,\n  });\n\n  return (\n    <div\n      ref={ref}\n      className='@container flex w-full max-w-[34rem] flex-col items-center gap-5'\n      {...hoverHandlers}\n    >\n      <p className='text-[length:clamp(2.25rem,14cqw,4rem)] leading-[1.1] font-semibold'>\n        <RollingLetters text='Launch day' speed={SPEEDS[pace]} />\n      </p>\n      <Segments\n        value={pace}\n        segments={PACES}\n        onValueChange={setPace}\n        label='Wave pace'\n      />\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/text-effect/text-effect-06.tsx"
    },
    {
      "path": "registry/ndeko/components/text-effect/_parts/use-hover.ts",
      "content": "'use client';\n\nimport { useState } from 'react';\n\n/** Whether a mouse or a pen rests on an element. A finger does not hover, so a touch never counts. */\nexport function useHover() {\n  const [isHovered, setIsHovered] = useState(false);\n\n  function hoverStarts(event: React.PointerEvent<HTMLElement>) {\n    if (event.pointerType !== 'touch') {\n      setIsHovered(true);\n    }\n  }\n\n  function hoverEnds() {\n    setIsHovered(false);\n  }\n\n  const hoverHandlers = {\n    onPointerEnter: hoverStarts,\n    onPointerLeave: hoverEnds,\n  };\n\n  return { isHovered, hoverHandlers };\n}\n",
      "type": "registry:component",
      "target": "components/text-effect/_parts/use-hover.ts"
    },
    {
      "path": "registry/ndeko/components/text-effect/_parts/use-play-while-seen.ts",
      "content": "'use client';\n\nimport { useInView } from 'motion/react';\nimport { useRef } from 'react';\n\nimport { useDocumentVisible } from '@/registry/ndeko/hooks/use-agent-run';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\n/**\n * A text effect plays only while someone can see it: on screen, in the tab that is shown, without\n * reduced motion. Everything that repeats hangs on `isPlaying`, and `isStill` asks for the still\n * frame, which must read as the finished text.\n */\nexport function usePlayWhileSeen<Element extends HTMLElement = HTMLElement>() {\n  const ref = useRef<Element>(null);\n  const isInView = useInView(ref);\n  const isTabVisible = useDocumentVisible();\n  const isStill = usePrefersReducedMotion();\n  const isOnScreen = isInView && isTabVisible;\n  const isPlaying = isOnScreen && !isStill;\n\n  return { ref, isPlaying, isStill };\n}\n",
      "type": "registry:component",
      "target": "components/text-effect/_parts/use-play-while-seen.ts"
    }
  ],
  "categories": [
    "components",
    "text-effect"
  ],
  "type": "registry:component"
}