Gallery

Media

Responsive image gallery grid.

Preview

Coding
Laptop
Code
Desktop

Usage

example.jsx
import { Gallery } from "@/components/ui/gallery";

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

Source Code

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

gallery.jsx
"use client";

import { forwardRef, useState } from "react";
import { cn } from "@/lib/utils";

const Gallery = forwardRef(({ className, images = [], columns = 3, ...props }, ref) => {
  return (
    <div ref={ref} className={cn("grid gap-2",
      columns === 2 && "grid-cols-2",
      columns === 3 && "grid-cols-3",
      columns === 4 && "grid-cols-4",
      className
    )} {...props}>
      {images.map((img, i) => (
        <div key={i} className="group relative aspect-square overflow-hidden rounded-lg bg-muted">
          <img src={typeof img === "string" ? img : img.src} alt={typeof img === "string" ? "" : img.alt || ""}
            className="h-full w-full object-cover transition-transform group-hover:scale-105"
          />
        </div>
      ))}
    </div>
  );
});
Gallery.displayName = "Gallery";

export { Gallery };

Quick Install

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

npm install clsx tailwind-merge