Resizable
Splits an area into panels whose sizes the user changes by dragging a handle or using the keyboard.
Installation
pnpm dlx shadcn@latest add @silkware/resizableTo copy the component by hand, install its dependencies:
pnpm add react-resizable-panels cnThen copy the source into components/ui/resizable.tsx:
"use client"
import { cn } from "cn"import * as ResizablePrimitive from "react-resizable-panels"
function ResizablePanelGroup({ className, ...props}: ResizablePrimitive.GroupProps) { return ( <ResizablePrimitive.Group data-slot="resizable-panel-group" className={cn( "flex h-full w-full aria-[orientation=vertical]:flex-col", className )} {...props} /> )}
function ResizablePanel({ ...props }: ResizablePrimitive.PanelProps) { return <ResizablePrimitive.Panel data-slot="resizable-panel" {...props} />}
function ResizableHandle({ withHandle, className, ...props}: ResizablePrimitive.SeparatorProps & { withHandle?: boolean}) { return ( <ResizablePrimitive.Separator data-slot="resizable-handle" className={cn( "relative flex w-px items-center justify-center bg-border ring-offset-background after:absolute after:inset-y-0 after:left-1/2 after:w-1 after:-translate-x-1/2 focus-visible:ring-1 focus-visible:ring-ring focus-visible:outline-hidden aria-[orientation=horizontal]:h-px aria-[orientation=horizontal]:w-full aria-[orientation=horizontal]:after:left-0 aria-[orientation=horizontal]:after:h-1 aria-[orientation=horizontal]:after:w-full aria-[orientation=horizontal]:after:translate-x-0 aria-[orientation=horizontal]:after:-translate-y-1/2 [&[aria-orientation=horizontal]>div]:rotate-90", className )} {...props} > {withHandle && ( <div className="z-10 flex h-6 w-1 shrink-0 rounded-lg bg-border" /> )} </ResizablePrimitive.Separator> )}
export { ResizableHandle, ResizablePanel, ResizablePanelGroup }Usage
import { ResizableHandle, ResizablePanel, ResizablePanelGroup,} from "@/components/ui/resizable"<ResizablePanelGroup orientation="horizontal" className="min-h-48 max-w-md rounded-lg border"> <ResizablePanel defaultSize="40%" minSize="20%"> Orders </ResizablePanel> <ResizableHandle /> <ResizablePanel defaultSize="60%"> Order detail </ResizablePanel></ResizablePanelGroup>There are two things to know before you start:
- Sizes are strings or numbers, and they mean different things. A string such as
"40%"is a percentage, a bare number such as40is pixels. Use strings. - The group sets its own
widthandheightto 100% of its parent. Limit it withmin-h-*andmax-w-*, or give its parent a size. Ah-48orw-64class on the group is overridden.
Examples
Horizontal
Panels side by side, split by a vertical handle. This is the default orientation.
Vertical
orientation="vertical" stacks the panels and uses a horizontal handle.
With handle
withHandle adds a small grip to the handle so the user sees that it can be dragged. Use it when the handle is the only sign that the layout is adjustable.
Nested
Put a group inside a panel to split one region again in the other direction.
Collapsible
collapsible lets a panel fold away when dragged below minSize, down to collapsedSize. Drag the handle left, or press Enter on it, to fold the filters.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the next handle. |
| ArrowLeft | Moves a vertical handle left and shrinks the panel before it. A horizontal handle ignores it. |
| ArrowRight | Moves a vertical handle right and grows the panel before it. A horizontal handle ignores it. |
| ArrowUp | Moves a horizontal handle up and shrinks the panel above it. A vertical handle ignores it. |
| ArrowDown | Moves a horizontal handle down and grows the panel above it. A vertical handle ignores it. |
| Home | Shrinks the panel before the handle to its minimum size. |
| End | Grows the panel before the handle to its maximum size. |
| Enter | Collapses or expands the panel, when the panel is collapsible. |
react-resizable-panels provides:
- A
separatorrole on each handle, witharia-orientation,aria-valuenow,aria-valuemin,aria-valuemaxandaria-controlsfor the panel it resizes. - Pointer and touch dragging, and the keyboard resizing above.
- Size limits that hold for every input method.
You provide:
- An
aria-labelon a handle when the purpose of the split is not obvious from the panels. - Content that works at the minimum size: set
minSizeso labels are not cut. - Another way to reach content in a collapsible panel, such as a button, because a folded panel has no width, so its content cannot be seen.
Guidelines
When to use
- When users work with two or more areas at once and need different sizes at different times: a list beside a detail view, a table above a preview.
- In desktop tools that have room to share.
When not to use
- For a layout that never needs to change. Use a normal layout, or a Sidebar with a fixed width.
- To show and hide a block. Use a Collapsible.
- On small screens. A split of 375px leaves each panel unusable. Stack the content instead.
Content
- Set
minSizeon every panel so content stays readable. - Use
maxSizewhen one panel must not take over. - Let each panel scroll on its own with a Scroll Area.
Write sizes as percentage strings and set a minimum.
Don’t pass a bare number. 30 is 30 pixels, so the panel collapses to a sliver.
API reference
ResizablePanelGroup
The container. Renders a div through the Group of react-resizable-panels, as a flex row, or a column when vertical. It accepts every Group prop.
| Prop | Type | Default | Description |
|---|---|---|---|
orientation? | "horizontal" | "vertical" | "horizontal" | The direction in which panels are laid out and resized. |
defaultLayout? | Record<string, number> | None | The sizes of the panels by panel id, for example to restore a saved layout. |
onLayoutChange? | (layout: Record<string, number>) => void | None | Called while the layout changes. |
onLayoutChanged? | (layout: Record<string, number>) => void | None | Called after the layout has changed, for example to save it. |
disabled? | boolean | false | Prevents resizing of the whole group. |
className? | string | None | Classes merged after the base classes. |
ResizablePanel
One panel. Renders a div through the Panel of react-resizable-panels.
| Prop | Type | Default | Description |
|---|---|---|---|
id? | string | None | Identifies the panel in layouts. Needed when you save and restore sizes. |
defaultSize? | number | string | None | The size at first. A string is a percentage, a number is pixels. |
minSize? | number | string | None | The smallest size. A string is a percentage, a number is pixels. |
maxSize? | number | string | None | The largest size. A string is a percentage, a number is pixels. |
collapsible? | boolean | false | Lets the panel fold to collapsedSize when it is dragged below minSize. |
collapsedSize? | number | string | None | The size of the panel when it is collapsed. |
disabled? | boolean | false | Prevents resizing of this panel. |
onResize? | (size: PanelSize, id: string | number | undefined, prevSize: PanelSize | undefined) => void | None | Called when the panel size changes. PanelSize holds asPercentage and inPixels. |
ResizableHandle
The divider between two panels. Renders a div with the separator role through the Separator of react-resizable-panels.
| Prop | Type | Default | Description |
|---|---|---|---|
withHandle? | boolean | false | Shows a grip in the middle of the handle. |
disabled? | boolean | false | Prevents dragging this handle. |
className? | string | None | Classes merged after the base classes. |