# Expanding gallery

> An image opens from its exact place in the grid, becomes a spacious viewer, and finds its way back.

A gallery with a sense of place. Select a study and the image grows from its thumbnail into a full-screen viewer, keeping its proportions. Its caption follows a moment later. Browse with the arrows, keyboard or a swipe; the next study arrives from the direction you moved. Close it and the current image returns to its own thumbnail. The viewer is a native modal dialog, so the page behind it stays inert and keyboard focus stays inside.

## Install

```bash
npx shadcn@latest add @hairline/expanding-gallery
```

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

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/expanding-gallery
- Registry item (source): https://hairlineui.com/r/expanding-gallery.json

## Usage

```tsx
import { ExpandingGallery } from "@/components/expanding-gallery";

<ExpandingGallery
  label="Selected work"
  images={[
    { id: "campaign", src: "/images/campaign.webp", alt: "Amber bottle in afternoon light", title: "Afternoon study", width: 1536, height: 1024, meta: "Campaign · 2026" },
    { id: "detail", src: "/images/detail.webp", alt: "A close-up of the bottle", title: "The essential form", width: 1536, height: 1024 },
  ]}
/>
```

## Track the selected study

Use stable image IDs and real intrinsic dimensions. Supply your own local or remote image URLs.

```tsx
import { ExpandingGallery, type GalleryImage } from "@/components/expanding-gallery";

export function Portfolio({ studies }: { studies: GalleryImage[] }) {
  return (
    <ExpandingGallery
      images={studies}
      label="Studio / Selected studies"
      onImageChange={(image) => {
        if (image) console.log("Opened study", image.id);
      }}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `images` | `GalleryImage[]` | – | Stable id, src, alt, title, width and height; optional description and meta. Intrinsic proportions are preserved. |
| `label` | `string` | `Selected work` | The gallery heading and viewer heading. |
| `onImageChange` | `(image: GalleryImage \| null) => void` | – | Called when a study opens or changes. Receives null on close. |
| `className` | `string` | – | Additional classes on the gallery root. Standard div attributes are forwarded. |

## States

- **open**: The viewer measures the thumbnail and destination, then expands the image over 560ms. The caption follows after 180ms.
- **browse**: Previous and next images enter from the direction of travel in 380ms. Navigation stops at the ends.
- **swipe**: The image follows horizontal dragging. A 64px drag or a deliberate quick flick advances; a short drag settles back. The ends resist movement.
- **close**: The current image returns to its measured thumbnail in 380ms. If its thumbnail has left the viewport, it fades away instead.
- **unavailable**: A failed image shows a labelled fallback without changing the gallery’s dimensions.

## What it handles

A gallery that opens a lightbox from nowhere loses the sense of which image you picked. This opens each image from its own place in the grid and sends it back there.

- **It opens from its thumbnail**: The image grows from its exact place in the grid into the viewer, keeping its proportions. The caption follows a moment later.
- **It returns where it came from**: Closing sends the current image back to its own thumbnail, or fades it if the thumbnail has scrolled out of view.
- **Browse by arrow, key or swipe**: The next image arrives from the direction you moved. A swipe follows your finger and the ends resist.
- **Focus is handled**: Focus stays inside the viewer, the page behind does not scroll, and Escape closes it and returns focus to the thumbnail.
- **Failed images are handled**: A broken image shows a labelled fallback without changing the gallery’s size.
- **Real proportions**: Each image has its own width and height, so wide and tall pictures sit in the grid without being cropped in the viewer.

## Replaces

a lightbox library, a modal dialog, image size measuring, swipe handling, focus trapping

## Where it fits

- **Portfolio and case studies**: Open a project from its thumbnail into a large viewer.
- **Product screenshot galleries**: Let visitors look closely at the interface on a landing page.
- **Listings and product photos**: Browse several photos of a property, a product or a venue.
- **Template and theme previews**: Show a set of designs that open at full size.

## Notes

- The viewer is a top-layer popover with role="dialog" and aria-modal: it opens above everything without making the page inert, which on a large page restyles every element in the frame it opens. Focus is trapped inside, the page behind does not scroll, Escape closes and focus returns to the current thumbnail, including after browsing.
- Left and Right browse, Home and End jump to the first and last image. Buttons expose disabled states at the ends.
- Pointer cancellation restores the image. Horizontal swipes leave vertical touch movement to the browser.
- Reduced motion removes expansion, sliding, overshoot and thumbnail zoom. Navigation and all controls remain available.
- Image IDs must be unique; width and height should match the source asset. The component handles an empty collection.
- The entry point, viewer and motion helpers install together. No global animation stylesheet or animation library is required.

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