Skip to content

Instantly share code, notes, and snippets.

@jckw
Created December 30, 2021 17:49
Show Gist options
  • Save jckw/2a11bb7251483252deb2115c1d35044e to your computer and use it in GitHub Desktop.
Save jckw/2a11bb7251483252deb2115c1d35044e to your computer and use it in GitHub Desktop.
Naive approach to Masonry layout in React with CSS Grid
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