Documentation
Yet Another React Lightbox allows you to add a lightbox component to your React project in minutes. To get started, follow the Installation and Minimal Setup Example guides, or feel free to explore the collection of StackBlitz demos.
Yet Another React Lightbox provides all components and types as named exports. Lightbox is exported as both default and named export.
The below documentation covers Yet Another React Lightbox API. For advanced features, please see the Advanced API documentation.
Parameters marked with an asterisk () are required.
Styles
Yet Another React Lightbox comes with CSS stylesheet that needs to be imported once in your project. All examples across this site include CSS import, but you can omit the import statement if you already imported lightbox styles in your application.
import "yet-another-react-lightbox/styles.css";
Lightbox
Property | Type | Description |
---|---|---|
open | boolean | If true , the lightbox is open. |
close | () => void | A callback to close the lightbox. |
index | number | Slide index. The lightbox reads this property when it opens (in this case the Default value: 0 |
slides | Slide[] | Slides to display in the lightbox. See Slide for details. Please note that updating the Default value: [] |
render | Render | Custom render functions. See Render for details. |
plugins | Plugin[] | Enabled plugins. Example: plugins={[Fullscreen, Video]} |
labels | object | Custom UI labels / translations. Example: labels={{ Next: "Next slide" }} |
toolbar | { | Toolbar settings.
Default value: { buttons: ["close"] } |
carousel | { | Carousel settings.
Default value: { finite: false, preload: 2, padding: "16px", spacing: "30%", imageFit: "contain" } |
animation | { | Animation settings.
Default value: { fade: 250, swipe: 500, easing: { fade: "ease", swipe: "ease-out", navigation: "ease-in-out" } } |
controller | { | Controller settings.
Default value: { ref: null, focus: true, aria: false, touchAction: "none" } |
portal | { | Portal settings.
|
noScroll | { | NoScroll module settings. The NoScroll module is responsible for hiding the vertical scrollbar and
preventing document
Additionally, the NoScroll module adds extra padding to the
fixed-positioned elements to avoid visual layout shifts. The primary
use case for this feature is a fixed-position page header / appbar.
However, if this behavior causes undesired side effects, you can
deactivate it for specific elements by marking them with the
|
on | { | Lifecycle callbacks.
|
styles | { | Customization styles.
Note that some plugins extend this list with their own customization slots. |
className | string | CSS class of the lightbox root element |
Slide
Image slides are supported by default. Additional slide types can be added via plugins or custom render function. Please see Custom Slides example for details.
Property | Type | Description |
---|---|---|
type | "image" | Image slide type |
src | string | Image URL |
alt | string | Image alt attribute |
width | number | Image width in pixels |
height | number | Image height in pixels |
imageFit | "contain" | "cover" | Image object-fit setting |
srcSet | { | Alternative images to be included in the srcset |
As a bare minimum, you need to provide src
attribute for each image slide.
const slides = [
{ src: "/image1.jpg" },
{ src: "/image2.jpg" },
{ src: "/image3.jpg" },
// ...
];
However, the recommended configuration is to provide multiple files of different
resolution for each slide. Yet Another React Lightbox uses all supplied
images to populate srcset
and sizes
attributes on the fly. Please note that
width
and height
attributes are required in this case.
const slides = [
{
src: "/image1x3840.jpg",
alt: "image 1",
width: 3840,
height: 2560,
srcSet: [
{ src: "/image1x320.jpg", width: 320, height: 213 },
{ src: "/image1x640.jpg", width: 640, height: 427 },
{ src: "/image1x1200.jpg", width: 1200, height: 800 },
{ src: "/image1x2048.jpg", width: 2048, height: 1365 },
{ src: "/image1x3840.jpg", width: 3840, height: 2560 },
],
},
// ...
];
Yet Another React Lightbox is optimized to preload and display only a limited number of slides at a time, so there are no performance penalties or UX impact in supplying a large number of slides.
Render
Custom render functions can be passed via render
prop.
// render function usage example
<Lightbox
render={{
slide: ({ slide, offset, rect }) => {
// ...
},
}}
// ...
/>
Property | Type | Description |
---|---|---|
slide | ({ slide, offset, rect }: { slide: Slide; offset: number; rect: ContainerRect }) => React.ReactNode | Render custom slide type, or override the default image slide. |
slideHeader | ({ slide }: { slide: Slide }) => React.ReactNode | Render custom slide elements into the DOM right before the slide. Use absolute or fixed positioning. |
slideFooter | ({ slide }: { slide: Slide }) => React.ReactNode | Render custom slide elements into the DOM right after the slide. Use absolute or fixed positioning. |
slideContainer | ({ slide, children }: { slide: Slide; children: React.ReactNode }) => React.ReactNode | Render custom slide container. |
controls | () => React.ReactNode | Render custom controls or additional elements in the lightbox. Use absolute or fixed positioning. |
iconPrev | () => React.ReactNode | Render custom Prev icon. |
iconNext | () => React.ReactNode | Render custom Next icon. |
iconClose | () => React.ReactNode | Render custom Close icon. |
iconLoading | () => React.ReactNode | Render custom Loading icon. |
iconError | () => React.ReactNode | Render custom Error icon. |
buttonPrev | () => React.ReactNode | Render custom Prev button. Return null if you want to hide the button. |
buttonNext | () => React.ReactNode | Render custom Prev button. Return null if you want to hide the button. |
buttonClose | () => React.ReactNode | Render custom Close button. |
Controller Ref
Controller ref provides external controls for the lightbox.
// Controller ref usage example
const ref = React.useRef(null);
// ...
return (
<Lightbox
controller={{ ref }}
on={{ click: () => ref.current?.close() }}
// ...
/>
);
Property | Type | Description |
---|---|---|
prev | ({ count }: { count: number }) => void | () => void | Navigate to the previous slide. |
next | ({ count }: { count: number }) => void | () => void | Navigate to the next slide. |
close | () => void | Close the lightbox. |
focus | () => void | Transfer focus to the lightbox controller. |
getLightboxProps | () => ComponentProps | Get lightbox props (see type definitions for more details). |
getLightboxState | () => LightboxState | Get lightbox state (see type definitions for more details). |
Tracking Slide Index
While the lightbox maintains the slide index state internally, some use cases may require you to keep track of the slide index in a local state variable. You can easily accomplish this using the following approach:
const [index, setIndex] = React.useState(0);
// ...
return (
<Lightbox
index={index}
on={{ view: ({ index: currentIndex }) => setIndex(currentIndex) }}
// ...
/>
);
Previous Versions
Are you looking for documentation for one of the previous versions?