Skip to content

Instantly share code, notes, and snippets.

@pawelztef
Last active August 23, 2020 18:48
Show Gist options
  • Save pawelztef/8f70b2c63625e7ae390c74be94d120bc to your computer and use it in GitHub Desktop.
Save pawelztef/8f70b2c63625e7ae390c74be94d120bc to your computer and use it in GitHub Desktop.
React Masonry and Lightbox Gallery
import React, { Component} from 'react';
import RespGallery from './components/RespGallery';
const IMAGES = [{
src: '/images/1.jpg',
thumbnail: '/images/1.jpg',
caption: 'Lorem 1',
}, {
src: '/images/2.jpg',
thumbnail: '/images/2.jpg',
caption: 'Lorem 2',
}];
export default class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<RespGallery images={DEFAULT_IMAGES.map(({ src, thumbnail, caption }) => ({
src,
thumbnail,
caption,
}))} />
)
}
}
import React, { Component} from 'react';
import Lightbox from 'react-images';
import PropTypes from 'prop-types';
import Masonry from 'react-masonry-component';
const masonryOptions = {
transitionDuration: 0,
gutter: 10,
percentPosition: true,
columnWidth: 300,
};
export default class RespGallery extends Component {
constructor(props) {
super(props);
this.state = {
lightboxIsOpen: false,
currentImage: 0,
};
this.closeLightbox = this.closeLightbox.bind(this);
this.gotoNext = this.gotoNext.bind(this);
this.gotoPrevious = this.gotoPrevious.bind(this);
this.gotoImage = this.gotoImage.bind(this);
this.handleClickImage = this.handleClickImage.bind(this);
this.openLightbox = this.openLightbox.bind(this);
}
openLightbox(index, event) {
event.preventDefault();
this.setState({
currentImage: index,
lightboxIsOpen: true,
});
}
closeLightbox() {
this.setState({
currentImage: 0,
lightboxIsOpen: false,
});
}
gotoPrevious() {
this.setState({
currentImage: this.state.currentImage - 1,
});
}
gotoNext() {
this.setState({
currentImage: this.state.currentImage + 1,
});
}
gotoImage(index) {
this.setState({
currentImage: index,
});
}
handleClickImage() {
if (this.state.currentImage === this.props.images.length - 1) return;
this.gotoNext();
}
renderGallery() {
const {
images
} = this.props;
if (!images) return;
const gallery = images.map((obj, i) => {
return (
<div key={i} className={`grid-item`}>
<img onClick={(e) => this.openLightbox(i, e)} src={obj.thumbnail} className={`gallery-img`}/>
</div>
);
});
return (
<Masonry
className={'grid'} // default ''
options={masonryOptions}
disableImagesLoaded={false}
updateOnEachImageLoad={true}>
{gallery}
</Masonry>
);
}
render() {
return (
<div className={`content container`}>
<h4>Gallery</h4>
{this.renderGallery()}
<Lightbox
currentImage={this.state.currentImage}
images={this.props.images}
isOpen={this.state.lightboxIsOpen}
onClickImage={this.handleClickImage}
onClickNext={this.gotoNext}
onClickPrev={this.gotoPrevious}
onClickThumbnail={this.gotoImage}
onClose={this.closeLightbox}
showThumbnails={this.props.showThumbnails}
theme={this.props.theme} />
</div>
);
}
}
RespGallery.propTypes = {
images: PropTypes.array,
showThumbnails: PropTypes.bool,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment