//layout
Resizable
Panels that users resize by dragging a handle or with the keyboard, for editors, mail clients and split views, with nesting, collapsing and saved layouts. Copy-paste ready.
// title: Code editor layout with nested resizable panels
import { FileCodeIcon, FileIcon, FolderIcon } from "lucide-react";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
const FILES = [
{ depth: 0, icon: FolderIcon, name: "app" },
{ depth: 1, icon: FileCodeIcon, name: "layout.tsx" },
{ depth: 0, icon: FolderIcon, name: "lib" },
{ active: true, depth: 1, icon: FileCodeIcon, name: "retry.ts" },
{ depth: 1, icon: FileCodeIcon, name: "retry.test.ts" },
{ depth: 0, icon: FileIcon, name: "package.json" },
];
const CODE = `export async function retry<T>(fn: () => Promise<T>) {
for (let attempt = 0; ; attempt++) {
try {
return await fn();
} catch (error) {
if (attempt === 4) throw error;
await sleep(Math.min(2 ** attempt * 500, 30_000));
}
}
}`;
export default function Particle() {
return (
<ResizablePanelGroup className="h-80 w-full max-w-2xl overflow-hidden rounded-xl border text-sm">
<ResizablePanel collapsible defaultSize="26%" minSize="18%">
<div className="flex h-full flex-col gap-0.5 overflow-auto p-2">
<p className="px-2 py-1 font-medium text-muted-foreground text-xs">
Explorer
</p>
{FILES.map((f) => (
<div
className={`flex items-center gap-1.5 rounded-md py-1 pe-2 ${f.active ? "bg-accent text-accent-foreground" : "text-muted-foreground"}`}
key={f.name}
style={{ paddingInlineStart: `${0.5 + f.depth}rem` }}
>
<f.icon aria-hidden="true" className="size-3.5 shrink-0" />
<span className="truncate">{f.name}</span>
</div>
))}
</div>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel minSize="30%">
<ResizablePanelGroup orientation="vertical">
<ResizablePanel defaultSize="68%" minSize="25%">
<div className="flex h-full flex-col">
<div className="border-b px-3 py-1.5 text-muted-foreground text-xs">
lib/retry.ts
</div>
<pre className="flex-1 overflow-auto p-3 font-mono text-xs leading-relaxed">
{CODE}
</pre>
</div>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel minSize="15%">
<div className="flex h-full flex-col overflow-auto bg-muted/40 p-3 font-mono text-muted-foreground text-xs leading-relaxed">
<span>$ bun test lib/retry.test.ts</span>
<span className="text-success-foreground">✓ 6 passed</span>
<span>Done in 412ms</span>
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
</ResizablePanelGroup>
);
}
Installation
pnpm dlx shadcn@latest add @cnippet/resizable
Usage
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable"<ResizablePanelGroup className="h-64 rounded-xl border">
<ResizablePanel defaultSize="30%" minSize="20%">Sidebar</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel>Content</ResizablePanel>
</ResizablePanelGroup>Good to know
- Built on react-resizable-panels v4. Every prop it takes passes through.
- Sizes: a string is a percentage (
"30%"or"30") and a number is pixels (200). Other units such as"16rem"work too. - The group needs a height. Give it one with
className, or let it fill a parent that has one. - Stack panels with
orientation="vertical"on the group, and nest a group inside a panel for editor-style layouts. - Handles are focusable: the arrow keys resize, Home and End jump to the limits, and double-clicking a handle resets its panels to their default sizes.
- Add
collapsible(and optionallycollapsedSize) to a panel to let it snap shut when dragged pastminSize. - To remember the layout, pass
useDefaultLayout({ id, storage: localStorage })to the group, and give each panel anid, as in the mail example.
API Reference
ResizablePanelGroup
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | "horizontal" | "vertical" | "horizontal" | Direction panels are laid out |
defaultLayout | Layout | — | Saved sizes, e.g. from useDefaultLayout |
onLayoutChanged | (layout: Layout) => void | — | Called when a resize finishes |
ResizablePanel
| Prop | Type | Default | Description |
|---|---|---|---|
defaultSize | number | string | auto | Starting size |
minSize / maxSize | number | string | — | Size limits |
collapsible | boolean | false | Snaps to collapsedSize past minSize |
collapsedSize | number | string | 0% | Size when collapsed |
id | string | — | Needed for saved layouts |
ResizableHandle
| Prop | Type | Default | Description |
|---|---|---|---|
withHandle | boolean | false | Shows a grip so the handle reads as draggable |
Examples
Horizontal
// title: Two horizontal panels
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
export default function Particle() {
return (
<ResizablePanelGroup className="h-48 w-full max-w-md rounded-xl border">
<ResizablePanel defaultSize="50%">
<div className="flex h-full items-center justify-center text-muted-foreground text-sm">
One
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel defaultSize="50%">
<div className="flex h-full items-center justify-center text-muted-foreground text-sm">
Two
</div>
</ResizablePanel>
</ResizablePanelGroup>
);
}
Vertical
// title: Vertical panels with grip handle
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
export default function Particle() {
return (
<ResizablePanelGroup
className="h-64 w-full max-w-md rounded-xl border"
orientation="vertical"
>
<ResizablePanel defaultSize="35%" minSize="20%">
<div className="flex h-full items-center justify-center text-muted-foreground text-sm">
Header
</div>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel minSize="20%">
<div className="flex h-full items-center justify-center text-muted-foreground text-sm">
Content
</div>
</ResizablePanel>
</ResizablePanelGroup>
);
}
Collapsible with saved layout
The nav collapses to icons when dragged past its minimum, and the sizes survive a reload.
// title: Mail layout with collapsible nav and saved sizes
"use client";
import { InboxIcon, SendIcon, StarIcon } from "lucide-react";
import { useDefaultLayout } from "react-resizable-panels";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
const NAV = [
{ icon: InboxIcon, label: "Inbox" },
{ icon: StarIcon, label: "Starred" },
{ icon: SendIcon, label: "Sent" },
];
const MAIL = [
{ from: "Stripe", subject: "Your October invoice" },
{ from: "Maya Patel", subject: "Re: retry backoff" },
{ from: "GitHub", subject: "[cnippet/ui] PR #482 merged" },
];
export default function Particle() {
// Saves the sizes to localStorage, so a reload keeps the layout.
const { defaultLayout, onLayoutChanged } = useDefaultLayout({
id: "v-resizable-4",
storage: typeof window === "undefined" ? undefined : localStorage,
});
return (
<ResizablePanelGroup
className="h-72 w-full max-w-2xl overflow-hidden rounded-xl border text-sm"
defaultLayout={defaultLayout}
onLayoutChanged={onLayoutChanged}
>
<ResizablePanel
collapsedSize="52px"
collapsible
defaultSize="24%"
id="nav"
minSize="18%"
>
<nav className="flex h-full flex-col gap-0.5 overflow-hidden p-2">
{NAV.map((item, i) => (
<span
className={`flex items-center gap-2 rounded-md px-2.5 py-1.5 ${i === 0 ? "bg-accent text-accent-foreground" : "text-muted-foreground"}`}
key={item.label}
>
<item.icon aria-hidden="true" className="size-4 shrink-0" />
<span className="truncate">{item.label}</span>
</span>
))}
</nav>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel defaultSize="36%" id="list" minSize="24%">
<ul className="flex h-full flex-col divide-y overflow-auto">
{MAIL.map((m, i) => (
<li
className={`px-3 py-2.5 ${i === 1 ? "bg-muted/60" : ""}`}
key={m.subject}
>
<p className="truncate font-medium">{m.from}</p>
<p className="truncate text-muted-foreground text-xs">
{m.subject}
</p>
</li>
))}
</ul>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel id="detail" minSize="30%">
<div className="flex h-full flex-col gap-2 p-4">
<p className="font-medium">Re: retry backoff</p>
<p className="text-muted-foreground">
Capping at 30s works. Drag the nav past its minimum to collapse it
to icons; the sizes survive a reload.
</p>
</div>
</ResizablePanel>
</ResizablePanelGroup>
);
}