{
  "name": "media-gallery",
  "type": "registry:ui",
  "framework": "react",
  "title": "Media Gallery",
  "description": "One to ten images in a mosaic, laid out by how many there are.",
  "files": [
    {
      "path": "ui/media-gallery.tsx",
      "content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/*\n * One to ten images in a mosaic. The layout is decided by how many there are,\n * not by what each one is: two side by side, three as one large and two\n * stacked, more than that in a grid. The count drives it because that is the\n * only thing the gallery knows before the images load.\n */\nfunction layoutFor(count: number): string {\n  if (count <= 1) return \"grid-cols-1\"\n  if (count === 2) return \"grid-cols-2\"\n  /* Three: the first takes the full height beside a stacked pair. */\n  if (count === 3) return \"grid-cols-2 grid-rows-2 [&>*:first-child]:row-span-2\"\n  if (count === 4) return \"grid-cols-2\"\n  return \"grid-cols-3\"\n}\n\nexport interface MediaGalleryProps extends React.HTMLAttributes<HTMLDivElement> {\n  /**\n   * How many items the grid holds. Counted from the children when it is not\n   * given, which is what a static list wants.\n   */\n  count?: number\n}\n\nconst MediaGallery = React.forwardRef<HTMLDivElement, MediaGalleryProps>(\n  ({ className, count, children, ...props }, ref) => (\n    <div\n      ref={ref}\n      className={cn(\n        \"grid max-w-[550px] auto-rows-[1fr] gap-1 overflow-hidden rounded-[var(--newt-radius-lg)]\",\n        layoutFor(count ?? React.Children.count(children)),\n        className\n      )}\n      {...props}\n    >\n      {children}\n    </div>\n  )\n)\nMediaGallery.displayName = \"MediaGallery\"\n\n/*\n * The props of the element or component `as` names, so a custom image\n * component's own props type-check.\n */\nexport type MediaGalleryItemProps<E extends React.ElementType = \"img\"> = {\n  /**\n   * The image element or component to render. Defaults to `img`; pass your\n   * framework's image component for optimised sources. It receives `src`,\n   * `alt`, `className` and the spoiler attributes and handlers.\n   */\n  as?: E\n  /** Blurs the image until it is clicked. */\n  spoiler?: boolean\n  /** Accessible name of a hidden spoiler, read in place of the image. */\n  spoilerLabel?: string\n} & Omit<React.ComponentPropsWithRef<E>, \"as\" | \"spoiler\">\n\ntype MediaGalleryItemComponent = {\n  <E extends React.ElementType = \"img\">(\n    props: MediaGalleryItemProps<E>\n  ): React.ReactNode\n  displayName?: string\n}\n\n/*\n * SAFETY: `forwardRef` drops the generic signature. The render hands every\n * prop it does not set itself to `as` unchanged, so the props of `as` are\n * the props the part receives.\n */\n// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- see SAFETY\nconst MediaGalleryItem = React.forwardRef<\n  HTMLImageElement,\n  MediaGalleryItemProps\n>(\n  (\n    {\n      as: Tag = \"img\",\n      className,\n      spoiler = false,\n      spoilerLabel = \"Spoiler, click to reveal\",\n      alt = \"\",\n      ...props\n    },\n    ref\n  ) => {\n    const [revealed, setRevealed] = React.useState(false)\n    const hidden = spoiler && !revealed\n\n    return (\n      <Tag\n        ref={ref}\n        alt={alt}\n        data-spoiler={hidden || undefined}\n        role={hidden ? \"button\" : undefined}\n        tabIndex={hidden ? 0 : undefined}\n        aria-label={hidden ? spoilerLabel : undefined}\n        onClick={() => setRevealed(true)}\n        onKeyDown={(event: React.KeyboardEvent<HTMLImageElement>) => {\n          if (!hidden) return\n          if (event.key === \"Enter\" || event.key === \" \") {\n            event.preventDefault()\n            setRevealed(true)\n          }\n        }}\n        /*\n         * `object-cover` on a stretched cell: the mosaic keeps its shape whatever\n         * aspect ratio arrives, which is the point of a mosaic.\n         */\n        className={cn(\n          \"h-full w-full object-cover\",\n          hidden && \"cursor-pointer blur-lg\",\n          className\n        )}\n        {...props}\n      />\n    )\n  }\n) as MediaGalleryItemComponent\nMediaGalleryItem.displayName = \"MediaGalleryItem\"\n\nexport { MediaGallery, MediaGalleryItem }\n",
      "type": "registry:ui"
    }
  ]
}