Created
December 30, 2021 17:49
-
-
Save jckw/2a11bb7251483252deb2115c1d35044e to your computer and use it in GitHub Desktop.
Naive approach to Masonry layout in React with CSS Grid
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 from 'react' | |
interface Props { | |
colCount: number | |
gap: number | |
children: React.ReactNode[] | |
} | |
const PrimitiveMasonry: React.FC<Props> = ({ | |
colCount = 2, | |
gap = 40, | |
children, | |
}) => { | |
const columns = new Array(colCount).fill([]) | |
return ( | |
<div | |
style={{ | |
display: 'grid', | |
gridTemplateColumns: `repeat(${colCount}, 1fr)`, | |
gap, | |
}} | |
> | |
{columns.map((col, i) => ( | |
<div> | |
{children && children.filter((child, j) => j % colCount === i)} | |
</div> | |
))} | |
</div> | |
) | |
} | |
export default PrimitiveMasonry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment