# Resizable Panels

> Panels with handles you drag or move with the keys. Sizes are kept between visits.

Page: https://uicontext.com/docs/components/resizable-panels

Group: Navigation.

## When to use

Panels with handles you drag or move with the keys. Sizes are kept between visits. Use [Scroll Area](https://uicontext.com/docs/components/scroll-area.md) instead when the content needs to scroll, not to take more room.

## Usage

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

export const DocWithPreview = <M>(h: Html.H<M>, editor: View.Content<M>, preview: View.Content<M>) =>
	Resizable(h, {
		label: "Editor and preview",
		storageKey: "doc-preview",
		panels: [
			{ id: "editor", label: "Editor", defaultSize: 50, min: 30, content: editor },
			{ id: "preview", label: "Preview", defaultSize: 50, min: 20, collapsible: true, content: preview },
		],
	})
```

## Examples

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

### Mail

Drag a handle, or focus it and use the arrow keys. Double-click the first handle, or press Enter on it, to fold the folders away. Reload: the sizes stay.

```ts title="src/demo.ts" source="www/src/examples/resizable-panels/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Resizable } from "./ui/uicontext/components/Resizable.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, density, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	frame: {
		inlineSize: "100%",
		blockSize: 320,
		borderWidth: 1,
		borderStyle: "solid",
		borderColor: color.line,
		borderRadius: corner.card,
		backgroundColor: color.panel,
		overflow: "hidden",
	},
	list: { margin: 0, padding: space.xs, listStyle: "none" },
	row: { display: "grid", gap: 2, paddingBlock: space.sm, paddingInline: space.md, borderRadius: corner.tag, fontSize: font.sm },
	current: { backgroundColor: color.selected },
	from: { fontWeight: 600, color: color.fg, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
	subject: { color: color.muted, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
	message: { padding: space.xl, display: "grid", gap: space.md, alignContent: "start" },
	title: { margin: 0, fontSize: font.lg, fontWeight: 600, letterSpacing: "-0.01em" },
	body: { margin: 0, fontSize: font.base, lineHeight: 1.6, color: color.muted, maxInlineSize: "65ch" },
	folders: { padding: space.md, display: "grid", gap: space.xs, alignContent: "start", fontSize: font.sm },
	// a finger gets a whole row
	folder: { color: color.muted, textDecoration: "none", paddingBlock: { default: space.xs, "@media (pointer: coarse)": 0 }, lineHeight: { default: null, "@media (pointer: coarse)": density.control }, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" },
})

const mail = [
	{ id: "1", from: "Mira Patel", subject: "Q3 launch checklist: two items left" },
	{ id: "2", from: "Ben Okafor", subject: "Re: Checkout errors since the 5 PM deploy" },
	{ id: "3", from: "Billing", subject: "Your invoice for November is ready" },
	{ id: "4", from: "Aleksandra Wiśniewska-Kowalczyk", subject: "Contract review: redlines on sections 4 and 7 attached" },
]

// a mail app: folders fold away, the list and the message share the rest; sizes come back after a reload
export const MailLayout = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.frame)).children(
		Resizable(h, {
			label: "Mail",
			storageKey: "docs-mail-layout",
			panels: [
				{
					id: "folders", label: "Folders", defaultSize: 20, min: 14, max: 30, collapsible: true,
					content: h.nav.attrs({ "aria-label": "Folders", ...sx(styles.folders) }).children(["Inbox", "Sent", "Drafts", "Archive"].map((f) => h.a.key(f).attrs({ href: `#${f.toLowerCase()}`, ...sx(styles.folder) }).children(f))),
				},
				{
					id: "list", label: "Messages", defaultSize: 32, min: 22, max: 50,
					content: h.ul.attrs(sx(styles.list)).children(
						mail.map((m, i) =>
							h.li.key(m.id).attrs(sx(styles.row, i === 1 && styles.current)).children(h.span.attrs(sx(styles.from)).children(m.from), h.span.attrs(sx(styles.subject)).children(m.subject))),
					),
				},
				{
					id: "message", label: "Message", defaultSize: 48, min: 30,
					content: h.article.attrs(sx(styles.message)).children(
						h.h3.attrs(sx(styles.title)).children("Re: Checkout errors since the 5 PM deploy"),
						h.p.attrs(sx(styles.body)).children("Rolled back the payment client. Error rate is back under 0.1%. I'll write up what happened tomorrow morning, and we can decide whether the retry change goes out again on Thursday."),
					),
				},
			],
		}),
	)
```

### Stacked

`direction: "vertical"`: the arrow keys move the handle up and down.

```ts title="src/vertical.ts" source="www/src/examples/resizable-panels/vertical.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Resizable } from "./ui/uicontext/components/Resizable.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({
	frame: { inlineSize: "100%", blockSize: 300, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, overflow: "hidden", backgroundColor: color.panel },
	code: { margin: 0, padding: space.lg, fontFamily: font.mono, fontSize: font.sm, lineHeight: 1.6, color: color.fg, whiteSpace: "pre" },
	terminal: { margin: 0, padding: space.lg, fontFamily: font.mono, fontSize: font.xs, lineHeight: 1.6, color: color.muted, backgroundColor: color.raised, blockSize: "100%", boxSizing: "border-box", whiteSpace: "pre-wrap" },
})

// an editor over its terminal: drag the handle up and down, or fold the terminal away
export const EditorTerminal = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.frame)).children(
		Resizable(h, {
			label: "Editor and terminal",
			direction: "vertical",
			panels: [
				{ id: "editor", label: "Editor", defaultSize: 65, min: 30, content: h.pre.attrs(sx(styles.code)).children("export const retry = (attempt: number) =>\n  Math.min(30_000, 2 ** attempt * 250)\n") },
				{ id: "terminal", label: "Terminal", defaultSize: 35, min: 20, collapsible: true, content: h.pre.attrs(sx(styles.terminal)).children("$ bun test retry\n✓ backs off exponentially (2 ms)\n✓ caps at 30 seconds (1 ms)\n\n2 passed") },
			],
		}),
	)
```

## API reference

### ResizableParts (component)

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | What the group is: "Mail". |
| `panels` (required) | `ResizablePanel[]` | — |
| `direction` | `"horizontal" \| "vertical"` | — |
| `storageKey` | `string` | Keeps the sizes in this browser under this key. Without it they reset on reload. |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedResize` | `{ sizes: Sizes }` | The sizes the person settled on, after a drag, a key or a fold: for an app that keeps them on its server. |

### Resizable (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `key` | `string` | — | Names the group among others in the same view; needed for more than one. |
| `label` (required) | `string` | — | What the group is: "Mail". |
| `panels` (required) | `ReadonlyArray<ResizablePanel & { readonly content: View.Content }>` | — | — |
| `direction` | `"horizontal" \| "vertical"` | `horizontal` | Side by side, or stacked; `horizontal` by default. |
| `storageKey` | `string` | — | Keeps the sizes in this browser under this key, so the layout comes back after a reload. |
| `onResize` | `(sizes: number[]) => Message` | — | The sizes the person settled on, for an app that keeps them on its server. |
| `style` | `Style` | — | StyleX styles applied after the group's own. |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Left arrow, Right arrow | Move the handle by a small step |
| Shift+Left arrow, Shift+Right arrow | Move the handle by a large step |
| Home, End | Shrink or grow the panel to its limit |
| Enter | Fold the panel, or unfold it |

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/resizable-panels
```

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

It writes these files:

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

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

## Source

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

## Used in

- [Agent studio](https://uicontext.com/blocks/agent-studio.md)

## 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.
