# lightbox

A click opens an image full screen: its copy flies out of the grid, pages and zooms.

A click lifts the image out of the grid towards the viewer and carries it to the center of the screen while its crop opens to the full picture, the rest of the grid sinks back and the backdrop darkens; the large file crossfades in after the flight. Inside, arrows, buttons and swipes page, and a double click, the wheel or a pinch zoom. Escape, the close button, a click on the backdrop, a swipe down or scrolling the page brings it back to its place the same way.

Module `media`, category Galleries and viewers. Import `@dioramajs/media/lightbox`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/lightbox.ts)

Six photos of a coastal walk in a grid; a click lifts one out of the grid and opens it full screen.

```html title="index.html"
<ul class="coast" data-dio="lightbox">
  <li><a href="/media/coast/slope-1440.avif"><img src="/media/coast/slope-800.avif" alt="A green slope falling to the sea below the cliffs" width="800" height="534" /></a></li>
  <li><a href="/media/coast/shore-1440.avif"><img src="/media/coast/shore-800.avif" alt="White rocks and low bushes above a grey sea" width="800" height="534" /></a></li>
  <li><a href="/media/coast/islets-960.avif"><img src="/media/coast/islets-600.avif" alt="Dark islets in a blue bay, a flat-topped mountain in the haze" width="600" height="600" /></a></li>
  <li><a href="/media/coast/surf-1440.avif"><img src="/media/coast/surf-800.avif" alt="Surf breaking over brown rocks at the foot of the cliffs" width="800" height="534" /></a></li>
  <li><a href="/media/coast/boulders-960.avif"><img src="/media/coast/boulders-600.avif" alt="Rounded boulders at the edge of the water" width="600" height="750" /></a></li>
  <li><a href="/media/coast/headland-1440.avif"><img src="/media/coast/headland-800.avif" alt="A grassy headland dropping into the sea under a pale sky" width="800" height="534" /></a></li>
</ul>
```

## Usage

Put it on: a container of linked images, or one img.

`start()` registers `lightbox`, so the attribute alone puts it on an element.

```html
<ul class="rooms" data-dio="lightbox"><li><a href="/img/suite-2400.avif"><img src="/img/suite-600.avif" alt="Garden suite" /></a></li><li><figure><a href="/img/veranda-2400.avif"><img src="/img/veranda-600.avif" alt="Veranda" /></a><figcaption>Breakfast on the veranda</figcaption></figure></li></ul>
```

- `data-dio-full`: URL of an item’s large picture; without it, the linked image, the largest srcset candidate or currentSrc.

In page transitions: Fades with the page; leaving the page closes the viewer at once.

- Event `dio:open`, detail `{ index: number }`: The viewer opened a picture; index is its place among the items.

- Event `dio:index`, detail `{ index: number }`: The viewer paged to another picture.

- Event `dio:close`, detail `{ index: number }`: The viewer closed; index is the picture it closed on.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `data-dio-index` | `number` -1 to 20 | `-1` | Open picture from 0, -1 when closed; the effect writes it, the site can open one. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the images that open; ignored on a single img. |
| `motion` (rebuilds) | `data-dio-motion` | `'lift' \| 'fly' \| 'zoom' \| 'flip' \| 'fade'` | `'lift'` | How the picture opens and closes: lift arcs it towards the viewer, tipped back and leaning into the move, while the grid sinks back; fly carries it flat; zoom grows it straight to the center while the grid sinks back; flip turns it over like a card on the way; fade dissolves it in place without a flight. |
| `margin` | `data-dio-margin` | `number` 0 to 120 px | `24` | Space around the open picture, and at least the band a shown caption takes. |
| `zoom` | `data-dio-zoom` | `number` 0 to 8 | `3` | Zoom limit over the fitted picture, never below 1:1 of the source; 0 turns zoom off, so the wheel, a pinch, a double click and the + - 0 keys do nothing. |
| `duration` | `data-dio-duration` | `number` 0.1 to 1.5 s | `0.45` | Length of the flight in and out. |
| `backdrop` | `data-dio-backdrop` | `color` | `'#0b0b0c'` | Color of the backdrop. |
| `dim` | `data-dio-dim` | `number` 0 to 1 | `0.92` | Opacity of the backdrop, blended like CSS: copies on the canvas dim like the page. |
| `loop` | `data-dio-loop` | `boolean` | `false` | Paging wraps around from the last picture to the first. |
| `swipe` | `data-dio-swipe` | `boolean` | `true` | Swipes page through pictures, and a swipe down closes. |
| `close` (rebuilds) | `data-dio-close` | `string` | `'esc backdrop swipe scroll button'` | What closes the viewer, names split by spaces: esc the Escape key, backdrop a click or tap beside the picture, swipe a swipe down, scroll scrolling the page once the picture has landed, button the close button; all for every one. Escape always closes, as a modal dialog must, so an empty list leaves only Escape. |
| `controls` (rebuilds) | `data-dio-controls` | `string` | `'arrows counter caption close'` | Parts of the viewer shown, names split by spaces: arrows the previous and next buttons, counter such as 3 / 12, caption, close the close button; all for every one, empty for none. The keys still page, and the screen reader still hears the caption and the counter. |
| `labels` (rebuilds) | `data-dio-labels` | `string` | `'Close\|Previous\|Next\|{i} / {n}\|Image viewer'` | Labels of the buttons, the counter and the dialog, split by \|. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 512 to 8192 px | `4096` | Cap on the long side when decoding the large picture. At most low 2048, medium 3072. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `lift`, `fly`, `zoom`, `flip`, `fade`
  - `lift` arcs it towards the viewer, tipped back and leaning into the move, while the grid sinks back.
  - `fly` carries it flat.
  - `zoom` grows it straight to the center while the grid sinks back.
  - `flip` turns it over like a card on the way.
  - `fade` dissolves it in place without a flight.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`reducedMotion`](/docs/api/inputs#input-reducedMotion). Draws only while something on it moves, then stops. Only during flights, paging, zoom easing and inertia; none while open and still, none when closed. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. No flight whatever motion says: opening, closing and paging fade in 0.15 s, and zoom does not animate.

Runs on every device. Inside the viewer: pinch, double tap, drag, swipe to page and swipe down to close. On the page a tap follows the link as usual.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { lightbox: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 10.5 KB the first time a page uses it. GPU: The copies of the previews, a backdrop and one high-resolution plane. Memory: Every preview of the container as a texture plus up to three large pictures; keep containers to about 60 previews.

- low: Decodes at most 2048 px on the long side.
- medium: Decodes at most 3072 px on the long side.

See [the quality plugin](/docs/effects/quality).

## Recipes

### One image in an article

Put `data-dio="lightbox"` on a single `<img>` inside a link to its large file, as you would for a zoomable figure in a blog post. A floor plan, a map or a chart gains the most: the reader opens it, zooms with the wheel, a pinch or a double click, and drags to the part they need. The caption of the viewer comes from the `<figcaption>` of the image’s figure, or from its alt text.

A paragraph and a floor plan; a click opens the plan full screen, where it zooms up to three times.

```html title="index.html"
<div class="plan-note">
  <p>The Garden Room and Room 7 open onto the veranda, where breakfast is served from half past seven.</p>
  <figure>
    <a href="/media/plan/ground-floor-2400.webp"><img src="/media/plan/ground-floor-1200.webp" alt="Plan of the ground floor: dining room, drawing room, kitchen, salon, two guest rooms and the veranda" width="1200" height="800" data-dio="lightbox" /></a>
    <figcaption>The ground floor as it is today</figcaption>
  </figure>
</div>
```

### A quiet viewer for screenshots

For documentation or a press kit, where readers open a picture to read it and go back, set `motion` to `fade`, show only the counter and the close button, and leave `scroll` and `swipe` out of `close`, so only a deliberate press closes it. Escape always closes, as a modal dialog must.

```html
<ul
  class="screens"
  data-dio="lightbox"
  data-dio-motion="fade"
  data-dio-controls="counter close"
  data-dio-close="esc backdrop button"
>
  <li>
    <a href="/media/plan/ground-floor-2400.webp">
      <img src="/media/plan/ground-floor-1200.webp" />
    </a>
  </li>
  <li>
    <a href="/media/coast/slope-1440.avif">
      <img src="/media/coast/slope-800.avif" />
    </a>
  </li>
</ul>
```

### Large files without links

When the previews are not links, as in a product page where the photos sit in buttons, name each large file with `data-dio-full` on the image. Without it the viewer takes the largest `srcset` candidate or the file on screen. Keep a link where you can: without WebGL a link still opens the large file, a button does nothing.

```html
<ul
  class="views"
  data-dio="lightbox"
  data-dio-motion="zoom"
>
  <li>
    <button type="button">
      <img
        src="/media/chair/front.webp"
        data-dio-full="/media/chair/front-2400.webp"
      />
    </button>
  </li>
  <li>
    <button type="button">
      <img
        src="/media/chair/side.webp"
        data-dio-full="/media/chair/side-2400.webp"
      />
    </button>
  </li>
</ul>
```

### Open it from a button of your own

A listing often ends its grid with a button such as “Show all 24 photos”. Set `index` on the instance to open the viewer at that picture; it opens while the grid is on screen. `dio:open` and `dio:close` tell the page when the viewer comes and goes, for example to pause a video behind it.

```ts
const photos = document.querySelector('.coast')

document.querySelector('.show-all').addEventListener('click', () => {
  app.effects.get(photos, 'lightbox')?.set({ index: 0 })
})
photos.addEventListener('dio:open', (e) =>
  console.log('opened photo', e.detail.index),
)
```

## Notes

The viewer is a modal dialog. Enter on a focused preview opens it, focus stays inside while it is open, Escape closes it and focus returns to the preview’s link. A screen reader hears the dialog’s name, the caption and the counter as you page, even when `controls` hides them on screen. Give each preview a link or a button, so it takes focus.

Ctrl-click, Cmd-click and the middle button open the link the usual way, and before the engine runs or without WebGL a click follows the link to the large file. `labels` translates the buttons, the counter and the dialog’s name: `data-dio-labels="Fermer|Précédent|Suivant|{i} sur {n}|Visionneuse"`.

Every preview in the container is a texture while the page shows it, and up to three large files are decoded at a time. Split long galleries into containers of about 60 previews.

`motion`, `close`, `controls` and `labels` rebuild the effect when they change, which closes an open viewer. Set them in the markup.

## Related

- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [coverflow](/docs/effects/coverflow.md): A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.
- [flipbook](/docs/effects/flipbook.md): The page images of a container become a book whose pages bend as they turn.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [hover-preview](/docs/effects/hover-preview.md): Over a list of projects, the picture of the item under the pointer follows the pointer, stands beside the item or holds a fixed place.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [stack](/docs/effects/stack.md): The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.
- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
