Created
November 27, 2024 19:46
-
-
Save brijr/9ddb430c15f383b8d49feb10fbe90636 to your computer and use it in GitHub Desktop.
Tailwind Masonry 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 * as React from "react"; | |
| import { cn } from "@/lib/utils"; | |
| import { Card } from "./card"; | |
| import { Project } from "@/lib/data"; | |
| export type GridProps = React.HTMLAttributes<HTMLElement> & { | |
| columns?: 1 | 2 | 3 | 4; | |
| projects: Project[]; | |
| animation?: boolean; | |
| }; | |
| export const Grid = ({ | |
| className, | |
| projects, | |
| columns = 4, | |
| animation, | |
| }: GridProps) => { | |
| const columnStyles = { | |
| 1: "sm:w-full", | |
| 2: "sm:w-1/2", | |
| 3: "sm:w-1/2 md:w-1/3", | |
| 4: "sm:w-1/2 md:w-1/4", | |
| }; | |
| const columnClass = columnStyles[columns]; | |
| return ( | |
| <div | |
| className={cn( | |
| "-mx-2 flex flex-wrap p-4 sm:p-8", | |
| animation ? "fade-up" : "", | |
| className, | |
| )} | |
| > | |
| {Array.from({ length: columns }).map((_, columnIndex) => ( | |
| <div | |
| key={columnIndex} | |
| className={cn("flex w-full flex-col px-2", columnClass)} | |
| > | |
| {projects | |
| .filter( | |
| (_: Project, index: number) => index % columns === columnIndex, | |
| ) | |
| .map((project: Project) => ( | |
| <Card key={project.id} project={project} /> | |
| ))} | |
| </div> | |
| ))} | |
| </div> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment