{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gallery-coverflow",
  "type": "registry:component",
  "title": "Gallery Coverflow",
  "description": "A classic cover flow: the center image faces you while the others turn away in 3D and stack to each side, with a soft reflection on the floor. Drag, click a side image or use the arrow keys.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "components",
    "gallery",
    "coverflow",
    "3d",
    "carousel",
    "reflection",
    "drag"
  ],
  "docs": "https://hyperblur.dev/effects/gallery-coverflow",
  "files": [
    {
      "path": "registry/hyperblur/gallery-coverflow.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/gallery-coverflow.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type GalleryCoverflowImage = { src?: string; title: string; caption?: string };\n\nexport interface GalleryCoverflowProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  images?: GalleryCoverflowImage[];\n  value?: number;\n  defaultValue?: number;\n  onChange?: (index: number) => void;\n  autoplay?: boolean;\n  interval?: number;\n  height?: number;\n  angle?: number;\n  reflection?: boolean;\n  accent?: string;\n  label?: string;\n}\n\nconst sampleImages: GalleryCoverflowImage[] = [\n  { title: \"Dune Hour\", caption: \"Erg Chebbi, Morocco\" },\n  { title: \"Harbor Lights\", caption: \"Lisbon, Portugal\" },\n  { title: \"Glacier Line\", caption: \"Jokulsarlon, Iceland\" },\n  { title: \"Pine Ridge\", caption: \"Cascade Range, Oregon\" },\n  { title: \"Polar Night\", caption: \"Tromso, Norway\" },\n  { title: \"Sun Room\", caption: \"Brooklyn, New York\" },\n];\n\nfunction mix(n: number) {\n  let x = Math.imul((n + 7) | 0, 0x9e3779b1) >>> 0;\n  x = Math.imul(x ^ (x >>> 16), 0x85ebca6b) >>> 0;\n  x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35) >>> 0;\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nfunction sky(a: string, b: string, c: string) {\n  return `<defs><linearGradient id=\"s\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\"><stop offset=\"0\" stop-color=\"${a}\"/><stop offset=\"0.62\" stop-color=\"${b}\"/><stop offset=\"1\" stop-color=\"${c}\"/></linearGradient></defs><rect width=\"320\" height=\"240\" fill=\"url(#s)\"/>`;\n}\n\nfunction sceneSvg(k: number) {\n  let b = \"\";\n  if (k === 0) {\n    b =\n      sky(\"#ffd28f\", \"#f2866b\", \"#8a4a7c\") +\n      '<circle cx=\"214\" cy=\"98\" r=\"54\" fill=\"#fff3cf\" opacity=\"0.18\"/><circle cx=\"214\" cy=\"98\" r=\"30\" fill=\"#fff3cf\"/><path d=\"M0 168 Q90 122 172 160 T320 150 V240 H0Z\" fill=\"#dc8752\"/><path d=\"M0 198 Q110 160 204 194 T320 186 V240 H0Z\" fill=\"#ab5840\"/><path d=\"M0 224 Q120 200 236 222 T320 216 V240 H0Z\" fill=\"#6f3437\"/>';\n  } else if (k === 1) {\n    b = sky(\"#0c1230\", \"#28205e\", \"#5c2c6c\") + '<circle cx=\"258\" cy=\"48\" r=\"13\" fill=\"#fff1d6\"/>';\n    for (let i = 0; i < 18; i += 1) {\n      const v = mix(i + 40);\n      b += `<circle cx=\"${v % 320}\" cy=\"${mix(i + 90) % 90}\" r=\"${(0.6 + (v % 4) / 5).toFixed(2)}\" fill=\"#ffffff\" opacity=\"0.7\"/>`;\n    }\n    for (let i = 0; i < 10; i += 1) {\n      const x = i * 34 - 6;\n      const w = 30 + (mix(i) % 10);\n      const h = 70 + (mix(i + 20) % 96);\n      b += `<rect x=\"${x}\" y=\"${222 - h}\" width=\"${w}\" height=\"${h}\" fill=\"#161333\"/>`;\n      for (let r = 0; r < Math.floor((h - 12) / 14); r += 1) {\n        for (let c = 0; c < Math.floor((w - 8) / 9); c += 1) {\n          const q = mix(i * 97 + r * 13 + c);\n          if (q % 3 === 0) b += `<rect x=\"${x + 5 + c * 9}\" y=\"${230 - h + r * 14}\" width=\"4\" height=\"6\" fill=\"${q % 2 ? \"#ffcf7a\" : \"#ff9f86\"}\"/>`;\n        }\n      }\n    }\n    b += '<rect y=\"222\" width=\"320\" height=\"18\" fill=\"#0b0a1f\"/><rect x=\"40\" y=\"228\" width=\"60\" height=\"2\" fill=\"#ffcf7a\" opacity=\"0.35\"/><rect x=\"190\" y=\"232\" width=\"80\" height=\"2\" fill=\"#ff9f86\" opacity=\"0.3\"/>';\n  } else if (k === 2) {\n    b =\n      sky(\"#d9efff\", \"#86bde8\", \"#2f6fa8\") +\n      '<polygon points=\"0,150 46,96 82,128 124,78 170,134 214,92 262,140 320,104 320,170 0,170\" fill=\"#f4fbff\"/><polygon points=\"46,96 60,150 30,150\" fill=\"#c4def2\"/><polygon points=\"124,78 142,150 104,150\" fill=\"#c4def2\"/><polygon points=\"214,92 230,150 196,150\" fill=\"#c4def2\"/><rect y=\"164\" width=\"320\" height=\"76\" fill=\"#17508a\"/><path d=\"M0 182 Q40 176 80 182 T160 182 T240 182 T320 182\" stroke=\"#8ec6f0\" stroke-width=\"2\" fill=\"none\" opacity=\"0.5\"/><path d=\"M0 204 Q40 198 80 204 T160 204 T240 204 T320 204\" stroke=\"#8ec6f0\" stroke-width=\"2\" fill=\"none\" opacity=\"0.35\"/><polygon points=\"88,176 126,176 116,190 96,190\" fill=\"#e8f6ff\"/><polygon points=\"220,196 268,196 256,212 230,212\" fill=\"#d2ecff\"/>';\n  } else if (k === 3) {\n    b = sky(\"#e4ece4\", \"#a9c2b2\", \"#6f8f7e\");\n    const rows: [number, number, string][] = [\n      [120, 50, \"#86a596\"],\n      [150, 62, \"#4b6d5d\"],\n      [186, 78, \"#22392f\"],\n    ];\n    rows.forEach(([base, tall, fill], r) => {\n      for (let i = 0; i < 12; i += 1) {\n        const x = i * 30 + (mix(r * 31 + i) % 18) - 10;\n        const h = tall - 14 + (mix(r * 57 + i) % 28);\n        b += `<polygon points=\"${x},${base - h} ${x - 15},${base + 6} ${x + 15},${base + 6}\" fill=\"${fill}\"/>`;\n      }\n      b += `<rect y=\"${base + 4}\" width=\"320\" height=\"${240 - base}\" fill=\"${fill}\"/>`;\n      if (r === 0) b += '<rect y=\"118\" width=\"320\" height=\"30\" fill=\"#e4ece4\" opacity=\"0.55\"/>';\n    });\n  } else if (k === 4) {\n    b =\n      sky(\"#040a18\", \"#0b1d38\", \"#12304a\") +\n      '<path d=\"M0 96 C70 40 160 126 320 56 L320 104 C170 168 92 84 0 146Z\" fill=\"#3ef0a8\" opacity=\"0.32\"/><path d=\"M0 70 C90 30 170 96 320 30 L320 60 C180 120 96 60 0 110Z\" fill=\"#8f6bff\" opacity=\"0.22\"/><path d=\"M40 110 C120 76 200 140 300 90\" stroke=\"#9fffd6\" stroke-width=\"3\" fill=\"none\" opacity=\"0.4\"/>';\n    for (let i = 0; i < 26; i += 1) {\n      const v = mix(i + 300);\n      b += `<circle cx=\"${v % 320}\" cy=\"${mix(i + 500) % 120}\" r=\"${(0.5 + (v % 3) / 4).toFixed(2)}\" fill=\"#ffffff\" opacity=\"0.75\"/>`;\n    }\n    b +=\n      '<polygon points=\"0,200 60,140 104,176 160,120 220,180 266,150 320,190 320,240 0,240\" fill=\"#0a1626\"/><polygon points=\"160,120 176,136 168,134 160,142 152,134 146,134\" fill=\"#dfeaf5\"/><polygon points=\"60,140 72,152 60,150 50,152\" fill=\"#dfeaf5\"/><rect y=\"214\" width=\"320\" height=\"26\" fill=\"#06101d\"/>';\n  } else {\n    b =\n      '<defs><radialGradient id=\"s\" cx=\"0.32\" cy=\"0.3\" r=\"0.9\"><stop offset=\"0\" stop-color=\"#ffe2c8\"/><stop offset=\"0.55\" stop-color=\"#e09a7c\"/><stop offset=\"1\" stop-color=\"#7c4648\"/></radialGradient><radialGradient id=\"o\" cx=\"0.35\" cy=\"0.3\" r=\"0.75\"><stop offset=\"0\" stop-color=\"#fff6ea\"/><stop offset=\"1\" stop-color=\"#c46f58\"/></radialGradient></defs><rect width=\"320\" height=\"240\" fill=\"url(#s)\"/><path d=\"M188 240 V112 A52 52 0 0 1 292 112 V240Z\" fill=\"#f6c9a8\" opacity=\"0.55\"/><rect x=\"40\" y=\"176\" width=\"140\" height=\"64\" fill=\"#5d3236\"/><rect x=\"40\" y=\"170\" width=\"140\" height=\"8\" fill=\"#7a4447\"/><circle cx=\"110\" cy=\"138\" r=\"32\" fill=\"url(#o)\"/><rect x=\"150\" y=\"128\" width=\"16\" height=\"42\" rx=\"3\" fill=\"#2f2a44\"/><ellipse cx=\"158\" cy=\"128\" rx=\"8\" ry=\"3\" fill=\"#4a4366\"/>';\n  }\n  return `<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 320 240\" preserveAspectRatio=\"xMidYMid slice\">${b}</svg>`;\n}\n\nconst sceneCache = new Map<number, string>();\n\nfunction sceneUrl(index: number) {\n  const k = ((index % 6) + 6) % 6;\n  let url = sceneCache.get(k);\n  if (!url) {\n    url = `url(\"data:image/svg+xml,${encodeURIComponent(sceneSvg(k))}\")`;\n    sceneCache.set(k, url);\n  }\n  return url;\n}\n\nfunction imageUrl(src: string | undefined, index: number) {\n  return src ? `url(${JSON.stringify(src)})` : sceneUrl(index);\n}\n\nconst css = `\n.hb-zw5 {\n  --hb-zw5-accent: #2f7bff;\n  box-sizing: border-box;\n  display: flex;\n  flex-direction: column;\n  align-items: center;\n  width: 100%;\n  color: #ededed;\n  font-family: inherit;\n}\n.hb-zw5 *, .hb-zw5 *::before, .hb-zw5 *::after { box-sizing: border-box; }\n.hb-zw5-stage {\n  position: relative;\n  width: 100%;\n  height: var(--hb-zw5-h);\n  overflow: hidden;\n  perspective: var(--hb-zw5-persp);\n  cursor: grab;\n  touch-action: pan-y;\n  user-select: none;\n  -webkit-user-select: none;\n}\n.hb-zw5-stage[data-drag=\"true\"] { cursor: grabbing; }\n.hb-zw5-track {\n  position: absolute;\n  inset: 0;\n  transform-style: preserve-3d;\n}\n.hb-zw5-item {\n  position: absolute;\n  left: 50%;\n  top: var(--hb-zw5-top);\n  width: var(--hb-zw5-s);\n  height: var(--hb-zw5-s);\n  margin: 0 0 0 calc(var(--hb-zw5-s) / -2);\n  padding: 0;\n  border: 0;\n  background: none;\n  cursor: pointer;\n  outline: none;\n  transform-style: preserve-3d;\n  transition: transform 0.6s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.4s ease;\n}\n.hb-zw5-stage[data-drag=\"true\"] .hb-zw5-item { transition: none; }\n.hb-zw5-face {\n  position: absolute;\n  inset: 0;\n  overflow: hidden;\n  border-radius: 6px;\n  background-color: #171717;\n  background-size: cover;\n  background-position: center;\n  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08), 0 14px 30px -12px rgba(0, 0, 0, 0.8);\n}\n.hb-zw5-shade {\n  position: absolute;\n  inset: 0;\n  border-radius: inherit;\n  background: #000000;\n  pointer-events: none;\n  transition: opacity 0.6s ease;\n}\n.hb-zw5-stage[data-drag=\"true\"] .hb-zw5-shade { transition: none; }\n.hb-zw5-ref {\n  position: absolute;\n  left: 0;\n  right: 0;\n  top: calc(100% + 2px);\n  height: 100%;\n  border-radius: 6px;\n  background-size: cover;\n  background-position: center;\n  transform: scaleY(-1);\n  opacity: 0.9;\n  -webkit-mask-image: linear-gradient(180deg, transparent 58%, rgba(0, 0, 0, 0.42) 100%);\n  mask-image: linear-gradient(180deg, transparent 58%, rgba(0, 0, 0, 0.42) 100%);\n  pointer-events: none;\n}\n.hb-zw5-item:focus-visible .hb-zw5-face { box-shadow: 0 0 0 2px var(--hb-zw5-accent), 0 14px 30px -12px rgba(0, 0, 0, 0.8); }\n.hb-zw5-floor {\n  position: absolute;\n  left: 0;\n  right: 0;\n  bottom: 0;\n  height: 34%;\n  background: linear-gradient(180deg, transparent, rgba(10, 10, 10, 0.6));\n  pointer-events: none;\n}\n.hb-zw5-cap {\n  display: flex;\n  flex-direction: column;\n  align-items: center;\n  min-height: 2.6em;\n  max-width: 100%;\n  margin-top: 4px;\n  text-align: center;\n}\n.hb-zw5-title {\n  margin: 0;\n  max-width: 100%;\n  font-size: 1em;\n  font-weight: 600;\n  line-height: 1.3;\n  white-space: nowrap;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  animation: hb-zw5-in 0.5s cubic-bezier(0.2, 0.7, 0.2, 1) both;\n}\n.hb-zw5-sub {\n  margin: 1px 0 0;\n  max-width: 100%;\n  font-size: 0.76em;\n  color: #8f8f8f;\n  white-space: nowrap;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  animation: hb-zw5-in 0.5s 0.06s cubic-bezier(0.2, 0.7, 0.2, 1) both;\n}\n@keyframes hb-zw5-in { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-zw5-item, .hb-zw5-shade { transition: none; }\n  .hb-zw5-title, .hb-zw5-sub { animation: none; }\n}\n`;\n\nexport function GalleryCoverflow({\n  images,\n  value,\n  defaultValue,\n  onChange,\n  autoplay = true,\n  interval = 2600,\n  height = 300,\n  angle = 55,\n  reflection = true,\n  accent = \"#2f7bff\",\n  label = \"Cover flow\",\n  className,\n  style,\n  ...rest\n}: GalleryCoverflowProps) {\n  const list = images && images.length ? images : sampleImages;\n  const n = list.length;\n  const clamp = (v: number) => Math.min(Math.max(v, 0), n - 1);\n  const [inner, setInner] = React.useState(() => clamp(defaultValue ?? Math.floor((n - 1) / 2)));\n  const current = clamp(Math.round(value ?? inner));\n  const [width, setWidth] = React.useState(360);\n  const [dragPos, setDragPos] = React.useState<number | null>(null);\n  const [hover, setHover] = React.useState(false);\n  const [focused, setFocused] = React.useState(false);\n  const [visible, setVisible] = React.useState(true);\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const stageRef = React.useRef<HTMLDivElement>(null);\n  const itemRefs = React.useRef<(HTMLButtonElement | null)[]>([]);\n  const drag = React.useRef<{ id: number; x: number; start: number; moved: boolean; lastX: number; lastT: number; v: number } | null>(null);\n  const dirRef = React.useRef(1);\n\n  const size = Math.max(40, Math.min(height * 0.6, width * 0.4));\n  const top = Math.max(4, height * 0.42 - size / 2);\n\n  React.useEffect(() => {\n    const el = stageRef.current;\n    if (!el) return;\n    const ro = typeof ResizeObserver !== \"undefined\" ? new ResizeObserver(([e]) => setWidth(e.contentRect.width)) : null;\n    ro?.observe(el);\n    const io =\n      typeof IntersectionObserver !== \"undefined\"\n        ? new IntersectionObserver(([e]) => setVisible(e.isIntersecting))\n        : null;\n    io?.observe(el);\n    return () => {\n      ro?.disconnect();\n      io?.disconnect();\n    };\n  }, []);\n\n  const go = React.useCallback(\n    (next: number, focusItem = false) => {\n      const k = Math.min(Math.max(Math.round(next), 0), n - 1);\n      if (value === undefined) setInner(k);\n      if (k !== current) onChange?.(k);\n      if (focusItem) itemRefs.current[k]?.focus({ preventScroll: true });\n    },\n    [n, value, current, onChange],\n  );\n\n  const goRef = React.useRef(go);\n  React.useEffect(() => {\n    goRef.current = go;\n  }, [go]);\n\n  React.useEffect(() => {\n    if (!autoplay || hover || focused || !visible || dragPos !== null || n < 2) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const t = window.setInterval(() => {\n      let d = dirRef.current;\n      if (current + d > n - 1 || current + d < 0) d = -d;\n      dirRef.current = d;\n      goRef.current(current + d);\n    }, interval);\n    return () => window.clearInterval(t);\n  }, [autoplay, hover, focused, visible, dragPos, n, interval, current]);\n\n  const step = size * 0.45;\n\n  const onPointerDown = (e: React.PointerEvent<HTMLDivElement>) => {\n    if (e.button !== 0) return;\n    drag.current = { id: e.pointerId, x: e.clientX, start: current, moved: false, lastX: e.clientX, lastT: e.timeStamp, v: 0 };\n  };\n\n  const onPointerMove = (e: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (!d || d.id !== e.pointerId) return;\n    if (!d.moved) {\n      if (Math.abs(e.clientX - d.x) < 5) return;\n      d.moved = true;\n      e.currentTarget.setPointerCapture(e.pointerId);\n    }\n    const dt = Math.max(1, e.timeStamp - d.lastT);\n    d.v = (e.clientX - d.lastX) / dt;\n    d.lastX = e.clientX;\n    d.lastT = e.timeStamp;\n    const p = d.start - (e.clientX - d.x) / step;\n    setDragPos(Math.min(Math.max(p, -0.45), n - 0.55));\n  };\n\n  const endDrag = (e: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (!d || d.id !== e.pointerId) return;\n    drag.current = null;\n    if (!d.moved) return;\n    if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n    const p = d.start - (e.clientX - d.x) / step - d.v * 6;\n    setDragPos(null);\n    go(p);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\") next = current + 1;\n    else if (e.key === \"ArrowLeft\") next = current - 1;\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = n - 1;\n    if (next === null) return;\n    e.preventDefault();\n    go(next, true);\n  };\n\n  const pos = dragPos ?? current;\n  const item = list[current];\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"region\"\n      aria-roledescription=\"carousel\"\n      aria-label={label}\n      className={className ? `hb-zw5 ${className}` : \"hb-zw5\"}\n      style={\n        {\n          \"--hb-zw5-accent\": accent,\n          \"--hb-zw5-h\": `${height}px`,\n          \"--hb-zw5-s\": `${size.toFixed(1)}px`,\n          \"--hb-zw5-top\": `${top.toFixed(1)}px`,\n          \"--hb-zw5-persp\": `${(size * 3.4).toFixed(0)}px`,\n          ...style,\n        } as React.CSSProperties\n      }\n      onPointerEnter={(e) => {\n        if (e.pointerType === \"mouse\") setHover(true);\n      }}\n      onPointerLeave={() => setHover(false)}\n      onFocus={() => setFocused(true)}\n      onBlur={(e) => {\n        if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setFocused(false);\n      }}\n      onKeyDown={onKeyDown}\n      {...rest}\n    >\n      <style href=\"hb-gallery-coverflow\" precedence=\"default\">\n        {css}\n      </style>\n      <div\n        ref={stageRef}\n        className=\"hb-zw5-stage\"\n        data-drag={dragPos !== null ? \"true\" : \"false\"}\n        onPointerDown={onPointerDown}\n        onPointerMove={onPointerMove}\n        onPointerUp={endDrag}\n        onPointerCancel={endDrag}\n      >\n        <div className=\"hb-zw5-track\">\n          {list.map((img, i) => {\n            const d = i - pos;\n            const a = Math.abs(d);\n            const sg = d === 0 ? 0 : d > 0 ? 1 : -1;\n            const near = Math.min(a, 1);\n            const far = Math.max(a - 1, 0);\n            const x = sg * (near * size * 0.72 + far * size * 0.3);\n            const z = -(near * size * 0.6 + far * size * 0.14);\n            const ry = -sg * near * angle;\n            const opacity = a > 4.6 ? 0 : Math.min(1, 4.6 - a);\n            const bg = imageUrl(img.src, i);\n            return (\n              <button\n                key={i}\n                ref={(el) => {\n                  itemRefs.current[i] = el;\n                }}\n                type=\"button\"\n                className=\"hb-zw5-item\"\n                aria-label={img.title}\n                aria-current={i === current ? \"true\" : undefined}\n                tabIndex={i === current ? 0 : -1}\n                style={{\n                  transform: `translate3d(${x.toFixed(1)}px, 0px, ${z.toFixed(1)}px) rotateY(${ry.toFixed(2)}deg)`,\n                  zIndex: 100 - Math.round(a * 10),\n                  opacity: Number(opacity.toFixed(3)),\n                  pointerEvents: opacity < 0.05 ? \"none\" : undefined,\n                }}\n                onClick={() => {\n                  if (i !== current) go(i);\n                }}\n              >\n                <span className=\"hb-zw5-face\" style={{ backgroundImage: bg }}>\n                  <span className=\"hb-zw5-shade\" style={{ opacity: Number((near * 0.42 + Math.min(far, 3) * 0.08).toFixed(3)) }} />\n                </span>\n                {reflection ? <span className=\"hb-zw5-ref\" style={{ backgroundImage: bg }} aria-hidden=\"true\" /> : null}\n              </button>\n            );\n          })}\n        </div>\n        <div className=\"hb-zw5-floor\" />\n      </div>\n      <div className=\"hb-zw5-cap\" key={current} aria-live=\"polite\">\n        <p className=\"hb-zw5-title\">{item.title}</p>\n        {item.caption ? <p className=\"hb-zw5-sub\">{item.caption}</p> : null}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}