A list's footer: how many show, and a button for the next page that waits while it loads.
- 1.4 kB JS 2.8 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
- #4120 Harborview Supply$48.00
- #4119 Halvorsen & Co$85.00
- #4118 Okafor Studio$122.00
- #4117 Rivera Bakery$159.00
- #4116 Lindqvist Design$196.00
- #4115 Tanaka Freight$233.00
- #4114 Mendes Clinic$270.00
- #4113 Novak Print$307.00
Showing 8 of 23 orders
import { Command, UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { ShowMore } from "./ui/uicontext/components/ShowMore.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ card: { inlineSize: "min(100%, 30rem)", paddingInline: space.md, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel }, list: { margin: 0, padding: 0, listStyle: "none" }, row: { display: "flex", justifyContent: "space-between", gap: space.md, paddingBlock: space.sm, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line, fontSize: font.sm }, id: { fontVariantNumeric: "tabular-nums", color: color.muted }, amount: { fontVariantNumeric: "tabular-nums", fontWeight: 500 },})const TOTAL = 23const STEP = 8const customers = ["Harborview Supply", "Halvorsen & Co", "Okafor Studio", "Rivera Bakery", "Lindqvist Design", "Tanaka Freight", "Mendes Clinic", "Novak Print"]const order = (i: number) => ({ id: `#${String(4120 - i)}`, customer: customers[i % customers.length]!, amount: `$${(48 + ((i * 37) % 260)).toFixed(2)}` })const Message = UiSchema.TaggedEnum({ RequestedMore: {}, LoadedMore: {} })// stands in for the server: waits, then the next page is thereconst FetchPage = Command.make("FetchPage", {}, () => Effect.sleep("700 millis").pipe(Effect.as(Message.LoadedMore())))// a store's recent orders, eight at a time; the button waits while a page loadsexport const RecentOrders = Html.component({ name: "RecentOrders", model: Schema.Struct({ shown: Schema.Number, loading: Schema.Boolean }), message: Message,}).make({ init: () => ({ shown: STEP, loading: false }), update: { RequestedMore: ($) => $.evolve({ loading: () => true }).run(FetchPage.make({})), LoadedMore: ($) => $.evolve({ shown: (n) => Math.min(TOTAL, n + STEP), loading: () => false }), }, view: (h, model) => h.div.attrs(sx(styles.card)).children( h.ul.attrs({ "aria-label": "Recent orders", ...sx(styles.list) }).children( Array.from({ length: model.shown }, (_, i) => order(i)).map((o) => h.li.key(o.id).attrs(sx(styles.row)).children( h.span.children(h.span.attrs(sx(styles.id)).children(o.id), ` ${o.customer}`), h.span.attrs(sx(styles.amount)).children(o.amount), )), ), ShowMore(h, { shown: model.shown, total: TOTAL, noun: "order", step: STEP, loading: model.loading, onMore: Message.RequestedMore() }), ),})When to use
A list's footer: how many show, and a button for the next page that waits while it loads. Use Pagination instead when people jump to a page, not just the next one.
Usage
import { ShowMore } from "./ui/uicontext/components/ShowMore.ts"export const Footer = <M>(h: Html.H<M>, shown: number, loading: boolean, more: M) => ShowMore(h, { shown, total: 134, noun: "order", step: 20, loading, onMore: more })Examples
Cut short in place
moreLabel writes “Ada, Tomás, and 5 more” in English; align: "center" stacks the count over the button.
Watching: Ada Okafor, Tomás García, and 5 more
- Ada Okafor
- Tomás García
- Priya Raman
Showing 3 of 7 watchers
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { moreLabel, ShowMore } from "./ui/uicontext/components/ShowMore.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.sm, inlineSize: "min(100%, 26rem)" }, who: { margin: 0, fontSize: font.sm, color: color.muted }, list: { display: "grid", gap: space.xs, margin: 0, paddingInlineStart: space.lg, fontSize: font.sm },})const watchers = ["Ada Okafor", "Tomás García", "Priya Raman", "Wei Chen", "Lena Fischer", "Marisol Kowalczyk-Hernández", "Yusuf Demir"]const Message = UiSchema.TaggedEnum({ Expanded: {} })// who watches an issue: two names and "5 more" in a sentence, then the whole list centeredexport const Watchers = Html.component({ name: "Watchers", model: Schema.Struct({ shown: Schema.Number }), message: Message,}).make({ init: () => ({ shown: 3 }), update: { Expanded: ($) => $.evolve({ shown: () => watchers.length }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( h.p.attrs(sx(styles.who)).children(`Watching: ${moreLabel(watchers.slice(0, 2), watchers.length, "en")}`), h.ul.attrs({ "aria-label": "Watchers", ...sx(styles.list) }).children(watchers.slice(0, model.shown).map((name) => h.li.key(name).children(name))), ShowMore(h, { shown: model.shown, total: watchers.length, noun: "watcher", align: "center", onMore: Message.Expanded() }), ),})API reference
Options
| Name | Type | Description |
|---|---|---|
shownrequired | number | How many show now. |
total | number | How many there are, when it's known. |
nounrequired | string | What they are, singular: "order". The plural adds an s; give nouns otherwise. |
nouns | string | — |
onMorerequired | Message | Loads or shows the next page. |
step | number | How many the next page adds; the button says "Show 20 more". |
loading | boolean | The next page is on its way. |
align | "split" | "center" | The count above the button, centered, instead of beside it. |
locale | string | — |
style | Style | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/show-more