A simple, responsive lightbox component for displaying an array of images, texts and videos.
npm install --save react-images-texts-videos
with images:
import React from 'react';
import Lightbox from 'react-images-texts-videos';
export default class Sample extends React.Component {
...
render() {
return (
<Lightbox
items={{type:'images', items: [{ src: 'http://example.com/img1.jpg' }, { src: 'http://example.com/img2.jpg' }]}}
isOpen={this.state.lightboxIsOpen}
onClickPrev={this.gotoPrevious}
onClickNext={this.gotoNext}
onClose={this.closeLightbox}
/>
);
}
}
or with texts:
import React from 'react';
import Lightbox from 'react-images-texts-videos';
export default class Sample extends React.Component {
...
render() {
return (
<Lightbox
items={{type:'texts', items: ['text1...', 'text2...']}}
isOpen={this.state.lightboxIsOpen}
onClickPrev={this.gotoPrevious}
onClickNext={this.gotoNext}
onClose={this.closeLightbox}
/>
);
}
}
or with videos (Youtube videos only):
import React from 'react';
import Lightbox from 'react-images-texts-videos';
export default class Sample extends React.Component {
...
render() {
return (
<Lightbox
items={{type:'videos', items: ['videoId1', 'videoId2']}}
isOpen={this.state.lightboxIsOpen}
onClickPrev={this.gotoPrevious}
onClickNext={this.gotoNext}
onClose={this.closeLightbox}
/>
);
}
}
Live demo: acamposruiz.github.io/react-images-texts-videos
To build the examples locally, run:
npm install
npm start
Then open localhost:8000
in a browser.
Example using srcset:
<Lightbox
images={LIGHTBOX_IMAGE_SET}
...
/>
const LIGHTBOX_IMAGE_SET = [
{
src: 'http://example.com/example/img1.jpg',
srcset: [
'http://example.com/example/img1_1024.jpg 1024w',
'http://example.com/example/img1_800.jpg 800w',
'http://example.com/example/img1_500.jpg 500w',
'http://example.com/example/img1_320.jpg 320w',
],
},
{
src: 'http://example.com/example/img2.jpg',
srcset: [
'http://example.com/example/img2_1024.jpg 1024w',
'http://example.com/example/img2_800.jpg 800w',
'http://example.com/example/img2_500.jpg 500w',
'http://example.com/example/img2_320.jpg 320w',
],
}
];
Notes on srcset support:
The srcset attribute is supported by some modern browsers. Results of browser implementation and behaviour may vary. The sizes attribute uses the default maxWidth CSS property set to the image. By default this is 80% so 80vw.
Another thing to note is that 'h' or height in the srcset attribute does not yet exist. Because of the nature of the fixed height of a Lightbox this is problematic for portrait sized images. You will need to calculate what the best 'w' size for a portrait size ought to be given the height of the fixed viewport otherwise unnecessarily large images will be fetched. See issue: ResponsiveImagesCG/picture-element#86
Read more about the srcset and sizes attributes here: https://ericportis.com/posts/2014/srcset-sizes/.
Example using caption for the first image:
<Lightbox
images={LIGHTBOX_IMAGE_SET}
...
/>
const LIGHTBOX_IMAGE_SET = [
{
src: 'http://example.com/example/img1.jpg',
caption: 'Sydney, Australia - Photo by Jill Smith',
},
{
src: 'http://example.com/example/img2.jpg',
}
];
Note that the caption is an entirely optional property, as can be seen in the first gallery on the example page. The first image has a single line caption, the second demonstrates multiline, and the remaining images are without captions, entirely.
Property | Type | Default | Description |
---|---|---|---|
backdropClosesModal | bool | false | Allow users to exit the lightbox by clicking the backdrop |
closeButtonTitle | string | ' Close (Esc) ' | Customize close esc title |
enableKeyboardInput | bool | true | Supports keyboard input - esc , arrow left , and arrow right |
currentItem | number | 0 | The index of the item to display initially |
customControls | array | undefined | An array of elements to display as custom controls on the top of lightbox |
items | object | undefined | A object containing two values, the type of element (images, texts or videos), and an array with the items |
imageCountSeparator | String | ' of ' | Customize separator in the image count |
isOpen | bool | false | Whether or not the lightbox is displayed |
leftArrowTitle | string | ' Previous (Left arrow key) ' | Customize of left arrow title |
onClickPrev | func | undefined | Fired on request of the previous image |
onClickNext | func | undefined | Fired on request of the next image |
onClose | func | undefined | Required. Handle closing of the lightbox |
onClickImage | func | undefined | Handle click on image |
onClickThumbnail | func | undefined | Handle click on thumbnail |
preloadNextImage | bool | true | Based on the direction the user is navigating, preload the next available image |
rightArrowTitle | string | ' Next (Right arrow key) ' | Customize right arrow title |
showCloseButton | bool | true | Optionally display a close "X" button in top right corner |
showImageCount | bool | true | Optionally display image index, e.g., "3 of 20" |
width | number | 1024 | Maximum width of the carousel; defaults to 1024px |