# Split View

> A list beside the open row's detail, one at a time with Back on a narrow screen.

Page: https://uicontext.com/docs/components/split-view

Group: Navigation.

## When to use

A list beside the open row's detail, one at a time with Back on a narrow screen. Use [Resizable Panels](https://uicontext.com/docs/components/resizable-panels.md) instead when people size the panes themselves.

## Usage

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

export const Mail = <M>(h: Html.H<M>, open: boolean, closed: M) =>
	SplitView(h, { listLabel: "Inbox", detailLabel: "Message", backLabel: "Inbox", open, onBack: closed, list: h.ul.children(), detail: h.article.children() })
```

## Examples

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

### Inbox and message

The example the page opens with: Split View in “Inbox and message”.

```ts title="src/demo.ts" source="www/src/examples/split-view/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { SplitView } from "./ui/uicontext/components/SplitView.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, motion, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	box: { inlineSize: "100%", blockSize: 420, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, overflow: "hidden" },
	list: { display: "grid", margin: 0, padding: space.xs, listStyle: "none" },
	row: {
		display: "grid",
		gap: 2,
		inlineSize: "100%",
		minBlockSize: density.control,
		paddingBlock: space.sm,
		paddingInline: space.md,
		borderWidth: 0,
		borderRadius: corner.control,
		backgroundColor: { default: "transparent", ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": color.hover } },
		color: color.fg,
		fontFamily: "inherit",
		textAlign: "start",
		cursor: "pointer",
		outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` },
		outlineOffset: -2,
		transitionProperty: "background-color",
		transitionDuration: motion.fast,
	},
	current: { backgroundColor: color.selected },
	top: { display: "flex", justifyContent: "space-between", gap: space.sm, fontSize: font.sm, fontWeight: 600 },
	time: { flexShrink: 0, fontSize: font.xs, fontWeight: 400, color: color.muted, fontVariantNumeric: "tabular-nums" },
	subject: { fontSize: font.sm, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
	preview: { fontSize: font.xs, color: color.muted, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
	detail: { display: "grid", gap: space.md, paddingBlock: space.md, paddingInline: space.lg },
	title: { margin: 0, fontSize: font.lg, fontWeight: 600, textWrap: "balance" },
	meta: { margin: 0, fontSize: font.sm, color: color.muted },
	body: { margin: 0, fontSize: font.sm, lineHeight: 1.6, whiteSpace: "pre-line" },
	empty: { display: "grid", placeItems: "center", blockSize: "100%", padding: space.lg },
})

const mail = [
	{ id: "m1", from: "Priya Raman", time: "09:42", subject: "Renewal terms for next year", preview: "Attached are the renewal terms we discussed.", body: "Hi,\n\nThanks for the call yesterday. Attached are the renewal terms we discussed: 50 seats on the annual plan, with the onboarding credit carried over.\n\nCould you sign by Friday so finance can close the quarter?\n\nPriya" },
	{ id: "m2", from: "Tomás García", time: "08:15", subject: "Can't export reports since this morning", preview: "Every CSV export fails with “Something went wrong”.", body: "Every CSV export fails with “Something went wrong”. We need the monthly report for a board meeting at 2pm.\n\nTomás" },
	{ id: "m3", from: "Wei Chen", time: "Yesterday", subject: "Invoice INV-20419 paid", preview: "Payment of $4,800.00 went out this morning.", body: "Payment of $4,800.00 went out this morning by bank transfer. Remittance advice attached.\n\nWei" },
	{ id: "m4", from: "Lena Fischer", time: "Mon", subject: "Moving our standup to Thursdays", preview: "From next week, Thursdays at 10:00.", body: "From next week the support standup moves to Thursdays at 10:00, same room.\n\nLena" },
]

const Message = UiSchema.TaggedEnum({ Opened: { id: Schema.String }, Closed: {} })

// a help desk inbox: the list beside the open message; narrow, one at a time
export const Inbox = Html.component({
	name: "SplitInbox",
	model: Schema.Struct({ open: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ open: "m1" }),
	update: { Opened: ($, { id }) => $.evolve({ open: () => id }), Closed: ($) => $.evolve({ open: () => null }) },
	view: (h, model) => {
		const open = mail.find((m) => m.id === model.open)
		return h.div.attrs(sx(styles.box)).children(
			SplitView(h, {
				listLabel: "Inbox",
				detailLabel: "Message",
				backLabel: "Inbox",
				open: open !== undefined,
				onBack: Message.Closed(),
				list: h.ul.attrs(sx(styles.list)).children(
					mail.map((m) =>
						h.li.key(m.id).children(
							h.button.attrs({ type: "button", ...(m.id === model.open ? { "aria-current": "true" } : {}), ...sx(styles.row, m.id === model.open && styles.current) }).on("click", Message.Opened({ id: m.id })).children(
								h.span.attrs(sx(styles.top)).children(h.span.attrs({ dir: "auto" }).children(m.from), h.span.attrs(sx(styles.time)).children(m.time)),
								h.span.attrs({ dir: "auto", ...sx(styles.subject) }).children(m.subject),
								h.span.attrs({ dir: "auto", ...sx(styles.preview) }).children(m.preview),
							),
						)),
				),
				detail: open === undefined
					? h.p.attrs(sx(styles.empty, styles.meta)).children("Pick a message to read it.")
					: h.article.attrs(sx(styles.detail)).children(
						h.h3.attrs({ dir: "auto", ...sx(styles.title) }).children(open.subject),
						h.p.attrs(sx(styles.meta)).children(`${open.from} · ${open.time}`),
						h.p.attrs({ dir: "auto", ...sx(styles.body) }).children(open.body),
					),
			}),
		)
	},
})
```

### One at a time

In a box under 640px wide, a row opens in place of the list, with Back to return.

```ts title="src/narrow.ts" source="www/src/examples/split-view/narrow.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { SplitView } from "./ui/uicontext/components/SplitView.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, motion, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	// a phone-width frame, so the one-at-a-time layout shows on any screen
	box: { inlineSize: "min(100%, 360px)", blockSize: 360, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel, overflow: "hidden" },
	list: { display: "grid", margin: 0, padding: space.xs, listStyle: "none" },
	row: {
		display: "flex",
		justifyContent: "space-between",
		alignItems: "center",
		gap: space.sm,
		inlineSize: "100%",
		minBlockSize: density.control,
		paddingInline: space.md,
		borderWidth: 0,
		borderRadius: corner.control,
		backgroundColor: { default: "transparent", ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": color.hover } },
		color: color.fg,
		fontFamily: "inherit",
		fontSize: font.sm,
		textAlign: "start",
		cursor: "pointer",
		outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` },
		outlineOffset: -2,
		transitionProperty: "background-color",
		transitionDuration: motion.fast,
	},
	state: { margin: 0, flexShrink: 0, fontSize: font.xs, color: color.muted, fontVariantNumeric: "tabular-nums" },
	failed: { color: color.danger },
	detail: { display: "grid", gap: space.sm, paddingBlock: space.sm, paddingInline: space.lg },
	title: { margin: 0, fontSize: font.base, fontWeight: 600 },
	log: { margin: 0, padding: space.md, borderRadius: corner.control, backgroundColor: color.app, fontFamily: font.mono, fontSize: font.xs, lineHeight: 1.6, whiteSpace: "pre-wrap", overflowWrap: "anywhere" },
})

const runs = [
	{ id: "r1", name: "Nightly invoice sync", state: "Failed · 2m 10s", failed: true, log: "02:00:01 Fetching invoices since 4 Dec\n02:00:44 1,208 invoices\n02:02:11 Error: ledger API answered 503 three times\n02:02:11 Run stopped; no rows written" },
	{ id: "r2", name: "Refresh customer health", state: "Done · 48s", failed: false, log: "03:00:00 Scoring 2,940 accounts\n03:00:48 Done: 112 moved to At risk" },
	{ id: "r3", name: "Send trial reminders", state: "Done · 6s", failed: false, log: "09:00:00 38 trials end in 3 days\n09:00:06 38 emails queued" },
]

const Message = UiSchema.TaggedEnum({ Opened: { id: Schema.String }, Closed: {} })

// a phone-width runs list: tapping a run shows its log, Back returns to the list
export const Runs = Html.component({
	name: "SplitRuns",
	model: Schema.Struct({ open: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ open: null }),
	update: { Opened: ($, { id }) => $.evolve({ open: () => id }), Closed: ($) => $.evolve({ open: () => null }) },
	view: (h, model) => {
		const open = runs.find((r) => r.id === model.open)
		return h.div.attrs(sx(styles.box)).children(
			SplitView(h, {
				listLabel: "Runs",
				detailLabel: "Run log",
				backLabel: "Runs",
				open: open !== undefined,
				onBack: Message.Closed(),
				list: h.ul.attrs(sx(styles.list)).children(
					runs.map((r) =>
						h.li.key(r.id).children(
							h.button.attrs({ type: "button", ...sx(styles.row) }).on("click", Message.Opened({ id: r.id })).children(
								h.span.attrs({ dir: "auto" }).children(r.name),
								h.span.attrs(sx(styles.state, r.failed && styles.failed)).children(r.state),
							),
						)),
				),
				detail: open === undefined
					? null
					: h.article.attrs(sx(styles.detail)).children(
						h.h3.attrs({ dir: "auto", ...sx(styles.title) }).children(open.name),
						h.p.attrs(sx(styles.state, open.failed && styles.failed)).children(open.state),
						h.pre.attrs({ tabindex: "0", "aria-label": "Log", ...sx(styles.log) }).children(open.log),
					),
			}),
		)
	},
})
```

## API reference

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `list` (required) | `View.Content` | The rows; each opens its detail. |
| `detail` (required) | `View.Content \| null` | The open row's detail, or what shows before one is open. |
| `open` (required) | `boolean` | A row is open: narrow, the detail shows in place of the list. |
| `onBack` (required) | `Message` | Back to the list, narrow. Close the open row here. |
| `backLabel` (required) | `string` | Where Back goes, named: "Inbox". The button reads "Back to Inbox". |
| `listLabel` (required) | `string` | Names the list's region: "Inbox". |
| `detailLabel` (required) | `string` | Names the detail's region: "Message". |
| `listWidth` | `string` | The list's width beside the detail; "minmax(16rem, 22rem)" by default. |
| `style` | `Style` | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/split-view
```

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

It writes these files:

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

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

## Source

- [src/SplitView.ts](https://github.com/checkfu/uicontext/blob/main/src/SplitView.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.
