{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gallery",
  "type": "registry:ui",
  "title": "Gallery",
  "description": "A set of picture tiles that open a full-screen viewer: a mosaic of one large and several small, an even grid, or a row that scrolls sideways.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/lightbox"
  ],
  "files": [
    {
      "path": "registry/ui/gallery.tsx",
      "type": "registry:ui",
      "target": "components/ui/gallery.tsx",
      "content": "\"use client\";\n/**\n * @name gallery\n * @category ui\n * @description A set of picture tiles that open a full-screen viewer: a mosaic of one large and several small, an even grid, or a row that scrolls sideways.\n * @when Showing several pictures of one thing where each can be looked at closely: a place, a product, a piece of work. GalleryLightbox takes a list of pictures and does everything; Gallery, GalleryItem and GalleryImage are the parts, to put inside your own Lightbox or to use with no viewer at all. Pictures are plain `<img>` elements unless the project gives its framework's image component: `as` on GalleryImage, `renderImage` on GalleryLightbox. For pictures that are decoration, use plain images in a grid; for one picture use aspect.\n * @example\n * <GalleryLightbox\n *   title=\"Photos of the studio\"\n *   layout=\"mosaic\"\n *   items={[\n *     { src: \"/studio-1.jpg\", alt: \"The studio from the street\" },\n *     { src: \"/studio-2.jpg\", alt: \"The workbench\", caption: \"The workbench, built in 1962\" },\n *   ]}\n * />\n *\n * // The parts, in your own lightbox\n * <Lightbox count={photos.length}>\n *   <Gallery layout=\"grid\" aria-label=\"Photos\">\n *     {photos.map((photo, index) => (\n *       <LightboxTrigger key={photo.src} index={index} asChild>\n *         <GalleryItem><GalleryImage src={photo.src} alt={photo.alt} /></GalleryItem>\n *       </LightboxTrigger>\n *     ))}\n *   </Gallery>\n *   <LightboxContent>…</LightboxContent>\n * </Lightbox>\n *\n * // With the framework's image component (here next/image) in place of <img>\n * <GalleryImage as={Image} fill sizes=\"(min-width: 48rem) 25vw, 50vw\" src={photo.src} alt={photo.alt} />\n * <GalleryLightbox\n *   title=\"Photos of the studio\"\n *   items={photos}\n *   renderImage={({ use, picture, props }) =>\n *     use === \"tile\" ? <GalleryImage as={Image} fill sizes=\"25vw\" {...props} /> : <Image {...props} width={use === \"slide\" ? picture.width : 96} height={use === \"slide\" ? picture.height : 72} />\n *   }\n * />\n */\nimport { ImageOff } from \"lucide-react\";\nimport { Children, createContext, use, useState, type ComponentProps, type ElementType, type ReactNode, type SyntheticEvent } from \"react\";\nimport {\n  Lightbox,\n  LightboxCaption,\n  LightboxClose,\n  LightboxContent,\n  LightboxCounter,\n  LightboxFooter,\n  LightboxHeader,\n  LightboxNext,\n  LightboxPrevious,\n  LightboxSlide,\n  LightboxStage,\n  LightboxThumbnail,\n  LightboxThumbnails,\n  LightboxTitle,\n  LightboxTrigger,\n  LightboxViewport,\n  LightboxZoom,\n  useLightbox,\n  type LightboxProps,\n} from \"@/components/ui/lightbox\";\nimport { cn } from \"@/lib/cn\";\n\nexport type GalleryLayout = \"mosaic\" | \"grid\" | \"row\";\n\n/** How many tiles a mosaic shows: one large and four small. */\nconst MOSAIC_TILES = 5;\n\nconst GalleryContext = createContext<{ layout: GalleryLayout }>({ layout: \"grid\" });\n\nexport type GalleryProps = ComponentProps<\"div\"> & {\n  /**\n   * \"mosaic\": the first picture large and four small beside it (below it on phones); pictures beyond the\n   * fifth are not shown as tiles, and their number is written on the last tile. \"grid\": even tiles, two to\n   * four across by the gallery's own width. \"row\": one line that scrolls sideways and snaps, for phones.\n   */\n  layout?: GalleryLayout;\n  /** The words on the last tile of a mosaic when there are more pictures than tiles. Default \"+4 more\". */\n  moreLabel?: (count: number) => string;\n};\n\n/**\n * The tiles' layout. Its children are GalleryItems (each may be wrapped in a LightboxTrigger). It is a\n * group: name it with `aria-label` or `aria-labelledby`.\n */\nexport function Gallery({ layout = \"grid\", moreLabel = (count) => `+${count} more`, className, children, ...props }: GalleryProps) {\n  const more = layout === \"mosaic\" ? Children.count(children) - MOSAIC_TILES : 0;\n  return (\n    <div data-slot=\"gallery\" data-layout={layout} role=\"group\" className={cn(\"@container w-full min-w-0\", className)} {...props}>\n      <GalleryContext value={{ layout }}>\n        <div\n          data-slot=\"gallery-tiles\"\n          className={cn(\n            layout === \"grid\" && \"grid grid-cols-2 gap-2 @md:grid-cols-3 @3xl:grid-cols-4\",\n            layout === \"mosaic\" &&\n              \"relative grid grid-cols-2 gap-2 @xl:aspect-[2/1] @xl:grid-cols-4 @xl:grid-rows-2 [&>[data-slot=gallery-item]:first-child]:col-span-2 @xl:[&>[data-slot=gallery-item]:first-child]:row-span-2 [&>[data-slot=gallery-item]:nth-child(n+6)]:hidden\",\n            layout === \"row\" && \"flex snap-x snap-mandatory gap-2 overflow-x-auto overscroll-x-contain pb-2 [scrollbar-width:thin]\",\n          )}\n        >\n          {children}\n          {more > 0 ? (\n            <span\n              data-slot=\"gallery-more\"\n              className=\"pointer-events-none absolute end-0 bottom-0 flex aspect-[4/3] w-[calc(50%-0.25rem)] items-center justify-center rounded-control bg-overlay text-sm font-medium text-background @xl:aspect-auto @xl:h-[calc(50%-0.25rem)] @xl:w-[calc(25%-0.375rem)] dark:text-foreground\"\n            >\n              <bdi>{moreLabel(more)}</bdi>\n            </span>\n          ) : null}\n        </div>\n      </GalleryContext>\n    </div>\n  );\n}\n\nexport type GalleryItemProps = ComponentProps<\"button\"> & {\n  /** Read before the picture's own description, so that the button says what it does. Default \"Open picture:\". */\n  openLabel?: string;\n};\n\n/**\n * One tile: a button that holds a GalleryImage. It keeps its shape (4:3, or the cell of a mosaic) before\n * the picture arrives, with the theme's flat muted ground showing until then, so nothing moves as pictures\n * load. Its name is `openLabel` and the picture's `alt` together (\"Open picture: The workbench\").\n */\nexport function GalleryItem({ className, children, openLabel = \"Open picture:\", type = \"button\", ...props }: GalleryItemProps) {\n  const { layout } = use(GalleryContext);\n  return (\n    <button\n      type={type}\n      className={cn(\n        \"relative block min-w-0 overflow-hidden rounded-control bg-muted transition-opacity duration-instant ease-out hover:opacity-90 motion-reduce:transition-none\",\n        layout === \"grid\" && \"aspect-[4/3]\",\n        layout === \"mosaic\" && \"aspect-[4/3] @xl:aspect-auto\",\n        layout === \"row\" && \"aspect-[4/3] w-[72%] max-w-80 shrink-0 snap-start\",\n        className,\n      )}\n      {...props}\n      // After the spread: as the child of a LightboxTrigger it is given that part's data-slot, and the layout selects tiles by this one.\n      data-slot=\"gallery-item\"\n    >\n      <span className=\"sr-only\">{openLabel} </span>\n      {children}\n    </button>\n  );\n}\n\ntype GalleryImageOwnProps = {\n  /** What the picture shows. Required: it is also the tile's name. */\n  alt: string;\n  /** The words shown in place of a picture that cannot be loaded. Default \"Picture unavailable\". */\n  unavailableLabel?: string;\n};\n\nexport type GalleryImageProps<T extends ElementType = \"img\"> = GalleryImageOwnProps & {\n  /**\n   * The component that draws the picture, in place of `<img>`: the framework's image component (`Image`\n   * from next/image, for one). It is given `src`, `alt`, `className` (which makes it fill the tile),\n   * `onError`, a `ref` to the picture element, and every other prop passed here (`fill`, `sizes`, `width`).\n   */\n  as?: T;\n} & Omit<ComponentProps<T>, keyof GalleryImageOwnProps | \"as\">;\n\n/**\n * The picture, cropped to fill its tile. A picture that fails to load is replaced by a labelled placeholder\n * that keeps the description, never the browser's broken-image icon. It is an `<img>` (lazy, decoded off\n * the main thread) unless `as` names another component, which then decides how it loads.\n */\nexport function GalleryImage<T extends ElementType = \"img\">(all: GalleryImageProps<T>) {\n  const { as, className, alt, unavailableLabel = \"Picture unavailable\", onError, ...props } = all as GalleryImageProps<\"img\"> & { as?: ElementType };\n  const Picture: ElementType = as ?? \"img\";\n  const [failed, setFailed] = useState(false);\n  if (failed) {\n    return (\n      <span data-slot=\"gallery-image-unavailable\" className=\"absolute inset-0 flex flex-col items-center justify-center gap-1 p-2 text-center text-xs text-muted-foreground\">\n        <ImageOff aria-hidden=\"true\" strokeWidth={1.5} className=\"size-5\" />\n        <span>{unavailableLabel}</span>\n        <span className=\"sr-only\">: {alt}</span>\n      </span>\n    );\n  }\n  return (\n    <Picture\n      data-slot=\"gallery-image\"\n      alt={alt}\n      // The plain element loads when it nears the screen; another component has its own way of loading.\n      {...(Picture === \"img\" ? { loading: \"lazy\", decoding: \"async\" } : {})}\n      // A picture that failed before the page became interactive never fires onError: look at it once.\n      ref={(node: HTMLImageElement | null) => {\n        if (node?.complete && node.naturalWidth === 0 && node.currentSrc !== \"\") setFailed(true);\n      }}\n      onError={(event: SyntheticEvent<HTMLImageElement>) => {\n        onError?.(event);\n        setFailed(true);\n      }}\n      className={cn(\"absolute inset-0 size-full object-cover\", className)}\n      {...props}\n    />\n  );\n}\n\nexport type GalleryPicture = {\n  src: string;\n  /** What the picture shows. */\n  alt: string;\n  /** A smaller file for the tile and the thumbnail strip. Default: `src`. */\n  thumbnail?: string;\n  /** Words shown under the picture in the viewer. */\n  caption?: ReactNode;\n};\n\n/** What `renderImage` is given for one picture in one place. */\nexport type GalleryRenderImage<P extends GalleryPicture = GalleryPicture> = {\n  /** Where the picture is drawn: a \"tile\" of the gallery, the large \"slide\" in the viewer, or a \"thumbnail\" in the viewer's strip. */\n  use: \"tile\" | \"slide\" | \"thumbnail\";\n  /** The item, with whatever else the project keeps on it (a width and height, a blurred placeholder). */\n  picture: P;\n  /** Its place in `items`, from 0. */\n  index: number;\n  /** What the picture must be given: spread them onto the element. `alt` is empty for a thumbnail, whose button carries the name. */\n  props: { src: string; alt: string; className?: string; draggable?: boolean };\n};\n\nexport type GalleryLightboxProps<P extends GalleryPicture = GalleryPicture> = Omit<LightboxProps, \"count\" | \"children\"> & {\n  items: P[];\n  /**\n   * Draws each picture, in place of `<img>`: return the framework's image component with `props` spread on\n   * it. For a tile, return a GalleryImage with `as` to keep the placeholder of a picture that fails to load.\n   * A slide must keep its own proportions (give it the picture's width and height).\n   */\n  renderImage?: (image: GalleryRenderImage<P>) => ReactNode;\n  /** The viewer's name for screen readers, and the gallery's: \"Photos of the studio\". */\n  title: string;\n  layout?: GalleryLayout;\n  /** Show the strip of small pictures in the viewer. Default: when there are more than one. */\n  thumbnails?: boolean;\n  /** Words for the interface, to translate. */\n  labels?: {\n    open?: string;\n    close?: string;\n    previous?: string;\n    next?: string;\n    zoomIn?: string;\n    zoomOut?: string;\n    zoomReset?: string;\n    zoom?: string;\n    pictures?: string;\n    allPictures?: string;\n    unavailable?: string;\n    more?: (count: number) => string;\n    /** The name of a thumbnail in the strip. Default \"Show picture 3: The workbench\". */\n    show?: (position: number, alt: string) => string;\n  };\n  className?: string;\n};\n\n/**\n * The large picture in the viewer: whole, never cropped, never larger than the slide. Its size is its own\n * (`h-auto w-auto`), so a component that writes `width` and `height` attributes keeps its proportions too.\n */\nconst SLIDE_PICTURE = \"h-auto max-h-full w-auto max-w-full object-contain\";\n\nfunction CurrentCaption({ items }: { items: GalleryPicture[] }) {\n  const { index } = useLightbox(\"GalleryLightbox\");\n  const caption = items[index]?.caption;\n  return caption ? <LightboxCaption>{caption}</LightboxCaption> : null;\n}\n\n/**\n * A gallery and its viewer from a list of pictures. Every option of Lightbox passes through (`open`,\n * `index`, `loop`, `sharedTransition`, `formatPosition` and the rest), so it can be controlled.\n */\nexport function GalleryLightbox<P extends GalleryPicture = GalleryPicture>({ items, title, layout = \"grid\", thumbnails, labels = {}, className, renderImage, ...lightbox }: GalleryLightboxProps<P>) {\n  const strip = thumbnails ?? items.length > 1;\n  return (\n    <Lightbox count={items.length} {...lightbox}>\n      <Gallery layout={layout} aria-label={title} className={className} {...(labels.more ? { moreLabel: labels.more } : {})}>\n        {items.map((item, index) => (\n          <LightboxTrigger key={`${index}-${item.src}`} index={index} asChild>\n            <GalleryItem {...(labels.open ? { openLabel: labels.open } : {})}>\n              {renderImage ? (\n                renderImage({ use: \"tile\", picture: item, index, props: { src: item.thumbnail ?? item.src, alt: item.alt } })\n              ) : (\n                <GalleryImage src={item.thumbnail ?? item.src} alt={item.alt} {...(labels.unavailable ? { unavailableLabel: labels.unavailable } : {})} />\n              )}\n            </GalleryItem>\n          </LightboxTrigger>\n        ))}\n      </Gallery>\n      <LightboxContent>\n        <LightboxHeader>\n          <LightboxCounter />\n          <LightboxTitle className=\"sr-only\">{title}</LightboxTitle>\n          <LightboxZoom className=\"ms-auto\" aria-label={labels.zoom} zoomInLabel={labels.zoomIn} zoomOutLabel={labels.zoomOut} resetLabel={labels.zoomReset} />\n          <LightboxClose aria-label={labels.close} />\n        </LightboxHeader>\n        <LightboxStage>\n          <LightboxViewport aria-label={labels.pictures}>\n            {items.map((item, index) => (\n              <LightboxSlide key={`${index}-${item.src}`} index={index}>\n                {renderImage ? (\n                  renderImage({ use: \"slide\", picture: item, index, props: { src: item.src, alt: item.alt, draggable: false, className: SLIDE_PICTURE } })\n                ) : (\n                  // eslint-disable-next-line @next/next/no-img-element -- The library is not tied to one framework: the default is the plain element, and `renderImage` takes the framework's image component.\n                  <img src={item.src} alt={item.alt} draggable={false} className={SLIDE_PICTURE} />\n                )}\n              </LightboxSlide>\n            ))}\n          </LightboxViewport>\n          <LightboxPrevious aria-label={labels.previous} />\n          <LightboxNext aria-label={labels.next} />\n        </LightboxStage>\n        {strip || items.some((item) => item.caption) ? (\n          <LightboxFooter>\n            <CurrentCaption items={items} />\n            {strip ? (\n              <LightboxThumbnails aria-label={labels.allPictures}>\n                {items.map((item, index) => (\n                  <LightboxThumbnail key={`${index}-${item.src}`} index={index} aria-label={(labels.show ?? ((position, alt) => `Show picture ${position}: ${alt}`))(index + 1, item.alt)}>\n                    {renderImage ? (\n                      renderImage({ use: \"thumbnail\", picture: item, index, props: { src: item.thumbnail ?? item.src, alt: \"\", draggable: false } })\n                    ) : (\n                      // eslint-disable-next-line @next/next/no-img-element -- As above: `renderImage` takes the framework's image component.\n                      <img src={item.thumbnail ?? item.src} alt=\"\" draggable={false} />\n                    )}\n                  </LightboxThumbnail>\n                ))}\n              </LightboxThumbnails>\n            ) : null}\n          </LightboxFooter>\n        ) : null}\n      </LightboxContent>\n    </Lightbox>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Showing several pictures of one thing where each can be looked at closely: a place, a product, a piece of work. GalleryLightbox takes a list of pictures and does everything; Gallery, GalleryItem and GalleryImage are the parts, to put inside your own Lightbox or to use with no viewer at all. Pictures are plain `<img>` elements unless the project gives its framework's image component: `as` on GalleryImage, `renderImage` on GalleryLightbox. For pictures that are decoration, use plain images in a grid; for one picture use aspect.",
    "example": "<GalleryLightbox\n  title=\"Photos of the studio\"\n  layout=\"mosaic\"\n  items={[\n    { src: \"/studio-1.jpg\", alt: \"The studio from the street\" },\n    { src: \"/studio-2.jpg\", alt: \"The workbench\", caption: \"The workbench, built in 1962\" },\n  ]}\n/>\n\n// The parts, in your own lightbox\n<Lightbox count={photos.length}>\n  <Gallery layout=\"grid\" aria-label=\"Photos\">\n    {photos.map((photo, index) => (\n      <LightboxTrigger key={photo.src} index={index} asChild>\n        <GalleryItem><GalleryImage src={photo.src} alt={photo.alt} /></GalleryItem>\n      </LightboxTrigger>\n    ))}\n  </Gallery>\n  <LightboxContent>…</LightboxContent>\n</Lightbox>\n\n// With the framework's image component (here next/image) in place of <img>\n<GalleryImage as={Image} fill sizes=\"(min-width: 48rem) 25vw, 50vw\" src={photo.src} alt={photo.alt} />\n<GalleryLightbox\n  title=\"Photos of the studio\"\n  items={photos}\n  renderImage={({ use, picture, props }) =>\n    use === \"tile\" ? <GalleryImage as={Image} fill sizes=\"25vw\" {...props} /> : <Image {...props} width={use === \"slide\" ? picture.width : 96} height={use === \"slide\" ? picture.height : 72} />\n  }\n/>",
    "client": true
  }
}
