A lightbox component for React.js
npm install react-image-lightbox-next

A flexible lightbox component for displaying images in a React project.
Features
- Keyboard shortcuts (with rate limiting)
- Image Zoom
- Flexible rendering using src values assigned on the fly
- Image preloading for smoother viewing
- Mobile friendly, with pinch to zoom and swipe (Thanks, @webcarrot!)
- No external CSS
``jsx
import React, { Component } from 'react';
import Lightbox from 'react-image-lightbox';
const images = [
'//placekitten.com/1500/500',
'//placekitten.com/4000/3000',
'//placekitten.com/800/1200',
'//placekitten.com/1500/1500'
];
export default class LightboxExample extends Component {
constructor(props) {
super(props);
this.state = {
photoIndex: 0,
isOpen: false
};
}
render() {
const {
photoIndex,
isOpen,
} = this.state;
return (
{isOpen &&
nextSrc={images[(photoIndex + 1) % images.length]}
prevSrc={images[(photoIndex + images.length - 1) % images.length]}
onCloseRequest={() => this.setState({ isOpen: false })}
onMovePrevRequest={() => this.setState({
photoIndex: (photoIndex + images.length - 1) % images.length,
})}
onMoveNextRequest={() => this.setState({
photoIndex: (photoIndex + 1) % images.length,
})}
/>
}
`Options
Property | Type | Default | Required | Description
:-------------------|:------:|:--------------:|:--------:|:----------------------------------------
mainSrc | string | | yes | Main display image url
prevSrc | string | | | Previous display image url (displayed to the left). If left undefined, onMovePrevRequest will not be called, and the button not displayedonMoveNextRequest
nextSrc | string | | | Next display image url (displayed to the right). If left undefined, will not be called, and the button not displayedprops.mainSrc
mainSrcThumbnail | string | | | Thumbnail image url corresponding to . Displayed as a placeholder while the full-sized image loads.props.prevSrc
prevSrcThumbnail | string | | | Thumbnail image url corresponding to . Displayed as a placeholder while the full-sized image loads.props.nextSrc
nextSrcThumbnail | string | | | Thumbnail image url corresponding to . Displayed as a placeholder while the full-sized image loads.props.prevSrc
onCloseRequest | func | | yes | Close window event. Should change the parent state such that the lightbox is not rendered
onMovePrevRequest | func | empty function | | Move to previous image event. Should change the parent state such that becomes props.mainSrc, props.mainSrc becomes props.nextSrc, etc.props.nextSrc
onMoveNextRequest | func | empty function | | Move to next image event. Should change the parent state such that becomes props.mainSrc, props.mainSrc becomes props.prevSrc, etc.false
onImageLoadError | func | empty function | | Called when an image fails to load.(imageSrc: string, srcType: string, errorEvent: object): void
onAfterOpen | func | empty function | | Called after the modal has rendered.
discourageDownloads | bool | | | Enable download discouragement (prevents [right-click -> Save Image As...])false
animationDisabled | bool | | | Disable all animationfalse
animationOnKeyInput | bool | | | Disable animation on actions performed with keyboard shortcuts300
animationDuration | number | | | Animation duration (ms)180
keyRepeatLimit | number | | | Required interval of time (ms) between key actions (prevents excessively fast navigation of images)40
keyRepeatKeyupBonus | number | | | Amount of time (ms) restored after each keyup (makes rapid key presses slightly faster than holding down the key to navigate images){}
imageTitle | node | | | Image title (Descriptive element above image)
imageCaption | node | | | Image caption (Descriptive element below image)
toolbarButtons | node[] | | | Array of custom toolbar buttons
reactModalStyle | Object | | | Set z-index style, etc., for the parent react-modal (react-modal style format)10
imagePadding | number | | | Padding (px) between the edge of the window and the lightboxtrue
clickOutsideToClose | bool | | | When true, clicks outside of the image close the lightboxtrue
enableZoom | bool | | | Set to false to disable zoom functionality and hide zoom buttons''
wrapperClassName | string | | | Class name which will be applied to root element after React Modal 'Next image'
nextLabel | string | | | aria-label set on the 'Next' button'Previous image'
prevLabel | string | | | aria-label set on the 'Previous' button'Zoom in'
zoomInLabel | string | | | aria-label set on the 'Zoom In' button'Zoom out'
zoomOutLabel | string | | | aria-label set on the 'Zoom Out' button'Close lightbox'
closeLabel | string | | | aria-label set on the 'Close Lightbox' button
| Browser | Works? |
|:---------|:------------------------------------|
| Chrome | Yes |
| Firefox | Yes |
| Safari | Yes |
| IE >= 10 | Yes |
After cloning the repository and running npm install inside, you can use the following commands to develop and build the project.
`shStarts a webpack dev server that hosts a demo page with the lightbox.
It uses react-hot-loader so changes are reflected on save.
npm start
-ed to this repository from another local project.Pull requests are welcome!
MIT