Masonry Grid
Items of any height packed into columns by width, the source order kept for reading and Tab — laid out again whenever anything changes size, the moves animated; native where the browser can.
Usage
Usage
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';
/** Photos of different heights: as many columns of at least 200px as fit, each photo into the shortest — in a list, the order the source’s, row by row. */@Component({Anatomy
Two parts on your elements: the grid (a ul when the items are a list) and each item. The gaps are your classes.
import { ScMasonryGrid, ScMasonryGridItem } from '@semantic-components/ui';<ul scMasonryGrid [columnWidth]="200" class="gap-4"> <li scMasonryGridItem>…</li> <li scMasonryGridItem [span]="2">…</li></ul>Layout
- Columns: as many of at least
columnWidthas fit the grid's width (at mostmaxColumns) — no breakpoints. The gaps: your classes (gap-4,gap-x-6 gap-y-2), read from the page. - Each item, in the source's order, goes into the shortest column — the first of the columns within 1em of it, as CSS
grid-lanesdoes, so the rows read evenly — or,sequential, into the next in turn.spanmakes it several columns wide. - Before it is measured — on the server, at the first paint — it is a plain grid of the same columns and gaps: rows, never a pile. Then laid out whenever the grid or an item changes size (an image loading, text wrapping) or items come and go; the items move into place (not under reduced motion).
- Right to left, the first column is on the right. Where the browser has
display: grid-lanes, it lays out the shortest-column grid itself (data-nativeon the grid).
Accessibility
- When the items are a list, use
ulandli: the grid adds no roles. - The order on screen follows the source, row by row — the order of reading and of Tab: keep the source in the order it should be read.
Examples
Cards and spans
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardDescription, ScCardHeader, ScCardTitle, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';Order
Shortest column
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Sequential
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';
/** Into the shortest column (balanced) — or, sequential, each into the next column in turn (predictable): the same tiles, both ways. */@Component({ selector: 'app-order-masonry-grid-demo',Load more
- 1
- 2
- 3
- 4
- 5
- 6
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton, ScFrame, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';
/** Items added, or removed: the grid lays out again, and the others move into place (not under reduced motion). */@Component({Columns and gaps
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';
/** maxColumns caps the count however wide; the gaps are your classes — wider across than down here (gap-x-6 gap-y-2). */@Component({ selector: 'app-columns-masonry-grid-demo',RTL
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
import { Dir } from '@angular/cdk/bidi';import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScMasonryGrid, ScMasonryGridItem,} from '@semantic-components/ui';
/** In a right-to-left page: the first column on the right, the rows read from the right. */@Component({API reference
Each part takes class, merged over its own (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScMasonryGrid | [scMasonryGrid] | The grid: columns by width, the items placed. exportAs: columns(), native(). |
ScMasonryGridItem | [scMasonryGridItem] | An item, any height; span columns wide. |
| Input | Type | Default | Description |
|---|---|---|---|
columnWidth | number | 240 | The least width of a column, in pixels: as many as fit. |
maxColumns | number | null | null | At most this many columns. |
sequential | boolean | false | Each item into the next column in turn, not the shortest. |
| Input | Type | Default | Description |
|---|---|---|---|
span | number | 1 | How many columns wide. |