Color mode

Lightbox

Click-to-zoom lightbox for images.

日本語

Overview

Two parts:

  • Build (rehype): wraps each rendered <img> in a trigger anchor
  • Client: opens an accessible dialog on click

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { lightboxPlugin } from "@riebeckite/plugin-lightbox";
 
export default defineConfig({
  // ...
  plugins: [lightboxPlugin()],
});

The plugin registers style.css and a client entry (initLightbox), which the app calls on page initialization.

Behavior

Build (rehypeLightbox)

  • Wraps every <img src> in <a class="rr-lightbox-trigger"> with data-lightbox-src, data-lightbox-alt, and an aria-label
  • Adds rr-lightbox-image to the image
  • Skips images that have data-lightbox-ignore="true", and images already inside an <a>, <button>, existing trigger, or the dialog
  • Uses expandLabel (default "Expand image") for the trigger and dialog accessible labels

Client (initLightbox)

  • Optionally wraps remaining img[src] that were not converted at build time (autoWrapImages, default on; images inside links/buttons are skipped)
  • Creates a role="dialog" overlay with the image, alt caption, and close button
  • Closes on Escape, backdrop click, or the close button
  • Restores focus to the previously focused element
  • Sets html[data-lightbox-open="true"] while open (locks scrolling via CSS)
  • Returns a cleanup function that removes listeners, the dialog, and any wrapped images

Options

Option Type Default Description
selectorClass string "rr-lightbox-trigger" Trigger class (build and client)
expandLabel string "Expand image" Accessible label for triggers and the dialog
closeLabel string "Close" Accessible label for the dialog close button
autoWrapImages boolean true Client-only: wrap unhandled images on init

LightboxOptions = { selectorClass?, expandLabel?, closeLabel? } (build), LightboxInitOptions = LightboxOptions & { autoWrapImages? } (client).

Exports

  • lightboxPlugin(options?) — plugin factory
  • rehypeLightbox(options?) — rehype transform
  • initLightbox(root?, options?) — client initializer, returns a cleanup function
  • initLightboxFromOptions(options?) — option-first browser entry the plugin's client script calls; wraps initLightbox
  • Types: LightboxOptions, LightboxInitOptions

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/lightbox/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Lightbox
4 +
5 + Click-to-zoom lightbox for images.
6 +
7 + [日本語](./lightbox.md)
8 +
9 + ## Overview
10 +
11 + Two parts:
12 +
13 + - **Build (rehype):** wraps each rendered `<img>` in a trigger anchor
14 + - **Client:** opens an accessible dialog on click
15 +
16 + ## Usage
17 +
18 + ```ts
19 + import { defineConfig } from "@riebeckite/core";
20 + import { lightboxPlugin } from "@riebeckite/plugin-lightbox";
21 +
22 + export default defineConfig({
23 + // ...
24 + plugins: [lightboxPlugin()],
25 + });
26 + ```
27 +
28 + The plugin registers `style.css` and a client entry (`initLightbox`), which
29 + the app calls on page initialization.
30 +
31 + ## Behavior
32 +
33 + ### Build (`rehypeLightbox`)
34 +
35 + - Wraps every `<img src>` in
36 + `<a class="rr-lightbox-trigger">` with `data-lightbox-src`,
37 + `data-lightbox-alt`, and an `aria-label`
38 + - Adds `rr-lightbox-image` to the image
39 + - Skips images that have `data-lightbox-ignore="true"`, and images already
40 + inside an `<a>`, `<button>`, existing trigger, or the dialog
41 + - Uses `expandLabel` (default `"Expand image"`) for the trigger and dialog
42 + accessible labels
43 +
44 + ### Client (`initLightbox`)
45 +
46 + - Optionally wraps remaining `img[src]` that were not converted at build time
47 + (`autoWrapImages`, default on; images inside links/buttons are skipped)
48 + - Creates a `role="dialog"` overlay with the image, `alt` caption, and close
49 + button
50 + - Closes on `Escape`, backdrop click, or the close button
51 + - Restores focus to the previously focused element
52 + - Sets `html[data-lightbox-open="true"]` while open (locks scrolling via CSS)
53 + - Returns a cleanup function that removes listeners, the dialog, and any
54 + wrapped images
55 +
56 + ## Options
57 +
58 + | Option | Type | Default | Description |
59 + | ------ | ---- | ------- | ----------- |
60 + | `selectorClass` | `string` | `"rr-lightbox-trigger"` | Trigger class (build and client) |
61 + | `expandLabel` | `string` | `"Expand image"` | Accessible label for triggers and the dialog |
62 + | `closeLabel` | `string` | `"Close"` | Accessible label for the dialog close button |
63 + | `autoWrapImages` | `boolean` | `true` | Client-only: wrap unhandled images on init |
64 +
65 + `LightboxOptions` = `{ selectorClass?, expandLabel?, closeLabel? }` (build),
66 + `LightboxInitOptions` = `LightboxOptions & { autoWrapImages? }` (client).
67 +
68 + ## Exports
69 +
70 + - `lightboxPlugin(options?)` — plugin factory
71 + - `rehypeLightbox(options?)` — rehype transform
72 + - `initLightbox(root?, options?)` — client initializer, returns a cleanup
73 + function
74 + - `initLightboxFromOptions(options?)` — option-first browser entry the plugin's
75 + client script calls; wraps `initLightbox`
76 + - Types: `LightboxOptions`, `LightboxInitOptions`
77 +
78 + ## See also
79 +
80 + - [Plugin guide](../reference/plugin-api.en.md)
81 +