Created
March 27, 2021 19:07
-
-
Save jrnxf/4d29d94ff1c2a0612f3a4edc684c26fb to your computer and use it in GitHub Desktop.
react-stack-grid overlapping fix
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, { 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> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍