{
  "$schema": "https://shadcn-svelte.com/schema/registry-item.json",
  "name": "photo-pile",
  "title": "PhotoPile",
  "type": "registry:ui",
  "description": "Layers draggable photos that spring back into a playful composition.",
  "dependencies": [
    "motion@^13.1.0"
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/svelte",
        "exportName": "PhotoPile",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      },
      "framework": "svelte"
    }
  },
  "categories": [
    "image"
  ],
  "files": [
    {
      "content": "<script lang=\"ts\">\nimport './photo-pile.css'\nimport { photoPile } from './photo-pile.motion.js'\nimport { untrack } from 'svelte'\nimport type { PhotoPileProps } from './photo-pile.types.js'\n\nlet {\n  photos,\n  spread = 1,\n  dragScale,\n  tilt,\n  element = $bindable(null),\n  'aria-label': ariaLabel = 'Draggable photo pile',\n  ...rest\n}: PhotoPileProps = $props()\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\nconst root = $derived(element)\nconst restartKey = $derived(JSON.stringify([photos.map((photo) => photo.id), dragScale, tilt]))\nconst layout = $derived(\n  photos.map((photo, index) => {\n    const fallback = positions[index % positions.length] ?? positions[0]\n    return {\n      photo,\n      label: `Bring ${photo.alt || `Photo ${index + 1}`} to front and drag`,\n      x: `${(photo.x ?? fallback.x) * spread}px`,\n      y: `${(photo.y ?? fallback.y) * spread}px`,\n      rotation: `${photo.rotation ?? fallback.rotation}deg`,\n      layer: index + 1,\n    }\n  }),\n)\n\n$effect(() => {\n  const node = root\n  void restartKey\n  if (!node) return\n  return untrack(() => {\n    node.querySelectorAll<HTMLElement>('[data-kida-photo-item]').forEach((item, index) => {\n      item.style.zIndex = String(index + 1)\n    })\n    return photoPile(node, { dragScale, tilt })\n  })\n})\n</script>\n\n<fieldset {...rest} bind:this={element} aria-label={ariaLabel} data-kida-photo-pile=\"\">\n  <span data-kida-photo-stage=\"\">\n    {#each layout as item (item.photo.id)}\n      <button\n        type=\"button\"\n        aria-label={item.label}\n        data-kida-photo-item=\"\"\n        style:--kida-photo-x={item.x}\n        style:--kida-photo-y={item.y}\n        style:--kida-photo-rotation={item.rotation}\n        style:z-index={item.layer}\n      >\n        <span data-kida-photo-card=\"\">\n          <img {...item.photo.imageProps} src={item.photo.src} alt={item.photo.alt} draggable=\"false\" />\n        </span>\n      </button>\n    {/each}\n  </span>\n</fieldset>\n",
      "type": "registry:ui",
      "target": "kida/photo-pile.svelte"
    },
    {
      "content": "import type { PhotoPileOptions } from './photo-pile.motion.js';\n\nimport type { HTMLAttributes, HTMLImgAttributes } from 'svelte/elements';\n\ntype TextAttributes<T extends EventTarget> = Omit<HTMLAttributes<T>, 'children'>;\n\nexport interface PhotoPilePhoto {\n    id: string;\n    src: string;\n    alt: string;\n    x?: number;\n    y?: number;\n    rotation?: number;\n    imageProps?: Omit<HTMLImgAttributes, 'src' | 'alt' | 'draggable'>;\n}\n\nexport interface PhotoPileProps extends PhotoPileOptions, TextAttributes<HTMLFieldSetElement> {\n    photos: readonly PhotoPilePhoto[];\n    spread?: number;\n    element?: HTMLFieldSetElement | null;\n}\n",
      "type": "registry:ui",
      "target": "kida/photo-pile.types.ts"
    },
    {
      "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": "kida/photo-pile.css"
    },
    {
      "content": "import { animate } from 'motion'\nimport { prefersReducedMotion } from './reduced-motion.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:ui",
      "target": "kida/photo-pile.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:ui",
      "target": "kida/reduced-motion.motion.ts"
    }
  ]
}
