Lightbox
Open image collections in a navigable, accessible viewer with fit modes, zoom, rotation, fullscreen, and download controls.
Lightbox renders an image gallery and opens the selected image in an immersive viewer. It supports responsive thumbnails, previous/next navigation, captions, titles, counters, fit modes, zoom, rotation, fullscreen, downloading, keyboard controls, localized labels, and ConfigProvider defaults.
Installation
import { Lightbox } from '@mosmox/ui';Basic usage
const images = [
{
id: 'lake',
src: '/images/lake.jpg',
thumbnailSrc: '/images/lake-thumb.jpg',
alt: 'Mountain lake at sunrise',
title: 'Morning light',
caption: 'A calm lake surrounded by mountains.',
},
{
id: 'valley',
src: '/images/valley.jpg',
alt: 'River running through a green valley',
title: 'Valley river',
},
];
<Lightbox items={images} aria-label="Landscape gallery" />;The component renders a responsive trigger gallery. Clicking a thumbnail opens the viewer at that image. If thumbnailSrc is omitted, src is used for both the thumbnail and the full-size image. If no caption is supplied, the viewer uses alt as the caption when captions are enabled.
Viewer layout and controls
The viewer contains:
- a header with the current item
title, anImage n of totalcounter, and a square close button; - previous and next image buttons;
- a fit switch with
Fit,Cover, and100%modes; - a toolbar with zoom out, zoom level, zoom in, rotate left, rotate right, fullscreen, and optional download controls;
- an optional caption below the image;
- an optional thumbnail strip/grid with the active image marked.
showToolbar controls the fit switch and the toolbar together. showThumbnails controls the thumbnails inside the viewer; showGallery controls the initial trigger gallery. A viewer can therefore be opened from a custom trigger by controlling open while setting showGallery={false}.
<Lightbox
items={images}
defaultIndex={1}
loop={false}
showGallery={false}
showThumbnails={false}
showCaptions={false}
showCounter={false}
showToolbar
downloadable={false}
zoom
/>Zoom ranges from 50% to 400% in 25% increments. The actual fit mode displays the image at its natural size when possible. Rotation changes in 90° steps and resets when the selected image changes or the viewer is opened again.
Controlled opening and events
Use open and onOpenChange when the parent owns the viewer state. defaultIndex determines the initial item; indexes outside the collection are clamped to a valid item.
const [open, setOpen] = useState(false);
const [selectedIndex, setSelectedIndex] = useState(0);
<Button onClick={() => setOpen(true)}>Open gallery</Button>
<Lightbox
items={images}
open={open}
defaultIndex={selectedIndex}
onOpenChange={setOpen}
onChange={(index, item) => {
setSelectedIndex(index);
analytics.track('lightbox-image-viewed', { index, id: item.id });
}}
/>onChange fires after navigation or thumbnail selection and receives the new zero-based index and the complete LightboxItem. onOpenChange fires when the viewer is opened or closed.
Configuration with ConfigProvider
The nearest ConfigProvider supplies defaults for variant, fit, showCaptions, showThumbnails, and showToolbar. An instance prop always takes precedence over the provider configuration.
<ConfigProvider
locale="uk"
components={{
lightbox: {
variant: 'glass',
fit: 'contain',
showCaptions: true,
showThumbnails: true,
showToolbar: true,
},
}}
>
<Lightbox items={images} />
</ConfigProvider>The fit-mode labels are localized from the provider locale. Supported locales are en, uk, fr, and de:
| Locale | Fit | Cover | Actual-size label |
|---|---|---|---|
en | Fit | Cover | 100% |
uk | Вписати | Заповнити | 100% |
fr | Ajuster | Couvrir | 100% |
de | Einpassen | Ausfüllen | 100% |
Variants and fit modes
variant="dark" is the default immersive presentation. glass adds a translucent blurred backdrop. light uses a light backdrop while keeping the image controls as dark glass overlays.
fit accepts:
contain— show the complete image without cropping;cover— fill the image viewport, cropping when the aspect ratios differ;actual— show the image at its natural size when possible.
<Lightbox items={images} variant="light" fit="cover" />Backdrop and keyboard behavior
With closeOnBackdrop enabled, clicking empty backdrop space closes the viewer. Clicking the image or a control does not. Escape also closes the viewer.
| Key | Action |
|---|---|
ArrowLeft | Previous image. |
ArrowRight | Next image. |
+ / = | Zoom in. |
- | Zoom out. |
0 | Reset zoom, rotation, and fit mode. |
Escape | Close the viewer. |
All viewer actions are native buttons with accessible labels, and the gallery receives the supplied aria-label.
API reference
Lightbox props
| Prop | Type | Default | Description |
|---|---|---|---|
items | LightboxItem[] | required | Images available in the gallery. An empty array renders nothing. |
aria-label | string | Image gallery | Accessible name for the trigger gallery and viewer dialog. |
className | string | — | Additional class name for the trigger gallery. |
closeOnBackdrop | boolean | true | Closes the viewer when empty backdrop space is clicked. |
defaultIndex | number | 0 | Zero-based image index shown when the viewer opens. |
downloadable | boolean | true | Shows the download button and enables downloading the current source image. |
fit | 'contain' | 'cover' | 'actual' | provider or contain | Initial image fitting mode. |
loop | boolean | true | Wraps previous/next navigation at the ends of the collection. |
onChange | (index: number, item: LightboxItem) => void | — | Called after the current image changes. |
onOpenChange | (open: boolean) => void | — | Called when the viewer opens or closes. |
open | boolean | uncontrolled | Controlled viewer state. Omit it to let the component manage opening from thumbnails. |
showCaptions | boolean | provider or true | Shows the current item caption, falling back to alt. |
showCounter | boolean | true | Shows the current index and total count in the header. |
showGallery | boolean | true | Shows the trigger thumbnail gallery. Set false when using a custom trigger with controlled open. |
showThumbnails | boolean | provider or true | Shows thumbnail navigation inside the viewer. |
showToolbar | boolean | provider or true | Shows fit, zoom, rotation, fullscreen, and download controls. |
thumbSize | number | 64 | Thumbnail width and height in pixels. |
variant | 'dark' | 'glass' | 'light' | provider or dark | Visual presentation of the overlay. |
zoom | boolean | true | Enables zoom buttons and +/- keyboard shortcuts. |
LightboxItem
| Property | Type | Description |
|---|---|---|
src | string | Required full-size image URL. |
alt | string | Accessible alternative text; also used as the fallback title and caption. |
caption | ReactNode | Content shown below the image when captions are enabled. |
download | string | Optional filename used by the download action. |
height | number | Optional source-height metadata accepted by the item type. |
id | string | Optional stable identity used for React keys. |
thumbnailSrc | string | Optional optimized thumbnail URL. Falls back to src. |
title | ReactNode | Content shown in the viewer header and used in the trigger's accessible label. |
width | number | Optional source-width metadata accepted by the item type. |
title and caption accept any React node, so applications can provide formatted or localized content. width and height are available as item metadata for consumers; the current viewer derives its layout from the loaded image and fit mode.