A masonry component for React.js
npm install react-masonry-componentReact Masonry Component
=======================


#### Introduction:
A React.js Masonry component. (Also available as a mixin if needed)
#### Live demo:
hearsay.me
#### Usage:
* 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
* To use the component just require the module.
##### Basic usage
`` npm install --save react-masonry-component``jsx
import * as React from 'react';
import Masonry from 'react-masonry-component';
const masonryOptions = {
transitionDuration: 0
};
const imagesLoadedOptions = { background: '.my-bg-image-el' }
class Gallery extends React.Component {
render() {
const childElements = this.props.elements.map(function(element){
return (
export default Gallery;
`
ES6-style modules are also supported, just use:
`js`
import Masonry from 'react-masonry-component';
##### Custom props
You can also include your own custom props - EG: inline-style and event handlers.
`jsx
import * as React from 'react';
import Masonry from 'react-masonry-component';
const masonryOptions = {
transitionDuration: 0
};
const style = {
backgroundColor: 'tomato'
};
class Gallery extends React.Component {
handleClick() {}
render() {
return (
style={style}
onClick={this.handleClick}
>
{...}
);
}
}
export default Gallery;
`
##### Accessing Masonry instance
Should you need to access the instance of Masonry (for example to listen to masonry events)
you can do so by using refs.
`jsx
import * as React from 'react';
import Masonry from 'react-masonry-component';
class Gallery extends React.Component {
handleLayoutComplete() { },
componentDidMount() {
this.masonry.on('layoutComplete', this.handleLayoutComplete);
},
componentWillUnmount() {
this.masonry.off('layoutComplete', this.handleLayoutComplete);
},
render() {
return (
>
{...}
);
}
}
export default Gallery;
`imagesloaded
##### Images Loaded Options
React Masonry Component uses Desandro's library to detect when images have loaded. Should you want to passimagesLoadedOptions
options down to it then you need to populate the 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:
`jsx
import * as React from 'react';
import Masonry from 'react-masonry-component';
class Gallery extends React.Component {
render() {
const imagesLoadedOptions = { background: '.my-bg-image-el' }
return (
elementType={'ul'}
options={masonryOptions}
imagesLoadedOptions={imagesLoadedOptions}
>
export default Gallery;
`
##### Events
- onImagesLoaded - triggered when all images are loaded or after each image is loaded when updateOnEachImageLoad is set to trueonLayoutComplete
- - triggered after a layout and all positioning transitions have completed.onRemoveComplete
- - triggered after an item element has been removed
`jsx``
class Gallery extends React.Component {
componentDidMount() {
this.hide();
},
handleImagesLoaded(imagesLoadedInstance) {
this.show();
},
render() {
return (
onLayoutComplete={laidOutItems => this.handleLayoutComplete(laidOutItems)}
onRemoveComplete={removedItems => this.handleRemoveComplete(removedItems)}
>
{...}
)
}
}