Lightbox
An image and video gallery that opens full screen with swipe and pinch to zoom, built on PhotoSwipe
Installation
pnpm dlx shadcn@latest add @alexandrejoly/lightboxUsage
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.
useLightbox
useLightbox gives the current slide to controls of your own, here a link that downloads it.
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
Lightbox
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.
| Prop | Type |
|---|---|
optionsPhotoSwipe options, read each time the viewer opens, such as | PhotoSwipeOptions |
LightboxItem
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.
| Prop | Type | Default |
|---|---|---|
width, heightThe full-size file’s width and height in pixels. | number | None |
typeA video plays in the viewer with its controls, from the slide it is on, and pauses when you leave it. Default: | "image" | "video" | "image" |
srcSet, sizesResponsive sources for the full-size image. | string | None |
LightboxItemCaption
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.
LightboxContent
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.
LightboxCaption
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.
LightboxActions
Lines up LightboxZoom and LightboxClose in the top end corner.
Takes every prop of div.
LightboxZoom
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).
LightboxClose
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).
LightboxPrevious
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).
LightboxNext
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 value | Type |
|---|---|
index, countThe current slide, from 0, and the slide count, 0 while the viewer is closed. | number |
canGoPrevious, canGoNextWhether a slide exists before or after the current one. | boolean |
canZoom, isZoomedWhether the current slide can zoom, and whether it is zoomed in. | boolean |
previous, next, toggleZoom, closeMove one slide, zoom in or out, or close the viewer. | () => void |