{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-stack-swipe",
  "type": "registry:component",
  "title": "Image Stack Swipe",
  "description": "A stack of photo cards. Drag the top one away with a tilt, or use the arrow keys, to reveal the next card underneath.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "transitions",
    "image",
    "stack",
    "swipe",
    "cards",
    "drag",
    "gesture"
  ],
  "docs": "https://hyperblur.dev/effects/image-stack-swipe",
  "files": [
    {
      "path": "registry/hyperblur/image-stack-swipe.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/image-stack-swipe.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nconst SCENE_COUNT = 4;\n\ntype SceneLayer = \"all\" | \"back\" | \"mid\" | \"front\";\n\nfunction mod(n: number, m: number) {\n  return ((n % m) + m) % m;\n}\n\nfunction hash(n: number) {\n  let x = Math.imul(n + 1, 2654435761) >>> 0;\n  x = Math.imul(x ^ (x >>> 15), 2246822519) >>> 0;\n  x = Math.imul(x ^ (x >>> 13), 3266489917) >>> 0;\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nconst sceneCache = new Map<string, string>();\n\nfunction sceneSvg(k: number, layer: SceneLayer) {\n  const on = (name: string) => layer === \"all\" || layer === name;\n  let s = '<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 320 200\" width=\"320\" height=\"200\">';\n  if (k === 0) {\n    if (on(\"back\")) {\n      s +=\n        '<defs><linearGradient id=\"g\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\"><stop offset=\"0\" stop-color=\"#ffb36b\"/><stop offset=\"0.5\" stop-color=\"#ff6f91\"/><stop offset=\"1\" stop-color=\"#4b3b8f\"/></linearGradient></defs><rect width=\"320\" height=\"200\" fill=\"url(#g)\"/><circle cx=\"220\" cy=\"96\" r=\"46\" fill=\"#ffe3a8\" opacity=\"0.22\"/><circle cx=\"220\" cy=\"96\" r=\"26\" fill=\"#ffe9bd\"/>';\n    }\n    if (on(\"mid\")) {\n      s +=\n        '<polygon points=\"0,140 50,92 90,122 140,70 200,128 250,96 320,138 320,200 0,200\" fill=\"#6a4a98\"/><polygon points=\"0,158 70,118 130,148 190,110 260,150 320,128 320,200 0,200\" fill=\"#3d2c70\"/>';\n    }\n    if (on(\"front\")) {\n      s += '<path d=\"M0 178 Q80 150 160 172 T320 166 L320 200 L0 200Z\" fill=\"#1b1640\"/>';\n      for (let i = 0; i < 9; i += 1) {\n        const x = 12 + i * 36 + (hash(i + 3) % 14);\n        const h = 26 + (hash(i + 9) % 18);\n        s += `<polygon points=\"${x},${200 - h - 6} ${x - 8},${206 - 6} ${x + 8},${206 - 6}\" fill=\"#120e30\"/><rect x=\"${x - 1}\" y=\"${200 - 8}\" width=\"2\" height=\"8\" fill=\"#120e30\"/>`;\n      }\n    }\n  } else if (k === 1) {\n    if (on(\"back\")) {\n      s +=\n        '<defs><linearGradient id=\"g\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\"><stop offset=\"0\" stop-color=\"#14183f\"/><stop offset=\"0.55\" stop-color=\"#6a3d8f\"/><stop offset=\"1\" stop-color=\"#ff8a7a\"/></linearGradient></defs><rect width=\"320\" height=\"200\" fill=\"url(#g)\"/><circle cx=\"252\" cy=\"46\" r=\"16\" fill=\"#fff1d6\"/><circle cx=\"259\" cy=\"41\" r=\"14\" fill=\"#3a2f78\" opacity=\"0.4\"/>';\n      for (let i = 0; i < 22; i += 1) {\n        const h = hash(i + 11);\n        s += `<circle cx=\"${h % 320}\" cy=\"${hash(i + 71) % 80}\" r=\"${(0.5 + (h % 5) / 6).toFixed(2)}\" fill=\"#fff\" opacity=\"0.${5 + (h % 4)}\"/>`;\n      }\n    }\n    if (on(\"mid\")) {\n      const fx = [0, 28, 64, 98, 132, 170, 208, 246, 284];\n      const fw = [32, 40, 38, 40, 42, 42, 42, 42, 40];\n      const fh = [96, 120, 88, 130, 104, 126, 92, 118, 100];\n      fx.forEach((x, i) => {\n        s += `<rect x=\"${x}\" y=\"${200 - fh[i]}\" width=\"${fw[i]}\" height=\"${fh[i]}\" fill=\"#3b3182\" opacity=\"0.92\"/>`;\n      });\n    }\n    if (on(\"front\")) {\n      const nx = [-6, 40, 86, 136, 190, 236, 282];\n      const nw = [50, 50, 54, 60, 52, 52, 50];\n      const nh = [74, 100, 64, 92, 110, 70, 90];\n      nx.forEach((x, b) => {\n        s += `<rect x=\"${x}\" y=\"${200 - nh[b]}\" width=\"${nw[b]}\" height=\"${nh[b]}\" fill=\"#16132f\"/>`;\n        const cols = Math.floor((nw[b] - 10) / 11);\n        const rows = Math.floor((nh[b] - 10) / 13);\n        for (let r = 0; r < rows; r += 1) {\n          for (let c = 0; c < cols; c += 1) {\n            const h = hash(b * 100 + r * 10 + c);\n            if (h % 3 !== 0) {\n              s += `<rect x=\"${x + 6 + c * 11}\" y=\"${200 - nh[b] + 8 + r * 13}\" width=\"5\" height=\"7\" fill=\"${h % 4 === 0 ? \"#ff9f80\" : \"#ffd37a\"}\"/>`;\n            }\n          }\n        }\n      });\n    }\n  } else if (k === 2) {\n    if (on(\"back\")) {\n      s +=\n        '<defs><linearGradient id=\"g\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#0b1f5c\"/><stop offset=\"0.55\" stop-color=\"#1c7ed6\"/><stop offset=\"1\" stop-color=\"#5ee0c8\"/></linearGradient></defs><rect width=\"320\" height=\"200\" fill=\"url(#g)\"/><circle cx=\"72\" cy=\"52\" r=\"20\" fill=\"#fff\" opacity=\"0.25\"/>';\n    }\n    if (on(\"mid\")) {\n      s +=\n        '<path d=\"M0 110 C60 80 120 140 180 108 S280 90 320 112 V200 H0Z\" fill=\"#fff\" opacity=\"0.16\"/><path d=\"M0 130 C70 100 130 160 200 128 S290 112 320 134 V200 H0Z\" fill=\"#7ee8d3\" opacity=\"0.32\"/>';\n    }\n    if (on(\"front\")) {\n      s +=\n        '<path d=\"M0 156 C80 130 140 184 210 154 S290 142 320 160 V200 H0Z\" fill=\"#0a2f6a\" opacity=\"0.88\"/><path d=\"M0 178 C90 160 160 196 240 176 S300 170 320 182 V200 H0Z\" fill=\"#061a42\"/>';\n    }\n  } else {\n    if (on(\"back\")) {\n      s +=\n        '<defs><radialGradient id=\"g\" cx=\"0.5\" cy=\"0.38\" r=\"0.8\"><stop offset=\"0\" stop-color=\"#ffd9b0\"/><stop offset=\"0.55\" stop-color=\"#ea6f8f\"/><stop offset=\"1\" stop-color=\"#43235f\"/></radialGradient></defs><rect width=\"320\" height=\"200\" fill=\"url(#g)\"/>';\n    }\n    if (on(\"mid\")) {\n      s +=\n        '<circle cx=\"160\" cy=\"100\" r=\"72\" fill=\"none\" stroke=\"#fff\" stroke-width=\"1.5\" opacity=\"0.24\"/><path d=\"M70 120 C60 70 120 40 170 52 C230 64 262 110 236 150 C210 190 100 180 70 120Z\" fill=\"#fff\" opacity=\"0.13\"/>';\n    }\n    if (on(\"front\")) {\n      s +=\n        '<path d=\"M70 200 C74 160 112 148 160 148 C208 148 246 160 250 200Z\" fill=\"#2a1a48\"/><rect x=\"146\" y=\"124\" width=\"28\" height=\"32\" fill=\"#d9987a\"/><ellipse cx=\"160\" cy=\"98\" rx=\"30\" ry=\"35\" fill=\"#f0b698\"/><path d=\"M128 96 C124 62 150 50 172 56 C196 62 198 84 192 98 C186 78 170 72 150 74 C140 76 132 84 128 96Z\" fill=\"#2b1230\"/><circle cx=\"160\" cy=\"160\" r=\"4\" fill=\"#ffd37a\"/>';\n    }\n  }\n  return `${s}</svg>`;\n}\n\nfunction sceneUri(index: number, layer: SceneLayer = \"all\") {\n  const k = mod(index, SCENE_COUNT);\n  const key = `${k}:${layer}`;\n  let uri = sceneCache.get(key);\n  if (!uri) {\n    uri = `data:image/svg+xml,${encodeURIComponent(sceneSvg(k, layer))}`;\n    sceneCache.set(key, uri);\n  }\n  return uri;\n}\n\nfunction cssUrl(list: string[] | null, index: number, layer: SceneLayer = \"all\") {\n  if (list) return `url(${JSON.stringify(list[mod(index, list.length)])})`;\n  return `url(\"${sceneUri(index, layer)}\")`;\n}\n\nfunction cx(base: string, extra?: string) {\n  return extra ? `${base} ${extra}` : base;\n}\n\nfunction rootBox(\n  width: number | string | undefined,\n  height: number | string | undefined,\n  aspect: string,\n  style: React.CSSProperties | undefined,\n): React.CSSProperties {\n  return {\n    width: width ?? \"100%\",\n    ...(height !== undefined ? { height } : { aspectRatio: aspect }),\n    ...style,\n  };\n}\n\nconst motionQuery = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribeMotion(callback: () => void) {\n  const query = window.matchMedia(motionQuery);\n  query.addEventListener(\"change\", callback);\n  return () => query.removeEventListener(\"change\", callback);\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(motionQuery).matches,\n    () => false,\n  );\n}\n\nfunction useInView(ref: React.RefObject<HTMLElement | null>) {\n  const [visible, setVisible] = React.useState(true);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || typeof IntersectionObserver === \"undefined\") return;\n    const observer = new IntersectionObserver((entries) => {\n      const last = entries[entries.length - 1];\n      if (last) setVisible(last.isIntersecting);\n    });\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [ref]);\n  return visible;\n}\n\nfunction usePause() {\n  const [paused, setPaused] = React.useState(false);\n  const handlers = {\n    onPointerEnter: () => setPaused(true),\n    onPointerLeave: () => setPaused(false),\n    onFocus: (e: React.FocusEvent<HTMLElement>) => {\n      if (e.target.matches(\":focus-visible\")) setPaused(true);\n    },\n    onBlur: (e: React.FocusEvent<HTMLElement>) => {\n      if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setPaused(false);\n    },\n  };\n  return [paused, handlers] as const;\n}\n\nfunction useAutoAdvance(\n  enabled: boolean,\n  interval: number,\n  step: () => void,\n  ref: React.RefObject<HTMLElement | null>,\n  paused: boolean,\n  resetKey: number,\n) {\n  const reduced = useReducedMotion();\n  const visible = useInView(ref);\n  const stepRef = React.useRef(step);\n  React.useEffect(() => {\n    stepRef.current = step;\n  });\n  React.useEffect(() => {\n    if (!enabled || reduced || !visible || paused) return;\n    const id = window.setInterval(() => stepRef.current(), interval);\n    return () => window.clearInterval(id);\n  }, [enabled, reduced, visible, paused, interval, resetKey]);\n}\n\nfunction useSlideIndex(\n  count: number,\n  value: number | undefined,\n  initial: number,\n  onChange?: (index: number) => void,\n) {\n  const [inner, setInner] = React.useState(initial);\n  const controlled = value !== undefined;\n  const current = mod(controlled ? value : inner, count);\n  const set = (next: number) => {\n    const n = mod(next, count);\n    if (n === current) return;\n    if (!controlled) setInner(n);\n    onChange?.(n);\n  };\n  return [current, set] as const;\n}\n\nexport interface ImageStackSwipeProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n  images?: string[];\n  alt?: string;\n  width?: number | string;\n  height?: number | string;\n  aspect?: string;\n  index?: number;\n  defaultIndex?: number;\n  onChange?: (index: number) => void;\n  onSwipe?: (direction: \"left\" | \"right\", index: number) => void;\n  visible?: number;\n  autoPlay?: boolean;\n  interval?: number;\n  accent?: string;\n}\n\nconst css = `\n.hb-qe9 {\n  --hb-qe9-a: #2f7bff;\n  position: relative;\n  display: block;\n  box-sizing: border-box;\n  overflow: hidden;\n  border-radius: 14px;\n  background: #0a0a0a;\n  isolation: isolate;\n  font-family: inherit;\n  user-select: none;\n  -webkit-user-select: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-qe9 *, .hb-qe9 *::before, .hb-qe9 *::after { box-sizing: border-box; }\n.hb-qe9:focus-visible { outline: 2px solid var(--hb-qe9-a); outline-offset: 2px; }\n.hb-qe9-layer { position: absolute; inset: 0; }\n.hb-qe9-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip-path: inset(50%);\n  white-space: nowrap;\n  border: 0;\n}\n.hb-qe9 {\n  overflow: visible;\n  background: transparent;\n  border-radius: 0;\n  touch-action: pan-y;\n}\n.hb-qe9-stage { position: absolute; inset: 0 0 18px 0; }\n.hb-qe9-card {\n  position: absolute;\n  inset: 0;\n  background-color: #151515;\n  background-size: cover;\n  background-position: center;\n  border-radius: 16px;\n  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1), 0 18px 32px -16px rgba(0, 0, 0, 0.7);\n  transform: translateY(calc(var(--hb-qe9-k) * 9px)) scale(calc(1 - var(--hb-qe9-k) * 0.055));\n  transform-origin: 50% 100%;\n  filter: brightness(calc(1 - var(--hb-qe9-k) * 0.2));\n  transition: transform 0.45s cubic-bezier(0.3, 0.8, 0.2, 1), filter 0.45s ease, opacity 0.45s ease;\n  will-change: transform;\n}\n.hb-qe9-card[data-top=\"true\"] { cursor: grab; touch-action: pan-y; transform-origin: 50% 90%; }\n.hb-qe9-card[data-top=\"true\"][data-drag=\"true\"] { cursor: grabbing; transition: none; }\n.hb-qe9-card[data-exit=\"true\"] {\n  transition: transform 0.38s cubic-bezier(0.5, 0, 0.8, 0.4), opacity 0.38s ease;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qe9-card { transition: none; }\n}\n`;\n\nexport function ImageStackSwipe({\n  images,\n  alt = \"Photo\",\n  width,\n  height,\n  aspect = \"4 / 3\",\n  index: indexProp,\n  defaultIndex = 0,\n  onChange,\n  onSwipe,\n  visible = 3,\n  autoPlay = true,\n  interval = 2600,\n  accent = \"#2f7bff\",\n  className,\n  style,\n  onKeyDown,\n  ...rest\n}: ImageStackSwipeProps) {\n  const list = images && images.length > 0 ? images : null;\n  const count = list ? list.length : SCENE_COUNT;\n  const shown = Math.max(1, Math.min(Math.round(visible), count));\n  const [index, setIndex] = useSlideIndex(count, indexProp, defaultIndex, onChange);\n  const [drag, setDrag] = React.useState({ dx: 0, dy: 0, active: false });\n  const [exit, setExit] = React.useState<0 | 1 | -1>(0);\n  const [paused, pause] = usePause();\n  const ref = React.useRef<HTMLDivElement>(null);\n  const start = React.useRef<{ x: number; y: number; t: number } | null>(null);\n  const reduced = useReducedMotion();\n  const label = (i: number) => `${alt} ${i + 1} of ${count}`;\n\n  const fling = (dir: 1 | -1) => {\n    if (exit !== 0 || count < 2) return;\n    onSwipe?.(dir === 1 ? \"right\" : \"left\", index);\n    if (reduced) {\n      setDrag({ dx: 0, dy: 0, active: false });\n      setIndex(index + 1);\n      return;\n    }\n    setExit(dir);\n  };\n\n  const finish = () => {\n    setExit(0);\n    setDrag({ dx: 0, dy: 0, active: false });\n    setIndex(index + 1);\n  };\n\n  useAutoAdvance(\n    autoPlay && count > 1,\n    interval,\n    () => fling(index % 2 === 0 ? 1 : -1),\n    ref,\n    paused || drag.active || exit !== 0,\n    index,\n  );\n\n  const topStyle: React.CSSProperties =\n    exit !== 0\n      ? {\n          transform: `translate(${exit * 140}%, ${(drag.dy * 0.4).toFixed(1)}px) rotate(${exit * 22}deg)`,\n          opacity: 0,\n        }\n      : drag.dx !== 0 || drag.dy !== 0\n        ? {\n            transform: `translate(${drag.dx.toFixed(1)}px, ${drag.dy.toFixed(1)}px) rotate(${(drag.dx * 0.07).toFixed(2)}deg)`,\n          }\n        : {};\n\n  return (\n    <div\n      tabIndex={0}\n      role=\"group\"\n      aria-roledescription=\"carousel\"\n      aria-label={`${alt} stack`}\n      {...rest}\n      {...pause}\n      ref={ref}\n      className={cx(\"hb-qe9\", className)}\n      style={{ ...rootBox(width, height, aspect, style), \"--hb-qe9-a\": accent } as React.CSSProperties}\n      onKeyDown={(e) => {\n        onKeyDown?.(e);\n        if (e.defaultPrevented) return;\n        if (e.key === \"ArrowLeft\") {\n          e.preventDefault();\n          fling(-1);\n        } else if (e.key === \"ArrowRight\") {\n          e.preventDefault();\n          fling(1);\n        }\n      }}\n    >\n      <style href=\"hb-image-stack-swipe\" precedence=\"default\">\n        {css}\n      </style>\n      <div className=\"hb-qe9-stage\">\n        {Array.from({ length: shown }, (_, k) => shown - 1 - k).map((k) => {\n          const ci = mod(index + k, count);\n          const top = k === 0;\n          return (\n            <div\n              key={ci}\n              className=\"hb-qe9-card\"\n              role=\"img\"\n              aria-label={label(ci)}\n              aria-hidden={top ? undefined : true}\n              data-top={top ? \"true\" : \"false\"}\n              data-drag={top && drag.active ? \"true\" : \"false\"}\n              data-exit={top && exit !== 0 ? \"true\" : \"false\"}\n              style={\n                {\n                  backgroundImage: cssUrl(list, ci),\n                  \"--hb-qe9-k\": k,\n                  zIndex: shown - k,\n                  ...(top ? topStyle : {}),\n                } as React.CSSProperties\n              }\n              onPointerDown={\n                top\n                  ? (e) => {\n                      if (exit !== 0 || count < 2) return;\n                      e.currentTarget.setPointerCapture(e.pointerId);\n                      start.current = { x: e.clientX, y: e.clientY, t: e.timeStamp };\n                      setDrag({ dx: 0, dy: 0, active: true });\n                    }\n                  : undefined\n              }\n              onPointerMove={\n                top\n                  ? (e) => {\n                      const s = start.current;\n                      if (!s) return;\n                      setDrag({ dx: e.clientX - s.x, dy: (e.clientY - s.y) * 0.5, active: true });\n                    }\n                  : undefined\n              }\n              onPointerUp={\n                top\n                  ? (e) => {\n                      const s = start.current;\n                      start.current = null;\n                      if (!s) return;\n                      const dx = e.clientX - s.x;\n                      const elapsed = Math.max(1, e.timeStamp - s.t);\n                      const far = Math.abs(dx) > e.currentTarget.offsetWidth * 0.28;\n                      const quick = Math.abs(dx) > 28 && Math.abs(dx) / elapsed > 0.6;\n                      if (far || quick) {\n                        setDrag((d) => ({ ...d, active: false }));\n                        fling(dx > 0 ? 1 : -1);\n                      } else {\n                        setDrag({ dx: 0, dy: 0, active: false });\n                      }\n                    }\n                  : undefined\n              }\n              onPointerCancel={\n                top\n                  ? () => {\n                      start.current = null;\n                      setDrag({ dx: 0, dy: 0, active: false });\n                    }\n                  : undefined\n              }\n              onTransitionEnd={\n                top\n                  ? (e) => {\n                      if (exit !== 0 && e.propertyName === \"transform\") finish();\n                    }\n                  : undefined\n              }\n            />\n          );\n        })}\n      </div>\n      <span className=\"hb-qe9-sr\" aria-live=\"polite\">\n        {label(index)}\n      </span>\n    </div>\n  );\n}\n"
    }
  ]
}