Photos or cards of different heights, packed into columns in their own order.
- 2.9 kB JS 2.5 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 35 tests
Dawn over LofotenInes Duarte Harbour, low tideKenji Mori Salt flatsAda Okafor Lavender rows, ValensolePriya Raman Rice terraces at noonTomas Berg Fog in the pinesJo Lovelace Dune ridgeRafael Costa Glacier lagoonBen Okafor Orchard in bloomMira Haas Night market lanternsLin Zhao Cliff path, AlgarveInes Duarte Tide poolsKenji Mori
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Masonry } from "./ui/uicontext/components/Masonry.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { picture } from "./pictures.ts"const styles = stylex.create({ wrap: { inlineSize: "100%" }, card: { margin: 0, display: "grid", gridTemplateRows: "minmax(0, 1fr) auto", blockSize: "100%", overflow: "hidden", borderRadius: corner.card, backgroundColor: color.raised }, image: { display: "block", inlineSize: "100%", blockSize: "100%", objectFit: "cover" }, caption: { display: "grid", alignContent: "center", blockSize: 44, paddingInline: space.md, fontSize: font.sm, color: color.fg }, name: { overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontWeight: 500 }, by: { overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontSize: font.xs, color: color.muted },})const photos = [ { id: "p1", title: "Dawn over Lofoten", by: "Ines Duarte", w: 1200, h: 1600 }, { id: "p2", title: "Harbour, low tide", by: "Kenji Mori", w: 1600, h: 1000 }, { id: "p3", title: "Salt flats", by: "Ada Okafor", w: 1200, h: 1200 }, { id: "p4", title: "Lavender rows, Valensole", by: "Priya Raman", w: 1000, h: 1500 }, { id: "p5", title: "Rice terraces at noon", by: "Tomas Berg", w: 1600, h: 900 }, { id: "p6", title: "Fog in the pines", by: "Jo Lovelace", w: 1080, h: 1350 }, { id: "p7", title: "Dune ridge", by: "Rafael Costa", w: 1500, h: 1000 }, { id: "p8", title: "Glacier lagoon", by: "Ben Okafor", w: 1200, h: 1800 }, { id: "p9", title: "Orchard in bloom", by: "Mira Haas", w: 1400, h: 1050 }, { id: "p10", title: "Night market lanterns", by: "Lin Zhao", w: 1000, h: 1250 }, { id: "p11", title: "Cliff path, Algarve", by: "Ines Duarte", w: 1600, h: 1067 }, { id: "p12", title: "Tide pools", by: "Kenji Mori", w: 1200, h: 1500 },]// a photo library: pictures of every shape, each with its title and who took itexport const PhotoWall = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( Masonry(h, { label: "Photos", items: photos.map((p) => ({ key: p.id, ratio: p.h / p.w, extra: 44 })), item: (_, i) => { const p = photos[i]! return h.figure.attrs(sx(styles.card)).children( h.img.attrs({ src: picture(p.w, p.h, i), alt: p.title, loading: "lazy", width: String(p.w), height: String(p.h), ...sx(styles.image) }), h.figcaption.attrs(sx(styles.caption)).children(h.span.attrs(sx(styles.name)).children(p.title), h.span.attrs(sx(styles.by)).children(p.by)), ) }, }), )When to use
Photos or cards of different heights, packed into columns in their own order. Use Gallery instead when the cards are records with fields to show.
Usage
import { Masonry } from "./ui/uicontext/components/Masonry.ts"interface Photo { readonly id: string readonly url: string readonly alt: string readonly width: number readonly height: number}export const Photos = <M>(h: Html.H<M>, photos: ReadonlyArray<Photo>) => Masonry(h, { label: "Photos", items: photos.map((p) => ({ key: p.id, ratio: p.height / p.width })), item: (_, i) => h.img.attrs({ src: photos[i]!.url, alt: photos[i]!.alt }), })Examples
Cards, at most three across
extra is the card's fixed part under its picture. maxColumns caps the columns for wider cards.
Charred leeks, romesco35 min · serves 4
Miso butter noodles20 min · serves 2
Slow-roast tomatoes2 h 30 min · serves 6
Lemon ricotta pancakes25 min · serves 4
Lamb shoulder, flatbreads4 h · serves 8
Green shakshuka30 min · serves 2
Burnt basque cheesecake1 h 10 min · serves 10
Crispy rice salad25 min · serves 3
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Masonry } from "./ui/uicontext/components/Masonry.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"import { picture } from "./pictures.ts"const styles = stylex.create({ wrap: { inlineSize: "100%" }, card: { display: "grid", gridTemplateRows: "minmax(0, 1fr) auto", blockSize: "100%", overflow: "hidden", borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, color: color.fg, textDecoration: "none", outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` }, outlineOffset: 2, }, image: { display: "block", inlineSize: "100%", blockSize: "100%", objectFit: "cover" }, body: { display: "grid", alignContent: "start", gap: space.xxs, blockSize: 64, boxSizing: "border-box", paddingBlock: space.sm, paddingInline: space.md }, name: { fontSize: font.sm, fontWeight: 600, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, meta: { fontSize: font.xs, color: color.muted, fontVariantNumeric: "tabular-nums" },})const recipes = [ { id: "r1", name: "Charred leeks, romesco", minutes: 35, serves: 4, w: 4, h: 5 }, { id: "r2", name: "Miso butter noodles", minutes: 20, serves: 2, w: 1, h: 1 }, { id: "r3", name: "Slow-roast tomatoes", minutes: 150, serves: 6, w: 3, h: 4 }, { id: "r4", name: "Lemon ricotta pancakes", minutes: 25, serves: 4, w: 4, h: 3 }, { id: "r5", name: "Lamb shoulder, flatbreads", minutes: 240, serves: 8, w: 2, h: 3 }, { id: "r6", name: "Green shakshuka", minutes: 30, serves: 2, w: 1, h: 1 }, { id: "r7", name: "Burnt basque cheesecake", minutes: 70, serves: 10, w: 4, h: 5 }, { id: "r8", name: "Crispy rice salad", minutes: 25, serves: 3, w: 16, h: 9 },]const time = (m: number) => (m < 60 ? `${m} min` : `${Math.floor(m / 60)} h${m % 60 === 0 ? "" : ` ${m % 60} min`}`)// a recipe wall: whole cards are links, at most three acrossexport const RecipeWall = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( Masonry(h, { label: "Recipes", maxColumns: 3, items: recipes.map((r) => ({ key: r.id, ratio: r.h / r.w, extra: 64 })), item: (_, i) => { const r = recipes[i]! return h.a.attrs({ href: `#${r.id}`, ...sx(styles.card) }).children( h.img.attrs({ src: picture(r.w * 100, r.h * 100, i + 2), alt: "", loading: "lazy", ...sx(styles.image) }), h.span.attrs(sx(styles.body)).children(h.span.attrs(sx(styles.name)).children(r.name), h.span.attrs(sx(styles.meta)).children(`${time(r.minutes)} · serves ${r.serves}`)), ) }, }), )API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What the wall holds, for screen readers: "Moodboard". |
itemsrequired | MasonryItem[] | — | — |
itemrequired | (item: MasonryItem, index: number) => View.Content | — | Draws an item; it fills its box. |
maxColumns | 1 | 2 | 3 | 4 | 4 | 4 by default; fewer for wide cards. |
style | Style | — | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/masonry