if you wish to have IE8 support, v2 with React 0.14 is the highest version available.
A React.js Masonry component. (Also available as a mixin if needed)
The component is bundled with Masonry, so no additional dependencies needed!
You can optionally include Masonry as a script tag if there should be any reason for doing so
<script src='//cdnjs.cloudflare.com/ajax/libs/masonry/3.1.5/masonry.pkgd.min.js' />To use the component just require the module.
npm install --save react-masonry-component
import*asReactfrom'react';importMasonryfrom'react-masonry-component';constmasonryOptions={transitionDuration: 0};constimagesLoadedOptions={background: '.my-bg-image-el'}classGalleryextendsReact.Component{render(){constchildElements=this.props.elements.map(function(element){return(<liclassName="image-element-class"><imgsrc={element.src}/></li>);});return(<MasonryclassName={'my-gallery-class'}// default ''elementType={'ul'}// default 'div'options={masonryOptions}// default {}disableImagesLoaded={false}// default falseupdateOnEachImageLoad={false}// default false and works only if disableImagesLoaded is falseimagesLoadedOptions={imagesLoadedOptions}// default {}>{childElements}</Masonry>);}}exportdefaultGallery;ES6-style modules are also supported, just use:
importMasonryfrom'react-masonry-component';You can also include your own custom props - EG: inline-style and event handlers.
import*asReactfrom'react';importMasonryfrom'react-masonry-component';constmasonryOptions={transitionDuration: 0};conststyle={backgroundColor: 'tomato'};classGalleryextendsReact.Component{handleClick(){}render(){return(<MasonryclassName={'my-gallery-class'}style={style}onClick={this.handleClick}>{...}</Masonry>);}}exportdefaultGallery;Should you need to access the instance of Masonry (for example to listen to masonry events)
you can do so by using refs.
import*asReactfrom'react';importMasonryfrom'react-masonry-component';classGalleryextendsReact.Component{handleLayoutComplete(){},componentDidMount(){this.masonry.on('layoutComplete',this.handleLayoutComplete);},componentWillUnmount(){this.masonry.off('layoutComplete',this.handleLayoutComplete);},render(){return(<Masonryref={function(c){this.masonry=this.masonry||c.masonry;}.bind(this)}>{...}</Masonry>);}}exportdefaultGallery;React Masonry Component uses Desandro's imagesloaded library to detect when images have loaded. Should you want to pass
options down to it then you need to populate the imagesLoadedOptions property on React Masonry Component.
This will most commonly be used when the elements in your gallery have CSS background images and you want to capture their load event. More info availabe on the imagesloaded website.
eg:
import*asReactfrom'react';importMasonryfrom'react-masonry-component';classGalleryextendsReact.Component{render(){constimagesLoadedOptions={background: '.my-bg-image-el'}return(<MasonryclassName={'my-gallery-class'}elementType={'ul'}options={masonryOptions}imagesLoadedOptions={imagesLoadedOptions}><divclassName="my-bg-image-el"></div></Masonry>);}}exportdefaultGallery;onImagesLoaded- triggered when all images are loaded or after each image is loaded whenupdateOnEachImageLoadis set totrueonLayoutComplete- triggered after a layout and all positioning transitions have completed.onRemoveComplete- triggered after an item element has been removed
classGalleryextendsReact.Component{componentDidMount(){this.hide();},handleImagesLoaded(imagesLoadedInstance){this.show();},render(){return(<MasonryonImagesLoaded={this.handleImagesLoaded}onLayoutComplete={laidOutItems=>this.handleLayoutComplete(laidOutItems)}onRemoveComplete={removedItems=>this.handleRemoveComplete(removedItems)}>{...}</Masonry>)}}