# Masonry Grid

> A masonry layout for React that keeps source order: a CSS grid with measured row spans, so keyboard and screen reader order match the visual flow.

- Kind: Component (Layout)
- Docs: https://ui.minidev.pro/docs/masonry-grid
- Source (registry JSON with the full file): https://ui.minidev.pro/r/masonry-grid.json
- License: MIT. One file, yours to edit after install.

## When to use

A CSS grid with tiny rows where each item spans as many rows as its measured height. Items flow left to right, so keyboard and screen reader order match what you see, and the column count follows the container width. Use it for galleries, notes and testimonials; for equal-height cards use a plain grid. Each child is measured, so pass one element per item (no fragments). Other div props (aria-label, role, id) go on the grid element.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/masonry-grid
```

Writes `components/ui/masonry-grid.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/masonry-grid.json`.

### npm package

```bash
npm i minidev-ui-kit
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { MasonryGrid } from "@/components/ui/masonry-grid"
```

From the npm package:

```tsx
import { MasonryGrid } from "minidev-ui-kit/ui/masonry-grid"
```

## Usage

```tsx
import { MasonryGrid } from "@/components/ui/masonry-grid"

export function Example() {
  return (
    <MasonryGrid />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `children` | `React.ReactNode` | - | no | One element per tile. Keyed children keep their measured height when reordered. |
| `minColumnWidth` | `number` | `180` | no | Minimum column width in pixels. |
| `gap` | `number` | `12` | no | Gap between tiles in pixels, both directions. Keep it on the 4px grid. |
| `className` | `string` | - | no | Classes for the grid. |

Also accepts every prop of `<div>`, passed through to the root element.

## Examples

### Note cards

Cards of different lengths pack into columns while keeping source order, so tabbing through them still reads left to right.

```tsx
const NOTES = [
  { author: "Maya Chen", text: "Northwind wants net 45 on the next renewal. Fine if the volume commit stays." },
  { author: "Theo Okafor", text: "Bank sync matched 212 of 219 transfers this week. The 7 misses were missing references." },
  { author: "Priya Raman", text: "Pricing page copy is drafted. Needs a pass on the enterprise tier wording, the current version promises SSO on every plan which is not true, and a decision on whether we show annual prices first." },
  { author: "Sam Lowe", text: "Deploy window moved to 17:00 Thursdays." },
  { author: "Maya Chen", text: "Helix Biotech asked for a PO field on invoices. Two other customers asked for the same thing in September, so it should move up the list." },
  { author: "Theo Okafor", text: "INV-2033 reminder sent." },
]

<MasonryGrid aria-label="Team notes" minColumnWidth={200} gap={16} className="max-w-2xl">
  {NOTES.map((n, i) => (
    <div key={i} className="rounded-xl border border-border bg-surface p-4 shadow-raised">
      <p className="text-sm leading-[1.55] text-fg">{n.text}</p>
      <p className="mt-3 text-xs text-fg-muted">{n.author}</p>
    </div>
  ))}
</MasonryGrid>
```

### Compact media tiles

Narrow columns and a small gap for a photo wall; each tile keeps its own aspect ratio.

```tsx
const TILES = [
  { id: 1, ratio: 1, tint: 10 },
  { id: 2, ratio: 3 / 4, tint: 18 },
  { id: 3, ratio: 4 / 3, tint: 6 },
  { id: 4, ratio: 1, tint: 14 },
  { id: 5, ratio: 2 / 3, tint: 22 },
  { id: 6, ratio: 4 / 3, tint: 8 },
  { id: 7, ratio: 3 / 4, tint: 12 },
  { id: 8, ratio: 1, tint: 20 },
]

<MasonryGrid minColumnWidth={120} gap={8} className="max-w-xl">
  {TILES.map((t) => (
    <AspectRatio
      key={t.id}
      ratio={t.ratio}
      className="grid place-items-center rounded-lg border border-border text-xs text-fg-subtle"
      style={{ background: "color-mix(in oklch, var(--accent) " + t.tint + "%, var(--surface))" }}
    >
      {t.id}
    </AspectRatio>
  ))}
</MasonryGrid>
```

## Accessibility

- DOM order matches visual order, so keyboard and screen reader users meet tiles in the order sighted users read them.
- The grid and each tile wrapper are plain `<div>`s. Other div props go on the grid, so name it with `aria-label` or `aria-labelledby` when it needs a name. Keep `role="list"` off unless every child also has `role="listitem"`.

## FAQ

### How is this different from CSS columns?

CSS `columns` fills the first column top to bottom, then the next, so Tab order jumps around the screen. MasonryGrid keeps items left to right, row by row. It measures each tile and sets `grid-row-end: span N` on rows of 4px.

### Why do tiles overlap before the page hydrates?

Heights are measured in the browser, in `useLayoutEffect`. Server HTML has every tile spanning one 4px row, so tiles stack on top of each other until React runs. On a server-rendered page, keep the grid below the fold or render it only after mount.

### Do images that load late break the layout?

No. A `ResizeObserver` watches each tile and updates its span when the height changes. Set `width` and `height` on images anyway so tiles do not jump.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Related

- [Image Gallery](https://ui.minidev.pro/docs/image-gallery/md): A responsive photo grid of square tiles, two columns on mobile and three on larger screens, each image cropped to fill. Built with React and Tailwind.
- [Accordion](https://ui.minidev.pro/docs/accordion/md): Accessible React accordion on Base UI, with bordered items, a rotating chevron and animated panels. Keyboard navigable, with one or many items open at once.
- [App Shell](https://ui.minidev.pro/docs/app-shell/md): Tailwind app shell layout with a fixed width sidebar slot, a topbar slot and a scrolling main area. The sidebar hides below the md breakpoint for mobile.
- [Aspect Ratio](https://ui.minidev.pro/docs/aspect-ratio/md): Shadcn compatible aspect ratio wrapper that locks media to a ratio such as 16:9 or 1:1 using CSS aspect-ratio, with overflow clipped for images and video.
- [Card](https://ui.minidev.pro/docs/card/md): Shadcn compatible card with header, title, description, action, content and footer slots, a compact size and edge to edge images. Built for Tailwind v4.
- [Center](https://ui.minidev.pro/docs/center/md): Layout helper that centers content horizontally and vertically in a narrow, text-centered column. Handy for empty states, auth screens and simple heroes.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
