Data displayLightbox
Searchastronomic.dev# Lightbox
To open images large over the page, with a caption and prev/next. Works on the image links you already have.
Interactive: inline script.
## Usage
```astro
import Lightbox from "@astronomic-dev/ui/components/Lightbox.astro";
```
## Examples
### The shell
Four image links in one group, and the shell with no recipe classes: close at the top end, the image, then the caption and the controls under it. Click a photo, or tab to one and press Enter. LightboxCentered and LightboxBar are recipes built on it.
```astro
---
import Lightbox from "@astronomic-dev/ui/components/Lightbox.astro";
import { lightboxAttrs } from "@astronomic-dev/ui/lib/lightbox.ts";
// Your own image files. Each link's href is the large image; here the same file as the thumbnail.
import desk from "../../photos/studio-desk.webp";
import plants from "../../photos/plants-shelf.webp";
import stairway from "../../photos/white-stairway.webp";
import concrete from "../../photos/concrete-slab.webp";
const photos = [
{ image: desk, alt: "A laptop, a framed print and a cactus on a light wood desk", caption: "The studio desk, first thing in the morning." },
{ image: plants, alt: "Two potted plants on a wooden shelf", caption: "Plants on the shelf by the window." },
{ image: stairway, alt: "A white stairwell seen from above", caption: "The stairwell, from the top floor." },
{ image: concrete, alt: "A concrete building against a blue sky", caption: "The library across the street." },
];
---
<ul class="m-0 grid list-none grid-cols-2 gap-5 md:grid-cols-4">
{
photos.map(({ image, alt, caption }) => (
<li>
<figure class="m-0 grid gap-2">
<a href={image.src} {...lightboxAttrs({ group: "studio", width: image.width, height: image.height })}>
<img src={image.src} alt={alt} width={image.width} height={image.height} class="block aspect-4/3 h-auto w-full rounded-media object-cover" />
</a>
<figcaption class="text-small text-muted">{caption}</figcaption>
</figure>
</li>
))
}
</ul>
<Lightbox />
```
## Props
### Lightbox
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | `"Image viewer"` | The dialog's accessible name. |
| `closeLabel` | `string` | `"Close"` | The close button's accessible name. |
| `prevLabel` | `string` | `"Previous image"` | The previous button's accessible name. |
| `nextLabel` | `string` | `"Next image"` | The next button's accessible name. |
| `counterText` | `string` | `"{n} of {total}"` | The counter, shown and read aloud: {n} is the image's place in the set and {total} the set's size. Pass your own words for a translation. |
| `closeAt` | `"start" \| "end"` | `"start"` | For recipes. Where the close button sits in the page order, which is the Tab order: `start` makes it the first area, drawn at the top; `end` makes it the last control in the bar, after next. |
| `class` | `string` | None | More classes for the <dialog>, merged with cn(), so yours win on conflicts. |
| `stageClass` | `string` | None | For recipes. Classes for the image box. It is a size container (lightbox-stage) the image fits inside. |
| `barClass` | `string` | None | For recipes. Classes for the bar around the caption and the controls. |
| `controlsClass` | `string` | None | For recipes. Classes for the row of controls: prev, the counter, next, and close with `closeAt="end"`. |
| `captionClass` | `string` | None | For recipes. Classes for the caption. |
| `closeClass` | `string` | None | For recipes. Classes for the close button. |
| `prevClass` | `string` | None | For recipes. Classes for the previous button. |
| `nextClass` | `string` | None | For recipes. Classes for the next button. |
| `counterClass` | `string` | None | For recipes. Classes for the visible counter. |
Render one per page, anywhere in the page (a second one logs a warning in development, and the first is used). It is a closed <dialog> until a link made with lightboxAttrs() is clicked; the content comes from the links, so it has no slots.
### lightboxAttrs()
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `group` | `string` | `""` | The set this image belongs to, as data-lightbox: prev and next walk every visible link with the same group, in page order. "" is a set of one. |
| `caption` | `string` | None | The caption, as plain text. Else the text of the <figcaption> of the link's closest <figure>. Else no caption. |
| `srcset` | `string` | None | A srcset for the large view, so the browser picks the right file. Else the link's href. |
| `sizes` | `string` | None | The sizes for `srcset`. "100vw" when `srcset` is set and sizes is not. |
| `width` | `number` | None | The large image's width in pixels, so the large view keeps its shape while it loads. Else the thumbnail's shape. |
| `height` | `number` | None | The large image's height in pixels. |
Spread the result on a link whose href is the large image: <a href={photo.src} {...lightboxAttrs({ group: "work" })}><img …></a>. The attributes are plain data-lightbox-* attributes, so CMS HTML can carry them too.
## Accessibility
| Key | Action |
| --- | --- |
| Enter | Opens the lightbox from an image link |
| Left and Right arrows | Show the previous and next image, wrapping at the ends (swapped on a right-to-left page) |
| Home and End | Show the first and last image |
| Tab and Shift+Tab | Move through the controls in the order they are drawn; focus stays inside |
| Escape | Closes the lightbox and returns focus to the link of the image shown last |
- It is a modal <dialog> named by label (default "Image viewer"), described by the caption when there is one. While it is open, the page behind cannot be reached and does not scroll.
- On open, focus goes to the close button. Opened with a click or a tap, the close button shows no focus ring; opened from the keyboard, it does.
- On close, focus goes back to the link of the image shown last, so the page follows the reader through the set.
- The image's alt is the alt of the link's own <img>, as written: alt="" stays empty, and the caption still describes the dialog. Only a link with no <img>, or an <img> with no alt attribute, uses the caption as the alt.
- The visible counter is hidden from screen readers. One polite live region says the count and the alt when the image changes ("2 of 4, Two potted plants on a wooden shelf"), not on open.
- A set of one (or a group whose other links are hidden) shows no prev, next or counter, and the arrow keys do nothing.
- Close, prev and next are at least 44px on a touch screen. A sideways swipe moves through the set; pinch zoom still works.
- Without JavaScript the links are plain links to the image files. A link that opens a new tab (target="_blank"), a download link, and a click with a modifier key are left to the browser.
- With ClientRouter there is nothing to do: the lightbox takes the click before the router, closes when the page changes, and uses the new page's lightbox after a swap.
- Under reduced motion or the theme's calm setting it opens and changes images at once.
Lightbox
FreeTo open images large over the page, with a caption and prev/next. Works on the image links you already have.
- File
- Lightbox.astro
- Group
- Data display
- Kind
- Interactive: inline script
- Examples
- 1
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
The shell
Four image links in one group, and the shell with no recipe classes: close at the top end, the image, then the caption and the controls under it. Click a photo, or tab to one and press Enter. LightboxCentered and LightboxBar are recipes built on it.
This preview did not load.
main.astro
examples/lightbox/main.astro
---
import Lightbox from "@astronomic-dev/ui/components/Lightbox.astro";
import { lightboxAttrs } from "@astronomic-dev/ui/lib/lightbox.ts";
// Your own image files. Each link's href is the large image; here the same file as the thumbnail.
import desk from "../../photos/studio-desk.webp";
import plants from "../../photos/plants-shelf.webp";
import stairway from "../../photos/white-stairway.webp";
import concrete from "../../photos/concrete-slab.webp";
const photos = [
{ image: desk, alt: "A laptop, a framed print and a cactus on a light wood desk", caption: "The studio desk, first thing in the morning." },
{ image: plants, alt: "Two potted plants on a wooden shelf", caption: "Plants on the shelf by the window." },
{ image: stairway, alt: "A white stairwell seen from above", caption: "The stairwell, from the top floor." },
{ image: concrete, alt: "A concrete building against a blue sky", caption: "The library across the street." },
];
---
<ul class="m-0 grid list-none grid-cols-2 gap-5 md:grid-cols-4">
{
photos.map(({ image, alt, caption }) => (
<li>
<figure class="m-0 grid gap-2">
<a href={image.src} {...lightboxAttrs({ group: "studio", width: image.width, height: image.height })}>
<img src={image.src} alt={alt} width={image.width} height={image.height} class="block aspect-4/3 h-auto w-full rounded-media object-cover" />
</a>
<figcaption class="text-small text-muted">{caption}</figcaption>
</figure>
</li>
))
}
</ul>
<Lightbox />Minimal · Light