{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-gallery",
  "title": "Image Gallery",
  "description": "A selected image with a thumbnail grid: arrow-key navigation, aria-current and the zoom view on the selected picture.",
  "dependencies": [
    "cn"
  ],
  "registryDependencies": [
    "litingyes/blocove/image",
    "litingyes/blocove/image-zoom",
    "button"
  ],
  "files": [
    {
      "path": "src/registry/blocove/ui/image/gallery.tsx",
      "content": "import type { KeyboardEvent, ReactNode } from 'react'\nimport type { ImageTransform } from '@/components/blocove/image/image'\nimport { cn } from 'cn'\nimport { useRef, useState } from 'react'\nimport { Image } from '@/components/blocove/image/image'\nimport { ImageZoom } from '@/components/blocove/image/zoom'\nimport { Button } from '@/components/ui/button'\n\nexport interface ImageGalleryItem {\n  /** The full-size source. */\n  src: string\n  /** Alternative text, also the thumbnail's accessible name. */\n  alt: string\n  /** The smaller source shown in the thumbnail grid; defaults to `src`. */\n  thumbnailSrc?: string\n  /** Caption rendered under the selected preview. */\n  caption?: ReactNode\n}\n\nexport interface ImageGalleryProps {\n  /** The images, in reading order. */\n  images: readonly ImageGalleryItem[]\n  /** The image selected on first render. */\n  defaultIndex?: number\n  /** Thumbnail columns at the narrowest breakpoint. */\n  columns?: 2 | 3 | 4\n  /** Aspect ratio of the selected preview. */\n  aspectRatio?: number | string\n  /** Widths the selected preview requests. */\n  widths?: readonly number[]\n  /** Turns a source and a width into the URL that serves it. */\n  transform?: ImageTransform\n  /** Layout sizes for the selected preview. */\n  sizes?: string\n  /** Accessible name of the thumbnail group. */\n  label?: string\n  /** Extra classes for the gallery. */\n  className?: string\n}\n\nconst columnClasses: Record<2 | 3 | 4, string> = {\n  2: 'grid-cols-2',\n  3: 'grid-cols-3',\n  4: 'grid-cols-4',\n}\n\nfunction clampIndex(index: number, length: number): number {\n  if (!Number.isFinite(index) || length <= 0) {\n    return 0\n  }\n\n  return Math.min(Math.max(Math.trunc(index), 0), length - 1)\n}\n\n/**\n * A selected image with a thumbnail grid. The preview keeps the zoom behaviour,\n * and the grid is a roving-focus group: arrow keys, Home and End move the\n * selection and the focus together, and the current thumbnail carries\n * `aria-current`.\n */\nexport function ImageGallery({\n  images,\n  defaultIndex = 0,\n  columns = 3,\n  aspectRatio,\n  widths,\n  transform,\n  sizes,\n  label = 'Image gallery',\n  className,\n}: ImageGalleryProps) {\n  const [selected, setSelected] = useState(() => clampIndex(defaultIndex, images.length))\n  const buttonRef = useRef<Array<HTMLElement | null>>([])\n\n  if (images.length === 0) {\n    return null\n  }\n\n  const active = clampIndex(selected, images.length)\n  const current = images[active]\n\n  function moveTo(index: number) {\n    setSelected(index)\n    buttonRef.current[index]?.focus()\n  }\n\n  function handleKeyDown(event: KeyboardEvent<HTMLDivElement>) {\n    // Move from the thumbnail the user is on, not from the selection: they can\n    // tab to another thumbnail before pressing an arrow.\n    const focusedButton = event.target instanceof HTMLElement\n      ? event.target.closest('button')\n      : null\n    const focusedIndex = focusedButton\n      ? buttonRef.current.indexOf(focusedButton)\n      : -1\n    const base = focusedIndex >= 0\n      ? focusedIndex\n      : active\n    let next: number\n\n    switch (event.key) {\n      case 'ArrowRight':\n        next = (base + 1) % images.length\n        break\n      case 'ArrowLeft':\n        next = (base - 1 + images.length) % images.length\n        break\n      case 'ArrowDown':\n        next = Math.min(base + columns, images.length - 1)\n        break\n      case 'ArrowUp':\n        next = Math.max(base - columns, 0)\n        break\n      case 'Home':\n        next = 0\n        break\n      case 'End':\n        next = images.length - 1\n        break\n      default:\n        return\n    }\n\n    event.preventDefault()\n    moveTo(next)\n  }\n\n  return (\n    <div\n      data-slot=\"image-gallery\"\n      className={cn('flex w-full flex-col gap-3', className)}\n    >\n      <ImageZoom\n        key={current.src}\n        alt={current.alt}\n        aspectRatio={aspectRatio}\n        caption={current.caption}\n        priority\n        sizes={sizes}\n        src={current.src}\n        transform={transform}\n        widths={widths}\n      />\n\n      {current.caption\n        ? (\n            <p className=\"text-sm text-muted-foreground\">\n              {current.caption}\n            </p>\n          )\n        : null}\n\n      <div\n        role=\"group\"\n        aria-label={label}\n        onKeyDown={handleKeyDown}\n        className={cn('grid gap-2', columnClasses[columns])}\n      >\n        {images.map((image, index) => (\n          <Button\n            key={`${image.src}-${image.alt}`}\n            ref={(node) => {\n              buttonRef.current[index] = node\n            }}\n            type=\"button\"\n            variant=\"ghost\"\n            aria-current={index === active\n              ? 'true'\n              : undefined}\n            aria-label={image.alt}\n            tabIndex={index === active\n              ? 0\n              : -1}\n            className={cn(\n              'h-auto w-full overflow-hidden rounded-lg border p-0',\n              index === active\n                ? 'ring-2 ring-ring'\n                : 'opacity-70 hover:opacity-100',\n            )}\n            onClick={() => setSelected(index)}\n          >\n            <Image\n              alt=\"\"\n              src={image.thumbnailSrc ?? image.src}\n              className=\"h-20 w-full\"\n              containerClassName=\"w-full\"\n            />\n          </Button>\n        ))}\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/image/gallery.tsx"
    }
  ],
  "categories": [
    "image"
  ],
  "type": "registry:ui"
}