{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "photo-pile",
  "title": "PhotoPile",
  "description": "Layers draggable photos that spring back into a playful composition.",
  "dependencies": [
    "motion@^13.1.0"
  ],
  "files": [
    {
      "path": "packages/react/src/photo-pile.tsx",
      "content": "'use client'\n\nimport './photo-pile.css'\nimport { type PhotoPileOptions, photoPile } from './motion/photo-pile.js'\nimport type { CSSProperties, HTMLAttributes, ImgHTMLAttributes } from 'react'\nimport { forwardRef, useRef } from 'react'\nimport { composeRefs } from './compose-refs.js'\nimport { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'\n\nexport interface PhotoPilePhoto {\n  /** Stable key for this photo. */\n  id: string\n  src: string\n  alt: string\n  /** Horizontal offset from the center, in CSS pixels. */\n  x?: number\n  /** Vertical offset from the center, in CSS pixels. */\n  y?: number\n  /** Resting rotation in degrees. */\n  rotation?: number\n  imageProps?: Omit<ImgHTMLAttributes<HTMLImageElement>, 'src' | 'alt' | 'draggable'>\n}\n\nexport interface PhotoPileProps\n  extends PhotoPileOptions,\n    Omit<HTMLAttributes<HTMLFieldSetElement>, 'children'> {\n  photos: readonly PhotoPilePhoto[]\n  /** Multiplies the default and authored x/y offsets. @default 1 */\n  spread?: number\n}\n\nconst POSITIONS = [\n  { x: -54, y: -10, rotation: -7 },\n  { x: 52, y: -18, rotation: 6 },\n  { x: 2, y: 30, rotation: -1.5 },\n  { x: -24, y: -38, rotation: 4 },\n  { x: 38, y: 24, rotation: -5 },\n] as const\n\ntype ItemStyle = CSSProperties & {\n  '--kida-photo-x': string\n  '--kida-photo-y': string\n  '--kida-photo-rotation': string\n}\n\nexport const PhotoPile = forwardRef<HTMLFieldSetElement, PhotoPileProps>(function PhotoPile(\n  { photos, spread = 1, dragScale, tilt, 'aria-label': ariaLabel, ...props },\n  forwardedRef,\n) {\n  const ref = useRef<HTMLFieldSetElement>(null)\n  const photoKey = photos.map((photo) => photo.id).join('\\u0000')\n\n  useIsomorphicLayoutEffect(() => {\n    const root = ref.current\n    if (!root) return\n    return photoPile(root, { dragScale, tilt })\n  }, [photoKey, dragScale, tilt])\n\n  return (\n    <fieldset\n      {...props}\n      ref={composeRefs(ref, forwardedRef)}\n      aria-label={ariaLabel ?? 'Draggable photo pile'}\n      data-kida-photo-pile=\"\"\n    >\n      <span data-kida-photo-stage=\"\">\n        {photos.map((photo, index) => {\n          const fallback = POSITIONS[index % POSITIONS.length] ?? POSITIONS[0]\n          const label = photo.alt || `Photo ${index + 1}`\n          const style: ItemStyle = {\n            '--kida-photo-x': `${(photo.x ?? fallback.x) * spread}px`,\n            '--kida-photo-y': `${(photo.y ?? fallback.y) * spread}px`,\n            '--kida-photo-rotation': `${photo.rotation ?? fallback.rotation}deg`,\n            zIndex: index + 1,\n          }\n\n          return (\n            <button\n              key={photo.id}\n              type=\"button\"\n              aria-label={`Bring ${label} to front and drag`}\n              data-kida-photo-item=\"\"\n              style={style}\n            >\n              <span data-kida-photo-card=\"\">\n                <img {...photo.imageProps} src={photo.src} alt={photo.alt} draggable={false} />\n              </span>\n            </button>\n          )\n        })}\n      </span>\n    </fieldset>\n  )\n})\n",
      "type": "registry:ui",
      "target": "@ui/kida/photo-pile.tsx"
    },
    {
      "path": "packages/react/src/compose-refs.ts",
      "content": "import type { Ref } from 'react'\n\n/** Feed one node to several refs — ours for measuring, Zag's for animation detection. */\nexport function composeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === 'function') ref(node)\n      else if (ref) (ref as { current: T | null }).current = node\n    }\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/compose-refs.ts"
    },
    {
      "path": "packages/react/src/use-isomorphic-layout-effect.ts",
      "content": "'use client'\n\nimport { useEffect, useLayoutEffect } from 'react'\n\n/**\n * `useLayoutEffect` in the browser, `useEffect` on the server.\n *\n * Motion has to be applied before paint or the user sees an unstyled frame, but React\n * warns when `useLayoutEffect` runs during SSR — where it does nothing anyway.\n */\nexport const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect\n",
      "type": "registry:hook",
      "target": "@ui/kida/use-isomorphic-layout-effect.ts"
    },
    {
      "path": "packages/styles/components/photo-pile.css",
      "content": "[data-kida-photo-pile] {\n  --kida-photo-width: clamp(9rem, 42%, 13.5rem);\n  --kida-photo-radius: 0.55rem;\n  --kida-photo-frame: #fffafb;\n  --kida-photo-shadow: 0 0.8rem 2rem rgb(50 30 42 / 16%);\n\n  position: relative;\n  display: block;\n  min-inline-size: 0;\n  width: min(100%, 36rem);\n  min-height: 22rem;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  isolation: isolate;\n}\n\n/* Chromium does not reliably use a fieldset itself as the containing block for absolutely\n   positioned children. A normal-flow stage keeps every card anchored to the pile wherever the\n   fieldset appears on the page. */\n[data-kida-photo-stage] {\n  position: relative;\n  /* Grid centers fractional card sizes without percentage transforms, which WebKit rounds. */\n  display: grid;\n  place-items: center;\n  /* Fill explicitly sized previews as well as the pile's default minimum height. */\n  height: 100%;\n  min-height: inherit;\n}\n\n[data-kida-photo-item] {\n  position: absolute;\n  width: var(--kida-photo-width);\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: var(--kida-photo-radius);\n  background: transparent;\n  cursor: grab;\n  touch-action: none;\n  transform: translate(var(--kida-photo-x), var(--kida-photo-y)) rotate(var(--kida-photo-rotation));\n}\n\n[data-kida-photo-item][data-dragging] {\n  cursor: grabbing;\n}\n\n[data-kida-photo-item]:focus-visible {\n  outline: 3px solid var(--kida-photo-focus, #6d52a8);\n  outline-offset: 5px;\n}\n\n[data-kida-photo-card] {\n  display: block;\n  padding: 0.45rem 0.45rem 1.8rem;\n  border: 1px solid rgb(55 35 48 / 12%);\n  border-radius: var(--kida-photo-radius);\n  background: var(--kida-photo-frame);\n  box-shadow: var(--kida-photo-shadow);\n  user-select: none;\n}\n\n[data-kida-photo-card] img {\n  display: block;\n  width: 100%;\n  aspect-ratio: 4 / 5;\n  border-radius: calc(var(--kida-photo-radius) - 0.2rem);\n  object-fit: cover;\n  pointer-events: none;\n}\n\n@media (max-width: 32rem) {\n  [data-kida-photo-pile] {\n    --kida-photo-width: min(48%, 10.5rem);\n\n    min-height: 18rem;\n  }\n}\n\n@media (prefers-reduced-motion: reduce) {\n  [data-kida-photo-card] {\n    /* biome-ignore lint/complexity/noImportantStyles: stops an in-flight inline transform */\n    transform: none !important;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/kida/photo-pile.css"
    },
    {
      "path": "packages/motion/src/photo-pile.ts",
      "content": "import { animate } from 'motion'\nimport { prefersReducedMotion } from './reduced-motion.js'\n\nexport interface PhotoPileOptions {\n  /** Scale applied while a photo is being dragged. @default 1.035 */\n  dragScale?: number\n  /** Degrees of responsive tilt per horizontal CSS pixel. @default 0.025 */\n  tilt?: number\n}\n\nconst DEFAULTS = { dragScale: 1.035, tilt: 0.025 } as const\nconst IDENTITY = 'translate3d(0px, 0px, 0) rotate(0deg) scale(1)'\n\n/**\n * Adds direct pointer dragging and a spring return to every photo card in a pile.\n * The positioned item stays still; only its inner card moves, so authored layout transforms\n * never compete with the drag transform.\n */\nexport function photoPile(root: HTMLElement, options: PhotoPileOptions = {}): () => void {\n  const { dragScale = DEFAULTS.dragScale, tilt = DEFAULTS.tilt } = options\n  const reduced = prefersReducedMotion()\n  const items = [...root.querySelectorAll<HTMLElement>('[data-kida-photo-item]')]\n  const cleanups: Array<() => void> = []\n  const originalZ = new Map(items.map((item) => [item, item.style.zIndex]))\n  let topLayer = items.length\n\n  const activate = (item: HTMLElement) => {\n    topLayer += 1\n    item.style.zIndex = String(topLayer)\n  }\n\n  for (const item of items) {\n    const card = item.querySelector<HTMLElement>('[data-kida-photo-card]')\n    if (!card) continue\n\n    let pointerId: number | undefined\n    let originX = 0\n    let originY = 0\n    let x = 0\n    let y = 0\n    let frame = 0\n    let settleFrame = 0\n    let controls: ReturnType<typeof animate> | undefined\n\n    const transform = () =>\n      `translate3d(${x}px, ${y}px, 0) rotate(${x * tilt}deg) scale(${dragScale})`\n\n    const paint = () => {\n      frame = 0\n      card.style.transform = transform()\n    }\n\n    const settle = () => {\n      controls?.cancel()\n      controls = undefined\n      card.style.transform = 'none'\n      card.style.willChange = 'auto'\n      delete item.dataset.dragging\n    }\n\n    const onPointerDown = (event: PointerEvent) => {\n      if (pointerId !== undefined || (event.pointerType === 'mouse' && event.button !== 0)) return\n      if (settleFrame) {\n        cancelAnimationFrame(settleFrame)\n        settleFrame = 0\n      }\n      controls?.cancel()\n      pointerId = event.pointerId\n      originX = event.clientX\n      originY = event.clientY\n      x = 0\n      y = 0\n      activate(item)\n      item.dataset.dragging = ''\n      card.style.willChange = 'transform'\n      card.style.transform = IDENTITY\n      try {\n        item.setPointerCapture(pointerId)\n      } catch {\n        // Synthetic pointer events and older browsers may not expose capture; window listeners\n        // below still keep the gesture alive.\n      }\n    }\n\n    const onPointerMove = (event: PointerEvent) => {\n      if (event.pointerId !== pointerId) return\n      x = event.clientX - originX\n      y = event.clientY - originY\n      if (!frame) frame = requestAnimationFrame(paint)\n    }\n\n    const onPointerEnd = (event: PointerEvent) => {\n      if (event.pointerId !== pointerId) return\n      pointerId = undefined\n      if (frame) {\n        cancelAnimationFrame(frame)\n        frame = 0\n        card.style.transform = transform()\n      }\n      try {\n        if (item.hasPointerCapture(event.pointerId)) item.releasePointerCapture(event.pointerId)\n      } catch {\n        // See the capture fallback above.\n      }\n\n      if (reduced) {\n        settle()\n        return\n      }\n\n      const from = transform()\n      controls = animate(\n        card,\n        { transform: [from, IDENTITY] },\n        {\n          type: 'spring',\n          stiffness: 420,\n          damping: 30,\n          mass: 0.7,\n          // Motion commits the final keyframe after calling onComplete. Clear that committed\n          // identity transform on the following frame so the card truly returns to authored CSS.\n          onComplete: () => {\n            settleFrame = requestAnimationFrame(() => {\n              settleFrame = 0\n              settle()\n            })\n          },\n        },\n      )\n    }\n\n    const onFocus = () => activate(item)\n    item.addEventListener('pointerdown', onPointerDown)\n    window.addEventListener('pointermove', onPointerMove)\n    window.addEventListener('pointerup', onPointerEnd)\n    window.addEventListener('pointercancel', onPointerEnd)\n    item.addEventListener('focusin', onFocus)\n\n    cleanups.push(() => {\n      item.removeEventListener('pointerdown', onPointerDown)\n      window.removeEventListener('pointermove', onPointerMove)\n      window.removeEventListener('pointerup', onPointerEnd)\n      window.removeEventListener('pointercancel', onPointerEnd)\n      item.removeEventListener('focusin', onFocus)\n      if (frame) cancelAnimationFrame(frame)\n      if (settleFrame) cancelAnimationFrame(settleFrame)\n      settle()\n    })\n  }\n\n  return () => {\n    for (const cleanup of cleanups) cleanup()\n    for (const item of items) item.style.zIndex = originalZ.get(item) ?? ''\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/photo-pile.ts"
    },
    {
      "path": "packages/motion/src/reduced-motion.ts",
      "content": "const QUERY = '(prefers-reduced-motion: reduce)'\n\n/**\n * Whether the user has asked for reduced motion.\n *\n * SSR-safe: returns `false` when there is no `window`, matching the CSS default\n * (the `prefers-reduced-motion` media query in `@kida-ui/styles` is the real guard;\n * this is for the JS-driven paths that CSS cannot express).\n */\nexport function prefersReducedMotion(): boolean {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false\n  return window.matchMedia(QUERY).matches\n}\n\n/**\n * Subscribe to changes of the reduced-motion preference.\n * Returns an unsubscribe function. No-op (and immediately unsubscribable) on the server.\n */\nexport function watchReducedMotion(onChange: (reduced: boolean) => void): () => void {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return () => {}\n\n  const list = window.matchMedia(QUERY)\n  const handler = (event: MediaQueryListEvent) => onChange(event.matches)\n  list.addEventListener('change', handler)\n  return () => list.removeEventListener('change', handler)\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/reduced-motion.ts"
    }
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/react",
        "exportName": "PhotoPile",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      }
    }
  },
  "categories": [
    "image"
  ],
  "type": "registry:ui"
}
