Yet Another React Lightbox
Yet Another React Lightbox (YARL) is a modern, performant, and highly customizable React component designed for displaying images and and videos in a lightbox interface. Currently at stable version 3.31.0, it receives frequent updates, with several patch and minor releases in recent months, indicating an active development cadence. Key differentiators include its focus on responsive images using `srcset` and `sizes` for optimal loading, support for various navigation methods (keyboard, mouse, touch), and a modular architecture that utilizes optional plugins for features like zoom, captions, and video support. It is built to work with React versions 16.8.0 through 19, provides built-in TypeScript definitions, and is compatible with RTL layouts, ensuring broad compatibility and developer experience.
Common errors
-
ReferenceError: Lightbox is not defined
cause The Lightbox component was not imported or was imported incorrectly.fixEnsure you have `import Lightbox from 'yet-another-react-lightbox';` at the top of your file. -
Lightbox opens but images are not centered, background is white, or navigation arrows are missing/unstyled.
cause The required CSS stylesheet for the lightbox was not imported.fixAdd `import 'yet-another-react-lightbox/styles.css';` to your application's entry point or the component file where the Lightbox is used. -
TypeError: Cannot read properties of undefined (reading 'useState') or similar errors related to Hooks.
cause Your React project is using a version older than 16.8.0, which does not support React Hooks.fixUpgrade your `react` and `react-dom` packages to version `^16.8.0` or newer in your `package.json` and reinstall dependencies. -
Property 'Zoom' does not exist on type 'typeof import("yet-another-react-lightbox")'.cause Attempting to import a plugin (like Zoom) directly from the main `yet-another-react-lightbox` package.fixPlugins must be imported from their specific paths, e.g., `import Zoom from 'yet-another-react-lightbox/plugins/zoom';`.
Warnings
- gotcha The base styles for the lightbox are not automatically included. You must explicitly import `yet-another-react-lightbox/styles.css` for the component to render with its intended appearance.
- gotcha Advanced features like image zoom, video support, captions, and thumbnails are implemented as separate plugins. These are not included by default to keep the core library lean and must be imported and added to the `plugins` prop of the `Lightbox` component.
- gotcha Yet Another React Lightbox is built on React Hooks and requires React version 16.8.0 or newer. Using it with older React versions will result in runtime errors related to missing hooks.
- gotcha While the basic `slides` array with just `src` works, the library strongly recommends providing `srcset` and `sizes` attributes for responsive images. This ensures optimal performance and user experience by allowing the browser to choose the most appropriate image resolution.
- breaking Version 3 introduced minor breaking changes. Key changes included `slides` and `index` props being updatable (previously might have required workarounds), and animation easing settings moved to `animation` prop. While migration is generally trivial, review the v3 release notes if upgrading from v2.
Install
-
npm install yet-another-react-lightbox -
yarn add yet-another-react-lightbox -
pnpm add yet-another-react-lightbox
Imports
- Lightbox
const Lightbox = require('yet-another-react-lightbox');import Lightbox from 'yet-another-react-lightbox';
- Lightbox styles
import 'yet-another-react-lightbox/styles.css';
- Zoom plugin
import { Zoom } from 'yet-another-react-lightbox';import Zoom from 'yet-another-react-lightbox/plugins/zoom'; import 'yet-another-react-lightbox/plugins/zoom.css';
Quickstart
import * as React from 'react';
import Lightbox from 'yet-another-react-lightbox';
import 'yet-another-react-lightbox/styles.css';
export default function App() {
const [open, setOpen] = React.useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)}>
Open Lightbox
</button>
<Lightbox
open={open}
close={() => setOpen(false)}
slides={[
{ src: '/image1.jpg' },
{ src: '/image2.jpg' },
{ src: '/image3.jpg' },
]}
/>
</>
);
}