Skip to content

Commit 70465c7

Browse files
authored
Merge pull request #6 from Maxnelson997/sizing
resize grid items on props update
2 parents ccb88e6 + 9c23151 commit 70465c7

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/components/pinterestGrid.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,35 @@ import { connect } from 'react-redux';
66

77
import { updateFavoriteStatusWithID } from '../actions/index';
88

9+
const resizeMasonryItem = (item) => {
10+
var grid = document.getElementsByClassName('pinterest-grid-photos')[0]
11+
var rowGap = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-row-gap'))
12+
var rowHeight = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-auto-rows'));
13+
14+
let heightWithGap = item.querySelector('.pinterest-grid-photos__brick__photo').getBoundingClientRect().height + rowGap
15+
let rowHeightWithGap = rowHeight + rowGap
16+
var rowSpan = Math.floor(heightWithGap / rowHeightWithGap);
17+
18+
item.style.gridRowEnd = 'span ' + rowSpan;
19+
}
20+
21+
const resizeAllMasonryItems = () => {
22+
var allItems = document.getElementsByClassName('pinterest-grid-photos__brick');
23+
for(var i = 0; i < allItems.length; i++){
24+
resizeMasonryItem(allItems[i]);
25+
}
26+
}
27+
28+
929
class PinterestGrid extends Component {
1030

31+
componentDidUpdate() {
32+
resizeAllMasonryItems()
33+
}
34+
1135
constructor() {
1236
super()
1337

14-
const resizeMasonryItem = (item) => {
15-
var grid = document.getElementsByClassName('pinterest-grid-photos')[0]
16-
var rowGap = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-row-gap'))
17-
var rowHeight = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-auto-rows'));
18-
19-
let heightWithGap = item.querySelector('.pinterest-grid-photos__brick__photo').getBoundingClientRect().height+rowGap
20-
let rowHeightWithGap = rowHeight+rowGap
21-
var rowSpan = Math.floor(heightWithGap/rowHeightWithGap);
22-
23-
item.style.gridRowEnd = 'span '+rowSpan;
24-
}
25-
26-
const resizeAllMasonryItems = () => {
27-
var allItems = document.getElementsByClassName('pinterest-grid-photos__brick');
28-
for(var i=0;i<allItems.length;i++){
29-
resizeMasonryItem(allItems[i]);
30-
}
31-
}
32-
3338
var masonryEvents = ['load', 'resize'];
3439
masonryEvents.forEach(event => {
3540
window.addEventListener(event, resizeAllMasonryItems);

0 commit comments

Comments
 (0)