Lightbox
A full-screen photo viewer for a gallery, as a native <dialog> a trigger opens through Invoker Commands.
View code
<div>
{photos.map((photo, index) => (
<Lightbox.Trigger lightbox="fern-cabin" index={index} aria-label={`Open photo ${index + 1}`}>
<img src={photo.thumb} alt="" />
</Lightbox.Trigger>
))}
<Lightbox.Trigger lightbox="fern-cabin">Show all photos</Lightbox.Trigger>
</div>
<Lightbox
id="fern-cabin"
label="Photos of Fern Cabin"
photos={photos.map((photo) => ({ src: photo.src, alt: photo.alt, caption: photo.caption }))}
/>Installation
An ordinary dependency: install it, import the theme once, and import the component where it is used.
npm add @sdxc/ui remiximport "@sdxc/ui/theme.css";Or copy it into your project to edit it:
sdxc ui add lightboxUsage
import { Lightbox } from "@sdxc/ui";Renders the viewer: a full-screen dark <dialog> with a close button, one slide per photo
(contained, with "n / N" and its caption) and step links between neighbours. Opened from a
Lightbox.Trigger; Escape and the close button dismiss it.
Composition
The parts the component publishes, each a static property of the host.
LightboxLightbox.TriggerRenders a button that opens the viewer named bylightboxas a modal, at photoindexonce the island has loaded.
Examples
<Lightbox id="cabin-photos" label="Photos" photos={photos} initial={Number(url.searchParams.get("photo")) - 1} />Props
Read from the component's own types, so every prop, every default and every allowed value is listed.
| Prop | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Targeted by each trigger's commandfor, and the prefix of every photo's own id. |
label | string | Yes | The gallery's name, such as "Photos of Fern Cabin", read when the viewer opens. |
photos | Photo[] | Yes | |
initial | number | No | Renders the viewer already open at this zero-based photo, for a page whose address names one (?photo=3). Without script it is a non-modal dialog covering the page; the island reopens it as a modal. |
labels | Partial<Labels> | No |
Also accepts everything in Omit<TagProps<"dialog">, "children" | "open">.
Lightbox.Trigger
| Prop | Type | Required | Description |
|---|---|---|---|
lightbox | string | Yes | id of the Lightbox this opens. |
index | number | No | Zero-based photo the viewer opens at once the island has loaded. |
Also accepts everything in Omit<TagProps<"button">, "commandfor" | "command">.