<!-- generated by scripts/gen-docs.mjs from docs/system/lightbox.md. Edit the source and rerun. -->
# Lightbox

## Description
Shows pictures one at a time as large as the window allows, so a person can look at a photo properly, move through the others with it and come back to where they were.

`import { Lightbox, LightboxTrigger } from "@/components/cobalt/ui/lightbox"`, source `registry/cobalt/ui/lightbox.tsx`, status `ready-with-gaps`.
Foundation: `Base UI Dialog`. Traps checked: `trap/overlay-focus-return`, `trap/button-icon-name`, `trap/illustration-announced`, `trap/loading-layout-shift`, `trap/hover-unguarded`, `trap/reduced-motion-ignored`, `trap/color-only-status`.

`trap/overlay-focus-return` is answered by `finalFocus`, which gives the focus to the thumbnail of the picture on show, or the nearest one before it, such as the "2 more" cell (Keyboard). `trap/button-icon-name` by every icon button's name, which is also its tooltip: Download, Close, Previous picture, Next picture. `trap/illustration-announced` by each picture's `alt`, read with "2 of 4" in a polite region each time the picture changes (ARIA). `trap/loading-layout-shift` by the caption row, held two lines tall, so a picture with no caption sits where one with a caption does (Limits). `trap/hover-unguarded` by the arrows, which are not drawn under a coarse pointer, where a swipe moves. `trap/reduced-motion-ignored` by the drift between pictures, which drops to a plain fade. `trap/color-only-status` by the count in words, "2 of 4".

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `Dialog`: the modal popup, its portal and backdrop, the focus trap, Escape, `initialFocus` and `finalFocus`, and the page held still behind it | The whole window as the stage, over the page blurred in its own colour, as in Tag's lightbox (`/Users/kaanaricioglu/tag/src/owner-ui/Inspector.svelte`, `style.css` `.inspector.lightbox`); a press off the picture closes; the picture fit to the window; a bar with the file's `FileTypeIcon` and name, "2 of 4", Download and Close; arrows at the sides; the arrow keys, Home and End; a swipe through `motion`'s drag; a cross-fade with a 24px drift in the direction of travel; the neighbours preloaded; a two-line caption row; `LightboxTrigger`, which opens at its picture and is where the focus comes back to. Dropped from Tag: the zoom pill, Open in default app and the in-bubble strip |

## Examples
A venue's four photos as thumbnails, each opening the lightbox at itself.

Real uses, 0 call sites (`rg -n "<Lightbox\b" app registry --glob '!registry/cobalt/ui/*'`). Inside the registry, `MessageImages` opens every message's pictures in it, `registry/cobalt/ui/message-attachment.tsx`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/lightbox/default.tsx` | default | A venue's photos on its booking page: each thumbnail opens the lightbox at that photo, with the arrows, the arrow keys or a swipe to move, and its caption under it |
| `docs/system/examples/lightbox/in-message.tsx` | composition:Message | Pictures in a message open the lightbox on their own: press one, move through all five, and Escape gives the focus back to the picture you are on |

Example: A venue's photos on its booking page: each thumbnail opens the lightbox at that photo, with the arrows, the arrow keys or a swipe to move, and its caption under it (`docs/system/examples/lightbox/default.tsx`)

```tsx
import { Lightbox, LightboxTrigger, type LightboxItem } from "@/components/cobalt/ui/lightbox";

const photos: LightboxItem[] = [
  { src: "/photos/gala-dinner.webp", alt: "A long table set with glasses and candles", caption: "The hall, set for a dinner of 40", width: 800, height: 800 },
  { src: "/photos/panel.webp", alt: "The stage with four chairs under spotlights", caption: "The stage seats a panel of four", width: 800, height: 800 },
  { src: "/photos/career-night.jpg", alt: "Guests talking at the reception", caption: "The foyer holds 120 standing", width: 800, height: 800 },
  { src: "/photos/assembly.jpg", alt: "The lecture room under its skylight", caption: "The lecture room upstairs, with a projector", width: 800, height: 800 },
];

export default function Example() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-3">
      <div className="flex items-baseline justify-between gap-3">
        <h3 className="text-base font-semibold">The Fenwick Rooms</h3>
        <span className="text-sm text-muted-foreground">4 photos</span>
      </div>
      <Lightbox items={photos}>
        <div className="grid grid-cols-4 gap-2">
          {photos.map((photo, index) => (
            <LightboxTrigger
              key={photo.src}
              index={index}
              aria-label={photo.alt}
              data-focus="custom"
              className="group/photo relative aspect-square cursor-zoom-in outline-none"
            >
              {/* The thumbnail draws the photo's corner, hairline and keyboard ring; the trigger is only the button. */}
              <span className="image-outline relative block size-full overflow-hidden rounded-lg bg-muted group-focus-visible/photo:outline-2 group-focus-visible/photo:outline-offset-2 group-focus-visible/photo:outline-ring">
                <img src={photo.src} alt="" width={800} height={800} className="size-full object-cover" />
              </span>
            </LightboxTrigger>
          ))}
        </div>
      </Lightbox>
    </div>
  );
}
```

Example: Pictures in a message open the lightbox on their own: press one, move through all five, and Escape gives the focus back to the picture you are on (`docs/system/examples/lightbox/in-message.tsx`)

```tsx
import type { LightboxItem } from "@/components/cobalt/ui/lightbox";
import { Message, MessageBubble, MessageContent } from "@/components/cobalt/ui/message";
import { MessageImages } from "@/components/cobalt/ui/message-attachment";

const images: LightboxItem[] = [
  { src: "/photos/fjord-walk.jpg", alt: "The fjord from the lodge's terrace", caption: "From the terrace, at 8 in the morning", width: 800, height: 800 },
  { src: "/photos/hike.webp", alt: "The forest trail up from the road", caption: "The trail from the road, 20 minutes up", width: 800, height: 800 },
  { src: "/photos/barbecue.jpg", alt: "Dinner cooking on the outdoor grill", caption: "Friday dinner is on the grill", width: 800, height: 800 },
  { src: "/photos/mentoring.webp", alt: "Coffee on the kitchen table", caption: "Breakfast is from 7", width: 800, height: 800 },
  { src: "/photos/study-session.webp", alt: "The desk in the reading room", caption: "The reading room has a desk if you need to work", width: 800, height: 800 },
];

export default function Example() {
  return (
    <div className="flex w-full max-w-lg flex-col">
      <Message position="first" author="Ingrid Solberg">
        <MessageContent>
          <MessageImages images={images} />
        </MessageContent>
      </Message>
      <Message position="last" author="Ingrid Solberg">
        <MessageContent>
          <MessageBubble>A few from this week, so you know what to pack.</MessageBubble>
        </MessageContent>
      </Message>
    </div>
  );
}
```

## Variants
Not applicable: a lightbox has one look; it takes the page's colour in either theme.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| closed | default | press a thumbnail | the thumbnails, each a button with `aria-haspopup="dialog"` | test |
| open | a `LightboxTrigger` pressed, or `open` | look, move, download, close | the picture over the blurred page, "2 of 4", the caption | screenshot |
| moving | an arrow, an arrow key, Home, End or a swipe | keep moving | the next picture fades in from the side it came from; the count changes | screenshot |
| first, last | the first or last picture on show | move the other way | that side's arrow is not drawn and leaves the tab order | by hand |
| one picture | `items` has one | look, download, close | no count and no arrows | by hand |
| touch | a coarse pointer | swipe | no arrows; the picture follows the finger and springs back short of 64px | screenshot |

### State precedence
- Open and closed: Not applicable: it is one or the other.
- Moving and first or last: first or last wins. A move past either end is ignored and the picture stays.
- Touch and moving: both show. A swipe moves like an arrow.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| it opens or closes | announce | `dialog-motion` and `overlay-motion`, `--t-arrive`, leaving in 70 percent of it | opacity | the same fade |
| the picture changes | announce | `times.crossfade` on `curves.out` | opacity, translate 24px in the direction of travel | opacity only |
| a swipe | input | `motion` drag, `dragSnapToOrigin` | translate | the same; it follows the finger |
| an arrow under the pointer | input | `pressable`, `--t-press` | background, box-shadow | the same |

## Props

<!-- Props generated from registry/cobalt/ui/lightbox.tsx with TypeScript by scripts/props-table.mjs -->
### Lightbox

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `items` | `readonly LightboxItem[]` |  |  |
| `children?` | `ReactNode` |  | The thumbnails, each in a `LightboxTrigger`. |
| `index?` | `number` |  | The picture on show, when the host keeps it. |
| `labels?` | `Partial<LightboxLabels>` |  |  |
| `onIndexChange?` | `(index: number) => void` |  |  |
| `onOpenChange?` | `(open: boolean) => void` |  |  |
| `open?` | `boolean` |  | Whether it is open, when the host keeps it. |

### LightboxTrigger

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `index` | `number` |  | Which of the lightbox's items it opens. |

Also accepts every native `<button>` attribute; from Base UI: `render`.

`Lightbox` takes `items` (`src`, `alt`, `width`, `height`, `caption`, `original`, and `name`, the file name in the bar, from the address by default), `labels`, and, when the host keeps them, `open` with `onOpenChange` and `index` with `onIndexChange`. Its children are the thumbnails. `LightboxTrigger` takes `index` and `render`, and passes the rest to its button. `MessageImages` passes its `lightboxLabels` through.

## Usage

### When to use
- Photos a person wants to see whole: pictures in a message, a venue's or a listing's photos.

### When not to use
- A document or a page to read beside the current one. Use Sheet instead.
- Photos to pick from in a listing, such as a venue's cover. Use MediaTile instead.

### Rules
- `rule/lightbox-open-from-thumbnail`: When pictures open in a lightbox, wrap each thumbnail in `LightboxTrigger` with its `index` instead of opening it from your own click handler, because the trigger is where the focus comes back to when it closes. Evidence: measured the focus back on the thumbnail of the picture on show after Escape, "Coffee on the kitchen table, 1 more", by Playwright at 1440 and 390. Check: review of every `Lightbox` with `open` and no triggers.
  - Don't: `<img onClick={() => setOpen(true)} />`
  - Do: `<LightboxTrigger index={i} aria-label={photo.alt}><img alt="" /></LightboxTrigger>`
- `rule/lightbox-original-for-download`: When the picture shown is a smaller copy, pass the full file as `original`, because Download saves `original` when it is given and the copy on screen when it is not. Evidence: principle heuristic: match between the system and the real world, Download means the file that was sent. Check: review of every item whose `src` is a resized copy.
  - Don't: `{ src: thumbnailUrl, alt }`
  - Do: `{ src: screenUrl, original: fileUrl, alt }`

### Content
- Follows `rule/message-attachment-alt-says-what`.
- `rule/lightbox-caption-adds`: When writing a caption, say what the picture cannot, such as when or where it was taken, instead of repeating its `alt`, because a screen reader reads the `alt` as the picture comes in and would hear it twice. Evidence: principle wcag: 1.1.1 Non-text content. Check: review.
  - Don't: `{ alt: "The fjord from the terrace", caption: "The fjord from the terrace" }`
  - Do: `{ alt: "The fjord from the terrace", caption: "From the terrace, at 8 this morning" }`

### Anti-slop
- `rule/lightbox-no-grow`: When a `LightboxTrigger` opens the lightbox, let it fade in over the page in `--t-arrive` instead of growing the thumbnail into it, because a thumbnail is cropped square and the picture is not, so a picture growing out of its thumbnail changes shape as it scales, and nothing in the system uses `scale`. Evidence: person D36. Check: review of lightbox motion for `scale` and `layoutId`.
  - Don't: `<motion.img layoutId={photo.src} />`
  - Do: `<Lightbox items={photos}>{thumbnails}</Lightbox>`

### Limits
- `rule/lightbox-caption-two-lines`: When a caption runs past 2 lines, about 109 characters at 390px, put the rest in the page beside the picture, because the caption row is held at 2 lines so the picture never moves between pictures, and the rest is cut. Evidence: measured 109 characters in 2 lines at 390 and 148 at 1440, and the picture at the same box with and without a caption, `.design-system/evidence/lightbox/caption.json`. Check: probe `.design-system/evidence/lightbox/caption.json` from docs/system/stress/lightbox.tsx.
  - Don't: a caption that tells the whole story of the photo
  - Do: `caption: "Friday dinner is on the grill"`

## Accessibility
Rests on Base UI `Dialog`, native buttons and `img`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Enter or Space | a thumbnail | opens the lightbox at that picture | the lightbox itself, so no button lights up | by hand |
| Arrow Right, Arrow Left | in the lightbox | the next or previous picture; swapped in a right-to-left page | stays; if it was on an arrow that goes away, the lightbox | by hand |
| Home, End | in the lightbox | the first or last picture | stays | by hand |
| Tab | in the lightbox | Download, Close, then the arrows | the next button, inside the lightbox | by hand |
| Escape | in the lightbox | closes it | the thumbnail of the picture on show, or the nearest before it | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| thumbnail | button | its `aria-label`, the picture's `alt` | `aria-haspopup="dialog"` | "The fjord from the terrace, button" | test |
| lightbox | dialog | `labels.title`, "Pictures" | modal | "Pictures, dialog" | by hand |
| where you are | none, a polite live region | "2 of 4" and the `alt` | `aria-live="polite"` | "2 of 4. The forest trail up from the road" | by hand |
| picture | img | `alt` | none | its `alt` when reached | by hand |
| Download, Close, arrows | button, Download a link | `labels` | `disabled` on an arrow at either end | "Next picture, button" | by hand |

## Tokens
| Part | State | Token |
|---|---|---|
| backdrop | open | `--background` at 90 percent, `--blur` from Tailwind's `backdrop-blur-xl`, solid under more contrast |
| picture | open | `--muted` while it loads, `--elev-sheet`, `--r-xl` |
| count, caption | open | `--muted-foreground` |
| file glyph | open | `--file-image` and the other `--file-*` kinds, `--file-ink` |
| arrows | open | `--card`, `--elev-glass`, `--r-control`, `--control-lg` |
| open, close | all | `--t-arrive`, `--curve` |

## Related
- Message: its pictures open here.
- MediaTile: a photo-first tile in a listing, whose photo can open here.
- Sheet: a document or a page over the current one.
