# Image viewer

> Wrap the grid you already have: a thumbnail grows into the full view, zooms where you aim, and shrinks back into its hole when you close.

A lightbox that keeps your grid. Wrap whatever already shows your images and every one of them opens: the thumbnail itself grows out of its place into the full view, its crop opening out to the whole picture, while the page fades behind it. Wheel, pinch or double-tap to zoom at the point you aim at, drag to pan with momentum, swipe or use the arrow keys for the next image, and swipe down or press Escape to close, when it shrinks back into the hole of whichever image you ended on. Everything runs on springs, so a gesture can take over from any motion mid-flight. Put data-full on an image to load a larger file inside the viewer.

## Install

```bash
npx shadcn@latest add @hairline/image-viewer
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/image-viewer
- Registry item (source): https://hairlineui.com/r/image-viewer.json

## Usage

```tsx
import { ImageViewer } from "@/components/image-viewer";

<ImageViewer>
  <div className="grid grid-cols-3 gap-2">
    {images.map((image) => (
      <img key={image.id} src={image.thumb} data-full={image.url} alt={image.prompt} />
    ))}
  </div>
</ImageViewer>
```

## Only some images, and knowing which is open

Limit it to the images you mark, and keep the open index in your state, here to load comments for the image being viewed.

```tsx
"use client";
import * as React from "react";
import { ImageViewer } from "@/components/image-viewer";

export function Results({ images }: { images: { id: string; thumb: string; url: string; prompt: string }[] }) {
  const [open, setOpen] = React.useState<number | null>(null);
  return (
    <>
      <ImageViewer selector="img[data-zoom]" onIndexChange={setOpen}>
        <ul className="grid grid-cols-2 gap-3">
          {images.map((image) => (
            <li key={image.id} className="overflow-hidden rounded-xl">
              <img data-zoom src={image.thumb} data-full={image.url} alt={image.prompt} className="aspect-square object-cover" />
            </li>
          ))}
        </ul>
      </ImageViewer>
      {open !== null && <p className="sr-only">Viewing {images[open].prompt}</p>}
    </>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | – | Your own grid, list or single image. Every matching image inside opens. |
| `selector` | `string` | `"img"` | Which images open, a CSS selector for img elements. |
| `data-full (on an img)` | `string` | – | A larger file to load in the viewer; the thumbnail shows until it has loaded. |
| `onIndexChange` | `(index: number \| null) => void` | – | The open slide’s index, and null when it closes. |
| `maxZoom` | `number` | `4` | Most it zooms, as a multiple of the fitted size. |
| `labels` | `{ close?, next?, previous?, zoomIn?, zoomOut?, viewer? }` | – | Accessible names for the buttons and the dialog. |
| `className` | `string` | – | On the wrapper around your markup. |

## States

- **opening**: The thumbnail’s place, cover crop and corner radius open out to the image fitted to the screen in 360ms, while the page fades behind it.
- **viewing**: A counter (3 / 12), zoom out, the zoom level, zoom in and close along the top; previous and next arrows at the sides on wide screens; the alt text along the foot.
- **zoomed**: Up to 4×, anchored at the pointer or the pinch. Drag pans with momentum and gives with a rubber band at the picture’s edges. Double-tap toggles between fit and 2.5×.
- **swiping**: At fit, a horizontal drag moves the strip; released past a quarter of the screen (projected from its speed) it glides to the next image. The ends give and spring back.
- **closing**: Drag down and the image shrinks a little as the backdrop clears; let go past about a fifth of the screen, press Escape or tap outside, and it flies back into its thumbnail’s place in 360ms. A thumbnail scrolled off screen gets a fade instead.
- **reduced motion**: It fades in and out in 160ms with no flight; zoom and pan still follow your hand.

## What it handles

Most lightboxes take over your markup with their own gallery, pop a modal in from nowhere and zoom with a fixed step around the centre.

- **Keeps your grid**: Wrap any markup; every image matching the selector opens, in page order, with its own alt text as the caption.
- **Grows from its place**: The thumbnail’s crop and rounded corners open out to the full picture, and closing lands back in the hole of the image you ended on.
- **Zoom where you aim**: Wheel, pinch and double-tap zoom around the pointer or between the fingers, up to 4×, and panning stops at the picture’s edges with a give.
- **Gestures that hand over**: Swipe to the next image, swipe down to close, drag to pan: springs carry their speed, so grabbing mid-motion never jumps.
- **Loads the big file**: The thumbnail shows instantly; data-full loads a larger file over it and fades in when ready.
- **Keyboard and focus**: Arrow keys, + and −, 0 to fit, Escape to close; focus stays in the viewer and returns to what opened it.

## Replaces

yet-another-react-lightbox, react-image-lightbox, photoswipe, medium-zoom, react-medium-image-zoom, image modal

## Where it fits

- **AI image history**: Open a generation full size from the grid, zoom into detail, swipe through the batch.
- **Product pages**: Product photos that open in place and zoom where the shopper looks.
- **Chat and comments**: Images attached to a message that open without leaving the thread.
- **Portfolios and docs**: Screenshots and work samples readable at full size.

## Notes

- The viewer is a modal dialog in a portal over the page, with focus kept inside it and returned to whatever opened it; the counter is a polite live region.
- Images not inside a link, a button or something focusable get tabindex="0" and role="button", so Enter and Space open them.
- Only the image being viewed and its two neighbours are in the page; zoom, pan and the strip are transforms written by one spring loop, with no React render per frame.
- The page behind doesn’t scroll while it is open: wheel and touchmove are stopped on the viewer, without touching the page’s own styles.
- Under reduced motion it fades in and out with no flight.

License: MIT (https://hairlineui.com/license#components)
