INFINITYUI
HomeComponentsDocsTemplates
Star

Getting Started

  • Introduction
  • Installation
  • CLI
  • Audit

Navigation

  • Spotlight Navbar
  • Glass DockNEW
  • Animated Tab Bar
  • Circle Menu
  • Magnet Tabs
  • Animated Sidebar
  • Apple Spotlight
  • Page TOC RailNEW

Text

  • Flip Text
  • Glitch Text
  • Liquid Text
  • Flip Fade Text
  • Mask Cursor Effect

Cards

  • Glow Border Card
  • Testimonials Card
  • Interactive Book
  • Trading CardsNEW
  • Hover Image
  • Chain of ThoughtNEW
  • Masonry Grid
  • Image Pile
  • Staggered Grid

Inputs

  • AI InputNEW
  • OTP Input
  • Leave Rating

Buttons

  • Social Flip Button
  • Creepy Button

Loaders

  • Jelly Loader
  • Rolling Ball Scroll
  • Glowing Scroll

Backgrounds

  • Light Lines
  • Perspective Grid
  • Liquid Ocean
  • Eagle Vision
  • Flow Scroll
  • Horizontal Scroll

Overlays

  • PersonaNEW
  • Infinite Moving Cards
  • Masked Avatars
  • Stacked Logos
  • Icon Wheel
  • Pixelated CarouselNEW
  • Pixelated Image Trail
  • Flip Scroll
  • Interactive Folder
  • Animated Folder IconNEW
  • Stack Scroll
  • Rubik Cube
Cards

Masonry Grid

#grid#masonry#gallery
Panel 1
Panel 2
Panel 3
Panel 4
Panel 5
Panel 6
Panel 7
Panel 8
Panel 9
Panel 10
implementedInfinityUI component

This component is fully implemented in InfinityUI and wired into the docs and registry flow.

Installation

Use the registry command to add the component source, and install any package dependencies if needed.

Infinity Registry

Install the component source into your project with the shadcn CLI.

npx shadcn@latest add https://infinityui-pearl.vercel.app/r/masonry-grid
Package Dependencies

Install the npm packages used by this component source.

npm install framer-motion

Component Code

Copy and paste this code into your component file.

tsx
"use client";

import { useState } from "react";
import Image from "next/image";
import { motion } from "framer-motion";

export interface MasonryGridProps {
    items: { image: string; title: string; description: string }[];
    columns?: number;
}

export function MasonryGrid({ items, columns }: MasonryGridProps) {
    const [imagesLoaded, setImagesLoaded] = useState<Record<string, boolean>>({});

    if (!items?.length) {
        return <div className="p-4 text-center text-sm text-zinc-400">No items to display</div>;
    }

    const columnCount = columns ?? 4;

    return (
        <div
            style={{ columns }}
            className={`${!columns ? "columns-1 sm:columns-2 md:columns-3 lg:columns-4" : ""} w-full max-w-4xl gap-2 overflow-y-auto p-3`}
        >
            {items.map((item, index) => {
                const rowIndex = Math.floor(index / columnCount);

                return (
                    <motion.div
                        key={`${item.title}-${index}`}
                        className="group relative mb-4 break-inside-avoid overflow-hidden rounded-xl border border-transparent p-1 hover:border-neutral-300 dark:hover:border-neutral-700"
                        initial={{ opacity: 0, y: 20 }}
                        animate={{
                            opacity: 1,
                            y: 0,
                            transition: {
                                duration: 0.5,
                                delay: rowIndex * 0.1,
                                ease: "easeOut",
                            },
                        }}
                        whileHover={{ scale: 1.05 }}
                    >
                        <div className="relative flex w-full flex-col items-start justify-start gap-1">
                            {!imagesLoaded[item.image] && (
                                <div className="absolute inset-0 h-[300px] w-full animate-pulse rounded-lg bg-neutral-500/50" />
                            )}
                            <Image
                                src={item.image}
                                alt={item.title}
                                width={400}
                                height={300}
                                sizes="(max-width: 640px) 100vw, (max-width: 768px) 50vw, (max-width: 1024px) 33vw, 25vw"
                                className={`h-auto w-full rounded-lg transition-transform duration-300 ${
                                    !imagesLoaded[item.image] ? "opacity-0" : "opacity-100"
                                }`}
                                onLoad={() =>
                                    setImagesLoaded((previous) => ({
                                        ...previous,
                                        [item.image]: true,
                                    }))
                                }
                                onError={() =>
                                    setImagesLoaded((previous) => ({
                                        ...previous,
                                        [item.image]: true,
                                    }))
                                }
                            />
                            {imagesLoaded[item.image] && (
                                <div className="w-full">
                                    <h3 className="text-sm font-medium">{item.title}</h3>
                                    <p className="mt-0 line-clamp-2 overflow-hidden text-xs text-neutral-500">
                                        {item.description}
                                    </p>
                                </div>
                            )}
                        </div>
                    </motion.div>
                );
            })}
        </div>
    );
}

export default MasonryGrid;