Feedback
Lightbox
A full-screen viewer for images and videos, single or as a gallery, on a native modal dialog.
<script>
import { Lightbox } from "@codeatlasdev/ui";
let open = $state(false);
</script>
<button onclick={() => (open = true)}>View photo</button>
<Lightbox bind:open src={url} alt="Photo sent by Maria" />
Pass items for a gallery of images and videos; src and alt are sugar for a single image.
<Lightbox bind:open bind:index items={[
{ src: photo, alt: "Beach", caption: "Praia", meta: "Maria, 10:42" },
{ type: "video", src: clip, poster, alt: "Short clip" },
]} ondownload={(item, i) => save(item)} />
With more than one item the viewer shows previous and next buttons, a counter, and answers to the ArrowLeft and ArrowRight keys and to a horizontal swipe. It does not wrap: the buttons at either end are aria-disabled and keep focus. Arrow keys are left alone while a video has focus so they keep seeking, and a video is paused as soon as you move to another item. The caption and meta (sender, time) overlay the bottom edge without changing the media box. A Download button appears for galleries: ondownload(item, index) replaces the default <a download> to item.download ?? item.src, which only works for same-origin or CORS-enabled files.
The media is contained in the viewport and respects the device safe areas. Escape, the close button and a click on the backdrop close it. Because it is a native modal <dialog>, the page behind is inert, focus returns to the trigger and scrolling is locked while it is open. alt is required.
Open from a button
Reference #091 · Pick in the gallery
Code
Gallery with video, caption and download
Arrow keys, buttons and swipe move through the items.
Reference #242 · Pick in the gallery
Code
Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | false | Bindable open state. |
src | string | – | Single-image sugar: same as `items={[{ src, alt }]}`. |
alt | string | – | Image description for the `src` form. |
items | LightboxItem[] | – | Gallery. Each item: `{ type?: 'image' | 'video', src, alt?, caption?, meta?, poster?, download? }`. Wins over `src`. |
index | number | 0 | Bindable current item. |
ondownload | (item: LightboxItem, index: number) => void | – | Replaces the default download link. The Download button shows for `items` galleries. |
onclose | () => void | – | Called after the viewer closes. |