

# Splitter

Two or more panels separated by dividers the user can drag or move with the keyboard, side by side or stacked. It follows the APG [window splitter](https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/) pattern and is built on `ResizeHandle` for the divider, so pointer capture, arrow keys and `Home`/`End` behave exactly like the resizable sidebars in `Layout`.

Sizes are **percentages of the combined panel space** (the container minus the dividers), one per panel, summing to 100. They are never pixels, so the same `defaultSizes` work at any width and on the server. Resizing writes one CSS custom property per panel (`flex: <size> 1 0%`); panels clip or scroll their content (`min-width: 0`, `overflow: auto`) instead of forcing themselves open.

Give the `Splitter` a size along its main axis (a `height` for `vertical`, a width from its parent for `horizontal`); the panels share what it has.

## Examples

### Two panes

`defaultSizes` seeds the split (uncontrolled), `minSizes` / `maxSizes` clamp each panel. Double-click a divider to reset its two neighbours to `defaultSizes`.

```tsx
import { Splitter } from '@var-ui/react';

export default function Preview() {
  return (
    <Splitter defaultSizes={[30, 70]} minSizes={[15, 30]} style={{ height: '12.5rem' }}>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Sidebar</div>
      </Splitter.Panel>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Content. Drag the divider, or focus it and use the arrow keys.</div>
      </Splitter.Panel>
    </Splitter>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Three panes

Each divider moves only the two panels next to it; the others keep their size.

```tsx
import { Splitter } from '@var-ui/react';

export default function Preview() {
  return (
    <Splitter defaultSizes={[20, 50, 30]} minSizes={[10, 20, 10]} style={{ height: '12.5rem' }}>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Navigation</div>
      </Splitter.Panel>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Editor</div>
      </Splitter.Panel>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Inspector</div>
      </Splitter.Panel>
    </Splitter>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Vertical

`orientation="vertical"` stacks the panels and uses `ArrowDown` / `ArrowUp`.

```tsx
import { Splitter } from '@var-ui/react';

export default function Preview() {
  return (
    <Splitter orientation="vertical" defaultSizes={[60, 40]} style={{ height: '16.25rem' }}>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Editor</div>
      </Splitter.Panel>
      <Splitter.Panel minSize={15}>
        <div style={{ padding: '0.75rem' }}>Terminal</div>
      </Splitter.Panel>
    </Splitter>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Collapsible sidebar

Mark a panel `collapsible` (or pass `collapsible` to `Splitter`, as `true` or one flag per panel). `Enter` on the divider collapses the panel to zero and restores its previous size. Dragging a collapsible panel below half its minimum, or arrowing below it, collapses it too. This demo is controlled with `sizes` and `onSizesChange`.

```tsx
import { Splitter } from '@var-ui/react';
import { useState } from 'react';

export default function Preview() {
  const [sizes, setSizes] = useState([25, 75]);

  return (
    <div style={{ display: 'grid', gap: '0.5rem' }}>
      <Splitter sizes={sizes} onSizesChange={setSizes} style={{ height: '12.5rem' }}>
        <Splitter.Panel collapsible minSize={15}>
          <div style={{ padding: '0.75rem' }}>Sidebar. Press Enter on the divider to collapse it.</div>
        </Splitter.Panel>
        <Splitter.Panel>
          <div style={{ padding: '0.75rem' }}>Content</div>
        </Splitter.Panel>
      </Splitter>
      <span>Sidebar: {sizes[0]}%</span>
    </div>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Persisted

`autoSaveId` saves the sizes in `localStorage` (`var-ui-splitter:<id>`). The first render always uses `defaultSizes` so server and client markup match; the saved sizes are applied right after mount.

```tsx
import { Splitter } from '@var-ui/react';

export default function Preview() {
  return (
    <Splitter autoSaveId="docs-splitter-demo" defaultSizes={[40, 60]} style={{ height: '12.5rem' }}>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Resize me, then reload the page.</div>
      </Splitter.Panel>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>The sizes are saved in localStorage.</div>
      </Splitter.Panel>
    </Splitter>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Nested

Splitters nest: put a `Splitter` inside a panel (give it `height: '100%'`).

```tsx
import { Splitter } from '@var-ui/react';

export default function Preview() {
  return (
    <Splitter defaultSizes={[30, 70]} style={{ height: '17.5rem' }}>
      <Splitter.Panel>
        <div style={{ padding: '0.75rem' }}>Files</div>
      </Splitter.Panel>
      <Splitter.Panel>
        <Splitter orientation="vertical" defaultSizes={[65, 35]} style={{ height: '100%' }}>
          <Splitter.Panel>
            <div style={{ padding: '0.75rem' }}>Editor</div>
          </Splitter.Panel>
          <Splitter.Panel>
            <div style={{ padding: '0.75rem' }}>Console</div>
          </Splitter.Panel>
        </Splitter>
      </Splitter.Panel>
    </Splitter>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (a splitter needs client state) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

## Customising a divider

Dividers are rendered between consecutive panels automatically. To label or style one, place a `Splitter.Handle` between the two panels; it renders nothing itself, and its props (`className`, `style`, `aria-label`, `id`, `ref`, handlers) are applied to that divider:

```tsx
<Splitter>
  <Splitter.Panel>Files</Splitter.Panel>
  <Splitter.Handle aria-label="Resize file tree" />
  <Splitter.Panel>Editor</Splitter.Panel>
</Splitter>
```

Panels and handles must be direct children (not wrapped in a fragment or component). The default divider name is "Resize panels"; override it with `handleLabel`.

## Props

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| autoSaveId | string \| undefined | no |  | Persist sizes in `localStorage` (`var-ui-splitter:<id>`). The first render always uses `defaultSizes`; stored sizes are applied right after mount. |
| children | ReactNode | no |  | Two or more `Splitter.Panel`s, optionally with `Splitter.Handle`s between them. |
| collapsible | boolean \| boolean[] \| undefined | no | false | Panels that may collapse to zero: `true` for all, or one flag per panel. |
| defaultSizes | number[] \| undefined | no | an equal split | Initial sizes (uncontrolled). |
| handleLabel | string \| undefined | no | 'Resize panels' | Accessible name of the dividers, unless a `Splitter.Handle` sets `aria-label`. |
| largeStep | number \| undefined | no | 20 | Arrow-key step with Shift held, in percent. |
| maxSizes | number[] \| undefined | no |  | Largest size per panel, in percent. A `Splitter.Panel`'s `maxSize` wins. |
| minSizes | number[] \| undefined | no |  | Smallest size per panel, in percent. A `Splitter.Panel`'s `minSize` wins. |
| onSizesChange | ((sizes: number[]) => void) \| undefined | no |  | Fires with the next sizes while dragging or using the keyboard. |
| orientation | "horizontal" \| "vertical" \| undefined | no | 'horizontal' | Layout direction. `horizontal` puts panels side by side (vertical dividers); `vertical` stacks them. |
| sizes | number[] \| undefined | no |  | Panel sizes (controlled), one per `Splitter.Panel`, as percentages of the combined panel space (the container minus the dividers; platform-free, never pixels). Sums to 100. |
| step | number \| undefined | no | 5 | Arrow-key step, in percent. |

`onChange` is the native `change` event of the root `<div>`: it only fires for form controls inside the panels, bubbling up. Use `onSizesChange` for sizes.

## Accessibility

Each divider is `role="separator"` with `tabindex="0"`, `aria-orientation` (a `vertical` separator for side-by-side panels, per the APG), `aria-valuenow` (the size of the panel before it, in percent), `aria-valuemin` / `aria-valuemax` (the limits implied by both neighbours), and `aria-controls` pointing at that panel's id. Give panels an `aria-label` or heading when they are landmarks.

Pointer drags use pointer capture, so they keep tracking outside the divider, and the hit area extends well past the visible line for touch. In a right-to-left context the horizontal arrows and drag direction are mirrored. The divider is drawn with `CanvasText` in forced-colors mode and shows the standard focus ring.

## Keyboard

| Key                        | Behavior                                                                                   |
| -------------------------- | ------------------------------------------------------------------------------------------ |
| `Tab` / `Shift+Tab`        | Moves focus to the next / previous divider.                                                |
| `ArrowRight` / `ArrowLeft` | Horizontal: grows / shrinks the panel before the divider by `step` (mirrored in RTL).      |
| `ArrowDown` / `ArrowUp`    | Vertical: grows / shrinks the panel before the divider by `step`.                          |
| `Shift` + arrow            | Same, by `largeStep`.                                                                      |
| `Home` / `End`             | Smallest / largest size allowed by both neighbours (`Home` collapses a collapsible panel). |
| `Enter`                    | Collapses or restores a collapsible neighbour.                                             |
| Double-click               | Resets the two neighbours to their `defaultSizes` ratio.                                   |


> Also accepts all standard HTML attributes (ARIA attributes, event handlers, styles, …). Run `var-ui component splitter --all-props` for the complete list.
