Skip to content
Focsd homeFocsd homeFocsd
Forms

Lightbox

A gallery of thumbnails that opens each picture in a full-size viewer with previous, next and close.

  • Tag: focsd-lightbox
  • Pattern: Modal dialog + image gallery
  • Category: Overlays

Example

A lake under a pale sky
Lake at dawn
Green hills in summer
Hills near the coast
A road into the hills Sand dunes at noon
HTML
<focsd-lightbox class="full" label="Landscapes">
<div class="row">
<figure>
<a href="/media/lake.svg"
><img
src="/media/lake.svg"
alt="A lake under a pale sky"
width="160"
height="120"
/></a>
<figcaption>Lake at dawn</figcaption>
</figure>
<figure>
<a href="/media/hill.svg"
><img src="/media/hill.svg" alt="Green hills in summer" width="160" height="120"
/></a>
<figcaption>Hills near the coast</figcaption>
</figure>
<a href="/media/road.svg"
><img src="/media/road.svg" alt="A road into the hills" width="160" height="120"
/></a>
<a href="/media/dunes.svg"
><img src="/media/dunes.svg" alt="Sand dunes at noon" width="160" height="120"
/></a>
</div>
</focsd-lightbox>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-lightbox.js';
</script>
<focsd-lightbox class="full" label="Landscapes">…</focsd-lightbox>

API

Attributes

AttributePropertyTypeDefault
openopenbooleanfalse
indexindexnumber0
labellabelstring—

Methods

MethodDescription
show(index, opener)Open the viewer at index (default: the current one). Focus returns to opener on close (default: that image's thumbnail).
close()Close the viewer.
next()Show the next image (wraps).
previous()Show the previous image (wraps).

Events

EventDescription
closeFired after the viewer closes.

Slots

SlotDescription
(default)The thumbnails: links to the full images (<a href><img alt></a>, optionally in a figure with a figcaption).

CSS parts

Style the inside of the component with ::part(), for example focsd-lightbox::part(thumbs).

PartDescription
thumbsThe wrapper of the thumbnails.
dialogThe viewer dialog.
figureThe figure that holds the image and its caption.
imageThe full image.
captionThe caption (the `figcaption`, else the alt).
counter"Image 3 of 8".
controlsThe row of buttons.
buttonEach button.
prevThe previous image button.
nextThe next image button.
closeThe close button.

Accessibility

  • The thumbnails are ordinary links to the full-size pictures, so without JavaScript each one opens its picture. A click with a modifier key, or a middle click, keeps its normal meaning.
  • The viewer is a native modal dialog named by label (default "Image viewer"): the page behind is inert and Escape closes it. Focus returns to the thumbnail that opened it.
  • Focus starts on Next (or on Close when there is one picture), so the arrow keys and Enter work at once.
  • Each change is announced politely, for example "Image 2 of 4, Hills near the coast". The counter and the caption describe the dialog.
  • Every picture needs alt text: the viewer shows it, and a missing one is warned about. Captions come from a figcaption.
  • Previous and Next wrap at the ends, so the counter tells you where you are. The slide fades in by opacity only, and not at all under reduced motion.
  • Swipe horizontally on a touch screen (at least 40 px) to move between pictures. The buttons are 44 px targets and the chevrons mirror in right-to-left text.

Keyboard

KeyAction
EnterOpen the viewer from a thumbnail
Arrow Left / RightPrevious / next picture (swapped in right-to-left text)
Home / EndFirst / last picture
EscapeClose and return to the thumbnail