From 03d19ed879b2c8e5cd49bcf107275d8c290afe8e Mon Sep 17 00:00:00 2001 From: Sandra Date: Sun, 10 Sep 2017 15:39:44 +0200 Subject: [PATCH] separate out examples from the app into separate component apps --- examples/src/ExampleBasic.js | 13 ++++ examples/src/ExampleWithLightbox.js | 59 ++++++++++++++++ examples/src/app.js | 102 ++++++++-------------------- examples/src/example.less | 9 ++- 4 files changed, 107 insertions(+), 76 deletions(-) create mode 100644 examples/src/ExampleBasic.js create mode 100644 examples/src/ExampleWithLightbox.js diff --git a/examples/src/ExampleBasic.js b/examples/src/ExampleBasic.js new file mode 100644 index 0000000..1691973 --- /dev/null +++ b/examples/src/ExampleBasic.js @@ -0,0 +1,13 @@ +import React from 'react'; +import Gallery from 'react-photo-gallery'; + +const ExampleBasic = ({photos, columns}) => { + return ( +
+

Basic:

+ +
+ ); +} + +export default ExampleBasic; diff --git a/examples/src/ExampleWithLightbox.js b/examples/src/ExampleWithLightbox.js new file mode 100644 index 0000000..2d71343 --- /dev/null +++ b/examples/src/ExampleWithLightbox.js @@ -0,0 +1,59 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import Gallery from 'react-photo-gallery'; +import Lightbox from 'react-images'; +import CustomImage from './CustomImage'; + +class ExampleWithLightbox extends React.Component { + constructor() { + super(); + this.state = { currentImage: 0 }; + this.closeLightbox = this.closeLightbox.bind(this); + this.openLightbox = this.openLightbox.bind(this); + this.gotoNext = this.gotoNext.bind(this); + this.gotoPrevious = this.gotoPrevious.bind(this); + } + openLightbox(event, obj) { + this.setState({ + currentImage: obj.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, + }); + } + render() { + return ( +
+

Using a Lightbox:

+ + ({ ...x, srcset: x.srcSet, caption: x.title }))} + backdropClosesModal={true} + onClose={this.closeLightbox} + onClickPrev={this.gotoPrevious} + onClickNext={this.gotoNext} + currentImage={this.state.currentImage} + isOpen={this.state.lightboxIsOpen} + width={1600} + /> +
+ ); + } +} + +export default ExampleWithLightbox; diff --git a/examples/src/app.js b/examples/src/app.js index 267b5c4..4f69d73 100644 --- a/examples/src/app.js +++ b/examples/src/app.js @@ -1,21 +1,15 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import Gallery from 'react-photo-gallery'; -import Lightbox from 'react-images'; import jsonp from 'jsonp'; import Measure from 'react-measure'; - -import CustomImage from './CustomImage'; +import ExampleBasic from './ExampleBasic'; +import ExampleWithLightbox from './ExampleWithLightbox'; class App extends React.Component { constructor() { super(); - this.state = { currentImage: 0, width: -1 }; + this.state = { width: -1 }; this.loadPhotos = this.loadPhotos.bind(this); - this.closeLightbox = this.closeLightbox.bind(this); - this.openLightbox = this.openLightbox.bind(this); - this.gotoNext = this.gotoNext.bind(this); - this.gotoPrevious = this.gotoPrevious.bind(this); } componentDidMount() { this.loadPhotos(); @@ -58,76 +52,36 @@ class App extends React.Component { photos: this.state.photos ? this.state.photos.concat(photos) : photos, }); }); - } - openLightbox(event, obj) { - this.setState({ - currentImage: obj.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, - }); - } - renderGallery(){ - const width = this.state.width; - return( - this.setState({ width: contentRect.bounds.width })}> - { - ({ measureRef }) => { - // fix flash of one col and large image - // don't try to load gallery until width is bigger - if (width < 1 ){ - return
; - } - let columns = 1; - if (width >= 480){ - columns = 2; - } - if (width >= 1024){ - columns = 3; - } - if (width >= 1824){ - columns = 4; - } - return
- -
- } - } -
- ); + } render() { if (this.state.photos) { + const width = this.state.width; return ( -
- {this.renderGallery()} - ({ ...x, srcset: x.srcSet, caption: x.title }))} - backdropClosesModal={true} - onClose={this.closeLightbox} - onClickPrev={this.gotoPrevious} - onClickNext={this.gotoNext} - currentImage={this.state.currentImage} - isOpen={this.state.lightboxIsOpen} - width={1600} - /> -
+ this.setState({ width: contentRect.bounds.width })}> + { + ({ measureRef }) => { + if (width < 1 ){ + return
; + } + let columns = 1; + if (width >= 480){ + columns = 2; + } + if (width >= 1024){ + columns = 3; + } + if (width >= 1824){ + columns = 4; + } + return
+ + +
+ } + } +
); } else { return ( diff --git a/examples/src/example.less b/examples/src/example.less index 20f5d21..13f9fd6 100644 --- a/examples/src/example.less +++ b/examples/src/example.less @@ -47,15 +47,20 @@ a:hover { h1, h2, h3 { color: #222; font-weight: 200; - width: 90%; margin: 0 auto; margin-top: 10px; - text-align: center; } h1{ + text-align: center; font-size: 35px; margin-top: 10px; } +h2{ + margin-top: 15px; +} +h3{ + text-align: center; +} iframe{ margin: 0 auto; margin-top: 10px;