# Splitter

> Resizable panes with px or percent sizes rendered on the server, min and max limits, collapsible panes, nested splitters and the APG window splitter keyboard model, with no React render during a drag.

Canonical: https://salmonui.dev/docs/layout/splitter

`Splitter` lays out panes side by side (or stacked) with draggable
separators. Panes are CSS flex items with a `flex-basis`, so the server
HTML already has the final layout: no measurement and no layout shift when
it hydrates. It lives in `@salmonui/layout/splitter` with its own
stylesheet.

```tsx
import { Splitter } from '@salmonui/layout/splitter';
import '@salmonui/layout/splitter/styles.css';

export function Workspace() {
  const [sizes, setSizes] = useState({ nav: 240 });
  return (
    <Splitter sizes={sizes} onSizesChange={setSizes} style={{ height: '100vh' }}>
      <Splitter.Pane id="nav" minSize={160} maxSize="40%" collapsible collapsedSize={48}>
        …
      </Splitter.Pane>
      <Splitter.Pane id="main">…</Splitter.Pane>
    </Splitter>
  );
}
```

### Live demo: Splitter

Live demo: https://salmonui.dev/demos/layout/splitter

A mail layout with a collapsible folder pane and a nested vertical splitter between the message list and the preview.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Splitter, type SplitterSize } from '@salmonui/layout/splitter';

export default function SplitterDemo() {
  const [sizes, setSizes] = useState<Record<string, SplitterSize>>({ nav: 180 });
  return (
    <div>
      <Splitter sizes={sizes} onSizesChange={setSizes} style={{ height: 260, border: '1px solid var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}>
        <Splitter.Pane id="nav" minSize={120} maxSize="50%" collapsible collapsedSize={32}>
          <div className="demo-pane">Folders</div>
        </Splitter.Pane>
        <Splitter.Pane id="main">
          <Splitter orientation="vertical" defaultSizes={{ list: '55%' }}>
            <Splitter.Pane id="list" minSize="20%">
              <div className="demo-pane">Messages</div>
            </Splitter.Pane>
            <Splitter.Pane id="preview">
              <div className="demo-pane">Preview</div>
            </Splitter.Pane>
          </Splitter>
        </Splitter.Pane>
      </Splitter>
      <p>
        <output>Folders: {String(sizes['nav'])}</output>
      </p>
    </div>
  );
}
```

## Panes and sizes

- `Splitter.Pane` (also `SplitterPane`) children, or a `panes` array of the
  same props plus `content`; both compile to the same markup.
- Panes are addressed by `id`, not index. `sizes` / `defaultSizes` /
  `onSizesChange` hold sizes by pane id; a pane's own `defaultSize` is used
  when `sizes` has no entry. A size is px (`240` or `'240px'`) or a percent
  of the splitter (`'30%'`); panes without a size share the remaining space.
- `minSize` / `maxSize` (px or %) limit pointer and keyboard resizing and
  are also CSS limits.
- `resizable={false}` on a pane: the separator between two such panes is
  not focusable and does not move. `disabled` makes every separator inert.
- Add or remove panes by rendering or removing `Splitter.Pane` children;
there are no imperative pane methods. Store `sizes` to
  persist them.

## Resizing

During a pointer drag the separator writes `flex-basis` once per animation
frame; React does not render. `onResizeStart([idA, idB])` fires first,
`onResize(sizes)` per frame with px sizes (not for React state), and
`onSizesChange(sizes)` once on release, with each pane in its own unit (a
percent pane stays a percent). Escape cancels a drag.

The separator is a 1 px line with a 24 px touch hit area (there is
the size is not configurable). Pointer events cover mouse, touch and pen.

## Collapse

`collapsible`, `collapsed` / `defaultCollapsed` / `onCollapsedChange`,
`collapsedSize` (default 0; keep a rail visible with e.g. 48). The change
callback receives an event whose `preventDefault()` vetoes the change for
an uncontrolled pane; `onAfterCollapsedChange` fires after it was applied.
To collapse or expand from code, set the controlled `collapsed` prop on the
pane, addressed by its `id`.

### Live demo: Splitter panes, collapse and events

Live demo: https://salmonui.dev/demos/layout/splitter-panes

An editor Splitter built from a panes array, with a fixed-width tool column, a collapsible outline whose collapse can be vetoed, an inspector collapsed through its controlled prop, a disabled switch, right-to-left and an event log.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Splitter } from '@salmonui/layout/splitter';
import { EventLog, useEventLog } from '../../components/demo-kit';

// An editor layout from a `panes` array: a fixed toolbar column, a collapsible
// outline whose collapse can be vetoed, the editor, and an inspector collapsed
// by id through its controlled `collapsed` prop.
export default function SplitterPanesDemo() {
  const [inspector, setInspector] = useState(false);
  const [lockOutline, setLockOutline] = useState(false);
  const [disabled, setDisabled] = useState(false);
  const [rtl, setRtl] = useState(false);
  const [log, record] = useEventLog(6);
  return (
    <div>
      <div className="demo-toolbar">
        <Button variant="outline" aria-pressed={inspector} onClick={() => setInspector(!inspector)}>
          {inspector ? 'Expand inspector' : 'Collapse inspector'}
        </Button>
        <Checkbox checked={lockOutline} onCheckedChange={(checked) => setLockOutline(checked)}>
          Lock the outline (veto collapse)
        </Checkbox>
        <Checkbox checked={disabled} onCheckedChange={(checked) => setDisabled(checked)}>
          Disabled
        </Checkbox>
        <Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
          Right to left
        </Checkbox>
      </div>
      <div dir={rtl ? 'rtl' : 'ltr'}>
        <Splitter
          aria-label="Editor layout"
          disabled={disabled}
          style={{ height: 220, border: '1px solid var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}
          onResizeStart={([a, b]) => record(`resize start: ${a} | ${b}`)}
          onSizesChange={(sizes) => record(`sizes: ${Object.entries(sizes).map(([id, s]) => `${id} ${s}`).join(', ')}`)}
          panes={[
            { id: 'tools', defaultSize: 56, resizable: false, content: <div className="demo-pane">Tools</div>, style: { background: 'var(--salmon-surface-sunken)' } },
            {
              id: 'outline',
              defaultSize: 160,
              minSize: 100,
              collapsible: true,
              collapsedSize: 24,
              content: <div className="demo-pane">Outline</div>,
              onCollapsedChange: (collapsed, event) => {
                if (lockOutline) event.preventDefault();
                record(`outline ${collapsed ? 'collapse' : 'expand'} requested (${event.source})${lockOutline ? ': vetoed' : ''}`);
              },
              onAfterCollapsedChange: (collapsed) => record(`outline ${collapsed ? 'collapsed' : 'expanded'}`),
            },
            { id: 'editor', minSize: 120, content: <div className="demo-pane">Editor</div> },
            {
              id: 'inspector',
              defaultSize: '25%',
              collapsible: true,
              collapsed: inspector,
              onCollapsedChange: (collapsed) => setInspector(collapsed),
              content: <div className="demo-pane">Inspector</div>,
            },
          ]}
        />
      </div>
      <EventLog entries={log} label="Splitter events" />
    </div>
  );
}
```

## Keyboard and accessibility

APG Window Splitter: each separator is `role="separator"` with
`aria-orientation`, `aria-controls` (the pane before it), an accessible
name (`localeText.resizePane`, default "Resize") and `aria-valuenow` /
`aria-valuemin` / `aria-valuemax` as the percent of that pane.

| Key | Action |
| --- | --- |
| Left / Right (horizontal), Up / Down (vertical) | Resize by 10 px |
| Shift + arrow | Resize by 50 px |
| Home / End | The pane's minimum / maximum |
| Enter | Collapse or expand the collapsible pane |

## Nesting, RTL and styling

- Nested splitters are plain composition: a `Splitter` inside a
  `Splitter.Pane`; each separator handles its own pointer and keyboard.
- In a horizontal splitter under `dir="rtl"` the first pane is on the
  right, and pointer moves and Left / Right keys follow the reading
  direction. Reverse panes by reordering children.
- `className` / `style` on `Splitter` and on each pane; hooks
  `.salmon-splitter[data-orientation]`, `.salmon-splitter-pane[data-pane]`
  (`data-collapsed`), `.salmon-splitter-separator` (`data-dragging`,
  `data-disabled`). Content is `ReactNode`; React owns the
  lifecycle.

API reference: [`@salmonui/layout/splitter`](https://salmonui.dev/docs/reference/layout/splitter).

## Bundle

The JavaScript is 2.6 KB gzip and the stylesheet 0.44 KB.
