{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"curved-gallery","type":"registry:component","title":"Curved Gallery","description":"A ring of images curving towards you. It drifts on its own, drags and flicks with momentum, settles neatly on an image, and keeps the same composition from desktop down to phones.","dependencies":[],"files":[{"path":"registry/dot/curved-gallery.tsx","type":"registry:component","target":"components/dot/curved-gallery.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type CSSProperties } from \"react\";\n\nexport interface CurvedGalleryItem {\n  /** Image URL */\n  image: string;\n  /** Describes the image for screen readers */\n  alt?: string;\n  /** Makes the image a link */\n  href?: string;\n}\n\nexport interface CurvedGalleryProps {\n  /** Images in order. Leave empty to see numbered placeholder cards while you set it up */\n  items?: CurvedGalleryItem[];\n  /** Card width in px; shrinks to fit small screens */\n  cardWidth?: number;\n  /** Card width ÷ height, e.g. 0.8 for portrait, 1 for square */\n  aspectRatio?: number;\n  /** Space between cards, in px */\n  gap?: number;\n  /** Degrees each card turns from its neighbour; 0 is a flat row */\n  curve?: number;\n  /** Tips the whole ring towards or away from the viewer, in degrees */\n  tilt?: number;\n  /** Camera distance in px; lower is more dramatic */\n  perspective?: number;\n  /** Keep drifting by itself */\n  autoplay?: boolean;\n  /** Cards per second while drifting */\n  speed?: number;\n  direction?: \"left\" | \"right\";\n  /** Ease to a stop on the nearest card while the pointer is over the gallery */\n  pauseOnHover?: boolean;\n  /** Drag, swipe and trackpad scrolling */\n  draggable?: boolean;\n  /** \"mono\" shows the photos in black and white */\n  tone?: \"color\" | \"mono\";\n  /** Corner radius in px */\n  radius?: number;\n  /** How much cards darken as they turn away (0–1) */\n  dimSides?: number;\n  /** Accessible name for the carousel */\n  ariaLabel?: string;\n  className?: string;\n  style?: CSSProperties;\n}\n\nconst MAX_CARDS = 24;\nconst DESIGN_WIDTH = 720;\nconst TAU_DEG = Math.PI / 180;\n\nconst mod = (n: number, m: number) => ((n % m) + m) % m;\n\n// Shown until `items` is passed, so a fresh install renders a full ring instead of an empty box.\n// Inline SVGs: no image files to copy and nothing to download.\nconst PLACEHOLDER_TONES = [\n  [\"#3A3A42\", \"#16161A\"],\n  [\"#4A3F36\", \"#1A1512\"],\n  [\"#2F3B44\", \"#11161A\"],\n  [\"#3F3646\", \"#17131A\"],\n  [\"#37413A\", \"#131714\"],\n];\nconst PLACEHOLDER_ITEMS: CurvedGalleryItem[] = PLACEHOLDER_TONES.map(([from, to], i) => {\n  const label = String(i + 1).padStart(2, \"0\");\n  const svg = `<svg xmlns='http://www.w3.org/2000/svg' width='400' height='500' viewBox='0 0 400 500'><defs><linearGradient id='g' x1='0' y1='0' x2='1' y2='1'><stop offset='0' stop-color='${from}'/><stop offset='1' stop-color='${to}'/></linearGradient></defs><rect width='400' height='500' fill='url(#g)'/><text x='32' y='468' fill='rgba(255,255,255,0.55)' font-family='ui-monospace, Menlo, monospace' font-size='28'>${label}</text></svg>`;\n  return { image: `data:image/svg+xml,${encodeURIComponent(svg)}`, alt: `Placeholder ${i + 1}` };\n});\n\n/**\n * A ring of images curving towards the viewer. It drifts on its own, can be dragged\n * or flicked with momentum, settles on a card when it stops, and answers to the arrow keys\n * (Space pauses). Fills its parent; cards shrink to fit narrow screens.\n */\nexport function CurvedGallery({\n  items = [],\n  cardWidth = 180,\n  aspectRatio = 0.8,\n  gap = 28,\n  curve = 24,\n  tilt = 0,\n  perspective = 700,\n  autoplay = true,\n  speed = 0.16,\n  direction = \"left\",\n  pauseOnHover = true,\n  draggable = true,\n  tone = \"color\",\n  radius = 0,\n  dimSides = 0.35,\n  ariaLabel = \"Gallery\",\n  className,\n  style,\n}: CurvedGalleryProps) {\n  const rootRef = useRef<HTMLDivElement | null>(null);\n  const cardRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const shadeRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const [box, setBox] = useState({ width: 0, height: 0 });\n\n  // Below DESIGN_WIDTH the whole scene (cards, gaps and camera distance) shrinks together, so\n  // phones see the same composition as desktop, just smaller. Cards also stay within the box.\n  const fit = box.width ? Math.min(1, Math.pow(box.width / DESIGN_WIDTH, 0.8)) : 1;\n  const width = box.width\n    ? Math.max(48, Math.min(cardWidth * fit, box.width * 0.72, box.height * 0.82 * aspectRatio))\n    : cardWidth;\n  const height = width / aspectRatio;\n  const scale = width / cardWidth;\n  const space = gap * scale;\n  const corner = Math.max(0, radius * scale);\n\n  // Repeat the items so the ring stays full however few there are\n  const list = items.length ? items : PLACEHOLDER_ITEMS;\n  const count = list.length;\n  const needed = Math.min(MAX_CARDS, Math.max(count, curve > 0.5 ? Math.ceil(200 / curve) : 0, box.width ? Math.ceil(box.width / (width + space)) + 4 : 8));\n  const repeats = count ? Math.max(1, Math.ceil(needed / count)) : 0;\n  const cards = Array.from({ length: count * repeats }, (_, i) => ({ item: list[i % count], index: i % count }));\n\n  // Everything the animation loop reads, kept current without restarting it\n  const live = useRef({ total: cards.length, step: width + space, curve, autoplay, speed, direction, pauseOnHover, draggable, dimSides, boxWidth: box.width, cardWidth: width });\n  const wake = useRef<() => void>(() => {});\n  useEffect(() => {\n    live.current = { total: cards.length, step: width + space, curve, autoplay, speed, direction, pauseOnHover, draggable, dimSides, boxWidth: box.width, cardWidth: width };\n    wake.current();\n  });\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const reduceMotion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\n    let pos = 0; // in cards; the card at `pos` faces the viewer\n    let vel = 0; // cards per second\n    let target: number | null = null;\n    let hovering = false;\n    let paused = false;\n    let holdUntil = 0; // after arrow-key navigation, stay put for a moment\n    let holdTimer = 0;\n    let visible = true;\n    let frameId = 0;\n    let running = false;\n    let last = 0;\n    let drag: { id: number; startX: number; startPos: number; lastX: number; lastT: number; moved: boolean } | null = null;\n    let suppressClick = false;\n\n    const autoVelocity = () => {\n      const s = live.current;\n      if (!s.autoplay || paused || reduceMotion.matches || (s.pauseOnHover && hovering)) return 0;\n      if (performance.now() < holdUntil) return 0;\n      return s.speed * (s.direction === \"left\" ? 1 : -1);\n    };\n\n    function render() {\n      const s = live.current;\n      const total = s.total;\n      if (!total) return;\n      const radians = s.curve * TAU_DEG;\n      const flat = s.curve < 0.5;\n      const ringRadius = flat ? 0 : s.step / radians;\n      const reach = s.boxWidth / 2 + s.cardWidth;\n      for (let i = 0; i < total; i++) {\n        const card = cardRefs.current[i];\n        if (!card) continue;\n        const offset = mod(i - pos + total / 2, total) - total / 2;\n        const angle = offset * s.curve;\n        const x = flat ? offset * s.step : ringRadius * Math.sin(angle * TAU_DEG);\n        const hidden = (!flat && Math.abs(angle) > 84) || (s.boxWidth > 0 && Math.abs(x) > reach);\n        card.style.visibility = hidden ? \"hidden\" : \"visible\";\n        if (hidden) continue;\n        card.style.transform = flat\n          ? `translate3d(${x}px, 0, 0)`\n          : `translateZ(${ringRadius}px) rotateY(${-angle}deg) translateZ(${-ringRadius}px)`;\n        card.style.zIndex = String(1000 - Math.round(Math.abs(offset) * 10));\n        const shade = shadeRefs.current[i];\n        if (shade) {\n          const away = flat ? Math.min(1, Math.abs(offset) / 2.5) : Math.min(1, Math.abs(angle) / 70);\n          shade.style.opacity = String(s.dimSides * away);\n        }\n      }\n    }\n\n    function tick(now: number) {\n      const dt = last ? Math.min((now - last) / 1000, 0.05) : 1 / 60;\n      last = now;\n      const auto = autoVelocity();\n\n      if (drag) {\n        // Position follows the pointer in onMove\n      } else if (target !== null) {\n        // Critically damped glide to a card (arrow keys, settling)\n        pos += (target - pos) * (1 - Math.exp(-dt * 9));\n        if (Math.abs(target - pos) < 0.0005) {\n          pos = target;\n          target = null;\n          vel = 0;\n        }\n      } else {\n        // Momentum from a flick blends back into the autoplay drift\n        vel += (auto - vel) * (1 - Math.exp(-dt * (auto ? 1.6 : 2.6)));\n        pos += vel * dt;\n        if (!auto && Math.abs(vel) < 0.08) target = Math.round(pos);\n      }\n      // Keep the number small so precision never drifts\n      const total = live.current.total;\n      if (total && Math.abs(pos) > total * 1000) {\n        const wrap = Math.floor(pos / total) * total;\n        pos -= wrap;\n        if (target !== null) target -= wrap;\n        if (drag) drag.startPos -= wrap;\n      }\n      render();\n\n      if (visible && (drag || target !== null || Math.abs(vel) > 0.0005 || auto)) {\n        frameId = requestAnimationFrame(tick);\n      } else {\n        running = false;\n        last = 0;\n      }\n    }\n\n    const start = () => {\n      render();\n      if (running || !visible) return;\n      running = true;\n      frameId = requestAnimationFrame(tick);\n    };\n    wake.current = start;\n\n    const onDown = (e: PointerEvent) => {\n      if (!live.current.draggable || e.button !== 0) return;\n      drag = { id: e.pointerId, startX: e.clientX, startPos: pos, lastX: e.clientX, lastT: performance.now(), moved: false };\n      target = null;\n      vel = 0;\n      start();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!drag || e.pointerId !== drag.id) return;\n      const now = performance.now();\n      const dx = e.clientX - drag.startX;\n      if (!drag.moved && Math.abs(dx) > 6) {\n        drag.moved = true;\n        root.setPointerCapture(e.pointerId);\n      }\n      const step = live.current.step;\n      pos = drag.startPos - dx / step;\n      const dt = (now - drag.lastT) / 1000;\n      if (dt > 0) vel = vel * 0.7 + (-(e.clientX - drag.lastX) / step / dt) * 0.3;\n      drag.lastX = e.clientX;\n      drag.lastT = now;\n    };\n    const onUp = (e: PointerEvent) => {\n      if (!drag || e.pointerId !== drag.id) return;\n      // A pause before letting go means no flick\n      if (performance.now() - drag.lastT > 90) vel = 0;\n      vel = Math.max(-6, Math.min(6, vel));\n      suppressClick = drag.moved;\n      drag = null;\n      start();\n    };\n    const onClick = (e: MouseEvent) => {\n      if (suppressClick) {\n        e.preventDefault();\n        e.stopPropagation();\n        suppressClick = false;\n      }\n    };\n    const onEnter = (e: PointerEvent) => {\n      if (e.pointerType === \"mouse\") hovering = true;\n      start();\n    };\n    const onLeave = () => {\n      hovering = false;\n      start();\n    };\n    const onWheel = (e: WheelEvent) => {\n      // Horizontal trackpad swipes only; vertical scrolling still scrolls the page\n      if (!live.current.draggable || Math.abs(e.deltaX) <= Math.abs(e.deltaY)) return;\n      e.preventDefault();\n      target = null;\n      pos += e.deltaX / live.current.step;\n      vel = (e.deltaX / live.current.step) * 20;\n      start();\n    };\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"ArrowRight\" || e.key === \"ArrowLeft\") {\n        e.preventDefault();\n        const base = target ?? Math.round(pos);\n        target = base + (e.key === \"ArrowRight\" ? 1 : -1);\n        vel = 0;\n        holdUntil = performance.now() + 5000;\n        clearTimeout(holdTimer);\n        holdTimer = window.setTimeout(start, 5050);\n        start();\n      } else if (e.key === \" \") {\n        e.preventDefault();\n        paused = !paused;\n        start();\n      }\n    };\n\n    const resize = new ResizeObserver(([entry]) => {\n      setBox({ width: entry.contentRect.width, height: entry.contentRect.height });\n    });\n    resize.observe(root);\n    const visibility = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n    });\n    visibility.observe(root);\n\n    root.addEventListener(\"pointerdown\", onDown);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerup\", onUp);\n    root.addEventListener(\"pointercancel\", onUp);\n    root.addEventListener(\"pointerenter\", onEnter);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"click\", onClick, true);\n    root.addEventListener(\"wheel\", onWheel, { passive: false });\n    root.addEventListener(\"keydown\", onKey);\n    reduceMotion.addEventListener(\"change\", start);\n    start();\n\n    return () => {\n      cancelAnimationFrame(frameId);\n      clearTimeout(holdTimer);\n      wake.current = () => {};\n      resize.disconnect();\n      visibility.disconnect();\n      root.removeEventListener(\"pointerdown\", onDown);\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerup\", onUp);\n      root.removeEventListener(\"pointercancel\", onUp);\n      root.removeEventListener(\"pointerenter\", onEnter);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"click\", onClick, true);\n      root.removeEventListener(\"wheel\", onWheel);\n      root.removeEventListener(\"keydown\", onKey);\n      reduceMotion.removeEventListener(\"change\", start);\n    };\n  }, []);\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"region\"\n      aria-roledescription=\"carousel\"\n      aria-label={ariaLabel}\n      tabIndex={0}\n      className={className}\n      style={{\n        position: \"relative\",\n        width: \"100%\",\n        height: \"100%\",\n        overflow: \"hidden\",\n        perspective: perspective * scale,\n        touchAction: \"pan-y\",\n        userSelect: \"none\",\n        WebkitUserSelect: \"none\",\n        cursor: draggable ? \"grab\" : undefined,\n        outline: \"none\",\n        ...style,\n      }}\n    >\n      <div style={{ position: \"absolute\", inset: 0, transformStyle: \"preserve-3d\", transform: `rotateX(${-tilt}deg)` }}>\n        {cards.map(({ item, index }, i) => {\n          const body = (\n            <>\n              {/* eslint-disable-next-line @next/next/no-img-element -- any image URL, also in Framer */}\n              <img\n                src={item.image}\n                alt={item.alt ?? \"\"}\n                draggable={false}\n                loading={i < count ? \"eager\" : \"lazy\"}\n                decoding=\"async\"\n                style={{\n                  position: \"absolute\",\n                  inset: 0,\n                  width: \"100%\",\n                  height: \"100%\",\n                  objectFit: \"cover\",\n                  display: \"block\",\n                  filter: tone === \"mono\" ? \"grayscale(1) contrast(1.06)\" : undefined,\n                }}\n              />\n              <div\n                ref={(el) => {\n                  shadeRefs.current[i] = el;\n                }}\n                style={{ position: \"absolute\", inset: 0, background: \"#000\", opacity: 0, pointerEvents: \"none\" }}\n              />\n            </>\n          );\n          const cardStyle: CSSProperties = {\n            position: \"absolute\",\n            left: \"50%\",\n            top: \"50%\",\n            width,\n            height,\n            marginLeft: -width / 2,\n            marginTop: -height / 2,\n            overflow: \"hidden\",\n            borderRadius: corner,\n            background: \"rgba(0,0,0,0.06)\",\n            backfaceVisibility: \"hidden\",\n            willChange: \"transform\",\n            visibility: \"hidden\",\n            display: \"block\",\n            textDecoration: \"none\",\n          };\n          const label = `${item.alt || \"Image\"}, ${index + 1} of ${count}`;\n          return item.href ? (\n            <a\n              key={i}\n              href={item.href}\n              aria-label={label}\n              tabIndex={i < count ? 0 : -1}\n              draggable={false}\n              ref={(el) => {\n                cardRefs.current[i] = el as unknown as HTMLDivElement | null;\n              }}\n              style={cardStyle}\n            >\n              {body}\n            </a>\n          ) : (\n            <div\n              key={i}\n              role=\"group\"\n              aria-roledescription=\"slide\"\n              aria-label={label}\n              aria-hidden={i >= count || undefined}\n              ref={(el) => {\n                cardRefs.current[i] = el;\n              }}\n              style={cardStyle}\n            >\n              {body}\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n"}],"docs":"Curved Gallery is ready. Props and a live canvas: https://bydot.co/component/curved-gallery","meta":{"tier":"free","category":"Interactive","version":"1.0.0"}}