Move through a long list a page at a time: where you are, nearby pages, previous and next, and rows per page.
- ENG-1284VAT shown twice on EU invoices
- ENG-1283Wallet button missing on tablets
- ENG-1282Export to CSV drops the last row
- ENG-1281Search ignores accents in names
- ENG-1280Invite email lands in spam
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Pagination } from "./ui/uicontext/components/Pagination.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.lg, inlineSize: "min(100%, 44rem)" }, list: { margin: 0, padding: 0, listStyle: "none", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, // a short last page keeps the list's height, so the bar below doesn't jump; a long one scrolls minBlockSize: "calc(5 * 41px)", maxBlockSize: "calc(8 * 41px)", overflowY: "auto", }, row: { display: "flex", gap: space.lg, alignItems: "center", blockSize: 40, paddingInline: space.xl, borderBlockEndWidth: 1, borderBlockEndStyle: "solid", borderBlockEndColor: color.line, fontSize: font.sm, }, id: { flexShrink: 0, inlineSize: "5.5rem", color: color.muted, fontVariantNumeric: "tabular-nums" }, title: { minInlineSize: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", color: color.fg },})const TOTAL = 1284const titles = ["Card declined when retrying payment", "VAT shown twice on EU invoices", "Wallet button missing on tablets", "Export to CSV drops the last row", "Search ignores accents in names", "Invite email lands in spam"]const Message = UiSchema.TaggedEnum({ RequestedPage: { page: Schema.Number }, RequestedPageSize: { pageSize: Schema.Number, page: Schema.Number } })export const IssuePages = Html.component({ name: "IssuePages", model: Schema.Struct({ page: Schema.Number, pageSize: Schema.Number }), message: Message,}).make({ init: () => ({ page: 1, pageSize: 5 }), update: { RequestedPage: ($, { page }) => $.evolve({ page }), RequestedPageSize: ($, { pageSize, page }) => $.evolve({ pageSize, page }), }, view: (h, { page, pageSize }) => { const start = (page - 1) * pageSize const rows = Array.from({ length: Math.max(0, Math.min(pageSize, TOTAL - start)) }, (_, i) => start + i + 1) return h.div.attrs(sx(styles.wrap)).children( h.ul.attrs({ "aria-label": "Issues", ...sx(styles.list) }).children( rows.map((n) => h.li.key(String(n)).attrs(sx(styles.row)).children( h.span.attrs(sx(styles.id)).children(`ENG-${TOTAL - n + 1}`), h.span.attrs(sx(styles.title)).children(titles[n % titles.length]), )), ), Pagination(h, { page, pageSize, total: TOTAL, noun: ["issue", "issues"], pageSizes: [5, 20, 50, 100], onPage: (next) => Message.RequestedPage({ page: next }), onPageSize: (size, next) => Message.RequestedPageSize({ pageSize: size, page: next }), }), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/paginationUsage
import { Pagination } from "./ui/uicontext/components/Pagination.ts"const Message = UiSchema.TaggedEnum({ RequestedPage: { page: Schema.Number } })export const IssuePagination = (h: Html.H<typeof Message.Type>, page: number) => Pagination(h, { page, pageSize: 20, total: 1284, onPage: (next) => Message.RequestedPage({ page: next }), })Examples
No total
With total: null the range has no "of", and Next stays on while the server says there's more.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { Pagination } from "./ui/uicontext/components/Pagination.ts"const Message = UiSchema.TaggedEnum({ RequestedPage: { page: Schema.Number } })// an audit log the server pages by cursor: no total, only whether there's moreexport const AuditLogPages = Html.component({ name: "AuditLogPages", model: Schema.Struct({ page: Schema.Number }), message: Message,}).make({ init: () => ({ page: 2 }), update: { RequestedPage: ($, { page }) => $.evolve({ page }) }, view: (h, { page }) => Pagination(h, { page, pageSize: 50, total: null, hasMore: page < 4, noun: ["event", "events"], label: "Audit log pages", onPage: (next) => Message.RequestedPage({ page: next }), }),})Edge cases
No results, a single page, six-figure counts, and a narrow container.
No results
One page
128,400 rows
320px wide
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Pagination } from "./ui/uicontext/components/Pagination.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ rows: { display: "grid", gap: 20, inlineSize: "100%" }, label: { fontSize: font.xs, color: color.faint, marginBlockEnd: 6 }, // a phone-width card: the numbered pages give way to "Page 412 of 6,420" narrow: { inlineSize: "min(100%, 320px)", padding: 12, borderWidth: 1, borderStyle: "dashed", borderColor: color.line, borderRadius: radius.md },})const Message = UiSchema.TaggedEnum({ RequestedPage: { key: Schema.String, page: Schema.Number } })const cases = [ { key: "none", label: "No results", total: 0 }, { key: "one", label: "One page", total: 7 }, { key: "huge", label: "128,400 rows", total: 128400 },] as constexport const PaginationEdges = Html.component({ name: "PaginationEdges", model: Schema.Struct({ pages: Schema.Record(Schema.String, Schema.Number) }), message: Message,}).make({ init: () => ({ pages: { none: 1, one: 1, huge: 412, narrow: 412 } }), update: { RequestedPage: ($, { key, page }) => $.evolve({ pages: (pages) => ({ ...pages, [key]: page }) }) }, view: (h, { pages }) => h.div.attrs(sx(styles.rows)).children( cases.map((c) => h.div.key(c.key).children( h.div.attrs(sx(styles.label)).children(c.label), Pagination(h, { page: pages[c.key] ?? 1, pageSize: 20, total: c.total, label: c.label, onPage: (page) => Message.RequestedPage({ key: c.key, page }) }), )), h.div.attrs(sx(styles.narrow)).children( h.div.attrs(sx(styles.label)).children("320px wide"), Pagination(h, { page: pages["narrow"] ?? 1, pageSize: 20, total: 128400, label: "Narrow", onPage: (page) => Message.RequestedPage({ key: "narrow", page }) }), ), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
pagerequired | number | — | The current page, from 1. The caller keeps it. |
pageSizerequired | number | — | Rows on a page. |
totalrequired | number | null | — | How many rows there are, or null when the server doesn't say. |
hasMore | boolean | — | With total: null: there's a page after this one. |
noun | readonly [one: string, other: string] | — | What a row is, for the range: "issue" reads "1–20 of 1,284 issues". |
pageSizes | number[] | — | The page sizes to offer. Without it there's no size menu. |
onPagerequired | (page: number) => Message | — | The page asked for: store it as page and fetch it. |
onPageSize | (pageSize: number, page: number) => Message | — | The page size asked for, with the page that keeps the first row in view. |
href | (page: number) => string | — | Pages as links, for pages a search engine or a new tab should reach: (page) => \?page=${page}\``. |
label | string | "Pagination" | Names the navigation landmark; "Pagination" by default. |
disabled | boolean | — | — |
style | Style | — | StyleX styles applied after the bar's own. |