# Swipe List

> Rows a phone user swipes to act on, such as archive or mark read, with the same actions as buttons.

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

Group: Records.

## When to use

Rows a phone user swipes to act on, such as archive or mark read, with the same actions as buttons. Use [Record List](https://uicontext.com/docs/components/record-list.md) instead when people use a mouse and keyboard more than a phone.

## Usage

```ts
import { SwipeList } from "./ui/uicontext/components/SwipeList.ts"

const Message = UiSchema.TaggedEnum({ Deleted: { id: Schema.String } })

export const Tasks = (h: Html.H<typeof Message.Type>, tasks: ReadonlyArray<{ readonly id: string; readonly title: string }>) =>
	SwipeList(h, {
		key: "tasks",
		props: { label: "Tasks", items: tasks, left: { id: "delete", label: "Delete", tone: "danger", removes: true } },
		onEmit: { Acted: ({ id }) => Message.Deleted({ id }) },
	})
```

## Examples

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

### Inbox

The example the page opens with: Swipe List in “Inbox”.

```ts title="src/demo.ts" source="www/src/examples/swipe-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 { SwipeItem, SwipeList } from "./ui/uicontext/components/SwipeList.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)" },
	note: { margin: 0, fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({ Acted: { id: Schema.String, action: Schema.String } })

const start: ReadonlyArray<SwipeItem> = [
	{ id: "1", title: "Priya Raman", description: "Pricing page copy is ready for review", meta: "9:41", unread: true },
	{ id: "2", title: "Billing", description: "Your November invoice is $1,240.00", meta: "8:02", unread: true },
	{ id: "3", title: "Kenji Watanabe", description: "Re: offline mode, sync conflicts", meta: "Yesterday" },
	{ id: "4", title: "Status", description: "Resolved: elevated API errors in eu-west-1", meta: "Mon" },
	{ id: "5", title: "Ada Okafor", description: "Q1 planning doc, comments welcome", meta: "Sun" },
]

// a phone inbox: swipe left to archive, right to mark read or unread
export const Inbox = Html.component({
	name: "Inbox",
	model: Schema.Struct({ items: Schema.Array(SwipeItem), last: Schema.String }),
	message: Message,
}).make({
	init: () => ({ items: start, last: "" }),
	update: {
		Acted: ($, { id, action }) => {
			const title = $.model.items.find((i) => i.id === id)?.title ?? ""
			return action === "archive"
				? $.evolve({ items: (items) => items.filter((i) => i.id !== id), last: `Archived ${title}.` })
				: $.evolve({ items: (items) => items.map((i) => (i.id === id ? { ...i, unread: i.unread !== true } : i)), last: `Marked ${title} ${$.model.items.find((i) => i.id === id)?.unread === true ? "read" : "unread"}.` })
		},
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			SwipeList(h, {
				key: "inbox",
				props: {
					label: "Inbox",
					items: model.items,
					left: { id: "archive", label: "Archive", tone: "success", removes: true },
					right: { id: "read", label: "Read", tone: "info" },
					empty: "Inbox zero.",
				},
				onEmit: { Acted: ({ id, action }) => Message.Acted({ id, action }) },
			}),
			h.p.attrs({ role: "status", ...sx(styles.note) }).children(model.last === "" ? "Swipe a row on a phone, or hover it for its buttons." : model.last),
		),
})
```

### Resolve or snooze

An action with `removes` slides the row away for good; one without slides back once the app updates the row.

```ts title="src/notifications.ts" source="www/src/examples/swipe-list/notifications.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { SwipeItem, SwipeList } from "./ui/uicontext/components/SwipeList.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 26rem)" } })

const Message = UiSchema.TaggedEnum({ Acted: { id: Schema.String, action: Schema.String } })

// on-call alerts: swipe left to resolve, right to snooze an hour
export const OnCallAlerts = Html.component({
	name: "OnCallAlerts",
	model: Schema.Struct({ items: Schema.Array(SwipeItem) }),
	message: Message,
}).make({
	init: () => ({
		items: [
			{ id: "a1", title: "p95 latency over 400 ms", description: "checkout-api · eu-west-1", meta: "2m", unread: true },
			{ id: "a2", title: "Disk 91% full", description: "postgres-primary", meta: "14m", unread: true },
			{ id: "a3", title: "Queue backlog over 10,000", description: "email-worker", meta: "1h" },
		],
	}),
	update: {
		Acted: ($, { id, action }) =>
			action === "resolve"
				? $.evolve({ items: (items) => items.filter((i) => i.id !== id) })
				: $.evolve({ items: (items) => items.map((i) => (i.id === id ? { ...i, meta: "Snoozed 1h", unread: false } : i)) }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			SwipeList(h, {
				key: "alerts",
				props: {
					label: "Alerts",
					items: model.items,
					left: { id: "resolve", label: "Resolve", tone: "success", removes: true },
					right: { id: "snooze", label: "Snooze 1h", tone: "warning" },
					empty: "No open alerts.",
				},
				onEmit: { Acted: ({ id, action }) => Message.Acted({ id, action }) },
			}),
		),
})
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | — |
| `items` (required) | `SwipeItem[]` | — |
| `left` | `SwipeAction` | What a swipe to the left does |
| `right` | `SwipeAction` | What a swipe to the right does |
| `empty` | `string` | What the list says with no rows |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `Acted` | `{ id: string; action: string }` | — |

## Installation

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

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

It writes these files:

- `src/ui/uicontext/components/SwipeList.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

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

## Source

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

## Related

- [Data Grid](https://uicontext.com/docs/components/data-grid.md) (`data-grid`): A spreadsheet-like grid for editing records, with typed columns, sorting and grouping.
- [Pivot Table](https://uicontext.com/docs/components/pivot-table.md) (`pivot-table`): Records summed two ways, such as revenue by region and quarter, with totals on both edges and cells tinted by size.
- [Table](https://uicontext.com/docs/components/table.md) (`table`): A simple read-only table with a caption, row headers and an empty state.
- [Group Header](https://uicontext.com/docs/components/group-header.md) (`group-header`): The line over a group of rows with its count, sticky while they scroll, folding the group.
- [Description List](https://uicontext.com/docs/components/description-list.md) (`description-list`): A record's facts as label and value pairs, in rows or columns, with Copy for ids.
- [Fact Grid](https://uicontext.com/docs/components/fact-grid.md) (`fact-grid`): A few numbers about one thing, such as a customer's plan and seats, as small tiles.
