Grid

Layout

CSS Grid layout with responsive columns.

Preview

1
2
3
4
5
6

Usage

example.jsx
import { Grid } from "@/components/ui/grid";

export default function Example() {
  return <Grid />;
}

Source Code

Copy this file into components/ui/grid.jsx in your project.

grid.jsx
import { forwardRef } from "react";
import { cn } from "@/lib/utils";

const Grid = forwardRef(({ className, cols = 1, gap = 4, ...props }, ref) => (
  <div
    ref={ref}
    className={cn(
      "grid",
      cols === 1 && "grid-cols-1",
      cols === 2 && "grid-cols-2",
      cols === 3 && "grid-cols-3",
      cols === 4 && "grid-cols-4",
      cols === 5 && "grid-cols-5",
      cols === 6 && "grid-cols-6",
      gap === 2 && "gap-2",
      gap === 4 && "gap-4",
      gap === 6 && "gap-6",
      gap === 8 && "gap-8",
      className
    )}
    {...props}
  />
));
Grid.displayName = "Grid";

export { Grid };

Quick Install

Make sure you have the cn() utility set up. It requires clsx and tailwind-merge.

npm install clsx tailwind-merge