# Sortable List

> Rows you put in order by dragging a grip or with the arrow keys, read out as they move.

Page: https://uicontext.com/docs/components/sortable-list

Group: Navigation.

## When to use

Rows you put in order by dragging a grip or with the arrow keys, read out as they move. Use [Board](https://uicontext.com/docs/components/board.md) instead when the rows move between stages, not just up and down.

## Usage

```ts
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

Each example is a complete file. Its imports point where the CLI installs the source.

### Ranked priorities

The example the page opens with: Sortable List in “Ranked priorities”.

```ts title="src/demo.ts" source="www/src/examples/sortable-list/demo.ts"
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 keys
export 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 }),
			}),
		),
})
```

### A dashboard's cards

`meta` holds anything at the row's end. Without `numbered` the rows carry no rank.

```ts title="src/widgets.ts" source="www/src/examples/sortable-list/widgets.ts"
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 shows
export 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. |
| `label` (required) | `string` | What the list holds: "Priorities". |
| `items` (required) | `SortableItem<Message>[]` | — |
| `onReorder` (required) | `(from: number, to: number) => Message` | — |
| `numbered` | `boolean` | Numbers the rows 1, 2, 3, for a ranking. |
| `disabled` | `boolean` | — |
| `style` | `Style` | — |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Space, Enter | On a grip, pick the row up, then drop it |
| Up arrow, Down arrow | Move the picked-up row |
| Home, End | Move it to the top or the bottom |
| Escape | Put it back where it was |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/sortable-list
```

With npm: `npx shadcn@latest add checkfu/uicontext/sortable-list`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/sortable-list`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/sortable-list`.

It writes these files:

- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/SortableList.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/SortableList.ts](https://github.com/checkfu/uicontext/blob/main/src/SortableList.ts)

## Related

- [App Shell](https://uicontext.com/docs/components/app-shell.md) (`app-shell`): The frame of an app: a sidebar, the page and an optional side panel, on any screen size.
- [Tab Strip](https://uicontext.com/docs/components/tab-strip.md) (`tab-strip`): Document tabs over the page to open, close and drag to reorder.
- [Sidebar](https://uicontext.com/docs/components/sidebar.md) (`sidebar`): A navigation tree of workspaces, teams and pages.
- [Tree View](https://uicontext.com/docs/components/tree-view.md) (`tree-view`): Nested rows you open and close, such as files and folders. Children can load when opened.
- [Page Header](https://uicontext.com/docs/components/page-header.md) (`page-header`): A page's title, description, breadcrumb and actions.
- [Tabs](https://uicontext.com/docs/components/tabs.md) (`tabs`): Switch between panels, or between views of the same data.
