@k8ordo/ui

ResizablePanels

Two panes split by a divider that the user drags or moves with the arrow keys.

Import

ts
import { ResizablePanels } from '@k8ordo/ui';

Usage

value is the first pane’s share in percent; the second pane takes the rest. The root fills its parent, so give the parent a size: a vertical split needs a height. The divider takes focus, and the arrow keys move it the way they point on screen. Home and End jump to min and max. In a right-to-left page the first pane sits on the right, so ArrowLeft widens it. The panes follow the line of text, so in vertical writing mode orientation="horizontal" stacks them.

Sidebar

Content

tsx
<div className={styles.frame}>
  <ResizablePanels.Root defaultValue={30}>
    <ResizablePanels.Panel>Sidebar</ResizablePanels.Panel>
    <ResizablePanels.Handle />
    <ResizablePanels.Panel>Content</ResizablePanels.Panel>
  </ResizablePanels.Root>
</div>

Vertical

orientation="vertical" stacks the two panes. The divider moves with the up and down arrow keys, and its value is the top pane’s share of the height.

Editor

Terminal

tsx
<div className={styles.frame}>
  <ResizablePanels.Root orientation="vertical">
    <ResizablePanels.Panel>Editor</ResizablePanels.Panel>
    <ResizablePanels.Handle />
    <ResizablePanels.Panel>Terminal</ResizablePanels.Panel>
  </ResizablePanels.Root>
</div>

Naming the divider

The divider’s value is the first pane’s size, so name it after the first pane. Point aria-labelledby at its heading when it has one. Without one it falls back to resizablePanelsHandle from the dictionary.

Files

Preview

tsx
<ResizablePanels.Root defaultValue={30}>
  <ResizablePanels.Panel>
    <h2 id="files-heading">Files</h2>
  </ResizablePanels.Panel>
  <ResizablePanels.Handle aria-labelledby="files-heading" />
  <ResizablePanels.Panel>Preview</ResizablePanels.Panel>
</ResizablePanels.Root>

Controlled

onChange receives the share while dragging, too. From the keyboard it moves by step.

Sidebar: 30%

Sidebar

Content

tsx
const [size, setSize] = useState(30);

<ResizablePanels.Root onChange={setSize} value={size}>
  …
</ResizablePanels.Root>

Props

ResizablePanels.Root

children?
Type: ReactNode
Default: -
defaultValue?
Type: number
Default: 50
max?
Type: number
Default: 90
min?
Type: number
Default: 10
onChange?
Type: (value: number) => void
Default: -
orientation?
Type: 'horizontal'|'vertical'
Default: 'horizontal'
ref?
Type: Ref<HTMLDivElement>
Default: -
step?
Type: number
Default: 5
value?
Type: number
Default: -
PropTypeDefault
children?ReactNode-
defaultValue?number50
max?number90
min?number10
onChange?(value: number) => void-
orientation?'horizontal'|'vertical''horizontal'
ref?Ref<HTMLDivElement>-
step?number5
value?number-

Inherited attributes (except the ones set internally): HTMLAttributes<HTMLDivElement>

ResizablePanels.Panel

children?
Type: ReactNode
Default: -
ref?
Type: Ref<HTMLDivElement>
Default: -
PropTypeDefault
children?ReactNode-
ref?Ref<HTMLDivElement>-

Inherited attributes (except the ones set internally): HTMLAttributes<HTMLDivElement>

ResizablePanels.Handle

PropTypeDefault

Inherited attributes (except the ones set internally): HTMLAttributes<HTMLDivElement>

The wording this component draws (labels, placeholders, and so on) comes from the message dictionary unless a prop sets it. To replace it, see: i18n

k8ordo

React libraries that use Baseline features without holding back.