Last active
March 17, 2017 21:50
-
-
Save scottdomes/485627b640ec4797f10232d189bc6a96 to your computer and use it in GitHub Desktop.
Starting ImageGrid Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react' | |
import Masonry from 'masonry-layout' | |
import './ImageGrid.css' | |
export default class ImageGrid extends Component{ | |
state = { allImagesLoaded: false } | |
imagesLoaded = 0 | |
handleImageLoad = () => { | |
this.imagesLoaded++ | |
if (this.imagesLoaded === this.props.profile.images.length) { | |
this.setState({ allImagesLoaded: true }) | |
} | |
} | |
createGrid() { | |
let grid = document.getElementById('ImageGrid') | |
if (grid) { | |
this.layoutGrid(grid) | |
} | |
} | |
layoutGrid(grid) { | |
const masonry = new Masonry( grid, { | |
itemSelector: '.grid-item', | |
columnWidth: '#grid-sizer', | |
percentPosition: true | |
}) | |
masonry.layout() | |
} | |
render() { | |
const { | |
profile | |
} = this.props | |
return ( | |
<div id="ImageGrid" className={!this.state.allImagesLoaded && 'loading'}> | |
<div id="grid-sizer"> | |
{ | |
profile.images.map(img => { | |
return ( | |
<img key={img} className="grid-item" src={img} onLoad={this.handleImageLoad}/> | |
); | |
}) | |
} | |
</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment