Skip to content
EspañolEnglish
All components (283)

Masonry

Structure · Masonry

What it is and when to use it

Pieces of different heights in columns, each one under the shortest: a gallery, a wall of cards or notes.

When to use it

  • Content of very different heights that a grid would leave with gaps.
  • columns fixes the columns as a maximum (in a narrow slot fewer fit, and none goes under 160 px); without it as many as fit with minColumnWidth (256 px by default) and gap (16 px) between pieces come out.
  • Each child is a piece and stays in the document in the order it is given: tabbing and the screen reader go through them row by row, as they are read.
  • Placement is stable: adding pieces at the end does not move the ones already there, which a CSS column split does not give.
  • breakpoints gives the columns by the width of the wall itself: { 0: 1, 480: 2, 900: 3 } is “from this width in pixels, this many”. It takes precedence over columns, which stays as a cap.

When not to

  • Pieces of the same height: a grid with Grid.
  • A long list that scrolls without stopping: VirtualList.
  • A board with fixed columns and draggable cards: Board.

Keyboard: it adds none; the pieces are the ones that carry their own controls, and their tab order is the order of the pieces.

Accessibility: it is a list (role="list", with label as its name) of list items, in the given order. Placement is done with transform, so it does not change what the screen reader reads. With reduced motion the pieces are repositioned without animation.

Performance: it measures each piece with ResizeObserver and repositions them only when their height or the width of the wall changes. Until they are measured (server and hydration) they come out in a single column, with no jumps.

Fine-tuning: no variables of its own; the wall takes the width of its slot and each piece the width of its column.

What it looks like

Team meeting

Review the quarter calendar and split up the pending deliveries.

Purchase

Paper.

Idea for the shop

A new-arrivals section on the home page with the five latest items and their availability by size.

Accountant's phone number

Call on Thursday morning.

Quote

Ask for three quotes and compare them by deadline, warranty and maintenance before deciding.

Reminder

Renew the domain.

<Masonry label="Notes" minColumnWidth={220} gap={16}>
  <Card title="Team meeting">Review the quarter's calendar.</Card>
  <Card title="Purchase">Paper.</Card>
  <Card title="Quote">Get three quotes and compare them before deciding.</Card>
</Masonry>

Props

Import it with import { Masonry } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
breakpointsReadonly<Record<number, number>>The columns by the width of the wall itself (not the window's, which is what makes it work in a narrow slot): `{ 0: 1, 480: 2, 900: 3, 1280: 4 }`, “from this width in pixels, this many”. It takes precedence over `columns`, which stays as a cap when the slot cannot fit that many.
childrenReactNodeThe pieces, in the order they are read. Each child is a piece.
classNamestringMerged with the library classes; it does not replace them.
columnsnumberHow many columns at most: in a narrow slot fewer fit, and none goes under 160 px. Without it, as many as fit with `minColumnWidth`.
gapnumberGap between pieces and between columns, in pixels. 16 by default.
labelstringWhat these pieces are ("Event photos"), for the screen reader.
minColumnWidthnumberMinimum width of a column in pixels when `columns` is not set. 256 by default.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: