{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lightbox",
  "title": "Lightbox",
  "description": "An image lightbox for any grid of photos: the thumbnail morphs into the full view, then arrows, keys and swipes step through, double-click zooms and pans, a swipe down closes, and a thumbnail strip tracks where you are.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/lightbox/lightbox.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence, useReducedMotion, type PanInfo } from \"motion/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 LightboxImage {\n  src: string;\n  alt: string;\n  caption?: string;\n  /** Optional smaller file for the grid and the thumbnail strip. */\n  thumb?: string;\n}\n\nexport interface LightboxProps {\n  images?: LightboxImage[];\n  /** Render the clickable thumbnail grid. Turn off to open it yourself with index / onIndexChange. */\n  showGrid?: boolean;\n  columns?: number;\n  /** The open image (null = closed). Leave out to let the grid manage it. */\n  index?: number | null;\n  onIndexChange?: (index: number | null) => void;\n  showThumbnails?: boolean;\n  /** Keep the viewer inside the nearest positioned ancestor instead of covering the page. */\n  contained?: boolean;\n  loop?: boolean;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.6)\", backdrop: \"rgba(6,6,6,0.94)\", button: \"rgba(255,255,255,0.08)\", buttonHover: \"rgba(255,255,255,0.16)\", border: \"rgba(255,255,255,0.12)\", focus: \"rgba(245,244,241,0.85)\", tile: \"rgba(255,255,255,0.04)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.6)\", backdrop: \"rgba(250,250,249,0.96)\", button: \"rgba(10,10,10,0.06)\", buttonHover: \"rgba(10,10,10,0.12)\", border: \"rgba(10,10,10,0.12)\", focus: \"rgba(10,10,10,0.85)\", tile: \"rgba(10,10,10,0.04)\" },\n};\n\nconst DEFAULT_IMAGES: LightboxImage[] = [\n  { src: \"/demo/34.webp\", alt: \"Cyclist leaning into a turn\", caption: \"Morning ride, Col de la Madone\" },\n  { src: \"/demo/31.webp\", alt: \"Sprinter coming out of the blocks\", caption: \"Track intervals, 400 m\" },\n  { src: \"/demo/12.webp\", alt: \"Skier carving through fresh snow\", caption: \"First tracks, Val Thorens\" },\n  { src: \"/demo/28.webp\", alt: \"Rower mid-stroke on still water\", caption: \"Dawn session on the lake\" },\n  { src: \"/demo/33.webp\", alt: \"Climber reaching for a hold\", caption: \"Bouldering, Fontainebleau\" },\n  { src: \"/demo/25.webp\", alt: \"Runner on a mountain trail\", caption: \"Ridge run, 18 km\" },\n];\n\nfunction IconButton({ label, onClick, children, p, disabled }: { label: string; onClick: () => void; children: React.ReactNode; p: (typeof PALETTES)[\"dark\"]; disabled?: boolean }) {\n  return (\n    <button\n      type=\"button\"\n      aria-label={label}\n      onClick={onClick}\n      disabled={disabled}\n      className=\"flex cursor-pointer items-center justify-center rounded-full border-none outline-none focus-visible:ring-2 disabled:cursor-default disabled:opacity-30\"\n      style={{ width: 40, height: 40, background: p.button, color: p.text, backdropFilter: \"blur(8px)\", transition: \"background 0.15s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n      onMouseEnter={(e) => (e.currentTarget.style.background = p.buttonHover)}\n      onMouseLeave={(e) => (e.currentTarget.style.background = p.button)}\n    >\n      {children}\n    </button>\n  );\n}\n\nconst chevron = (dir: \"l\" | \"r\") => (\n  <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d={dir === \"l\" ? \"M10 3.5 5.5 8l4.5 4.5\" : \"M6 3.5 10.5 8 6 12.5\"} />\n  </svg>\n);\n\nexport function Lightbox({\n  images = DEFAULT_IMAGES,\n  showGrid = true,\n  columns = 3,\n  index,\n  onIndexChange,\n  showThumbnails = true,\n  contained = false,\n  loop = true,\n  theme = \"dark\",\n  className,\n}: LightboxProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const [internal, setInternal] = React.useState<number | null>(null);\n  const current = index !== undefined ? index : internal;\n  const [dir, setDir] = React.useState(0);\n  const [zoom, setZoom] = React.useState(false);\n  const [mounted, setMounted] = React.useState(false);\n  const returnFocus = React.useRef<HTMLElement | null>(null);\n  const dialogRef = React.useRef<HTMLDivElement>(null);\n  const stripRef = React.useRef<HTMLDivElement>(null);\n  const count = images.length;\n\n  // eslint-disable-next-line react-hooks/set-state-in-effect\n  React.useEffect(() => setMounted(true), []);\n\n  const set = React.useCallback(\n    (i: number | null) => {\n      if (index === undefined) setInternal(i);\n      onIndexChange?.(i);\n    },\n    [index, onIndexChange],\n  );\n\n  const go = React.useCallback(\n    (step: number) => {\n      if (current === null) return;\n      let next = current + step;\n      if (loop) next = (next + count) % count;\n      if (next < 0 || next >= count) return;\n      setDir(step);\n      setZoom(false);\n      set(next);\n    },\n    [current, count, loop, set],\n  );\n\n  const close = React.useCallback(() => {\n    setZoom(false);\n    set(null);\n  }, [set]);\n\n  React.useEffect(() => {\n    if (current === null) return;\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") close();\n      else if (e.key === \"ArrowRight\") go(1);\n      else if (e.key === \"ArrowLeft\") go(-1);\n      else if (e.key.toLowerCase() === \"z\") setZoom((z) => !z);\n    };\n    document.addEventListener(\"keydown\", onKey);\n    return () => document.removeEventListener(\"keydown\", onKey);\n  }, [current, go, close]);\n\n  // Focus goes into the viewer on open and back to the thumbnail that opened it on close.\n  const isOpen = current !== null;\n  React.useEffect(() => {\n    if (!isOpen) return;\n    returnFocus.current = document.activeElement as HTMLElement | null;\n    dialogRef.current?.focus({ preventScroll: true });\n    return () => returnFocus.current?.focus?.({ preventScroll: true });\n  }, [isOpen]);\n\n  // Keep the active thumbnail centred in the strip.\n  React.useEffect(() => {\n    if (current === null) return;\n    const strip = stripRef.current;\n    const thumb = strip?.querySelector<HTMLElement>(`[data-lb-thumb=\"${current}\"]`);\n    if (strip && thumb) strip.scrollTo({ left: thumb.offsetLeft - (strip.clientWidth - thumb.offsetWidth) / 2, behavior: reduce ? \"auto\" : \"smooth\" });\n  }, [current, reduce]);\n\n  const onDragEnd = (_: unknown, info: PanInfo) => {\n    if (zoom) return;\n    if (Math.abs(info.offset.y) > 120 || info.velocity.y > 700) close();\n    else if (info.offset.x < -80 || info.velocity.x < -500) go(1);\n    else if (info.offset.x > 80 || info.velocity.x > 500) go(-1);\n  };\n\n  const img = current !== null ? images[current] : null;\n  const pos = contained ? \"absolute\" : \"fixed\";\n\n  const viewer = (\n    <AnimatePresence>\n      {img && current !== null && (\n        <motion.div\n          key=\"viewer\"\n          ref={dialogRef}\n          role=\"dialog\"\n          aria-modal=\"true\"\n          aria-label={`Image ${current + 1} of ${count}`}\n          tabIndex={-1}\n          className={cn(pos, \"inset-0 z-[80] flex flex-col outline-none\")}\n          style={{ background: p.backdrop, fontFamily: \"Inter, sans-serif\" }}\n          initial={{ opacity: 0 }}\n          animate={{ opacity: 1 }}\n          exit={{ opacity: 0 }}\n          transition={{ duration: reduce ? 0 : 0.22 }}\n        >\n          <div className=\"flex shrink-0 items-center justify-between\" style={{ padding: \"14px 16px\" }}>\n            <span className=\"font-medium tabular-nums\" style={{ color: p.muted, fontSize: 13 }} aria-live=\"polite\">\n              {current + 1} / {count}\n            </span>\n            <div className=\"flex items-center gap-2\">\n              <IconButton label={zoom ? \"Zoom out\" : \"Zoom in\"} onClick={() => setZoom((z) => !z)} p={p}>\n                <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                  <circle cx=\"7\" cy=\"7\" r=\"4.75\" />\n                  <path d={zoom ? \"M5 7h4M10.5 10.5 14 14\" : \"M5 7h4M7 5v4M10.5 10.5 14 14\"} />\n                </svg>\n              </IconButton>\n              <IconButton label=\"Close\" onClick={close} p={p}>\n                <svg width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                  <path d=\"M3 3l8 8M11 3l-8 8\" />\n                </svg>\n              </IconButton>\n            </div>\n          </div>\n\n          <div className=\"relative flex min-h-0 flex-1 items-center justify-center overflow-hidden\" style={{ padding: \"0 64px\" }}>\n            <AnimatePresence initial={false} custom={dir} mode=\"popLayout\">\n              <motion.figure\n                key={current}\n                custom={dir}\n                className=\"m-0 flex h-full w-full flex-col items-center justify-center\"\n                initial={reduce ? { opacity: 0 } : { opacity: 0, x: dir * 60 }}\n                animate={{ opacity: 1, x: 0 }}\n                exit={reduce ? { opacity: 0 } : { opacity: 0, x: dir * -60 }}\n                transition={{ duration: 0.28, ease: [0.22, 1, 0.36, 1] }}\n                style={{ gap: 12 }}\n              >\n                <motion.div\n                  className=\"relative flex min-h-0 max-w-full flex-1 items-center justify-center overflow-hidden\"\n                  style={{ borderRadius: 14, cursor: zoom ? \"grab\" : \"zoom-in\", touchAction: \"none\" }}\n                  drag={!reduce}\n                  dragConstraints={zoom ? { left: -300, right: 300, top: -220, bottom: 220 } : { left: 0, right: 0, top: 0, bottom: 0 }}\n                  dragElastic={zoom ? 0.1 : 0.6}\n                  dragSnapToOrigin={!zoom}\n                  onDragEnd={onDragEnd}\n                  onDoubleClick={() => setZoom((z) => !z)}\n                >\n                  <motion.img\n                    layoutId={reduce ? undefined : `${uid}-${current}`}\n                    src={img.src}\n                    alt={img.alt}\n                    draggable={false}\n                    className=\"block max-h-full max-w-full select-none object-contain\"\n                    style={{ borderRadius: 14 }}\n                    animate={{ scale: zoom ? 2 : 1 }}\n                    transition={{ type: \"spring\", stiffness: 300, damping: 32 }}\n                  />\n                </motion.div>\n                {img.caption && (\n                  <figcaption className=\"shrink-0 text-center\" style={{ color: p.muted, fontSize: 13.5 }}>\n                    {img.caption}\n                  </figcaption>\n                )}\n              </motion.figure>\n            </AnimatePresence>\n            <div className=\"absolute top-1/2 left-3 -translate-y-1/2\">\n              <IconButton label=\"Previous image\" onClick={() => go(-1)} p={p} disabled={!loop && current === 0}>\n                {chevron(\"l\")}\n              </IconButton>\n            </div>\n            <div className=\"absolute top-1/2 right-3 -translate-y-1/2\">\n              <IconButton label=\"Next image\" onClick={() => go(1)} p={p} disabled={!loop && current === count - 1}>\n                {chevron(\"r\")}\n              </IconButton>\n            </div>\n          </div>\n\n          {showThumbnails && (\n            <div ref={stripRef} className=\"flex shrink-0 justify-center overflow-x-auto\" style={{ gap: 8, padding: \"14px 16px 18px\", scrollbarWidth: \"none\" }}>\n              {images.map((im, i) => (\n                <button\n                  key={im.src + i}\n                  type=\"button\"\n                  data-lb-thumb={i}\n                  aria-label={`Show image ${i + 1}: ${im.alt}`}\n                  aria-current={i === current ? \"true\" : undefined}\n                  onClick={() => {\n                    setDir(i > current ? 1 : -1);\n                    setZoom(false);\n                    set(i);\n                  }}\n                  className=\"relative shrink-0 cursor-pointer overflow-hidden border-none p-0 outline-none focus-visible:ring-2\"\n                  style={{ width: 52, height: 40, borderRadius: 8, opacity: i === current ? 1 : 0.45, transition: \"opacity 0.2s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n                >\n                  {/* eslint-disable-next-line @next/next/no-img-element */}\n                  <img src={im.thumb ?? im.src} alt=\"\" className=\"h-full w-full object-cover\" draggable={false} />\n                  {i === current && <motion.span layoutId={reduce ? undefined : `${uid}-ring`} className=\"absolute inset-0\" style={{ borderRadius: 8, boxShadow: `inset 0 0 0 2px ${p.text}` }} />}\n                </button>\n              ))}\n            </div>\n          )}\n          {/* Preload the neighbours so stepping through feels instant. */}\n          <div aria-hidden=\"true\" className=\"hidden\">\n            {[current - 1, current + 1].map((i) => {\n              const n = images[(i + count) % count];\n              // eslint-disable-next-line @next/next/no-img-element\n              return n ? <img key={i} src={n.src} alt=\"\" /> : null;\n            })}\n          </div>\n        </motion.div>\n      )}\n    </AnimatePresence>\n  );\n\n  return (\n    <>\n      {showGrid && (\n        <div className={cn(\"grid\", className)} style={{ gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`, gap: 8 }}>\n          {images.map((im, i) => (\n            <button\n              key={im.src + i}\n              type=\"button\"\n              aria-label={`Open image ${i + 1}: ${im.alt}`}\n              onClick={() => {\n                setDir(0);\n                set(i);\n              }}\n              className=\"group relative aspect-[4/3] cursor-zoom-in overflow-hidden border-none p-0 outline-none focus-visible:ring-2\"\n              style={{ borderRadius: 12, background: p.tile, [\"--tw-ring-color\" as string]: p.focus }}\n            >\n              <motion.img\n                layoutId={reduce || current === i ? undefined : `${uid}-${i}`}\n                src={im.thumb ?? im.src}\n                alt=\"\"\n                draggable={false}\n                className=\"h-full w-full object-cover transition-transform duration-500 group-hover:scale-[1.04]\"\n                style={{ borderRadius: 12, visibility: current === i ? \"hidden\" : \"visible\" }}\n              />\n            </button>\n          ))}\n        </div>\n      )}\n      {contained ? viewer : mounted ? createPortal(viewer, document.body) : null}\n    </>\n  );\n}\n\nexport default Lightbox;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}