{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-word-rotate",
  "title": "Word rotate",
  "description": "Phrases on a drum that turns on a timer, with the sentence around them sliding to the new width.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/animated-size.json",
    "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/word-rotate.tsx",
      "content": "'use client';\nimport { cn } from '@/lib/utils';\nimport {\n  AnimatePresence,\n  motion,\n  useInView,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useEffect, useEffectEvent, useRef, useState } from 'react';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { AnimatedSize } from '@/registry/ndeko/ui/animated-size';\n\nexport type WordRotateDirection = 'up' | 'down';\n\nconst EASE_OUT_STRONG: [number, number, number, number] = [0.23, 1, 0.32, 1];\n\nconst TURN_IN: Transition = { duration: 0.5, ease: EASE_OUT_STRONG };\n\nconst TURN_OUT: Transition = {\n  duration: 0.35,\n  ease: EASE_OUT_STRONG,\n  opacity: { duration: 0.2, ease: EASE_OUT_STRONG },\n};\n\nconst FADE_IN: Transition = { duration: 0.2, ease: EASE_OUT_STRONG };\n\nconst FADE_OUT: Transition = { duration: 0.15, ease: EASE_OUT_STRONG };\n\nconst FACING = 'translateY(0%) rotateX(0deg)';\n\nconst LOWER_SIDE = 'translateY(55%) rotateX(-70deg)';\n\nconst UPPER_SIDE = 'translateY(-55%) rotateX(70deg)';\n\nconst BLURRED = 'blur(4px)';\n\nconst SHARP = 'blur(0px)';\n\nfunction arrivingSideOf(direction: WordRotateDirection) {\n  if (direction === 'down') {\n    return UPPER_SIDE;\n  }\n  return LOWER_SIDE;\n}\n\nfunction leavingSideOf(direction: WordRotateDirection) {\n  if (direction === 'down') {\n    return LOWER_SIDE;\n  }\n  return UPPER_SIDE;\n}\n\n/** The phrases sit on a drum: the next one turns in from one side while the last one turns away. */\nconst DRUM_VARIANTS: Variants = {\n  hidden: (direction: WordRotateDirection) => ({\n    transform: arrivingSideOf(direction),\n    filter: BLURRED,\n    opacity: 0,\n  }),\n  shown: {\n    transform: FACING,\n    filter: SHARP,\n    opacity: 1,\n    transition: TURN_IN,\n  },\n  gone: (direction: WordRotateDirection) => ({\n    transform: leavingSideOf(direction),\n    filter: BLURRED,\n    opacity: 0,\n    transition: TURN_OUT,\n  }),\n};\n\nconst FADE_VARIANTS: Variants = {\n  hidden: { opacity: 0 },\n  shown: { opacity: 1, transition: FADE_IN },\n  gone: { opacity: 0, transition: FADE_OUT },\n};\n\nfunction variantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return DRUM_VARIANTS;\n}\n\nfunction useDocumentVisible() {\n  const [isVisible, setIsVisible] = useState(true);\n\n  useEffect(() => {\n    const handleVisibilityChange = () => {\n      setIsVisible(document.visibilityState === 'visible');\n    };\n    document.addEventListener('visibilitychange', handleVisibilityChange);\n    return () => {\n      document.removeEventListener('visibilitychange', handleVisibilityChange);\n    };\n  }, []);\n\n  return isVisible;\n}\n\nfunction spokenTextOf(\n  words: readonly string[],\n  accessibleText: string | undefined\n) {\n  if (accessibleText !== undefined) {\n    return accessibleText;\n  }\n  return words.join(', ');\n}\n\nexport type WordRotateProps = {\n  words: readonly string[];\n  interval?: number;\n  direction?: WordRotateDirection;\n  paused?: boolean;\n  pauseOnHover?: boolean;\n  onIndexChange?: (index: number) => void;\n  accessibleText?: string;\n  className?: string;\n};\n\nexport function WordRotate({\n  words,\n  interval = 2.5,\n  direction = 'up',\n  paused = false,\n  pauseOnHover = true,\n  onIndexChange,\n  accessibleText,\n  className,\n}: WordRotateProps) {\n  const rootRef = useRef<HTMLSpanElement>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const isInView = useInView(rootRef);\n  const isDocumentVisible = useDocumentVisible();\n  const [index, setIndex] = useState(0);\n  const [isHovered, setIsHovered] = useState(false);\n  const count = words.length;\n  const slotCount = Math.max(count, 1);\n  const currentIndex = index % slotCount;\n  const hasSeveralWords = count > 1;\n  const isHeldByPointer = pauseOnHover && isHovered;\n  const isOnScreen = isInView && isDocumentVisible;\n  const isHeld = paused || isHeldByPointer;\n  const isTurning = hasSeveralWords && isOnScreen && !isHeld;\n\n  const turnToNext = useEffectEvent(() => {\n    const following = currentIndex + 1;\n    const next = following % count;\n    setIndex(next);\n    onIndexChange?.(next);\n  });\n\n  useEffect(() => {\n    if (!isTurning) {\n      return;\n    }\n    const intervalMs = interval * 1000;\n    const timer = window.setInterval(() => {\n      turnToNext();\n    }, intervalMs);\n    return () => window.clearInterval(timer);\n  }, [isTurning, interval]);\n\n  if (count === 0) {\n    return null;\n  }\n\n  return (\n    <span\n      ref={rootRef}\n      onPointerEnter={() => setIsHovered(true)}\n      onPointerLeave={() => setIsHovered(false)}\n      className={cn('inline-flex items-baseline', className)}\n    >\n      <span className='sr-only'>{spokenTextOf(words, accessibleText)}</span>\n      <AnimatedSize\n        as='span'\n        axis='width'\n        aria-hidden='true'\n        contentClassName='relative [perspective:4em]'\n      >\n        <AnimatePresence mode='popLayout' initial={false} custom={direction}>\n          <motion.span\n            key={currentIndex}\n            custom={direction}\n            variants={variantsOf(shouldReduceMotion)}\n            initial='hidden'\n            animate='shown'\n            exit='gone'\n            className='block whitespace-nowrap'\n          >\n            {words[currentIndex]}\n          </motion.span>\n        </AnimatePresence>\n      </AnimatedSize>\n    </span>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/word-rotate.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}