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.
columnsfixes the columns as a maximum (in a narrow slot fewer fit, and none goes under 160 px); without it as many as fit withminColumnWidth(256 px by default) andgap(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.
breakpointsgives 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 overcolumns, 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
Purchase
Idea for the shop
Accountant's phone number
Quote
Reminder
<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.
| Prop | Type | What it does |
|---|---|---|
breakpoints | Readonly<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. |
children | ReactNode | The pieces, in the order they are read. Each child is a piece. |
className | string | Merged with the library classes; it does not replace them. |
columns | number | How many columns at most: in a narrow slot fewer fit, and none goes under 160 px. Without it, as many as fit with `minColumnWidth`. |
gap | number | Gap between pieces and between columns, in pixels. 16 by default. |
label | string | What these pieces are ("Event photos"), for the screen reader. |
minColumnWidth | number | Minimum 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:
