Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-masonry-component

eiriklv257.5kMIT6.3.0TypeScript support: included

A masonry component for React.js

react, mixin, masonry, packery, isotope, react-component

readme

React Masonry Component

npm version Build Status

IE8 support

if you wish to have IE8 support, v2 with React 0.14 is the highest version available.

Table of contents

  1. Usage
    1. Basic usage
    2. Custom props
    3. Accessing Masonry instance
    4. Images Loaded Options
    5. Events

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 <script src='//cdnjs.cloudflare.com/ajax/libs/masonry/3.1.5/masonry.pkgd.min.js' />

  • To use the component just require the module.

Basic usage

npm install --save react-masonry-component

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 (
                <li className="image-element-class">
                    <img src={element.src} />
                </li>
            );
        });

        return (
            <Masonry
                className={'my-gallery-class'} // default ''
                elementType={'ul'} // default 'div'
                options={masonryOptions} // default {}
                disableImagesLoaded={false} // default false
                updateOnEachImageLoad={false} // default false and works only if disableImagesLoaded is false
                imagesLoadedOptions={imagesLoadedOptions} // default {}
            >
                {childElements}
            </Masonry>
        );
    }
}

export default Gallery;

ES6-style modules are also supported, just use:

import Masonry from 'react-masonry-component';
Custom props

You can also include your own custom props - EG: inline-style and event handlers.

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 (
            <Masonry
                className={'my-gallery-class'}
                style={style}
                onClick={this.handleClick}
            >
                {...}
            </Masonry>
        );
    }
}

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.

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 (
             <Masonry
                 ref={function(c) {this.masonry = this.masonry || c.masonry;}.bind(this)}
             >
                 {...}
             </Masonry>
         );
     }
}

export default Gallery;
Images Loaded Options

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 * as React from 'react';
import Masonry from 'react-masonry-component';

class Gallery extends React.Component {
  render() {
    const imagesLoadedOptions = { background: '.my-bg-image-el' }

    return (
        <Masonry
            className={'my-gallery-class'}
            elementType={'ul'}
            options={masonryOptions}
            imagesLoadedOptions={imagesLoadedOptions}
        >
            <div className="my-bg-image-el"></div>
        </Masonry>
    );
  }
}

export default Gallery;
Events
  • onImagesLoaded - triggered when all images are loaded or after each image is loaded when updateOnEachImageLoad is set to true
  • onLayoutComplete - triggered after a layout and all positioning transitions have completed.
  • onRemoveComplete - triggered after an item element has been removed
class Gallery extends React.Component {
    componentDidMount() {
        this.hide();
    },
    handleImagesLoaded(imagesLoadedInstance) {
        this.show();
    },
    render() {
        return (
            <Masonry
                onImagesLoaded={this.handleImagesLoaded}
                onLayoutComplete={laidOutItems => this.handleLayoutComplete(laidOutItems)}
                onRemoveComplete={removedItems => this.handleRemoveComplete(removedItems)}
            >
                {...}
            </Masonry>
        )
    }
}

changelog

6.3.0

  • Support React 17

6.2.1

  • Fixes bug on unmounting compononent with disablesImagesLoaded set to true

6.2.0

  • columnWidth TypeScript interface now also supports HTMLElement or null
  • transitionDuration TypeScript interface now also supports a string

6.1.1

New Features

  • Adds ability to pass in imagesloaded options to be passed on by React Masonry Component
  • Adds number type to the transitionDuration property on the Options TypeScript Interface

Bug Fixes

  • Removes old imagesloaded listeners so there is only ever 1 active listener.
  • Correctly cleans up reference to imagesloaded handlers when the component is unmounted

6.0.2

  • Allows gutter to be a number or string (Typescript)
  • Refactors item reloading to prevent race conditions

6.0.1

  • Removes string style refs

6.0.0

  • Updates React peer dependencies to include React@16.0.0
  • Uses Lodash library as dependency instead of individual lodash methods
  • Fixes children.length check on diff

5.0.7

  • Adds horizontalOrder to TypeScript Masonry options Interface

5.0.6

  • Adds support for TypeScript string columnWidth

5.0.5

  • Switch to create-react-class and prop-types libraries

5.0.4

  • Correctly removes onRemoveComplete listener

5.0.3

  • Fixes let to var
  • DEV-ONLY Adds eslint

5.0.2

  • Fixes es6 syntax used in index.js

5.0.1

  • reverted 5.0.0 change
  • Fixes removing first element

5.0.0

  • old children are now passed to the diffing algorithm without filtering

4.4.0

  • Handle onLayoutComplete and onRemoveComplete callback handlers as part of the component

4.3.2

  • Fixes TypeScript to use export as namespace syntax

4.3.1

  • Fixes unmount when monitor children resize is disabled

4.3.0

  • Removes willReceiveProps function as not needed
  • Make resizable children trigger masonry layout

4.2.3-beta

  • Removes willReceiveProps function as possibly not needed

4.2.2

  • Exports component as default to support import statement

4.2.0

  • Adds Typescript 2.0 typings definition
  • Fixes unknownProps error (no longer passes unknown props to masonry el)

4.1.0

  • Add onImagesLoaded event
  • Use lodash.assign instead of own extend function
  • Add option to fire imagesloaded after each image is loaded

4.0.4

  • Call masonry.destroy() on component unmount

4.0.3

  • Fix layout of prepended elements

4.0.2

  • Fix peer dependency typos...(derp)

4.0.1

  • Update React peer dependencies to include >=15.0.0

4.0.0

  • Update masonry-layout to ^4.0.0

3.0.0

  • Add peer dependency on React >= 0.14
  • No longer need to pass in React or execute component as a function
  • Use NPM dependencies instead of forked dependencies
  • Allow addition of custom props

2.0.0

  • Remove < React 0.14 compatibility.
  • Compatible with React 0.14 and above only