{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-image-trail",
  "title": "Image trail",
  "description": "Images that land along the pointer path, then fade and shrink away.",
  "dependencies": [],
  "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/image-trail.tsx",
      "content": "'use client';\nimport { useEffect, useRef, type ReactNode } from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nexport type ImageTrailImage = {\n  src: string;\n};\n\nexport type ImageTrailProps = {\n  images: ImageTrailImage[];\n  children?: ReactNode;\n  className?: string;\n  imageClassName?: string;\n  distance?: number;\n  maxVisible?: number;\n  lifetime?: number;\n  persist?: boolean;\n};\n\ntype TrailState = {\n  lastX: number;\n  lastY: number;\n  hasStarted: boolean;\n  spawnCount: number;\n};\n\nconst EASE_OUT_STRONG = 'cubic-bezier(0.23, 1, 0.32, 1)';\n\nconst ENTER_DURATION = 240;\n\nconst EXIT_DURATION = 200;\n\nconst ENTER_KEYFRAMES: Keyframe[] = [\n  { opacity: 0, transform: 'scale(0.9)' },\n  { opacity: 1, transform: 'scale(1)' },\n];\n\nconst EXIT_KEYFRAMES: Keyframe[] = [{ opacity: 0, transform: 'scale(0.82)' }];\n\nfunction slotCountOf(maxVisible: number) {\n  return maxVisible * 2;\n}\n\nfunction hasMovedFarEnough(\n  state: TrailState,\n  x: number,\n  y: number,\n  distance: number\n) {\n  if (!state.hasStarted) {\n    return true;\n  }\n  const travelled = Math.hypot(x - state.lastX, y - state.lastY);\n  return travelled >= distance;\n}\n\nfunction retire(slot: HTMLElement | undefined) {\n  if (!slot) {\n    return;\n  }\n  slot.animate(EXIT_KEYFRAMES, {\n    duration: EXIT_DURATION,\n    easing: EASE_OUT_STRONG,\n    fill: 'forwards',\n  });\n}\n\nfunction swapSource(picture: HTMLImageElement, src: string) {\n  const isSameSource = picture.getAttribute('src') === src;\n  if (isSameSource) {\n    return;\n  }\n  picture.src = src;\n}\n\nfunction settle(slot: HTMLElement) {\n  slot.getAnimations().forEach((animation) => animation.cancel());\n}\n\nfunction ImageTrail({\n  images,\n  children,\n  className,\n  imageClassName,\n  distance = 80,\n  maxVisible = 5,\n  lifetime = 900,\n  persist = false,\n}: ImageTrailProps) {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const slotsRef = useRef<Array<HTMLDivElement | null>>([]);\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const slotCount = slotCountOf(maxVisible);\n\n  useEffect(() => {\n    const container = containerRef.current;\n    if (!container) {\n      return;\n    }\n    if (prefersReducedMotion) {\n      return;\n    }\n    const hasImages = images.length > 0;\n    if (!hasImages) {\n      return;\n    }\n\n    const state: TrailState = {\n      lastX: 0,\n      lastY: 0,\n      hasStarted: false,\n      spawnCount: 0,\n    };\n    const timers = new Set<number>();\n    const slots = slotsRef.current;\n\n    images.forEach((image) => {\n      const preload = new Image();\n      preload.src = image.src;\n    });\n\n    const slotAt = (spawnIndex: number) => {\n      const slotIndex = spawnIndex % slotCount;\n      return slots[slotIndex] ?? undefined;\n    };\n\n    const scheduleRetire = (slot: HTMLElement, spawnIndex: number) => {\n      const timer = window.setTimeout(() => {\n        timers.delete(timer);\n        const isSameSpawn = slot.dataset.spawn === String(spawnIndex);\n        if (isSameSpawn) {\n          retire(slot);\n        }\n      }, lifetime);\n      timers.add(timer);\n    };\n\n    const spawn = (x: number, y: number) => {\n      const spawnIndex = state.spawnCount;\n      const slot = slotAt(spawnIndex);\n      if (!slot) {\n        return;\n      }\n      const image = images[spawnIndex % images.length];\n      const picture = slot.firstElementChild;\n      settle(slot);\n      if (picture instanceof HTMLImageElement) {\n        swapSource(picture, image.src);\n      }\n      slot.dataset.spawn = String(spawnIndex);\n      slot.style.translate = `${x}px ${y}px`;\n      slot.style.zIndex = String(spawnIndex + 1);\n      slot.animate(ENTER_KEYFRAMES, {\n        duration: ENTER_DURATION,\n        easing: EASE_OUT_STRONG,\n        fill: 'forwards',\n      });\n      const hasPushedOut = spawnIndex >= maxVisible;\n      if (hasPushedOut) {\n        const pushedOut = slotAt(spawnIndex - maxVisible);\n        retire(pushedOut);\n      }\n      if (!persist) {\n        scheduleRetire(slot, spawnIndex);\n      }\n      state.spawnCount = spawnIndex + 1;\n    };\n\n    const handlePointerMove = (event: PointerEvent) => {\n      if (event.pointerType === 'touch') {\n        return;\n      }\n      const bounds = container.getBoundingClientRect();\n      const x = event.clientX - bounds.left;\n      const y = event.clientY - bounds.top;\n      const shouldSpawn = hasMovedFarEnough(state, x, y, distance);\n      if (!shouldSpawn) {\n        return;\n      }\n      state.lastX = x;\n      state.lastY = y;\n      state.hasStarted = true;\n      spawn(x, y);\n    };\n\n    const handlePointerLeave = () => {\n      state.hasStarted = false;\n    };\n\n    container.addEventListener('pointermove', handlePointerMove, {\n      passive: true,\n    });\n    container.addEventListener('pointerleave', handlePointerLeave, {\n      passive: true,\n    });\n\n    return () => {\n      container.removeEventListener('pointermove', handlePointerMove);\n      container.removeEventListener('pointerleave', handlePointerLeave);\n      timers.forEach((timer) => window.clearTimeout(timer));\n      slots.forEach((slot) => {\n        if (slot) {\n          settle(slot);\n        }\n      });\n    };\n  }, [\n    images,\n    distance,\n    maxVisible,\n    lifetime,\n    persist,\n    slotCount,\n    prefersReducedMotion,\n  ]);\n\n  const slotIndexes = Array.from({ length: slotCount }, (_, index) => index);\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn('relative isolate overflow-hidden', className)}\n    >\n      <div\n        aria-hidden\n        className='pointer-events-none absolute inset-0 z-0 overflow-hidden'\n      >\n        {slotIndexes.map((slotIndex) => (\n          <div\n            key={slotIndex}\n            ref={(element) => {\n              slotsRef.current[slotIndex] = element;\n            }}\n            className='absolute top-0 left-0 opacity-0'\n          >\n            {/* eslint-disable-next-line @next/next/no-img-element -- registry items land in any React app, where next/image may not exist */}\n            <img\n              alt=''\n              draggable={false}\n              decoding='async'\n              className={cn(\n                'h-40 w-32 max-w-none -translate-x-1/2 -translate-y-1/2 rounded-lg object-cover shadow-[0_1px_2px_rgb(0_0_0/0.08)]',\n                imageClassName\n              )}\n            />\n          </div>\n        ))}\n      </div>\n      <div className='relative z-10'>{children}</div>\n    </div>\n  );\n}\n\nexport { ImageTrail };\n",
      "type": "registry:component",
      "target": "components/core/image-trail.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}