# Splitter (/docs/splitter)





`Splitter` puts panels side by side, or stacked, and a handle between every two of them. Dragging a handle gives one panel more room and its neighbour less. Use it where people need to choose the balance themselves: a project subject next to the code, or a list next to its detail view in a staff tool.

```tsx
import { Splitter } from '@42-central/ui-react/splitter';

<div className="h-96">
  <Splitter>
    <Splitter.Panel id="subject" defaultSize={40} min={25} max={60}>
      <Subject />
    </Splitter.Panel>
    <Splitter.Panel id="code" min={30}>
      <Code />
    </Splitter.Panel>
  </Splitter>
</div>
```

<Preview align="block" code="&#x22;<Splitter>\n  <Splitter.Panel id=\&#x22;subject\&#x22; defaultSize={40} min={25} max={60}>\n    <Subject />\n  </Splitter.Panel>\n  <Splitter.Panel id=\&#x22;code\&#x22; min={30}>\n    <Code />\n  </Splitter.Panel>\n</Splitter>&#x22;">
  <SubjectDemo />
</Preview>

The splitter fills its container (`height` and `width` are `100%`), so give the container a height, or size the splitter with `className`.

## Playground [#playground]

<story.Playground />

### Full prop browser [#full-prop-browser]

<story.WithControl />

## Orientation [#orientation]

`orientation="vertical"` stacks the panels, and the handles turn horizontal. Everything else works the same way: up and down instead of left and right.

<Preview align="block" code="&#x22;<Splitter orientation=\&#x22;vertical\&#x22;>\n  <Splitter.Panel id=\&#x22;code\&#x22; defaultSize={65} min={30}>\n    <Code />\n  </Splitter.Panel>\n  <Splitter.Panel id=\&#x22;output\&#x22; min={15}>\n    <Output />\n  </Splitter.Panel>\n</Splitter>&#x22;">
  <VerticalDemo />
</Preview>

## Sizes and limits [#sizes-and-limits]

Each panel sets its own sizes:

* `defaultSize` is where it starts. Panels without one share what's left equally.
* `min` and `max` are as far as a drag or a key press can take it.

A number is a percentage of the splitter. A string can also be a CSS length (`"240px"`, `"15rem"`), measured against the splitter's own size. Any number of panels works. A handle moves only its two neighbours, and when one of them hits its limit the next panel along gives way.

<Preview align="block" code="&#x22;<Splitter>\n  <Splitter.Panel id=\&#x22;files\&#x22; defaultSize=\&#x22;12rem\&#x22; min=\&#x22;8rem\&#x22; max=\&#x22;20rem\&#x22;>…</Splitter.Panel>\n  <Splitter.Panel id=\&#x22;editor\&#x22; min={30}>…</Splitter.Panel>\n  <Splitter.Panel id=\&#x22;preview\&#x22; defaultSize={25} min={15} max={40}>…</Splitter.Panel>\n</Splitter>&#x22;">
  <ThreePanelsDemo />
</Preview>

`Splitter.Panel`s must be **direct children** of `Splitter`: the root reads their sizes from their props, so a panel wrapped in your own component isn't seen. Give each panel an `id` when panels come and go, or when something refers to one (`Splitter.Toggle`, `useSplitterContext`). Without one, a panel is `panel-0`, `panel-1`, and so on.

## Collapsible panels [#collapsible-panels]

A `collapsible` panel snaps to its `collapsedSize` (0 by default) when it's dragged past half of its `min`, and back out when it's dragged open again. A collapsible panel needs a `min`: it's the size it reopens at when it has no earlier size to go back to.

There are three ways to collapse and expand it:

* **Drag** the handle past half of `min`.
* **Press <kbd>Enter</kbd>** on the handle after it. That toggles the panel *before* the handle.
* **`Splitter.Toggle`**, a button you put anywhere inside the splitter, usually in a panel's header. It's in the tab order, has `aria-expanded` and `aria-controls`, and takes its name from `translations`. Give it children to replace the chevron.

A panel collapsed to nothing is made `inert`, so its content leaves the tab order and the accessibility tree. With a `collapsedSize` above 0 (a rail of icons), it stays interactive.

<Preview align="block" code="&#x22;<Splitter>\n  <Splitter.Panel id=\&#x22;list\&#x22; defaultSize={35} min={25} max={50} collapsible>\n    <header className=\&#x22;sticky top-0 …\&#x22;>\n      Students\n      <Splitter.Toggle panel=\&#x22;list\&#x22; />\n    </header>\n    <StudentList />\n  </Splitter.Panel>\n  <Splitter.Panel id=\&#x22;detail\&#x22;>\n    <Detail />\n  </Splitter.Panel>\n</Splitter>\n\n// Inside the detail: a way back once the list is gone.\nconst splitter = useSplitterContext();\n{splitter.collapsed('list') && <Splitter.Toggle panel=\&#x22;list\&#x22; />}&#x22;">
  <ListDetailDemo />
</Preview>

`onCollapse` and `onExpand` tell you which panel changed, with its `id`.

## Panels scroll their own content [#panels-scroll-their-own-content]

Each panel is its own scroll container (`overflow: auto`), and it can shrink below its content (`min-width: 0`, `min-height: 0`), so a long subject or a wide table scrolls inside its pane instead of pushing the handle away. Put any content in a panel.

Because the panel is the scroller, `position: sticky` works inside it: the list's header above stays at the top of its panel while the students scroll under it. A sticky element inside a nested scroller of your own sticks to that one instead.

If you lay a panel's content out with grid or flex, the same rule applies one level down: a grid or flex item that should scroll needs `min-h-0` (or `min-w-0`) and its own `overflow`, or it grows to fit its content and the panel scrolls instead.

## Controlled sizes [#controlled-sizes]

`size` and `onResize` hold the sizes in your state: an array of percentages, in panel order. `onResize` fires on every change, while dragging too. Without `size`, the splitter keeps its own, starting at each panel's `defaultSize`.

`onResizeEnd` fires once per gesture (the end of a drag, a key press, a toggle or a reset) with the final sizes.

### Remembering the sizes [#remembering-the-sizes]

There's no storage prop. Keep the sizes in [`useLocalStorage`](/docs/hooks/use-local-storage) instead, and they come back on the next visit, and in every tab:

<Preview align="block" code="&#x22;import { useLocalStorage } from '@42-central/ui-react';\n\nconst [size, setSize] = useLocalStorage({ key: 'subject-split', defaultValue: [50, 50] });\n\n<Splitter size={size} onResize={setSize}>\n  <Splitter.Panel id=\&#x22;subject\&#x22; defaultSize={50} min={20}>…</Splitter.Panel>\n  <Splitter.Panel id=\&#x22;code\&#x22; min={20}>…</Splitter.Panel>\n</Splitter>&#x22;">
  <ControlledDemo />
</Preview>

`useLocalStorage` renders `defaultValue` on the server and on the first client render, then switches to the stored value, so a visitor with saved sizes sees the handle move once after the page loads. To write to storage less often, keep the live sizes in `useState` and save them in `onResizeEnd`.

## Reset [#reset]

A double-click on a handle puts every panel back to its `defaultSize`, and reports it through `onResize` and `onResizeEnd`. `useSplitterContext().reset()` does the same from your own button. A `disabled` handle doesn't reset.

## Nested splitters [#nested-splitters]

A panel can hold another splitter, in the other orientation. Each splitter has its own handles and its own sizes, and `useSplitterContext()` reads the nearest one.

<Preview align="block" code="&#x22;<Splitter>\n  <Splitter.Panel id=\&#x22;subject\&#x22; defaultSize={35} min={20}>…</Splitter.Panel>\n  <Splitter.Panel id=\&#x22;work\&#x22; min={30}>\n    <Splitter orientation=\&#x22;vertical\&#x22;>\n      <Splitter.Panel id=\&#x22;code\&#x22; defaultSize={70} min={30}>…</Splitter.Panel>\n      <Splitter.Panel id=\&#x22;terminal\&#x22; min={15} collapsible>…</Splitter.Panel>\n    </Splitter>\n  </Splitter.Panel>\n</Splitter>&#x22;">
  <NestedDemo />
</Preview>

## Configuring a handle [#configuring-a-handle]

The splitter adds a handle between every two panels by itself. Place a `Splitter.Handle` yourself only to configure one: lock it with `disabled`, or give it a class. A handle you place replaces the one the splitter would have added there.

```tsx
<Splitter>
  <Splitter.Panel id="nav" defaultSize="16rem">…</Splitter.Panel>
  <Splitter.Handle disabled />
  <Splitter.Panel id="main">…</Splitter.Panel>
</Splitter>
```

A disabled handle keeps its line, loses its grip, leaves the tab order, and ignores the pointer, the keyboard and double-clicks.

## Right-to-left [#right-to-left]

The direction comes from the locale, as for the kit's other interactive parts. Under an RTL locale the first panel sits on the right, a drag follows the pointer, and <kbd>←</kbd> moves the handle left, which grows the first panel.

<Preview align="block" code="&#x22;<IntlProvider locale=\&#x22;ar-EG\&#x22;>\n  <div dir=\&#x22;rtl\&#x22; lang=\&#x22;ar\&#x22;>\n    <Splitter translations={{ handle: 'تغيير الحجم' }}>\n      <Splitter.Panel id=\&#x22;list\&#x22; defaultSize={35} min={20}>…</Splitter.Panel>\n      <Splitter.Panel id=\&#x22;detail\&#x22; min={30}>…</Splitter.Panel>\n    </Splitter>\n  </div>\n</IntlProvider>&#x22;">
  <DirectionDemo />
</Preview>

`Splitter` stamps the locale's `dir` on its root. `<html dir="rtl">` alone leaves it left to right: see [Text direction](/docs/intl#text-direction).

## Anatomy [#anatomy]

```tsx
<Splitter testId="workspace">
  <Splitter.Panel id="list" testId="workspace-list">
    <Splitter.Toggle panel="list" testId="workspace-list-toggle" />
  </Splitter.Panel>
  <Splitter.Handle testId="workspace-handle" /> {/* optional: one is added for you */}
  <Splitter.Panel id="detail" testId="workspace-detail" />
</Splitter>
```

* **`Splitter`** (also `Splitter.Root`): the flex container. It reads its panels, adds the handles and runs the machine. `testId` lands on it.
* **`Splitter.Panel`**: one pane, a scroll container. `testId` lands on it. It takes `asChild`, to render as your own element (a `<section>`, an `<aside>`).
* **`Splitter.Handle`**: the separator between two panels, a `<button role="separator">` with the grip inside. `testId` lands on the button. Added automatically; place one only to configure it. No `asChild`: it renders its own grip.
* **`Splitter.Toggle`**: a collapse button for one panel. `testId` lands on the button. It takes `asChild`.

Every part also carries Ark's `data-scope="splitter"` and `data-part`. A panel has `data-collapsed` while it's collapsed, and a handle has `data-dragging` during a drag and `data-focus` while focused. See [Testing](/docs/testing).

## API [#api]

### Splitter [#splitter]

<TypeTable
  type="{
  children: {
    type: &#x22;ReactNode&#x22;,
    description: &#x22;The Splitter.Panels, as direct children, and optionally the Splitter.Handles between them.&#x22;,
  },
  orientation: {
    type: &#x22;'horizontal' | 'vertical'&#x22;,
    default: &#x22;'horizontal'&#x22;,
    description: &#x22;Panels side by side, or stacked.&#x22;,
  },
  size: {
    type: &#x22;number[]&#x22;,
    description: &#x22;The sizes, in percent, in panel order. Controlled: pair it with onResize.&#x22;,
  },
  onResize: {
    type: &#x22;(size: number[]) => void&#x22;,
    description: &#x22;Every change, while dragging too.&#x22;,
  },
  onResizeEnd: {
    type: &#x22;(size: number[]) => void&#x22;,
    description: &#x22;Once per drag, key press, toggle or reset, with the final sizes. The moment to save them.&#x22;,
  },
  onCollapse: {
    type: &#x22;(panel: string) => void&#x22;,
    description: &#x22;A panel reached its collapsedSize.&#x22;,
  },
  onExpand: {
    type: &#x22;(panel: string) => void&#x22;,
    description: &#x22;A panel left its collapsedSize.&#x22;,
  },
  step: {
    type: &#x22;number&#x22;,
    default: &#x22;1&#x22;,
    description: &#x22;How far one arrow key moves a handle, in percent. Shift + arrow moves it by 10.&#x22;,
  },
  translations: {
    type: &#x22;{ handle?, collapse?, expand? }&#x22;,
    description: &#x22;The accessible names. See [Internationalization](#internationalization).&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the root. It fills its container by default (h-full w-full); size it here.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the root.&#x22;,
  },
}"
/>

Every other `<div>` attribute is passed to the root. No `asChild`: the root adds handles between its children.

### Splitter.Panel [#splitterpanel]

<TypeTable
  type="{
  id: {
    type: &#x22;string&#x22;,
    default: &#x22;'panel-<index>'&#x22;,
    description: &#x22;Names the panel for Splitter.Toggle, useSplitterContext and the callbacks. Set it when panels are conditional.&#x22;,
  },
  defaultSize: {
    type: &#x22;number | string&#x22;,
    description: &#x22;The starting size: a percentage (30, \&#x22;30%\&#x22;) or a CSS length (\&#x22;240px\&#x22;, \&#x22;15rem\&#x22;). Panels without one share what's left.&#x22;,
  },
  min: {
    type: &#x22;number | string&#x22;,
    default: &#x22;0&#x22;,
    description: &#x22;The smallest size a drag or a key press can reach.&#x22;,
  },
  max: {
    type: &#x22;number | string&#x22;,
    default: &#x22;100&#x22;,
    description: &#x22;The largest size a drag or a key press can reach.&#x22;,
  },
  collapsible: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description: &#x22;Dragging past half of min snaps the panel to collapsedSize. Give it a min.&#x22;,
  },
  collapsedSize: {
    type: &#x22;number | string&#x22;,
    default: &#x22;0&#x22;,
    description: &#x22;The size of a collapsed panel. A panel collapsed to 0 is inert.&#x22;,
  },
  asChild: {
    type: &#x22;boolean&#x22;,
    description: &#x22;Render as your own element, which keeps the panel's props and styles.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the panel.&#x22;,
  },
}"
/>

### Splitter.Handle [#splitterhandle]

<TypeTable
  type="{
  disabled: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description: &#x22;Locks the handle: no drag, no key press, no double-click reset. The grip hides.&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the separator button, which is the 1px line.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the separator button.&#x22;,
  },
}"
/>

### Splitter.Toggle [#splittertoggle]

<TypeTable
  type="{
  panel: {
    type: &#x22;string&#x22;,
    required: true,
    description: &#x22;The id of the panel it collapses and expands.&#x22;,
  },
  children: {
    type: &#x22;ReactNode&#x22;,
    description: &#x22;Replaces the chevron. With children, the button takes its name from them, not from translations.&#x22;,
  },
  asChild: {
    type: &#x22;boolean&#x22;,
    description: &#x22;Render as your own button, Button or ActionIcon.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the button.&#x22;,
  },
}"
/>

### useSplitterContext [#usesplittercontext]

`useSplitterContext()` returns the nearest splitter's sizes and the calls that change them. Call it inside a panel.

<TypeTable
  type="{
  size: { type: &#x22;number[]&#x22;, description: &#x22;The current sizes, in percent, in panel order.&#x22; },
  collapsed: { type: &#x22;(panel: string) => boolean&#x22;, description: &#x22;Whether the panel is at its collapsedSize.&#x22; },
  collapse: { type: &#x22;(panel: string) => void&#x22;, description: &#x22;Collapses a collapsible panel.&#x22; },
  expand: { type: &#x22;(panel: string) => void&#x22;, description: &#x22;Expands a collapsed panel, to the size it had before, or to its min.&#x22; },
  toggle: { type: &#x22;(panel: string) => void&#x22;, description: &#x22;Collapses an expanded panel, or expands a collapsed one.&#x22; },
  resize: { type: &#x22;(panel: string, size: number) => void&#x22;, description: &#x22;Sets one panel's size, in percent. Its neighbour gives or takes the difference.&#x22; },
  reset: { type: &#x22;() => void&#x22;, description: &#x22;Puts every panel back to its defaultSize.&#x22; },
}"
/>

## Accessibility [#accessibility]

* **A separator per handle.** Each handle is a focusable `role="separator"` with `aria-orientation`, `aria-valuenow` (the size of the panel before it), `aria-valuemin`, `aria-valuemax` and `aria-controls` naming its two panels. It takes its name from `translations.handle`.

* **Keyboard.**

  | Key                                              | Action                                                                     |
  | ------------------------------------------------ | -------------------------------------------------------------------------- |
  | <kbd>Tab</kbd>                                   | Focuses the next handle (or toggle).                                       |
  | <kbd>←</kbd> <kbd>→</kbd>                        | Moves a vertical line by `step`, horizontal splitters. Mirrored under RTL. |
  | <kbd>↑</kbd> <kbd>↓</kbd>                        | Moves a horizontal line by `step`, vertical splitters.                     |
  | <kbd>Shift</kbd> + arrow                         | Moves by 10.                                                               |
  | <kbd>Home</kbd> / <kbd>End</kbd>                 | Shrinks / grows the panel before the handle as far as its limits allow.    |
  | <kbd>Enter</kbd>                                 | Collapses or expands the panel before the handle, when it's `collapsible`. |
  | <kbd>F6</kbd> / <kbd>Shift</kbd> + <kbd>F6</kbd> | Moves focus to the next / previous handle.                                 |

* **A collapse button.** `Splitter.Toggle` is a real `<button>` with `aria-expanded` and `aria-controls`, for anyone who'd rather not drag to the edge.

* **Pointer.** The line is 1px, but its hit area is 13px wide, so it's easy to grab. The grip darkens on hover and turns brand-coloured while dragging. A double-click resets the sizes.

* **Visible focus.** The kit's focus ring goes around the whole line when it's focused from the keyboard, not when it's clicked.

* **Collapsed panels leave the tab order.** A panel collapsed to 0 is `inert`.

* **Forced colors.** The line and the grip switch to system colors in Windows High Contrast.

* **Right-to-left.** The direction comes from the locale. See [Right-to-left](#right-to-left).

## Internationalization [#internationalization]

`translations` holds the three names the splitter announces. All are optional; a partial override keeps the other English defaults.

| Key        | Default            |                                                        |
| ---------- | ------------------ | ------------------------------------------------------ |
| `handle`   | `"Resize"`         | Each handle's accessible name.                         |
| `collapse` | `"Collapse panel"` | `Splitter.Toggle`'s name while its panel is expanded.  |
| `expand`   | `"Expand panel"`   | `Splitter.Toggle`'s name while its panel is collapsed. |

```tsx
<Splitter translations={{ handle: t('split.resize'), collapse: t('split.collapse'), expand: t('split.expand') }}>
  …
</Splitter>
```
