{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cylinder-gallery",
  "title": "Cylinder Gallery",
  "description": "A 3D image gallery arranged around an auto-rotating cylinder, with configurable rows/columns, tilt angle, rotation speed, and edge fade opacity.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/cylinder-gallery/cylinder-gallery.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface CylinderGalleryImage {\n  src: string;\n  alt?: string;\n}\n\nexport interface CylinderGalleryProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  images?: CylinderGalleryImage[];\n  rows?: number;\n  columns?: number;\n  cylinderRadius?: number;\n  cardWidth?: number;\n  cardHeight?: number;\n  gap?: number;\n  borderRadius?: number;\n  rotationSpeed?: number;\n  tiltAngle?: number;\n}\n\nconst DEFAULT_IMAGES: CylinderGalleryImage[] = [\n  { src: \"/demo/23.webp\" },\n  { src: \"/demo/24.webp\" },\n  { src: \"/demo/25.webp\" },\n  { src: \"/demo/26.webp\" },\n  { src: \"/demo/27.webp\" },\n  { src: \"/demo/28.webp\" },\n  { src: \"/demo/29.webp\" },\n  { src: \"/demo/30.webp\" },\n  { src: \"/demo/31.webp\" },\n  { src: \"/demo/32.webp\" },\n  { src: \"/demo/33.webp\" },\n  { src: \"/demo/34.webp\" },\n];\n\nexport function CylinderGallery({\n  images = DEFAULT_IMAGES,\n  rows = 3,\n  columns = 6,\n  cylinderRadius = 320,\n  cardWidth = 320,\n  cardHeight = 280,\n  gap = 16,\n  borderRadius = 18,\n  rotationSpeed = 2.2,\n  tiltAngle = -12,\n  className,\n  ...props\n}: CylinderGalleryProps) {\n  const cylinderRef = React.useRef<HTMLDivElement>(null);\n  const cardRefs = React.useRef<(HTMLDivElement | null)[]>([]);\n  const rafRef = React.useRef<number | null>(null);\n  const autoRotRef = React.useRef(0);\n  const smoothRotRef = React.useRef(0);\n  const lastTimeRef = React.useRef<number | null>(null);\n\n  const rotationSpeedRef = React.useRef(rotationSpeed);\n  const tiltAngleRef = React.useRef(tiltAngle);\n  React.useEffect(() => {\n    rotationSpeedRef.current = rotationSpeed;\n    tiltAngleRef.current = tiltAngle;\n  }, [rotationSpeed, tiltAngle]);\n\n  const { effectiveColumns, angleStep, totalCells } = React.useMemo(() => {\n    const effectiveColumns = images.length > 0 ? Math.max(columns, Math.ceil(images.length / rows)) : columns;\n    return { effectiveColumns, angleStep: 360 / effectiveColumns, totalCells: rows * effectiveColumns };\n  }, [images.length, columns, rows]);\n\n  const { totalRowHeight, autoHeight } = React.useMemo(() => {\n    const totalRowHeight = rows * cardHeight + (rows - 1) * gap;\n    return { totalRowHeight, autoHeight: totalRowHeight + Math.max(160, cylinderRadius * 0.25) };\n  }, [rows, cardHeight, gap, cylinderRadius]);\n\n  const cardCells = React.useMemo(() => {\n    return Array.from({ length: totalCells }, (_, cellIndex) => {\n      const rowIndex = Math.floor(cellIndex / effectiveColumns);\n      const colIndex = cellIndex % effectiveColumns;\n      const angle = colIndex * angleStep;\n      const rad = (angle * Math.PI) / 180;\n      return { cellIndex, angle, x: Math.sin(rad) * cylinderRadius, z: Math.cos(rad) * cylinderRadius, rowY: rowIndex * (cardHeight + gap) };\n    });\n  }, [totalCells, effectiveColumns, angleStep, cylinderRadius, cardHeight, gap]);\n\n  const filled: (CylinderGalleryImage | null)[] = React.useMemo(\n    () => Array.from({ length: totalCells }, (_, i) => (images.length > 0 ? images[i % images.length] : null)),\n    [images, totalCells],\n  );\n\n  // A fresh ref-callback per cell on every render would make React null out\n  // and immediately reattach every card ref on every re-render (identity\n  // change, not an actual DOM change) — cheap in isolation, but needless\n  // churn the RAF loop's own read of cardRefs.current has no reason to pay.\n  // One stable closure per cellIndex, regenerated only when totalCells\n  // itself changes — which is also when the RAF effect below restarts, so\n  // the two stay in lockstep.\n  const refCallbacks = React.useMemo(\n    () =>\n      Array.from({ length: totalCells }, (_, cellIndex) => (el: HTMLDivElement | null) => {\n        cardRefs.current[cellIndex] = el;\n      }),\n    [totalCells],\n  );\n\n  React.useEffect(() => {\n    const prev = cardRefs.current;\n    cardRefs.current = Array.from({ length: totalCells }, (_, i) => prev[i] ?? null);\n    autoRotRef.current = smoothRotRef.current;\n\n    const angleStepLocal = 360 / effectiveColumns;\n\n    const tick = (timestamp: number) => {\n      const delta = lastTimeRef.current ? Math.min((timestamp - lastTimeRef.current) / 1000, 0.1) : 0.016;\n      lastTimeRef.current = timestamp;\n\n      autoRotRef.current += delta * rotationSpeedRef.current * 18;\n      smoothRotRef.current += (autoRotRef.current - smoothRotRef.current) * 0.06;\n\n      const rot = smoothRotRef.current;\n\n      if (cylinderRef.current) {\n        cylinderRef.current.style.transform = `rotateX(${tiltAngleRef.current}deg) rotateY(${rot}deg)`;\n      }\n\n      cardRefs.current.forEach((card, idx) => {\n        if (!card) return;\n        const colIndex = idx % effectiveColumns;\n        const angle = colIndex * angleStepLocal;\n        const effectiveAngle = (((angle + rot) % 360) + 360) % 360;\n        const distFromFront = effectiveAngle > 180 ? 360 - effectiveAngle : effectiveAngle;\n        const fadeStart = 30;\n        const visibilityThreshold = 50;\n        const opacity = distFromFront <= fadeStart ? 1 : distFromFront >= visibilityThreshold ? 0 : 1 - (distFromFront - fadeStart) / (visibilityThreshold - fadeStart);\n        card.style.opacity = String(opacity);\n      });\n\n      rafRef.current = requestAnimationFrame(tick);\n    };\n\n    rafRef.current = requestAnimationFrame(tick);\n\n    return () => {\n      if (rafRef.current) cancelAnimationFrame(rafRef.current);\n      lastTimeRef.current = null;\n    };\n  }, [effectiveColumns, totalCells]);\n\n  return (\n    <div\n      className={cn(\"flex w-full items-center justify-center overflow-visible\", className)}\n      style={{ height: autoHeight, perspective: `${cylinderRadius * 2.8}px`, perspectiveOrigin: \"50% 50%\" }}\n      {...props}\n    >\n      <div\n        ref={cylinderRef}\n        className=\"relative will-change-transform\"\n        style={{ width: cardWidth, height: totalRowHeight, transformStyle: \"preserve-3d\", transform: `rotateX(${tiltAngle}deg) rotateY(0deg)` }}\n      >\n        {cardCells.map(({ cellIndex, angle, x, z, rowY }) => {\n          const img = filled[cellIndex];\n          const src = img?.src ?? \"\";\n\n          return (\n            <div\n              key={cellIndex}\n              ref={refCallbacks[cellIndex]}\n              className=\"absolute top-0 left-1/2 overflow-hidden will-change-[opacity]\"\n              style={{\n                width: cardWidth,\n                height: cardHeight,\n                marginTop: rowY,\n                marginLeft: -cardWidth / 2,\n                transform: `translate3d(${x}px,0px,${z}px) rotateY(${angle}deg)`,\n                transformStyle: \"preserve-3d\",\n                borderRadius,\n                boxShadow: \"0 4px 12px rgba(0,0,0,0.2)\",\n                backfaceVisibility: \"hidden\",\n              }}\n            >\n              {src ? (\n                <img src={src} alt={img?.alt || \"\"} draggable={false} className=\"pointer-events-none block h-full w-full object-cover select-none\" />\n              ) : (\n                <div className=\"flex h-full w-full items-center justify-center\" style={{ background: `hsl(${(cellIndex * 43) % 360},18%,${10 + (cellIndex % 5) * 3}%)` }}>\n                  <span className=\"font-mono text-[13px] text-white/12\">{cellIndex + 1}</span>\n                </div>\n              )}\n              <div className=\"pointer-events-none absolute inset-0\" style={{ background: \"linear-gradient(135deg,rgba(255,255,255,0.08) 0%,transparent 40%,rgba(0,0,0,0.25) 100%)\", borderRadius }} />\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}