{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-image-reveal",
  "title": "Image reveal",
  "description": "A list of titles where the hovered row shows its picture beside the pointer.",
  "dependencies": [
    "motion"
  ],
  "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-reveal.tsx",
      "content": "'use client';\nimport { motion, useMotionValue, useSpring } from 'motion/react';\nimport {\n  useRef,\n  useState,\n  type FocusEvent,\n  type PointerEvent,\n  type ReactNode,\n} from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nexport type ImageRevealItem = {\n  id: string;\n  title: string;\n  meta?: ReactNode;\n  src: string;\n  href?: string;\n};\n\nexport type ImageRevealProps = {\n  items: ImageRevealItem[];\n  onSelect?: (item: ImageRevealItem) => void;\n  className?: string;\n  rowClassName?: string;\n  titleClassName?: string;\n  imageClassName?: string;\n};\n\ntype Preview = {\n  id: string;\n  source: 'pointer' | 'focus';\n};\n\nconst FOLLOW_SPRING = { stiffness: 320, damping: 32, mass: 0.6 };\n\nconst FOCUS_GAP = 16;\n\nconst POINTER_GAP = 24;\n\nconst FRAME_SHOWN = { opacity: 1, scale: 1 };\n\nconst FRAME_HIDDEN = { opacity: 0, scale: 0.96 };\n\nconst FRAME_HIDDEN_STILL = { opacity: 0, scale: 1 };\n\nconst FRAME_ENTER = { duration: 0.18, ease: [0.23, 1, 0.32, 1] as const };\n\nconst FRAME_EXIT = { duration: 0.12, ease: [0.23, 1, 0.32, 1] as const };\n\nconst ROW_CLASS =\n  'group/row text-foreground focus-visible:ring-ring/25 relative flex w-full items-baseline justify-between gap-6 rounded-lg px-2 py-3 text-left outline-none focus-visible:ring-[3px]';\n\nfunction isKeyboardFocus(event: FocusEvent<HTMLElement>) {\n  return event.currentTarget.matches(':focus-visible');\n}\n\nfunction frameStateOf(\n  isVisible: boolean,\n  prefersReducedMotion: boolean | null\n) {\n  if (isVisible) {\n    return FRAME_SHOWN;\n  }\n  if (prefersReducedMotion) {\n    return FRAME_HIDDEN_STILL;\n  }\n  return FRAME_HIDDEN;\n}\n\n/** Keeps the image beside the pointer so the hovered title stays readable, and flips it left near the right edge. */\nfunction frameLeftOf(pointerX: number, frameWidth: number, listWidth: number) {\n  const rightSide = pointerX + POINTER_GAP;\n  const fitsOnTheRight = rightSide + frameWidth <= listWidth;\n  if (fitsOnTheRight) {\n    return rightSide;\n  }\n  return pointerX - POINTER_GAP - frameWidth;\n}\n\nfunction frameTransitionOf(isVisible: boolean) {\n  if (isVisible) {\n    return FRAME_ENTER;\n  }\n  return FRAME_EXIT;\n}\n\nfunction ImageReveal({\n  items,\n  onSelect,\n  className,\n  rowClassName,\n  titleClassName,\n  imageClassName,\n}: ImageRevealProps) {\n  const listRef = useRef<HTMLUListElement>(null);\n  const frameRef = useRef<HTMLDivElement>(null);\n  const [preview, setPreview] = useState<Preview | null>(null);\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const targetX = useMotionValue(0);\n  const targetY = useMotionValue(0);\n  const springX = useSpring(targetX, FOLLOW_SPRING);\n  const springY = useSpring(targetY, FOLLOW_SPRING);\n\n  const placeAt = (x: number, y: number, instantly: boolean) => {\n    targetX.set(x);\n    targetY.set(y);\n    const shouldJump = instantly || prefersReducedMotion;\n    if (shouldJump) {\n      springX.jump(x);\n      springY.jump(y);\n    }\n  };\n\n  const followPointer = (event: PointerEvent<HTMLElement>) => {\n    const list = listRef.current;\n    const frame = frameRef.current;\n    if (!list) {\n      return;\n    }\n    if (!frame) {\n      return;\n    }\n    const bounds = list.getBoundingClientRect();\n    const pointerX = event.clientX - bounds.left;\n    const x = frameLeftOf(pointerX, frame.offsetWidth, bounds.width);\n    const y = event.clientY - bounds.top - frame.offsetHeight / 2;\n    const isHidden = preview === null;\n    placeAt(x, y, isHidden);\n  };\n\n  const showForPointer = (\n    item: ImageRevealItem,\n    event: PointerEvent<HTMLElement>\n  ) => {\n    const isMouse = event.pointerType === 'mouse';\n    if (!isMouse) {\n      return;\n    }\n    followPointer(event);\n    setPreview({ id: item.id, source: 'pointer' });\n  };\n\n  const moveWithPointer = (event: PointerEvent<HTMLElement>) => {\n    const isFollowing = preview?.source === 'pointer';\n    if (isFollowing) {\n      followPointer(event);\n    }\n  };\n\n  const showForFocus = (\n    item: ImageRevealItem,\n    event: FocusEvent<HTMLElement>\n  ) => {\n    if (!isKeyboardFocus(event)) {\n      return;\n    }\n    const frame = frameRef.current;\n    if (!frame) {\n      return;\n    }\n    const row = event.currentTarget;\n    const x = row.offsetLeft + row.offsetWidth - frame.offsetWidth - FOCUS_GAP;\n    const y = row.offsetTop + row.offsetHeight / 2 - frame.offsetHeight / 2;\n    placeAt(x, y, true);\n    setPreview({ id: item.id, source: 'focus' });\n  };\n\n  const hidePreview = () => {\n    setPreview(null);\n  };\n\n  const isVisible = preview !== null;\n\n  return (\n    <div className={cn('relative', className)}>\n      <ul\n        ref={listRef}\n        onPointerLeave={hidePreview}\n        className='relative flex flex-col'\n      >\n        {items.map((item) => {\n          const isPreviewed = preview?.id === item.id;\n          const rowProps = {\n            'data-previewed': isPreviewed,\n            className: cn(ROW_CLASS, rowClassName),\n            onPointerEnter: (event: PointerEvent<HTMLElement>) =>\n              showForPointer(item, event),\n            onPointerMove: moveWithPointer,\n            onFocus: (event: FocusEvent<HTMLElement>) =>\n              showForFocus(item, event),\n            onBlur: hidePreview,\n          };\n\n          const content = (\n            <>\n              <span\n                className={cn(\n                  'text-2xl font-semibold text-balance sm:text-3xl',\n                  titleClassName\n                )}\n              >\n                {item.title}\n              </span>\n              {item.meta && (\n                <span className='text-muted-foreground shrink-0 text-[13px] tabular-nums'>\n                  {item.meta}\n                </span>\n              )}\n            </>\n          );\n\n          return (\n            <li key={item.id}>\n              {item.href && (\n                <a href={item.href} {...rowProps}>\n                  {content}\n                </a>\n              )}\n              {!item.href && (\n                <button\n                  type='button'\n                  onClick={() => onSelect?.(item)}\n                  {...rowProps}\n                >\n                  {content}\n                </button>\n              )}\n            </li>\n          );\n        })}\n      </ul>\n      <motion.div\n        ref={frameRef}\n        aria-hidden\n        initial={false}\n        animate={frameStateOf(isVisible, prefersReducedMotion)}\n        transition={frameTransitionOf(isVisible)}\n        style={{ x: springX, y: springY }}\n        className={cn(\n          'bg-foreground/[0.04] pointer-events-none absolute top-0 left-0 z-10 aspect-[3/2] w-60 overflow-hidden rounded-xl shadow-xl shadow-black/8 [@media(hover:none)]:hidden',\n          imageClassName\n        )}\n      >\n        {items.map((item) => {\n          const isShown = preview?.id === item.id;\n\n          return (\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              key={item.id}\n              src={item.src}\n              alt=''\n              draggable={false}\n              decoding='async'\n              data-shown={isShown}\n              className='absolute inset-0 size-full object-cover opacity-0 transition-opacity duration-150 data-[shown=true]:opacity-100'\n            />\n          );\n        })}\n      </motion.div>\n    </div>\n  );\n}\n\nexport { ImageReveal };\n",
      "type": "registry:component",
      "target": "components/core/image-reveal.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}