Enhancement
Lightbox
Image gallery viewer shown in a native modal dialog
- Import
@parallelogram-js/core/components/Lightbox- Selector
[data-lightbox]- Stylesheet
@parallelogram-js/core/styles/lightbox.css
Playground
Gallery
<div class="gallery">
<a href="images/harbour-1280.jpg" data-lightbox="harbour">
<img src="images/harbour-640.jpg" width="320" height="200" alt="Harbour at dawn">
</a>
<a href="images/terrace-1280.jpg" data-lightbox="harbour">
<img src="images/terrace-640.jpg" width="320" height="200" alt="Terrace tables at dusk">
</a>
<a href="images/kitchen-1280.jpg" data-lightbox="harbour">
<img src="images/kitchen-640.jpg" width="320" height="200" alt="The open kitchen">
</a>
</div>
Use the example to see the events it sends.
About
Links that share a data-lightbox gallery name open one viewer. It is named "Image viewer", takes focus on its close button, keeps the rest of the page inert and returns focus to the link when it closes. The arrow keys move between images, Escape and the backdrop close it, the counter is announced as it changes, and slides are skipped when the user prefers reduced motion.
Each link's href is the full-size image, so without JavaScript the links still open the images.
Usage
Register the component and the framework loads it the first time a page contains [data-lightbox].
import { Parallelogram } from '@parallelogram-js/core';
const app = Parallelogram.create();
app.components.add('[data-lightbox]', () => import('@parallelogram-js/core/components/Lightbox'));
app.run();
The framework is optional. Lightbox.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:
import Lightbox from '@parallelogram-js/core/components/Lightbox';
Lightbox.enhanceAll();
It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.
Its styles come from the package stylesheet:
@import '@parallelogram-js/core/styles/lightbox.css';
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
data-lightboxrequired | string | none | The gallery name; links with the same name browse together On a link to the full-size image. |
data-lightbox-srcset | string | none | A source set for the full-size image |
data-lightbox-sizes | string | none | Sizes for the source set (default 100vw) |
data-lightbox-close-escape | boolean | true | Close with Escape. When false, a second Escape without a click or other key press in between still closes the viewer, because browsers don't let a page keep a modal dialog open against it For every instance, Lightbox.defaults.closeOnEscape. |
data-lightbox-close-backdrop | boolean | true | Close when the backdrop is clicked For every instance, Lightbox.defaults.closeOnBackdrop. |
data-lightbox-show-counter | boolean | true | Show the image count, such as 2 of 5 For every instance, Lightbox.defaults.showCounter. |
data-lightbox-show-nav | boolean | true | Show the previous and next buttons For every instance, Lightbox.defaults.showNavigation. |
data-lightbox-key-nav | boolean | true | Move between images with the arrow keys For every instance, Lightbox.defaults.keyNavigation. |
data-lightbox-directional-transitions | boolean | true | Slide images in the direction of travel For every instance, Lightbox.defaults.useDirectionalTransitions. |
data-lightbox-preload | adjacent | all | none | adjacent | Which images to load ahead of time For every instance, Lightbox.defaults.preloadStrategy. |
data-lightbox-viewer-label | string | Image viewer | The accessible name of the viewer For every instance, Lightbox.defaults.viewerLabel. |
data-lightbox-close-label | string | Close | The accessible name of the close button For every instance, Lightbox.defaults.closeLabel. |
data-lightbox-prev-label | string | Previous image | The accessible name of the previous button For every instance, Lightbox.defaults.prevLabel. |
data-lightbox-next-label | string | Next image | The accessible name of the next button For every instance, Lightbox.defaults.nextLabel. |
data-lightbox-load-error | string | {image} couldn't be loaded | Shown in place of an image that fails to load, with {image} for the thumbnail's alt text For every instance, Lightbox.defaults.loadError. |
data-lightbox-load-error-untitled | string | The image couldn't be loaded | The same message for an image whose thumbnail has no alt text For every instance, Lightbox.defaults.loadErrorUntitled. |
data-lightbox-base-class | string | lightbox | Class on the viewer For every instance, Lightbox.defaults.baseClass. |
data-lightbox-overlay-class | string | lightbox__overlay | Class on the dialog For every instance, Lightbox.defaults.overlayClass. |
data-lightbox-container-class | string | lightbox__container | Class on the inner container For every instance, Lightbox.defaults.containerClass. |
data-lightbox-close-class | string | lightbox__close | Class on the close button For every instance, Lightbox.defaults.closeClass. |
data-lightbox-prev-class | string | lightbox__nav lightbox__nav--prev | Classes on the previous button For every instance, Lightbox.defaults.prevClass. |
data-lightbox-next-class | string | lightbox__nav lightbox__nav--next | Classes on the next button For every instance, Lightbox.defaults.nextClass. |
data-lightbox-content-class | string | lightbox__content | Class on the image area For every instance, Lightbox.defaults.contentClass. |
data-lightbox-image-class | string | lightbox__image | Class on the image For every instance, Lightbox.defaults.imageClass. |
data-lightbox-counter-class | string | lightbox__counter | Class on the counter For every instance, Lightbox.defaults.counterClass. |
data-lightbox-state-opening-class | string | is-opening | Class on the viewer while opening For every instance, Lightbox.defaults.stateOpeningClass. |
data-lightbox-state-open-class | string | is-open | Class on the viewer while open For every instance, Lightbox.defaults.stateOpenClass. |
data-lightbox-state-transitioning-class | string | is-transitioning | Class on the viewer while changing image For every instance, Lightbox.defaults.stateTransitioningClass. |
data-lightbox-state-closing-class | string | is-closing | Class on the viewer while closing For every instance, Lightbox.defaults.stateClosingClass. |
data-lightbox-show-class | string | show | Class on a shown element For every instance, Lightbox.defaults.showClass. |
data-lightbox-slide-left-class | string | slide-left | Class for a slide to the left For every instance, Lightbox.defaults.slideLeftClass. |
data-lightbox-slide-right-class | string | slide-right | Class for a slide to the right For every instance, Lightbox.defaults.slideRightClass. |
data-lightbox-stateset by the component | closed | opening | open | transitioning | closing | none | The viewer's state On the viewer. |
Events
| Name | Detail | Description |
|---|---|---|
lightbox:mounted | { element: HTMLElement; gallery: string } | A gallery link was set up Event bus |
lightbox:opened | { gallery: string; index: number; total: number } | The viewer opened Event bus |
lightbox:closed | Record<string, never> | The viewer closed Event bus |
lightbox:stateChange | { element: HTMLElement; oldState: string; newState: string; gallery: string } | The viewer's state changed Event bus |
CSS custom properties
| Name | Default | Description |
|---|---|---|
--lightbox-transition-duration | none | Length of the open, close and slide animations |
--lightbox-transition-easing | none | Easing of the animations |
--lightbox-overlay-bg | rgba(0, 0, 0, 0.9) | Background of the viewer, dark in both themes so it doesn't follow the page |
--lightbox-color | white | Colour of the buttons, counter, error message and focus outlines |
--lightbox-close-bg | transparent | Close button background |
--lightbox-close-hover-bg | transparent | Close button background on hover |
--lightbox-nav-bg | rgba(0, 0, 0, 0.5) | Previous and next button background |
--lightbox-nav-hover-bg | rgba(0, 0, 0, 0.7) | Previous and next button background on hover |
--lightbox-counter-bg | rgba(0, 0, 0, 0.7) | Counter background |