# Lightbox (`<z-lightbox>`)

> Media viewer overlay for images, videos, YouTube/Vimeo embeds, and iframes, collected from links, with zoom, swipe, captions, and dot/thumb navigation.

- **Kind:** hwc
- **Category:** overlays
- **Inherits:** `DialogMixin`, `Hwc`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `target` | `string` | `'a[href]'` | — | Selector for trigger elements inside the host (default `a[href]`); each match becomes one lightbox item. |
| `active` | `number` | `0` | — | Index of the item shown when opening; setting it while open navigates to that item. |
| `animation` | `'slide' \| 'fade' \| 'scale'` | `'slide'` | — | Item transition: `slide` (default), `fade`, or `scale`. |
| `finite` | `boolean` | `false` | — | Stops at the first/last item instead of wrapping. |
| `no-counter` | `boolean` | `false` | — | Hides the `current / total` counter. |
| `nav` | `'' \| 'dotnav' \| 'thumbnav' \| 'none' \| 'false'` | `'dotnav'` | — | Navigation style: `dotnav` (default), `thumbnav`, or `none`/`false`/empty to hide it. |
| `no-slidenav` | `boolean` | `false` | — | Hides the previous/next arrow buttons. |
| `no-zoom` | `boolean` | `false` | — | Disables the zoom toggle for images. |
| `no-swipe` | `boolean` | `false` | — | Disables swipe navigation between items. |
| `delay-controls` | `number` | `3000` | — | Milliseconds of inactivity before the toolbar/chrome auto-hides (default 3000); pointer movement reveals it again. |
| `preload` | `number` | `1` | — | Number of neighboring items to preload on each side of the active item (default 1). |
| `video-autoplay` | `boolean` | `false` | — | Autoplays video items when shown. |
| `duration` | `number` | `280` | — | Item transition duration in milliseconds (default 280). |
| `aria-label` | `string` | `'Media viewer'` | — | Accessible name of the viewer overlay (default `Media viewer`). |

### Inherited from `DialogMixin`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean` | `false` | `DialogMixin` | Opens the dialog on first render and stays in sync with `show()` and `hide()`. |
| `center` | `boolean` | `false` | `DialogMixin` | Vertically centers the panel within the overlay. |
| `container` | `boolean` | `false` | `DialogMixin` | Adds `z-modal-container` to the overlay for a contained presentation. |
| `full` | `boolean` | `false` | `DialogMixin` | Adds `z-modal-full` to the overlay for a full-screen presentation. |
| `stack` | `boolean` | `false` | `DialogMixin` | Keeps other open dialogs open; without it, showing this dialog hides them. Stacked overlays receive increasing z-index. |
| `inline` | `boolean` | `false` | `DialogMixin` | Keeps the component in its original DOM position instead of teleporting it to `document.body`. |
| `no-bg-close` | `boolean` | `false` | `DialogMixin` | Disables closing when the overlay background is clicked. |
| `no-esc-close` | `boolean` | `false` | `DialogMixin` | Disables closing with the Esc key. |
| `unmount` | `boolean` | `false` | `DialogMixin` | Removes the rendered DOM after the close transition; the next show mounts it again. |
| `dialog-role` | `'dialog' \| 'alertdialog'` | `'dialog'` | `DialogMixin` | ARIA role of the panel: `dialog` or `alertdialog`. |
| `aria-labelledby` | `string` | `''` | `DialogMixin` | Id of the element that names the panel; takes precedence over `aria-label`. |

### Inherited from `Hwc`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `i18n` | `string` | `''` | `Hwc` | Per-instance text overrides as `key: text` pairs, merged over the global `z-i18n` script and the built-in defaults. |
| `floating` | `string` | `''` | `Hwc` | Floating UI placement options (for example `offset` and `shiftPadding`) parsed as `key: value` pairs. |
| `force-prevent-rerender` | `boolean` | `false` | `Hwc` | Skips re-rendering after the first render; an existing `data-host-inner` child is adopted as the rendered DOM. |
| `template` | `string` | `''` | `Hwc` | Selector or id of an external element or `<template>` whose inner content is cloned in place of an inline `template[data-fn="template"]` child. |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `overlay`. Default `stl` target: `overlay`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `actions` | `z-lightbox-toolbar-actions` | ✓ | ✓ | Toolbar action group (default `z-lightbox-toolbar-actions`). |
| `caption` | `z-lightbox-caption` | ✓ | ✓ | Caption text under the active item (default `z-lightbox-caption`). |
| `close` | `z-slidenav z-lightbox-close` | ✓ | ✓ | Close button (default `z-slidenav z-lightbox-close`). |
| `counter` | `z-lightbox-counter` | ✓ | ✓ | The `current / total` counter (default `z-lightbox-counter`). |
| `dotnav` | `z-lightbox-dotnav z-dotnav` | ✓ | ✓ | Dot navigation (default `z-lightbox-dotnav z-dotnav`). |
| `footer` | `z-lightbox-footer z-lightbox-chrome` | ✓ | ✓ | Bottom chrome holding the caption (default `z-lightbox-footer z-lightbox-chrome`). |
| `items` | `z-lightbox-items` | ✓ | ✓ | The deck holding the item slides (default `z-lightbox-items`). |
| `next` | `z-slidenav z-lightbox-slidenav z-position-center-right z-position-small z-lightbox-chrome` | ✓ | ✓ | Next arrow (default `z-slidenav z-lightbox-slidenav z-position-center-right z-position-small z-lightbox-chrome`). |
| `overlay` | `z-lightbox` | ✓ | ✓ | The lightbox overlay root (default `z-lightbox`); the default `cls`/`stl` target. |
| `prev` | `z-slidenav z-lightbox-slidenav z-position-center-left z-position-small z-lightbox-chrome` | ✓ | ✓ | Previous arrow (default `z-slidenav z-lightbox-slidenav z-position-center-left z-position-small z-lightbox-chrome`). |
| `thumbnav` | `z-lightbox-thumbnav z-thumbnav` | ✓ | ✓ | Thumbnail navigation (default `z-lightbox-thumbnav z-thumbnav`). |
| `toolbar` | `z-lightbox-toolbar z-lightbox-chrome` | ✓ | ✓ | Top toolbar with counter and actions (default `z-lightbox-toolbar z-lightbox-chrome`). |
| `zoom` | `z-slidenav z-lightbox-zoom-btn` | ✓ | ✓ | Zoom toggle button (default `z-slidenav z-lightbox-zoom-btn`). |

## CSS Variables

| Variable | Default | Scope | Affects | Description |
| --- | --- | --- | --- | --- |
| `--z-lightbox-bg` | `var(--z-bg)` | `.z-lightbox` | `background-color` | Overlay base color, mixed with transparency using `--z-lightbox-bg-o`. |
| `--z-lightbox-bg-o` | `95%` | `.z-lightbox` | `background-color` | Overlay opacity percentage used in the background mix. |
| `--z-lightbox-blur` | `var(--z-global-blur)` | `.z-lightbox` | `-webkit-backdrop-filter`, `backdrop-filter` | Overlay backdrop blur radius. |
| `--z-lightbox-caption-bg` | `var(--z-bg)` | `.z-lightbox-caption` | `background-color` | Caption background base color, mixed with transparency using `--z-lightbox-caption-bg-o`. |
| `--z-lightbox-caption-bg-o` | `80%` | `.z-lightbox-caption` | `background-color` | Caption background opacity percentage used in the mix. |
| `--z-lightbox-caption-color` | `var(--z-bg-f)` | `.z-lightbox-caption` | `color` | Caption text base color, mixed with transparency using `--z-lightbox-caption-color-o`. |
| `--z-lightbox-caption-color-o` | `100%` | `.z-lightbox-caption` | `color` | Caption text opacity percentage used in the mix. |
| `--z-lightbox-caption-padding` | `0.625rem 1rem` | `.z-lightbox-caption` | `padding` | Caption padding. |
| `--z-lightbox-iframe-height` | `80%` | `.z-lightbox-iframe` | `height` | Height of iframe/embed items. |
| `--z-lightbox-iframe-width` | `80%` | `.z-lightbox-iframe` | `width` | Width of iframe/embed items. |
| `--z-lightbox-thumb-max-height` | `100px` | `.z-lightbox-thumbnav:not(.z-lightbox-thumbnav-vertical) :where(img, video)` | `max-height` | Maximum thumbnail height in thumbnav. |
| `--z-lightbox-thumb-max-width` | `100px` | `.z-lightbox-thumbnav-vertical :where(img, video)` | `max-width` | Maximum thumbnail width in thumbnav. |
| `--z-lightbox-transition` | `opacity 150ms cubic-bezier(0.16, 1, 0.3, 1)` | `.z-lightbox` | `transition` | Transition timing of the viewer chrome. |
| `--z-lightbox-z-index` | `1010` | `.z-lightbox` | `z-index` | Stacking order of the viewer. |
| `--z-lightbox-zoom-scale` | `1` | `.z-lightbox-items > .z-lightbox-zoomed > img` | `transform` | Scale applied to a zoomed image. |

## i18n Keys

| Key | Default | Description |
| --- | --- | --- |
| `close` | `Close` | Label of the close button. |
| `counter` | `{current} / {total}` | Counter template with `{current}` and `{total}` placeholders. |
| `error` | `Unable to load media.` | Message shown when an item fails to load. |
| `next` | `Next` | Label of the next arrow. |
| `previous` | `Previous` | Label of the previous arrow. |
| `slideLabel` | `Item {index} of {total}` | Accessible slide label template with `{index}` and `{total}` placeholders. |
| `zoomIn` | `Zoom in` | Label of the zoom toggle when zooming in. |
| `zoomOut` | `Zoom out` | Label of the zoom toggle when zooming out. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-lightbox:before-hide` | ✓ | — | Fires before closing; call `preventDefault()` to abort. |
| `z-lightbox:before-item-change` | ✓ | `{ index: number; previousIndex: number; item: LightboxItem; dir: 1 \| -1 }` | Cancelable; fires before navigating to another item. Detail: `index`, `previousIndex`, `item`, `dir`. |
| `z-lightbox:before-show` | ✓ | — | Fires before opening; call `preventDefault()` to abort. |
| `z-lightbox:destroy` | — | — | Fires when the mounted DOM is removed, via `unmount` or `destroy()`. |
| `z-lightbox:hidden` | — | — | Fires after the close transition completes and focus is restored to the opener. |
| `z-lightbox:hide` | — | — | Fires when the dialog starts closing. |
| `z-lightbox:item-change` | — | `Record<string, unknown>` | Fires when the active item changes, with the same detail as `before-item-change`. |
| `z-lightbox:item-error` | — | `{ index: number; item: LightboxItem }` | Fires when an item's media fails to load. Detail: `index`, `item`. |
| `z-lightbox:item-load` | — | `{ index: number; item: LightboxItem }` | Fires when an item's media finishes loading. Detail: `index`, `item`. |
| `z-lightbox:item-shown` | — | `Record<string, unknown>` | Fires after the item transition completes, with the same detail as `before-item-change`. |
| `z-lightbox:mount` | — | — | Fires once when the dialog DOM is first mounted. |
| `z-lightbox:show` | — | — | Fires when the dialog starts opening, before the transition completes. |
| `z-lightbox:shown` | — | — | Fires after the open transition completes. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `destroy()` | `removeHost?: boolean = false` | `Promise<void>` | Closes and unmounts the dialog DOM; optionally removes the host element as well. |
| `goTo()` | `target: number \| 'next' \| 'previous' \| 'first' \| 'last'`, `animate?: boolean = true` | `Promise<boolean>` | Navigates to an index (wraps unless `finite`); resolves to whether the item changed. |
| `hide()` | — | `Promise<void>` | Closes the dialog and restores focus to the opener. |
| `index()` | — | `number` | Returns the current item index. |
| `next()` | `animate?: boolean = true` | `Promise<boolean>` | Advances to the next item. |
| `previous()` | `animate?: boolean = true` | `Promise<boolean>` | Moves to the previous item. |
| `refresh()` | — | `void` | Re-collects items from the trigger elements. |
| `show()` | `target?: number \| HTMLElement \| null` | `Promise<void>` | Opens the dialog; the trigger element receives focus back on close. |
| `toggle()` | `trigger?: HTMLElement \| null` | `Promise<void>` | Opens the dialog if closed, otherwise closes it. |
| `toggleZoom()` | — | `void` | Toggles zoom on the active image item. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-alt` | — | `descendant` | `config` | On a trigger: alt text for the image item. |
| `data-caption` | — | `descendant` | `config` | On a trigger: caption shown under the item. |
| `data-close` | — | `external-trigger` | `trigger` | Clicking the element closes the dialog matched by the selector (usually `#id`). |
| `data-host-inner` | — | `descendant` | `config` | Marks the rendered inner wrapper so `force-prevent-rerender` can adopt server-rendered DOM without re-rendering. |
| `data-lightbox-ignore` | — | `descendant` | `config` | Excludes a trigger (or subtree) from item collection. |
| `data-lightbox-slide` | — | `descendant` | `marker` | Set by the component on each slide to its index. |
| `data-open` | — | `external-trigger` | `trigger` | Clicking the element opens the dialog matched by the selector without toggling. |
| `data-part` | `items`, `overlay` | `descendant` | `marker` | Marks the overlay and items deck in the rendered DOM (`data-part="overlay"` / `data-part="items"`). |
| `data-poster` | — | `descendant` | `config` | On a trigger: poster image for a video item. |
| `data-reactive` | — | `descendant` | `config` | Opts the marked child (config script, data source, icons, or template) into live re-parsing when its DOM mutates. |
| `data-sizes` | — | `descendant` | `config` | On a trigger: `sizes` attribute for the responsive image. |
| `data-source` | `#`, `#!` | `descendant` | `trigger` | On a trigger: explicit media URL when the href is a placeholder (`#`/`#!`) or differs from the media. |
| `data-srcset` | — | `descendant` | `config` | On a trigger: `srcset` attribute for the responsive image. |
| `data-thumb` | — | `descendant` | `config` | On a trigger: thumbnail URL for thumbnav. |
| `data-toggle` | — | `external-trigger` | `trigger` | Clicking the element toggles the dialog matched by the selector (usually `#id`). |
| `data-type` | `iframe`, `image`, `video`, `vimeo`, `youtube` | `descendant` | `trigger` | On a trigger: media type override — `image`, `video`, `youtube`, `vimeo`, or `iframe`; otherwise inferred from the URL. |

## Child Content

- `<script data-fn="config">` — JSON configuration script parsed by the base class (i18n, cls, stl, floating).

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `config` | Marks the script as configuration (value: `config`). |
  | `data-reactive` | — | `boolean` | Re-parses the configuration when the script mutates. |

- `<select data-fn="data-source">` — Hidden select parsed into grouped option data by the base class.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `data-source` | Marks the select as the data source (value: `data-source`). |
  | `data-reactive` | — | `boolean` | Re-parses the options when the select mutates. |

  - `<optgroup>` — Groups options under a label.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `label` | ✓ | `string` | Group header text; also the default group key. |
    | `disabled` | — | `boolean` | Disables every option in the group. |
    | `data-key` | — | `string` | Overrides the group key used in the parsed data. |
    | `data-keywords` | — | `comma-separated string` | Comma-separated keywords stored with the group's data. |

    - `<option>` — One option entry within the group.

      | Attribute | Required | Type | Description |
      | --- | --- | --- | --- |
      | `value` | — | `string` | Value carried by this option in the parsed data. |
      | `disabled` | — | `boolean` | Makes the option non-selectable. |
      | `selected` | — | `boolean` | Marks the option as initially selected. |
      | `data-keywords` | — | `comma-separated string` | Extra search terms, comma-separated; searched alongside the label and value. |

  - `<option>` — One ungrouped option entry.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `value` | — | `string` | Value carried by this option in the parsed data. |
    | `disabled` | — | `boolean` | Makes the option non-selectable. |
    | `selected` | — | `boolean` | Marks the option as initially selected. |
    | `data-keywords` | — | `comma-separated string` | Extra search terms, comma-separated. |

- `<template data-fn="icons">` — Instance-level icon repository.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `icons` | Marks the template as an icon source (value: `icons`). |
  | `data-reactive` | — | `boolean` | Re-parses the icons when the template mutates. |

  - `<*>` — One icon element.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `data-key` | ✓ | `string` | Icon key referenced by `$icons`. |

- `<template data-fn="template">` — Deferred content template mounted by the base class.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `template` | Marks the template for deferred mounting (value: `template`). |
  | `data-reactive` | — | `boolean` | Re-parses the template when it mutates. |

## CSS Classes

| Class | Role | Applies to | Description |
| --- | --- | --- | --- |
| `.z-lightbox` | `root` | `z-lightbox` | Overlay root: fixed, dimmed, blurred viewer. |
| `.z-lightbox-caption` | `state` | `.z-lightbox` | Caption text styling. |
| `.z-lightbox-chrome` | `element` | `.z-lightbox` | Shared chrome class faded out when controls auto-hide. |
| `.z-lightbox-controls-hidden` | `element` | `.z-lightbox` | State class while the chrome is hidden. |
| `.z-lightbox-counter` | `state` | `.z-lightbox` | The item counter. |
| `.z-lightbox-dotnav` | `element` | `.z-lightbox` | Dot navigation list. |
| `.z-lightbox-footer` | `element` | `.z-lightbox` | Bottom chrome holding the caption. |
| `.z-lightbox-iframe` | `element` | `.z-lightbox` | Sizing frame for iframe/embed items. |
| `.z-lightbox-items` | `state` | `.z-lightbox` | The deck holding the item slides. |
| `.z-lightbox-items-fit` | `modifier` | `.z-lightbox` | Fit mode sizing the deck to the media. |
| `.z-lightbox-loader` | `element` | `.z-lightbox` | Loading indicator shown while media loads. |
| `.z-lightbox-page` | `element` | `.z-lightbox` | Page root class clipping scroll while the viewer is open. |
| `.z-lightbox-slidenav` | `element` | `.z-lightbox` | Previous/next arrow buttons. |
| `.z-lightbox-thumbnav` | `element` | `.z-lightbox` | Thumbnail navigation list. |
| `.z-lightbox-thumbnav-vertical` | `element` | `.z-lightbox` | Vertical thumbnail navigation. |
| `.z-lightbox-toolbar` | `element` | `.z-lightbox` | Top toolbar chrome. |
| `.z-lightbox-toolbar-actions` | `state` | `.z-lightbox` | Toolbar action group. |
| `.z-lightbox-zoom` | `modifier` | `.z-lightbox` | Zoom affordance styling. |
| `.z-lightbox-zoomed` | `element` | `.z-lightbox` | State class while an image is zoomed. |

## Keyboard

| Keys | Action | When |
| --- | --- | --- |
| `ArrowLeft` / `ArrowRight` | Previous/next item (direction flips in RTL). | — |
| `Escape` | Closes the viewer (or exits zoom first). | — |

## Accessibility

| Part | Attributes | Description |
| --- | --- | --- |
| `actions` | — | Toolbar action group; no static ARIA attributes. |
| `caption` | — | Caption text; no static ARIA attributes. |
| `close` | `aria-label="${closeLabel}"` | Close button labeled by the close i18n text. |
| `counter` | `aria-live="polite"` | Item counter with `aria-live="polite"`. |
| `dotnav` | `role="tablist"` | Dot navigation with `role="tablist"`. |
| `footer` | — | Bottom chrome; no static ARIA attributes. |
| `items` | — | Slide deck; no static ARIA attributes (slides are labeled individually). |
| `next` | `aria-disabled="${atLast ? 'true' : 'false'}"`, `aria-label="${nextLabel}"` | Next arrow labeled by the next i18n text; `aria-disabled` at the last item when `finite`. |
| `overlay` | `aria-label="${this.ariaLabelText \|\| 'Media viewer'}"`, `aria-modal="true"`, `role="dialog"` | Viewer overlay: `role="dialog"`, `aria-modal="true"`, labeled by `aria-label` (default `Media viewer`). |
| `prev` | `aria-disabled="${atFirst ? 'true' : 'false'}"`, `aria-label="${prevLabel}"` | Previous arrow labeled by the previous i18n text; `aria-disabled` at the first item when `finite`. |
| `thumbnav` | `role="tablist"` | Thumbnail navigation with `role="tablist"`. |
| `toolbar` | — | Top toolbar; no static ARIA attributes. |
| `zoom` | `aria-label="${zoomLabel}"`, `aria-pressed="${this.isZoomed ? 'true' : 'false'}"` | Zoom toggle labeled by the zoomIn/zoomOut i18n text; `aria-pressed` reflects the zoom state. |

> Triggers are kept button-accessible and linked to the viewer; slides are labeled with the `slideLabel` template.

> The viewer renders into a portal appended to `document.body`; the host stays in place as the trigger container.

## Notes

- Items are collected from the `target` matches: href (or `data-source`) plus optional `data-type`, `data-caption`, `data-alt`, `data-poster`, `data-thumb`, `data-srcset`, `data-sizes`.
- YouTube/Vimeo URLs are converted to privacy-friendly embed URLs; unknown URLs fall back to iframe.
- The viewer defaults to `inline` + `unmount`: it renders in a portal and tears down after closing.
- Zoom applies to loaded images; swipe and arrow navigation wrap unless `finite` is set.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)

## Related

- [Dialog](https://next.0build.dev/docs/components/dialog.md)
- [Slider](https://next.0build.dev/docs/components/slider.md)
