A picture in a page opens full size in the shared dialog and shrinks back into place.
<div data-cap="lightbox">
<a class="image-link" style="display:inline-block;max-width:100%" id="sample-photo" href="data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='800' viewBox='0 0 1200 800'%3E%3Crect width='1200' height='800' fill='%23dbe4ee'/%3E%3Ccircle cx='600' cy='400' r='220' fill='%236b8cae'/%3E%3C/svg%3E">
<img src="data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200' viewBox='0 0 1200 800'%3E%3Crect width='1200' height='800' fill='%23dbe4ee'/%3E%3Ccircle cx='600' cy='400' r='220' fill='%236b8cae'/%3E%3C/svg%3E" style="max-width:100%;height:auto" width="300" height="200" alt="A blue disc on a pale ground" />
</a>
</div>
Copy
View code
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/lightbox.css";
import { enhance } from "capsomer/behavior/lightbox";
enhance();Copy
Usage
<article data-cap="lightbox">
<figure>
<a class="image-link" href="/media/cohort-2024.jpg">
<img src="/media/cohort-2024-800.jpg" alt="The 2024 cohort outside the lab" />
</a>
</figure>
<img class="diagram-image" src="/media/pipeline.svg" alt="Reads go to assembly, then annotation." />
</article>Copy
import { enhance } from "capsomer/behavior/lightbox";
enhance(); // every [data-cap="lightbox"]Copy
Examples
A diagram image
<div data-cap="lightbox">
<img class="diagram-image" id="sample-diagram" style="max-width:100%;height:auto" width="300" height="150" alt="Reads go to assembly, then annotation." src="data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300' viewBox='0 0 600 300'%3E%3Crect width='600' height='300' fill='white'/%3E%3Crect x='40' y='110' width='200' height='80' fill='%23dbe4ee' stroke='%23333'/%3E%3Crect x='360' y='110' width='200' height='80' fill='%23dbe4ee' stroke='%23333'/%3E%3Cpath d='M240 150h120' stroke='%23333'/%3E%3C/svg%3E" />
</div>
Copy
API reference
Functions Function Signature Does grow(from: HTMLElement | undefined, to: HTMLElement, change: () => void): voidThe figure grows into the viewer and shrinks back. It carries no transition type, so a site's page-to-page fade does not restyle it. A skipped transition rejects ready with an InvalidStateError (the tab went hidden, or another transition started); the change still ran, so only the animation is dropped. Anything else is rethrown. openViewer(options: { src: string; alt: string; label?: string | undefined; source?: HTMLElement | undefined; opener?: HTMLElement | null | undefined; }): HTMLDialogElementOpens the viewer. src is passed in, not read from currentSrc (whichever srcset rung the browser downloaded): a resized copy would be shown as full size. label names the dialog; an empty alt means decorative, so the dialog then gets a generic name. source is the picture to grow from and shrink back to; opener is where focus returns. closeViewer(dialog: HTMLDialogElement, source?: HTMLElement | undefined): voidCloses a viewer, shrinking its image back into source when there is one. enhance(root?: ParentNode): () => voidWires every [data-cap="lightbox"] under root. Returns a function that detaches them all.
CSS variables Variable Default --cap-dialog-gapset by the page --cap-dialog-wauto
Parts and data attributes Class Is .cap-lightboxthe root
Attribute Values data-caplightbox
Accessibility
Key Does Enter or Space on a diagram image Opens the viewer Tab Moves between the viewer's controls; focus stays inside Esc Closes the viewer; focus returns to the picture's link
The viewer is a modal dialog with an accessible name; the page behind is inert. The Close button is the dialog block's icon button (a target at least 24 px, larger on a coarse pointer) with the name "Close". The picture keeps its alt text. Forced colors: the Close button gains an edge.