# Image viewer

A full-screen dark lightbox with previous/next that cycle, arrow keys, a position caption and a filmstrip.

- Family: display
- Import: `import { ImageViewer } from "@senvenli/react/display";`
- Page: https://design.senvenli.com/docs/components/image-viewer
- JSON: https://design.senvenli.com/docs/components/image-viewer.json

## Example: Open a gallery

The viewer is open while it is mounted: render it from a trigger and unmount it on close.

```tsx
import { Button } from "@senvenli/react/action";
import { ImageViewer } from "@senvenli/react/display";
import { useState } from "react";

// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
  `data:image/svg+xml,${encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
  )}`;

const PHOTOS = [
  { src: photo("Front view", 210), alt: "The standing desk from the front" },
  { src: photo("Side view", 150), alt: "The desk from the side, raised" },
  { src: photo("Detail", 30), alt: "The height controls up close" },
];

export default function Example() {
  const [isOpen, setOpen] = useState(false);
  return (
    <>
      <Button iconBefore="images" onPress={() => setOpen(true)}>
        View photos
      </Button>
      <ImageViewer items={PHOTOS} title="Standing desk" isOpen={isOpen} onOpenChange={setOpen} />
    </>
  );
}
```

## Examples

### Open on a chosen photo

Each trigger opens the viewer on its own photo.

```tsx
import { Button } from "@senvenli/react/action";
import { ImageViewer } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
  `data:image/svg+xml,${encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
  )}`;

const PHOTOS = [
  { label: "Front view", src: photo("Front view", 210) },
  { label: "Side view", src: photo("Side view", 150) },
  { label: "Detail", src: photo("Detail", 30) },
];

export default function Example() {
  const [openAt, setOpenAt] = useState<number | null>(null);
  return (
    <>
      <Stack direction="horizontal" space="space-100">
        {PHOTOS.map((item, index) => (
          <Button key={item.label} onPress={() => setOpenAt(index)}>
            {item.label}
          </Button>
        ))}
      </Stack>
      {openAt !== null && (
        <ImageViewer
          items={PHOTOS.map((item) => ({ src: item.src, alt: item.label }))}
          defaultIndex={openAt}
          title="Standing desk"
          onOpenChange={(open) => !open && setOpenAt(null)}
        />
      )}
    </>
  );
}
```

## Guidelines

### When to use it

- Looking at photos full size.

### Use something else when

- [carousel](https://design.senvenli.com/docs/components/carousel.md): Pictures page in place, inside the layout.
- [image](https://design.senvenli.com/docs/components/image.md): One picture is shown at its place in the page.

## Accessibility

Keyboard:

- Arrow left / right: Previous or next photo, following the reading direction.
- Esc: Closes the viewer.

Built in:

- Focus stays inside while open and returns to what opened it.

You provide:

- A title and alt text.

## API reference

### ImageViewer

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| image | `ImageViewerImage` | `PlainImage` | no | The image renderer (e.g. next/image); a plain `<img>` by default. |
| items | `CarouselItem[]` |  | yes | The photos in display order, each with its own alt text: the same shape as Carousel's `items`. |
| defaultIndex | `number` | `0` | no | The photo to open on; wrapped into range. |
| isOpen | `boolean` | `true` | no | Open by default, so rendering it opens it; or control it with `onOpenChange`. |
| onOpenChange | `(isOpen: boolean) => void` |  | no | Called with `false` on Esc, the close button or a press on the scrim. |
| title | `string` |  | yes | Caption shown at the bottom and used as the accessible dialog title. |
| labels | `{ close?: string; previous?: string; next?: string; position?: (index: number, total: number) => string; }` |  | no | The viewer's words; the DS strings by default ("Close", "Previous image", "Next image", "Image 2 of 5"). |

### Tokens it reads

`--ds-background-neutral-bold`, `--ds-blanket`, `--ds-border-focused`, `--ds-border-selected`, `--ds-icon`, `--ds-icon-brand`, `--ds-icon-danger`, `--ds-icon-information`, `--ds-icon-subtle`, `--ds-icon-success`, `--ds-icon-warning`, `--ds-radius-full`, `--ds-radius-medium`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-size-icon`, `--ds-size-icon-large`, `--ds-size-icon-small`, `--ds-space-025`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-250`, `--ds-space-800`, `--ds-surface`, `--ds-surface-hovered`, `--ds-surface-overlay`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-inverse`, `--ds-text-subtle`

## Related

- [carousel](https://design.senvenli.com/docs/components/carousel.md)
