lightbox
A click opens an image full screen: its copy flies out of the grid, pages and zooms.
Live preview needs WebGPU or WebGL 2. Six photos of a coastal walk in a grid; a click lifts one out of the grid and opens it full screen.
This example failed to draw.
Click a photo or press Enter on it. Inside, the arrow keys page, + and − zoom, Escape closes.
Usage
Put it on a container of linked images, or one img. start() registers lightbox, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add lightbox<ul class="coast" data-dio="lightbox"> <li> <a href="/media/coast/slope-1440.avif"> <img src="/media/coast/slope-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/media/coast/shore-1440.avif"> <img src="/media/coast/shore-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/media/coast/islets-960.avif"> <img src="/media/coast/islets-600.avif" width="600" height="600" /> </a> </li> <li> <a href="/media/coast/surf-1440.avif"> <img src="/media/coast/surf-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/media/coast/boulders-960.avif"> <img src="/media/coast/boulders-600.avif" width="600" height="750" /> </a> </li> <li> <a href="/media/coast/headland-1440.avif"> <img src="/media/coast/headland-800.avif" width="800" height="534" /> </a> </li></ul>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.coast', 'lightbox', { motion: 'lift', margin: 24, zoom: 3, duration: 0.45, backdrop: '#0b0b0c', dim: 0.92, loop: false, swipe: true,})import { start } from '@dioramajs/diorama'import lightbox from '@dioramajs/media/lightbox'
const app = start()app.attach('.coast', lightbox({ motion: 'lift', margin: 24, zoom: 3, duration: 0.45, backdrop: '#0b0b0c', dim: 0.92, loop: false, swipe: true,}))Markup it reads
data-dio-full: URL of an item’s large picture; without it, the linked image, the largest srcset candidate or currentSrc.
Events
It dispatches these on its element; they bubble.
dio:open,detail: { index: number }. The viewer opened a picture; index is its place among the items.dio:index,detail: { index: number }. The viewer paged to another picture.dio:close,detail: { index: number }. The viewer closed; index is the picture it closed on.
Parts
Its own controls take your styles through ::part().
close: Close button.prev: Previous picture button.next: Next picture button.counter: Counter such as 3 / 12.caption: Caption from the item’s figcaption or alt text.
Page transitions
Fades with the page; leaving the page closes the viewer at once. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
indexdata-dio-index | number-1 to 20 | -1 | Open picture from 0, -1 when closed; the effect writes it, the site can open one. |
itemsrebuildsdata-dio-items | string | 'img' | Selector of the images that open; ignored on a single img. |
motionrebuildsdata-dio-motion | 'lift' | 'fly' | 'zoom' | 'flip' | 'fade' | 'lift' |
|
margindata-dio-margin | number0 to 120 px | 24 | Space around the open picture, and at least the band a shown caption takes. |
zoomdata-dio-zoom | number0 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. |
durationdata-dio-duration | number0.1 to 1.5 s | 0.45 | Length of the flight in and out. |
backdropdata-dio-backdrop | color | '#0b0b0c' | Color of the backdrop. |
dimdata-dio-dim | number0 to 1 | 0.92 | Opacity of the backdrop, blended like CSS: copies on the canvas dim like the page. |
loopdata-dio-loop | boolean | false | Paging wraps around from the last picture to the first. |
swipedata-dio-swipe | boolean | true | Swipes page through pictures, and a swipe down closes. |
closerebuildsdata-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. |
controlsrebuildsdata-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. |
labelsrebuildsdata-dio-labels | string | 'Close|Previous|Next|{i} / {n}|Image viewer' | Labels of the buttons, the counter and the dialog, split by |. |
resolutionrebuildsdata-dio-resolution | number512 to 8192 px | 4096 | Cap on the long side when decoding the large picture.At most low 2048, medium 3072 by quality tier. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta and 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. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { lightbox: { reducedMotion: 'off' } } })Cost and quality
- Download
- 10.5 KB min+gzip 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.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | Decodes at most 2048 px on the long side. |
medium | Decodes at most 3072 px on the long side. |
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.
<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" 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.
<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.
<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.
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.






