Infinite Masonry

Infinite Masonry: Responsive virtualized masonry that measures variable-height cards and loads more data as the user nears the end. Free MIT React & Next.js source with Motion and Tailwind CSS, live preview, CLI install and docs.

Install Infinite Masonry

npx motion-ui-skill add infinite-masonry

Components install as source into your project. The interactive page also includes preview, code, documentation and install tabs.

Dependencies

  • @tanstack/react-virtual
  • clsx
  • lucide-react
  • motion
  • react
  • tailwind-merge

Source files

Infinite Masonry

Responsive virtualized masonry that measures variable-height cards and loads more data as the user nears the end.

Install

npx motion-ui-skill add infinite-masonry

Dependencies

  • @tanstack/react-virtual
  • clsx
  • lucide-react
  • motion
  • next
  • react
  • tailwind-merge

Usage

"use client";

import Image from "next/image";
import { useCallback, useState } from "react";
import { InfiniteMasonry } from "@/components/motion/infinite-masonry";

type GalleryItem = {
  id: string;
  title: string;
  category: string;
  image: string;
  imageHeight: number;
};

const PAGE_SIZE = 6;
const MAX_ITEMS = 42;

const gallerySource = [
  {
    title: "Soft geometry",
    category: "Architecture",
    image:
      "https://images.unsplash.com/photo-1511818966892-d7d671e672a2?auto=format&fit=crop&w=720&q=80",
    imageHeight: 260,
  },
  {
    title: "Open horizon",
    category: "Landscape",
    image:
      "https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?auto=format&fit=crop&w=720&q=80",
    imageHeight: 190,
  },
  {
    title: "Working rhythm",
    category: "Workspace",
    image:
      "https://images.unsplash.com/photo-1497366754035-f200968a6e72?auto=format&fit=crop&w=720&q=80",
    imageHeight: 230,
  },
  {
    title: "Shared table",
    category: "Studio",
    image:
      "https://images.unsplash.com/photo-1524758631624-e2822e304c36?auto=format&fit=crop&w=720&q=80",
    imageHeight: 300,
  },
  {
    title: "In session",
    category: "People",
    image:
      "https://images.unsplash.com/photo-1521737711867-e3b97375f902?auto=format&fit=crop&w=720&q=80",
    imageHeight: 210,
  },
  {
    title: "After hours",
    category: "Office",
    image:
      "https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=720&q=80",
    imageHeight: 280,
  },
] as const;

function createItems(start: number, count: number): GalleryItem[] {
  return Array.from({ length: count }, (_, offset) => {
    const index = start + offset;
    const source = gallerySource[index % gallerySource.length];

    return {
      ...source,
      id: `gallery-${index}`,
      title: `${source.title} ${Math.floor(index / gallerySource.length) + 1}`,
    };
  });
}

export function InfiniteMasonryPreview() {
  const [items, setItems] = useState(() => createItems(0, 12));
  const [loading, setLoading] = useState(false);
  const hasMore = items.length < MAX_ITEMS;

  const loadMore = useCallback(async () => {
    if (loading || !hasMore) return;
    setLoading(true);

    await new Promise((resolve) => setTimeout(resolve, 650));
    setItems((current) => {
      const count = Math.min(PAGE_SIZE, MAX_ITEMS - current.length);
      return count > 0
        ? [...current, ...createItems(current.length, count)]
        : current;
    });
    setLoading(false);
  }, [hasMore, loading]);

  return (
    <div className="w-full max-w-5xl">
      <div className="mb-3 flex items-end justify-between gap-4 px-1">
        <div>
          <p className="text-sm font-semibold text-foreground">Visual index</p>
          <p className="mt-0.5 text-xs text-muted-foreground">
            Scroll to load the next page
          </p>
        </div>
        <p className="font-mono text-xs tabular-nums text-muted-foreground">
          {items.length} items
        </p>
      </div>

      <InfiniteMasonry
        items={items}
        getItemKey={(item) => item.id}
        renderItem={(item) => (
          <figure className="overflow-hidden rounded-2xl border border-border bg-card">
            <div
              className="relative w-full overflow-hidden"
              style={{ height: item.imageHeight }}
            >
              <Image
                src={item.image}
                alt={item.title}
                fill
                sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 25vw"
                className="object-cover"
              />
            </div>
            <figcaption className="flex items-start justify-between gap-3 p-3">
              <p className="text-sm font-medium text-foreground">
                {item.title}
              </p>
              <p className="shrink-0 text-[10px] uppercase tracking-wider text-muted-foreground">
                {item.category}
              </p>
            </figcaption>
          </figure>
        )}
        onLoadMore={loadMore}
        hasMore={hasMore}
        loading={loading}
        estimateSize={(item) => item.imageHeight + 66}
        ariaLabel="Visual inspiration gallery"
        className="h-[34rem]"
      />
    </div>
  );
}

API Reference

InfiniteMasonry

Prop Type Default Required Description
items readonly T[] — Yes —
getItemKey (item: T, index: number) => InfiniteMasonryKey — Yes —
renderItem (item: T, index: number) => ReactNode — Yes —
onLoadMore () => void | Promise<void> — Yes —
hasMore boolean — Yes —
loading boolean false No —
error ReactNode — No —
onRetry (() => void) — No —
estimateSize ((item: T, index: number) => number) () => 240 No —
renderLoadingItem ((index: number) => ReactNode) (index) => <DefaultLoadingItem index={index} /> No —
emptyState ReactNode <DefaultEmptyState /> No —
endState ReactNode — No —
minColumnWidth number 208 No —
maxColumns number 4 No —
gap number 12 No —
overscan number 4 No —
prefetch number 3 No —
animateItems boolean true No —
ariaLabel string Infinite masonry feed No —
className string — No —
contentClassName string — No —
itemClassName string — No —

Browse this category