Rows you put in order by dragging a grip or with the arrow keys, read out as they move.
- 1.9 kB JS 3.4 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- 4 keys checked
- Right-to-left
- 9 tests
- SAML single sign-onPlatform13 pts
- Usage-based billingBilling21 pts
- Offline mode on mobileMobile8 pts
- Audit log exportPlatform5 pts
- Summaries in the inboxAI8 pts
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { moveItem, SortableList } from "./ui/uicontext/components/SortableList.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 30rem)" } })const Goal = Schema.Struct({ id: Schema.String, title: Schema.String, owner: Schema.String, points: Schema.Number })const Message = UiSchema.TaggedEnum({ Reordered: { from: Schema.Number, to: Schema.Number } })// a quarter's priorities, ranked: drag a grip, or press it and use the arrow keysexport const QuarterPriorities = Html.component({ name: "QuarterPriorities", model: Schema.Struct({ goals: Schema.Array(Goal) }), message: Message,}).make({ init: () => ({ goals: [ { id: "sso", title: "SAML single sign-on", owner: "Platform", points: 13 }, { id: "usage", title: "Usage-based billing", owner: "Billing", points: 21 }, { id: "mobile", title: "Offline mode on mobile", owner: "Mobile", points: 8 }, { id: "audit", title: "Audit log export", owner: "Platform", points: 5 }, { id: "ai", title: "Summaries in the inbox", owner: "AI", points: 8 }, ], }), update: { Reordered: ($, { from, to }) => $.evolve({ goals: (g) => moveItem(g, from, to) }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( SortableList(h, { label: "Q1 priorities", numbered: true, items: model.goals.map((g) => ({ id: g.id, label: g.title, description: g.owner, meta: `${g.points} pts` })), onReorder: (from, to) => Message.Reordered({ from, to }), }), ),})When to use
Rows you put in order by dragging a grip or with the arrow keys, read out as they move. Use Board instead when the rows move between stages, not just up and down.
Usage
import { SortableList } from "./ui/uicontext/components/SortableList.ts"const Message = UiSchema.TaggedEnum({ Reordered: { from: Schema.Number, to: Schema.Number } })export const Steps = (h: Html.H<typeof Message.Type>, steps: ReadonlyArray<{ readonly id: string; readonly title: string }>) => SortableList(h, { label: "Onboarding steps", items: steps.map((s) => ({ id: s.id, label: s.title })), onReorder: (from, to) => Message.Reordered({ from, to }) })Examples
A dashboard's cards
meta holds anything at the row's end. Without numbered the rows carry no rank.
- Revenue this monthShown
- New signupsShown
- Churn riskHidden
- Open ticketsShown
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { moveItem, SortableList } from "./ui/uicontext/components/SortableList.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 26rem)" }, dot: { inlineSize: 8, blockSize: 8, borderRadius: corner.pill, backgroundColor: color.success }, off: { backgroundColor: color.lineStrong }, state: { fontSize: font.xs }, meta: { display: "inline-flex", alignItems: "center", gap: space.xs },})const Message = UiSchema.TaggedEnum({ Reordered: { from: Schema.Number, to: Schema.Number } })// the order of a home dashboard's cards, with whether each showsexport const DashboardCards = Html.component({ name: "DashboardCards", model: Schema.Struct({ cards: Schema.Array(Schema.Struct({ id: Schema.String, title: Schema.String, shown: Schema.Boolean })) }), message: Message,}).make({ init: () => ({ cards: [ { id: "revenue", title: "Revenue this month", shown: true }, { id: "signups", title: "New signups", shown: true }, { id: "churn", title: "Churn risk", shown: false }, { id: "tickets", title: "Open tickets", shown: true }, ], }), update: { Reordered: ($, { from, to }) => $.evolve({ cards: (c) => moveItem(c, from, to) }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( SortableList(h, { label: "Dashboard cards", items: model.cards.map((c) => ({ id: c.id, label: c.title, meta: h.span.attrs(sx(styles.meta)).children(h.span.attrs({ "aria-hidden": "true", ...sx(styles.dot, !c.shown && styles.off) }), h.span.attrs(sx(styles.state)).children(c.shown ? "Shown" : "Hidden")), })), onReorder: (from, to) => Message.Reordered({ from, to }), }), ),})API reference
Options
| Name | Type | Description |
|---|---|---|
key | string | Names the list among others in the same view; needed for more than one. |
labelrequired | string | What the list holds: "Priorities". |
itemsrequired | SortableItem<Message>[] | — |
onReorderrequired | (from: number, to: number) => Message | — |
numbered | boolean | Numbers the rows 1, 2, 3, for a ranking. |
disabled | boolean | — |
style | Style | — |
Keyboard and accessibility
| Keys | Does |
|---|---|
| SpaceSpace EnterEnter | On a grip, pick the row up, then drop it |
| ↑Up arrow ↓Down arrow | Move the picked-up row |
| HomeHome EndEnd | Move it to the top or the bottom |
| EscEscape | Put it back where it was |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/sortable-list