Lightbox

An image and video gallery that opens full screen with swipe and pinch to zoom, built on PhotoSwipe

A turquoise mountain lakeClouds mirrored in a mountain lakeSnowy mountains in sunlightA wave breaking on a sandy shoreA wintry evergreen forest from aboveA wet mountain valley

Installation

pnpm dlx shadcn@latest add @alexandrejoly/lightbox

Usage

import {  Lightbox,  LightboxActions,  LightboxClose,  LightboxContent,  LightboxCounter,  LightboxItem,  LightboxNext,  LightboxPrevious,  LightboxZoom,} from "@/components/ui/lightbox";
<Lightbox>  {photos.map((photo) => (    <LightboxItem      height={photo.height}      href={photo.src}      key={photo.src}      width={photo.width}    >      <img alt={photo.alt} src={photo.thumbnail} />    </LightboxItem>  ))}  <LightboxContent>    <LightboxCounter />    <LightboxActions>      <LightboxZoom />      <LightboxClose />    </LightboxActions>    <LightboxPrevious />    <LightboxNext />  </LightboxContent></Lightbox>

Examples

Captions

Put a LightboxItemCaption in each item and a LightboxCaption in the content. The image shrinks to keep the caption in view, right under it.

Video

An item with type="video" opens its file in a video player. Its thumbnail is any image, such as the first frame of the video.

A misty forest on a slopeLandscapes fading into one anotherFoggy woodlands

useLightbox

useLightbox gives the current slide to controls of your own, here a link that downloads it.

Snowy mountains in sunlightA wave breaking on a sandy shoreA wet mountain valley

Accessibility

Each item is a link to the full-size file, so the gallery still works before the script loads or without it. The thumbnail’s alt names the link, and the viewer reuses it for the full-size image: give every thumbnail one. LightboxItemCaption is hidden in the thumbnail, so it does not change the link’s name.

The viewer is a modal: focus moves into it and stays there, the arrow keys move between slides, Z toggles the zoom, and Escape closes it and returns focus to the item that opened it. The buttons carry their names as visually hidden text, and the zoom button’s name switches between Zoom in and Zoom out.

Meets WCAG 2.2: 4.1.2 Name, Role, Value (opens in a new tab)

API reference

The gallery: it finds every LightboxItem inside it and opens the viewer on the one pressed. A grid of thumbnails at least 96px wide, with as many columns as its width holds.

Takes every prop of div.

PropType
options

PhotoSwipe options, read each time the viewer opens, such as loop. PhotoSwipe’s own buttons and counter are off in favour of the parts below, the background takes --background at full opacity, and the mouse wheel zooms (wheelToZoom). The image keeps a 64px frame for the parts, none on the sides below 640px, and a caption below it takes the room it needs; padding or paddingFn replace that frame.

PhotoSwipeOptions

A link to one full-size file, holding its thumbnail. The viewer zooms out of the thumbnail and back into it, cropped and rounded as it is.

Takes every prop of a.

PropTypeDefault
width, height

The full-size file’s width and height in pixels.

numberNone
type

A video plays in the viewer with its controls, from the slide it is on, and pauses when you leave it.

Default: "image"

"image" | "video""image"
srcSet, sizes

Responsive sources for the full-size image.

stringNone

The item’s caption, placed inside its LightboxItem. It stays hidden in the thumbnail and shows in LightboxCaption while its item is open.

Takes every prop of div.

The layer over the open viewer, portaled into it; it renders nothing while the viewer is closed. It stacks its children in one grid cell over the viewer, 16px in from its edges: place each with self-* and justify-self-*, as the parts below do. Pointer events pass through it to the slides and stop on its children. It fades with the viewer’s controls, which a tap on touch screens hides and shows. Its children can read --lightbox-image-bottom, the image’s bottom edge from the top of the cell they share, match data-zoomed while the image is zoomed in and data-animating while the viewer opens and closes, when --lightbox-image-bottom does not follow the image.

Takes every prop of div.

The current slide and the count, centred at the top.

Takes every prop of div.

The open item’s LightboxItemCaption, centred under the image. Its top margin sets the gap to the image and its bottom margin the room kept below it. It follows the image as it is dragged, hides while the image is zoomed in, stays hidden until the viewer has opened and hides at once when it closes, and the image makes room again when its text changes. Items without a caption leave it empty.

Takes every prop of div.

Lines up LightboxZoom and LightboxClose in the top end corner.

Takes every prop of div.

Outline button that zooms the image in and out; put it in LightboxActions beside LightboxClose. Disabled on a video and on an image already shown at full size. An onClick runs before the zoom.

Takes every prop of shadcn Button (opens in a new tab).

Outline button in the top end corner that closes the viewer. An onClick runs before it closes.

Takes every prop of shadcn Button (opens in a new tab).

Outline button on the start side, centred, that moves to the previous slide. It loops from the first slide to the last; with loop off, it is disabled there instead. Hidden below 640px, where you swipe, and with a single item. An onClick runs before the slide changes.

Takes every prop of shadcn Button (opens in a new tab).

Outline button on the end side, centred, that moves to the next slide. It loops from the last slide to the first; with loop off, it is disabled there instead. Hidden below 640px, where you swipe, and with a single item. An onClick runs before the slide changes.

Takes every prop of shadcn Button (opens in a new tab).

useLightbox()

Reads and drives the open viewer from inside LightboxContent, for controls of your own.

Return valueType
index, count

The current slide, from 0, and the slide count, 0 while the viewer is closed.

number
canGoPrevious, canGoNext

Whether a slide exists before or after the current one.

boolean
canZoom, isZoomed

Whether the current slide can zoom, and whether it is zoomed in.

boolean
previous, next, toggleZoom, close

Move one slide, zoom in or out, or close the viewer.

() => void

View as Markdown