• Stars
    star
    1,436
  • Rank 32,788 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React.js component for using @desandro's Masonry

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
  2. Basic usage
  3. Custom props
  4. Accessing Masonry instance
  5. Images Loaded Options
  6. 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>
        )
    }
}

More Repositories

1

react-masonry-mixin

A React.js mixin for using @desandro's Masonry
JavaScript
174
star
2

json-populate

Tool for populating JSON data with circular references. Sort of like Falcor and GraphQL, but for plain JSON with a transparent interface.
JavaScript
147
star
3

hearsay-frontend

Hearsay frontend application
JavaScript
93
star
4

express-passport-app

Express 4.x application with Passport and React (DEPRECATED)
JavaScript
83
star
5

react-packery-component

A React.js component for using @desandro's Packery
JavaScript
43
star
6

react-packery-mixin

A React mixin for using Metafizzy Packery
JavaScript
21
star
7

spotipi

Cast Spotify to your Raspberry Pi via the browser!
JavaScript
13
star
8

fluxity-starter-auth

Starter-kit with React, (custom) Flux, React Router and Passport. So isomorphic that you can even turn off browser JavaScript.
JavaScript
12
star
9

tailspin

Reversible Javascript interpreter (in Javascript)
JavaScript
11
star
10

react-redux-router-universal

React, Redux and React Router universal rendering example
JavaScript
11
star
11

collectify

Functional streaming news aggregator. For educational purposes. And fun :-)
JavaScript
11
star
12

congregator-sitescraper

Website crawler
JavaScript
9
star
13

react-redux-autosave

React and Redux example with form autosave
JavaScript
8
star
14

interprit

Interpreter and effects engine (think redux-saga, except on steriods with erlang actor and golang error handling semantics)
JavaScript
8
star
15

event-clock

Create timed event callbacks
JavaScript
7
star
16

congregator-rssreader

RSS feed parser and mapper
JavaScript
5
star
17

everscroll

Really simple infinite scroll
JavaScript
5
star
18

express-basic-app

(DEPRECATED) Basic Application made with Node.js and Express 4.x and React
JavaScript
5
star
19

fluxomorph

Minimal Isomorphic Flux Implementation
JavaScript
4
star
20

site-parser

Parse websites with JSON templates
JavaScript
4
star
21

building-abstractions-demo-app

Describing a stepwise form as data and mapping it to a user interface
JavaScript
4
star
22

unexceptional

Exception safe functions and promises by converting thrown errors to values (like Go and Lua)
JavaScript
4
star
23

instagram-api-lib

Instagram REST API Library
JavaScript
3
star
24

ndc-isomorphic-flux

Isomorphic JavaScript Demo App with Flux for NDC 2015
JavaScript
3
star
25

react-redux-universal-starter

Starter-kit for React and Redux with universal rendering
JavaScript
3
star
26

react-redux-router-forms

React + Redux + React Router + Forms example
JavaScript
3
star
27

redux-reactor

Redux middleware for handling side effects by reacting to dispatched actions
JavaScript
3
star
28

tic-tac-toe

Example of modeling problems with data and functions using JavaScript / React.
JavaScript
3
star
29

hearsay-collector

Hearsay content aggregator
JavaScript
3
star
30

volatile

Pass information on the web - that evaporates when accessed
JavaScript
3
star
31

congregator-jsonfetcher

JSON fetcher and mapper
JavaScript
3
star
32

hoardr

Save all your stuff from the web in article form (a self hosted pocket clone)
JavaScript
2
star
33

academy-git-test

Git Training
JavaScript
2
star
34

mod-op

Modulo operator for JavaScript numbers
JavaScript
2
star
35

prop-by-string

Get and set an object property by string literal.
JavaScript
2
star
36

ndc-isomorphic

Isomorphic JavaScript Demo App for NDC 2015
JavaScript
2
star
37

HearsayNative

Native iOS app for hearsay.me
JavaScript
2
star
38

resolve-relative-url

Resolve a possibly relative URL agains a source URL
JavaScript
2
star
39

w23-twitter-api

Twitter API
JavaScript
2
star
40

colorsine

Colors 'n Waves http://www.vullum.io/colorsine/
JavaScript
2
star
41

w23-twitter-frontend

JavaScript W23 Twitter Frontend Application
JavaScript
1
star
42

screenfetch

Web service for capturing full page screenshots of web pages
JavaScript
1
star
43

snow-drop

Snow Simulator
JavaScript
1
star
44

hearsay-common

Hearsay common resources
JavaScript
1
star
45

pokedex-demo

Pokedex Academy Demo
JavaScript
1
star
46

license-test

1
star
47

testapp

Test app
JavaScript
1
star
48

asyncify

Make a sync function async
JavaScript
1
star
49

aw-twitter-api

JavaScript W21 Twitter API
JavaScript
1
star
50

feed-mapper

Parse and map RSS feeds using JSON templates
JavaScript
1
star
51

academy-quiz-app-typescript

TypeScript
1
star
52

twitter-clone

Twitter Clone
JavaScript
1
star
53

simple-express-boilerplate

(DEPRECATED) Express boilerplate with the essentials
JavaScript
1
star
54

academy-twitter-clone

JavaScript
1
star
55

react-karma-boilerplate

React and Karma boilerplate
JavaScript
1
star
56

event-schedule

Create scheduled event callbacks
JavaScript
1
star
57

interprocess-push-stream

Streaming push interface for use between processes via Redis
JavaScript
1
star
58

object-transform

A small library for object transforms for use in functional programming.
JavaScript
1
star
59

academy-twitter-api

Academy Twitter API (server)
JavaScript
1
star
60

hearsay-admin

Hearsay Admin Interface
JavaScript
1
star
61

interprocess-pull-stream

Streaming interface for use between processes (with backpressure) via Redis
JavaScript
1
star
62

academy-typescript-demo

JavaScript
1
star
63

redis-cache-client

Redis cache client wrapper / abstraction
JavaScript
1
star
64

crud-examples

CRUD Examples for Academy JavaScript Fall 2019
JavaScript
1
star
65

library-code-along

Library Code Along
JavaScript
1
star
66

pokedex-academy

Pokedex Academy Demo
JavaScript
1
star
67

json-mapper

Map JSON API calls using JSON templates
JavaScript
1
star
68

blip

A minimal personal blogging platform
JavaScript
1
star
69

social-data

Get social shares data for a url from the most popular social networks
JavaScript
1
star
70

webshop-lecture

From design to application with React
JavaScript
1
star
71

aw-twitter-frontend

JavaScript W21 Twitter React Frontend
JavaScript
1
star
72

academy-twitter-frontend

Academy Twitter Frontend (React)
JavaScript
1
star
73

test

Test App React
JavaScript
1
star