Skip to content

Instantly share code, notes, and snippets.

@jrnxf
Created March 27, 2021 19:07
Show Gist options
  • Save jrnxf/4d29d94ff1c2a0612f3a4edc684c26fb to your computer and use it in GitHub Desktop.
Save jrnxf/4d29d94ff1c2a0612f3a4edc684c26fb to your computer and use it in GitHub Desktop.
react-stack-grid overlapping fix
import React, { useEffect, useRef } from 'react'
import { useWindowSize } from '@react-hook/window-size'
import StackGrid from 'react-stack-grid'
export const Grid = ({ children }) => {
const ANIMATION_DURATION = 400
const [windowWidth] = useWindowSize()
const grid = useRef(null)
const resizeGrid = () => {
if (grid.current) {
setTimeout(() => {
grid.current.updateLayout()
}, ANIMATION_DURATION + 50) // 50 is a guess for buffer
}
}
useEffect(resizeGrid, [children, windowWidth])
return (
<StackGrid
gridRef={(r) => (grid.current = r)}
columnWidth={
windowWidth > 1440
? '25%'
: windowWidth > 1200
? '33.33%'
: windowWidth > 1024
? '50%'
: '100%'
}
duration={ANIMATION_DURATION}
gutterWidth={16}
gutterHeight={16}
>
{children}
</StackGrid>
)
}
@0xkarambit
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment