{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gallery-flow",
  "title": "Gallery Flow",
  "description": "A gallery of scattered, gently drifting image cards with random rotation, cursor repel/attract mode, pause-on-hover, and a click-to-open lightbox.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/gallery-flow/gallery-flow.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 type GalleryFlowCardSize = \"small\" | \"medium\" | \"large\";\nexport type GalleryFlowDirection = \"up\" | \"down\" | \"left\" | \"right\";\nexport type GalleryFlowCursorMode = \"none\" | \"repel\" | \"attract\";\n\nexport interface GalleryFlowProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  images?: string[];\n  backgroundColor?: string;\n  speed?: number;\n  cardShadow?: boolean;\n  borderRadius?: number;\n  rotationAmount?: number;\n  cardSize?: GalleryFlowCardSize;\n  overlapAmount?: number;\n  scrollDirection?: GalleryFlowDirection;\n  pauseOnHover?: boolean;\n  pauseAllOnHover?: boolean;\n  cursorMode?: GalleryFlowCursorMode;\n  cursorStrength?: number;\n}\n\ninterface FlowItem {\n  x: number;\n  y: number;\n  w: number;\n  h: number;\n  rot: number;\n  dirX: number;\n  dirY: number;\n  speedMult: number;\n  zIndex: number;\n}\n\nconst SIZE_MAP: Record<GalleryFlowCardSize, number> = { small: 0.75, medium: 1, large: 1.35 };\n\nconst DEFAULT_IMAGES = [\n  \"/demo/23.webp\",\n  \"/demo/24.webp\",\n  \"/demo/25.webp\",\n  \"/demo/26.webp\",\n  \"/demo/27.webp\",\n  \"/demo/28.webp\",\n  \"/demo/29.webp\",\n  \"/demo/30.webp\",\n  \"/demo/31.webp\",\n  \"/demo/32.webp\",\n  \"/demo/33.webp\",\n  \"/demo/34.webp\",\n  \"/demo/52.webp\",\n];\n\nfunction useContainerSize(ref: React.RefObject<HTMLDivElement | null>) {\n  const [size, setSize] = React.useState({ w: 900, h: 600 });\n  React.useEffect(() => {\n    if (!ref.current) return;\n    const ro = new ResizeObserver(([e]) => setSize({ w: e.contentRect.width, h: e.contentRect.height }));\n    ro.observe(ref.current);\n    return () => ro.disconnect();\n  }, [ref]);\n  return size;\n}\n\nfunction seededRandom(seed: number) {\n  const x = Math.sin(seed + 1) * 10000;\n  return x - Math.floor(x);\n}\n\nexport function GalleryFlow({\n  images = DEFAULT_IMAGES,\n  backgroundColor = \"#ffffff\",\n  speed = 0.4,\n  cardShadow = true,\n  borderRadius = 12,\n  rotationAmount = 6,\n  cardSize = \"medium\",\n  overlapAmount = 0.4,\n  scrollDirection = \"up\",\n  pauseOnHover = true,\n  pauseAllOnHover = false,\n  cursorMode = \"none\",\n  cursorStrength = 80,\n  className,\n  ...props\n}: GalleryFlowProps) {\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const { w: cw, h: ch } = useContainerSize(containerRef);\n  const isMobile = cw <= 480;\n  const isTablet = cw <= 768 && cw > 480;\n  const responsiveSizeMult = isMobile ? 1.4 : isTablet ? 1.15 : 1;\n\n  const srcs = images.filter((s): s is string => Boolean(s));\n  const count = srcs.length;\n\n  const [lightbox, setLightbox] = React.useState<string | null>(null);\n  const [lightboxIn, setLightboxIn] = React.useState(false);\n  const [hoveredIdx, setHoveredIdx] = React.useState<number | null>(null);\n  const [galleryPaused, setGalleryPaused] = React.useState(false);\n  const hoveredIdxRef = React.useRef<number | null>(null);\n  const galleryPausedRef = React.useRef(false);\n  const cursorRef = React.useRef<{ x: number; y: number } | null>(null);\n  const cardRefs = React.useRef<(HTMLDivElement | null)[]>([]);\n\n  React.useEffect(() => {\n    if (!lightbox) return;\n    const id = requestAnimationFrame(() => setLightboxIn(true));\n    return () => cancelAnimationFrame(id);\n  }, [lightbox]);\n\n  const closeTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const closeLightbox = () => {\n    setLightboxIn(false);\n    if (closeTimerRef.current) clearTimeout(closeTimerRef.current);\n    closeTimerRef.current = setTimeout(() => setLightbox(null), 400);\n  };\n  React.useEffect(\n    () => () => {\n      if (closeTimerRef.current) clearTimeout(closeTimerRef.current);\n    },\n    [],\n  );\n\n  const sizeMult = SIZE_MAP[cardSize] * responsiveSizeMult;\n\n  const items = React.useMemo<FlowItem[]>(() => {\n    return Array.from({ length: count }, (_, i) => {\n      const r = (s: number) => seededRandom(i * 17 + s);\n      const maxCols = isMobile ? 2 : isTablet ? 3 : 4;\n      const cols = Math.min(count <= 4 ? 2 : count <= 6 ? 3 : count <= 9 ? 3 : 4, maxCols);\n      const rows = Math.ceil(count / cols);\n      const col = i % cols;\n      const row = Math.floor(i / cols);\n\n      const sizeVariant = r(3);\n      const baseW = sizeVariant < 0.33 ? 0.13 : sizeVariant < 0.66 ? 0.17 : 0.2;\n      const w = baseW * sizeMult;\n      const h = w * (r(4) * 0.5 + 0.9);\n\n      const cellW = 1 / cols;\n      const cellH = 1 / rows;\n      const jitterX = (r(1) - 0.5) * cellW * overlapAmount * 0.8;\n      const jitterY = (r(2) - 0.5) * cellH * overlapAmount * 0.8;\n      const x = (col + 0.5) * cellW + jitterX - w / 2;\n      const y = (row + 0.5) * cellH + jitterY - h / 2;\n\n      const rot = (r(5) - 0.5) * rotationAmount * 2 * (isMobile ? 0.5 : 1);\n      const isHorizontal = scrollDirection === \"left\" || scrollDirection === \"right\";\n      const dirX = isHorizontal ? (scrollDirection === \"left\" ? -1 : 1) : (r(6) - 0.5) * 0.3;\n      const dirY = isHorizontal ? (r(7) - 0.5) * 0.3 : scrollDirection === \"down\" ? 1 : -1;\n      const speedMult = r(8) * 0.6 + 0.7;\n      const zIndex = Math.floor(r(9) * 10) + 1;\n      return { x, y, w, h, rot, dirX, dirY, speedMult, zIndex };\n    });\n  }, [count, isMobile, isTablet, sizeMult, overlapAmount, rotationAmount, scrollDirection]);\n\n  const offsetsRef = React.useRef<{ x: number; y: number }[]>(items.map((item) => ({ x: item.x * 900, y: item.y * 600 })));\n  const rafRef = React.useRef<number | undefined>(undefined);\n\n  React.useEffect(() => {\n    offsetsRef.current = items.map((item, i) => ({ x: item.x * cw, y: item.y * ch + (i / count) * ch * 0.2 }));\n    cardRefs.current.forEach((card, i) => {\n      if (!card) return;\n      const off = offsetsRef.current[i];\n      card.style.left = off.x + \"px\";\n      card.style.top = off.y + \"px\";\n    });\n  }, [cw, ch, count, items]);\n\n  React.useEffect(() => {\n    // Scale each step by elapsed time (in 60 Hz frames) so the drift runs at the same speed on\n    // 60 Hz and 120 Hz screens.\n    let last = 0;\n    const animate = (now: number) => {\n      const dt = last ? Math.min((now - last) / (1000 / 60), 3) : 1;\n      last = now;\n      if (!galleryPausedRef.current) {\n        offsetsRef.current = offsetsRef.current.map((off, i) => {\n          if (pauseOnHover && hoveredIdxRef.current === i) return off;\n          const item = items[i];\n          const itemW = item.w * cw;\n          const itemH = item.h * ch;\n          let nx = off.x + item.dirX * speed * item.speedMult * 0.4 * dt;\n          let ny = off.y + item.dirY * speed * item.speedMult * 0.4 * dt;\n\n          if (cursorMode !== \"none\" && cursorRef.current) {\n            const cx = cursorRef.current.x;\n            const cy = cursorRef.current.y;\n            const dx = nx + itemW / 2 - cx;\n            const dy = ny + itemH / 2 - cy;\n            const dist = Math.sqrt(dx * dx + dy * dy);\n            const maxDist = cursorStrength * 2.5;\n            if (dist < maxDist && dist > 0) {\n              const force = (1 - dist / maxDist) * cursorStrength * 0.08;\n              const dir = cursorMode === \"repel\" ? 1 : -1;\n              nx += (dx / dist) * force * dir;\n              ny += (dy / dist) * force * dir;\n            }\n          }\n          if (nx > cw + itemW) nx = -itemW;\n          if (nx < -itemW) nx = cw + itemW;\n          if (ny > ch + itemH) ny = -itemH;\n          if (ny < -itemH) ny = ch + itemH;\n\n          const card = cardRefs.current[i];\n          if (card) {\n            card.style.left = nx + \"px\";\n            card.style.top = ny + \"px\";\n          }\n          return { x: nx, y: ny };\n        });\n      }\n      rafRef.current = requestAnimationFrame(animate);\n    };\n    rafRef.current = requestAnimationFrame(animate);\n    return () => {\n      if (rafRef.current) cancelAnimationFrame(rafRef.current);\n    };\n  }, [speed, cursorMode, cursorStrength, cw, ch, items, pauseOnHover]);\n\n  if (count < 3) {\n    return (\n      <div ref={containerRef} className={cn(\"flex h-full w-full items-center justify-center\", className)} style={{ background: backgroundColor }} {...props}>\n        <span className=\"text-[13px] text-black/30\" style={{ fontFamily: \"'Helvetica Neue', sans-serif\" }}>\n          Add at least 3 images\n        </span>\n      </div>\n    );\n  }\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn(\"relative h-full w-full\", className)}\n      onMouseMove={(e) => {\n        const rect = containerRef.current?.getBoundingClientRect();\n        if (rect) cursorRef.current = { x: e.clientX - rect.left, y: e.clientY - rect.top };\n      }}\n      onMouseEnter={() => {\n        if (pauseAllOnHover) {\n          galleryPausedRef.current = true;\n          setGalleryPaused(true);\n        }\n      }}\n      onMouseLeave={() => {\n        galleryPausedRef.current = false;\n        setGalleryPaused(false);\n        setHoveredIdx(null);\n        hoveredIdxRef.current = null;\n        cursorRef.current = null;\n      }}\n      {...props}\n    >\n      <div className=\"absolute inset-0 overflow-hidden\" style={{ background: backgroundColor }}>\n        {srcs.map((src, i) => {\n          const item = items[i];\n          const initOff = { x: item.x * cw, y: item.y * ch };\n          const isHovered = hoveredIdx === i;\n          const iw = item.w * cw;\n          const ih = item.h * ch;\n\n          return (\n            <div\n              key={i}\n              ref={(el) => {\n                cardRefs.current[i] = el;\n              }}\n              onMouseEnter={() => {\n                setHoveredIdx(i);\n                hoveredIdxRef.current = i;\n              }}\n              onMouseLeave={() => {\n                setHoveredIdx(null);\n                hoveredIdxRef.current = null;\n              }}\n              onClick={() => {\n                setLightboxIn(false);\n                setLightbox(src);\n              }}\n              className=\"absolute cursor-pointer overflow-hidden transition-[transform,box-shadow] duration-[400ms] will-change-transform\"\n              style={{\n                left: initOff.x,\n                top: initOff.y,\n                width: iw,\n                height: ih,\n                borderRadius,\n                zIndex: isHovered ? 99 : item.zIndex,\n                transform: `rotate(${isHovered ? item.rot * 0.3 : item.rot}deg) scale(${isHovered ? 1.04 : 1})`,\n                transitionTimingFunction: \"cubic-bezier(0.34,1.2,0.64,1), ease\",\n                boxShadow: cardShadow ? (isHovered ? \"0 24px 60px rgba(0,0,0,0.28), 0 8px 20px rgba(0,0,0,0.16)\" : \"0 8px 30px rgba(0,0,0,0.14), 0 2px 8px rgba(0,0,0,0.08)\") : \"none\",\n              }}\n            >\n              <img\n                src={src}\n                alt=\"\"\n                className=\"pointer-events-none block h-full w-full object-cover transition-transform duration-[600ms]\"\n                style={{ transform: isHovered ? \"scale(1.08)\" : \"scale(1)\", transitionTimingFunction: \"cubic-bezier(0.34,1.1,0.64,1)\" }}\n              />\n              <div className=\"pointer-events-none absolute inset-0 transition-colors duration-300 ease-[ease]\" style={{ background: isHovered ? \"rgba(0,0,0,0.08)\" : \"transparent\" }} />\n            </div>\n          );\n        })}\n\n        {galleryPaused && pauseAllOnHover && (\n          <div className=\"pointer-events-none absolute bottom-4 left-1/2 z-50 -translate-x-1/2 rounded-full bg-black/45 px-3.5 py-[5px] text-[10px] tracking-[0.12em] text-white/70 uppercase backdrop-blur-sm\" style={{ fontFamily: \"'Helvetica Neue', sans-serif\" }}>\n            Paused\n          </div>\n        )}\n      </div>\n\n      \n      {lightbox && (\n        <div onClick={closeLightbox} className=\"absolute inset-0 z-[200] flex cursor-zoom-out items-center justify-center bg-black/85 backdrop-blur-lg\">\n          <button onClick={closeLightbox} aria-label=\"Close\" className=\"absolute top-5 right-5 z-[201] cursor-pointer border-0 bg-transparent p-2 text-[22px] leading-none text-white/50 transition-colors duration-200 ease-[ease] hover:text-white\">\n            ✕\n          </button>\n          <img\n            src={lightbox}\n            alt=\"\"\n            onClick={(e) => e.stopPropagation()}\n            className=\"block cursor-default object-contain transition-[opacity,transform] duration-[400ms]\"\n            style={{\n              maxWidth: isMobile ? \"92%\" : \"75%\",\n              maxHeight: isMobile ? \"78vh\" : \"80vh\",\n              borderRadius: borderRadius * 1.5,\n              boxShadow: \"0 40px 120px rgba(0,0,0,0.7)\",\n              opacity: lightboxIn ? 1 : 0,\n              transform: lightboxIn ? \"scale(1)\" : \"scale(0.92)\",\n              transitionTimingFunction: \"cubic-bezier(0.34,1.2,0.64,1)\",\n            }}\n          />\n        </div>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}