Thousands of cards in a scrolling grid that draws only the ones in view, with as many columns as fit.
- 1.7 kB JS 5.2 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 13 tests
4,800 photos
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"import { VirtualCardGrid } from "./ui/uicontext/components/VirtualCardGrid.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.sm, inlineSize: "100%" }, bar: { display: "flex", alignItems: "center", justifyContent: "space-between", gap: space.md, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" }, jump: { display: "flex", gap: space.xs }, card: { display: "grid", gridTemplateRows: "minmax(0, 1fr) auto", margin: 0, blockSize: "100%", overflow: "hidden", borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, }, image: { minBlockSize: 0, backgroundImage: "linear-gradient(160deg, color-mix(in srgb, var(--tone) 35%, white), var(--tone))" }, meta: { display: "grid", gap: 2, paddingBlock: space.sm, paddingInline: space.md }, name: { fontSize: font.sm, fontWeight: 500, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, detail: { fontSize: font.xs, color: color.muted, fontVariantNumeric: "tabular-nums" }, button: { paddingBlock: space.xs, minBlockSize: { default: 0, "@media (pointer: coarse)": 44 }, minInlineSize: { default: 0, "@media (pointer: coarse)": 44 }, paddingInline: space.sm, borderWidth: stroke.control, borderStyle: "solid", borderColor: color.line, borderRadius: corner.control, backgroundColor: color.panel, color: color.fg, fontFamily: "inherit", fontSize: font.xs, cursor: "pointer", },})const count = 4_800const tones = ["#f97316", "#38bdf8", "#a855f7", "#fb7185", "#84cc16", "#94a3b8"]const places = ["Lisbon", "Kyoto", "Oaxaca", "Tromsø", "Cape Town", "Hobart", "Reykjavík", "Valparaíso"]// a photo's details from its number, so 4,800 cards need no data up frontconst photo = (i: number) => ({ name: `IMG_${String(2041 + i).padStart(5, "0")}.heic`, place: places[i % places.length] ?? "", date: `${(i % 28) + 1} ${["Jan", "Feb", "Mar", "Apr", "May", "Jun"][Math.floor(i / 28) % 6]} 2026`, tone: tones[i % tones.length] ?? "",})const Message = UiSchema.TaggedEnum({ Resized: { width: Schema.Number }, Jumped: { index: Schema.Number } })// a photo library of 4,800 pictures: only the cards near the view are drawnexport const PhotoLibrary = Html.component({ name: "PhotoLibrary", model: Schema.Struct({ width: Schema.Number, jump: Schema.optional(Schema.Number) }), message: Message,}).make({ init: () => ({ width: 0 }), update: { Resized: ($, { width }) => $.evolve({ width }), Jumped: ($, { index }) => $.evolve({ jump: index }), }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( h.div.attrs(sx(styles.bar)).children( h.span.attrs({ dir: "auto" }).children(`${count.toLocaleString("en-US")} photos`), h.div.attrs(sx(styles.jump)).children( h.button.attrs({ type: "button", ...sx(styles.button) }).on("click", Message.Jumped({ index: 0 })).children("First"), h.button.attrs({ type: "button", ...sx(styles.button) }).on("click", Message.Jumped({ index: 2_400 })).children("Middle"), h.button.attrs({ type: "button", ...sx(styles.button) }).on("click", Message.Jumped({ index: count - 1 })).children("Last"), ), ), VirtualCardGrid(h, { key: "photos", label: "Photos", count, width: model.width, onResize: (width) => Message.Resized({ width }), minWidth: 180, rowHeight: 200, height: "440px", ...(model.jump === undefined ? {} : { scrollToIndex: model.jump }), item: (i) => { const p = photo(i) return h.figure.attrs(sx(styles.card)).children( h.div.attrs({ role: "img", "aria-label": `${p.place}, ${p.date}`, ...sx(styles.image) }).style({ "--tone": p.tone }), h.figcaption.attrs(sx(styles.meta)).children(h.span.attrs(sx(styles.name)).children(p.name), h.span.attrs(sx(styles.detail)).children(`${p.place} · ${p.date}`)), ) }, }), ),})When to use
Thousands of cards in a scrolling grid that draws only the ones in view, with as many columns as fit. Use Gallery instead when there are at most a few hundred cards.
Usage
import { VirtualCardGrid } from "./ui/uicontext/components/VirtualCardGrid.ts"const Message = UiSchema.TaggedEnum({ Resized: { width: Schema.Number } })export const Products = (h: Html.H<typeof Message.Type>, width: number, names: ReadonlyArray<string>) => VirtualCardGrid(h, { label: "Products", count: names.length, width, onResize: (w) => Message.Resized({ width: w }), item: (i) => h.div.children(names[i] ?? "") })Examples
A company directory
Short rows with rowHeight: 76; columns at least minWidth wide fill the space.
This example also uses Avatar. Install it too:
npx shadcn@latest add checkfu/uicontext/avatarimport { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Avatar } from "./ui/uicontext/components/Avatar.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"import { VirtualCardGrid } from "./ui/uicontext/components/VirtualCardGrid.ts"const styles = stylex.create({ wrap: { inlineSize: "100%" }, card: { display: "grid", gridTemplateColumns: "auto minmax(0, 1fr)", alignItems: "center", gap: space.md, blockSize: "100%", boxSizing: "border-box", paddingInline: space.md, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, }, text: { display: "grid", gap: 2, minInlineSize: 0 }, name: { fontSize: font.sm, fontWeight: 500, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, role: { fontSize: font.xs, color: color.muted, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },})const first = ["Ada", "Priya", "Kenji", "Tomás", "Amara", "Lena", "Omar", "Sofia", "Wei", "Noah", "Ines", "Ravi"]const last = ["Okafor", "Raman", "Watanabe", "García", "Nwosu", "Fischer", "Haddad", "Rossi", "Chen", "Kim", "Silva", "Patel"]const roles = ["Engineering", "Design", "Sales", "Support", "Finance", "Marketing", "Data", "Legal"]const person = (i: number) => ({ name: `${first[i % first.length]} ${last[Math.floor(i / first.length) % last.length]}`, role: `${roles[i % roles.length]} · ${["Lisbon", "Berlin", "Toronto", "Singapore"][i % 4]}`,})const Message = UiSchema.TaggedEnum({ Resized: { width: Schema.Number } })// a company directory of 1,200 people, in short cardsexport const Directory = Html.component({ name: "Directory", model: Schema.Struct({ width: Schema.Number }), message: Message,}).make({ init: () => ({ width: 0 }), update: { Resized: ($, { width }) => $.evolve({ width }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( VirtualCardGrid(h, { key: "people", label: "People", count: 1_200, width: model.width, onResize: (width) => Message.Resized({ width }), minWidth: 220, rowHeight: 76, height: "360px", item: (i) => { const p = person(i) return h.div.attrs(sx(styles.card)).children( Avatar(h, { id: String(i), name: p.name, size: "md", decorative: true }), h.div.attrs(sx(styles.text)).children(h.span.attrs(sx(styles.name)).children(p.name), h.span.attrs(sx(styles.role)).children(p.role)), ) }, }), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the instance among others in the same view; needed for more than one. |
labelrequired | string | — | What the cards are: "Photos". |
countrequired | number | — | — |
widthrequired | number | — | The grid's measured width; 0 before it's measured, which draws for fallbackWidth. |
onResizerequired | (width: number) => Message | — | — |
itemrequired | (index: number) => View.Content | — | Draws the card at an index. |
minWidth | number | 200 | The narrowest a column gets, in pixels; 200 by default. |
rowHeight | number | 240 | A row's height, card and gap together, in pixels; 240 by default. |
height | string | "480px" | The scrolling box's height; "480px" by default. |
fallbackWidth | number | 960 | The width to lay out for before the first measure; 960 by default. |
scrollToIndex | number | — | Scrolls to the row holding this card. |
style | Style | — | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/virtual-card-grid