# Splitter

Panels side by side or stacked, with a gutter between each two that users drag or move with the keyboard to share the space. Use it for master–detail views, editors with a sidebar or a console.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-splitter`, `vig-splitter-panel`
- Source: [libs/ui/src/lib/organisms/splitter/splitter.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/splitter/splitter.ts)

```typescript title="Import"
import { Splitter, SplitterPanel } from '@vigdx/ui/splitter';
```

## Basic

Panel sizes are percentages of the space the panels share, two-way bound with `[(sizes)]`; without them the panels share it equally. `minSize` (percent) is the least a panel can have, also for bound and restored sizes. Panels added or removed later keep the others' shares. Give a splitter a height; a panel scrolls its own overflow. Panels must be the splitter's own content (`@if` and `@for` are fine), not part of another component's template.

```typescript title="splitter-basic.ts"
import { DecimalPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Splitter, SplitterPanel } from '@vigdx/ui/splitter';

@Component({
  selector: 'app-splitter-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [DecimalPipe, Splitter, SplitterPanel],
  template: `
    <vig-splitter
      [(sizes)]="sizes"
      class="h-56 w-full rounded-card border border-border"
      [labels]="{ gutterAriaLabel: 'Resize navigation' }"
    >
      <vig-splitter-panel [minSize]="20">
        <p class="p-4 text-sm">Navigation</p>
      </vig-splitter-panel>
      <vig-splitter-panel [minSize]="40">
        <p class="p-4 text-sm">Content</p>
      </vig-splitter-panel>
    </vig-splitter>
    <p class="mt-2 text-sm text-muted-foreground">
      Sizes: {{ sizes()[0] | number: '1.0-0' }}% / {{ sizes()[1] | number: '1.0-0' }}%
    </p>
  `,
})
export class SplitterBasicDemo {
  readonly sizes = signal<readonly number[]>([30, 70]);
}
```

## Nested and collapsible

A `vertical` splitter inside a panel of a horizontal one. `collapsible` panels collapse with Enter on their gutter or when dragged below half their minimum; Enter again restores the size they had.

```typescript title="splitter-nested.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Splitter, SplitterPanel } from '@vigdx/ui/splitter';

@Component({
  selector: 'app-splitter-nested',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Splitter, SplitterPanel],
  template: `
    <vig-splitter
      [sizes]="[25, 75]"
      class="h-72 w-full rounded-card border border-border"
      [labels]="{ gutterAriaLabel: 'Resize explorer' }"
    >
      <vig-splitter-panel [minSize]="15" collapsible>
        <p class="p-4 text-sm">Explorer (collapsible: Enter on its gutter)</p>
      </vig-splitter-panel>
      <vig-splitter-panel>
        <vig-splitter
          orientation="vertical"
          [sizes]="[65, 35]"
          class="h-full"
          [labels]="{ gutterAriaLabel: 'Resize terminal' }"
        >
          <vig-splitter-panel [minSize]="30">
            <p class="p-4 text-sm">Editor</p>
          </vig-splitter-panel>
          <vig-splitter-panel collapsible [minSize]="15">
            <p class="p-4 text-sm">Terminal</p>
          </vig-splitter-panel>
        </vig-splitter>
      </vig-splitter-panel>
    </vig-splitter>
  `,
})
export class SplitterNestedDemo {}
```

## Remembered sizes

`state` keeps the sizes the user set: a key for `localStorage`, or `{ key, storage }` with `'session'` or any storage object (the same storages as vig-table's `state`). They are restored in the browser after the first render (or once the panels arrived), so server-rendered and hydrated markup match; every storage, custom ones too, keeps them under `vig-splitter:<key>`. `(resized)` reports the sizes once a drag or key press ended.

```typescript title="splitter-persisted.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Splitter, SplitterPanel } from '@vigdx/ui/splitter';

@Component({
  selector: 'app-splitter-persisted',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Splitter, SplitterPanel],
  template: `
    <vig-splitter
      state="docs-splitter-demo"
      class="h-40 w-full rounded-card border border-border"
      [labels]="{ gutterAriaLabel: 'Resize remembered panels' }"
    >
      <vig-splitter-panel [minSize]="20">
        <p class="p-4 text-sm">Resize me, then reload the page.</p>
      </vig-splitter-panel>
      <vig-splitter-panel [minSize]="20">
        <p class="p-4 text-sm">The sizes come back from localStorage.</p>
      </vig-splitter-panel>
    </vig-splitter>
  `,
})
export class SplitterPersistedDemo {}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="splitter"][data-part="root"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

### Splitter `[data-scope="splitter"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `vig-splitter` host; also carries `data-orientation` (`horizontal`, `vertical`). |   |

### SplitterPanel `[data-scope="splitter"]`

| Part | Element | data-state |
| --- | --- | --- |
| `gutter` | The `role="separator"` before every panel but the first. | `dragging` |
| `handle` | The grip drawn in the gutter. |   |
| `panel` | The sized, scrolling panel. | `expanded, collapsed` |

## Accessibility

- Each gutter is a WAI-ARIA APG window splitter: a focusable `role="separator"` whose `aria-valuenow` is the size of the panel before it (`aria-controls`), with `aria-valuemin` and `aria-valuemax` from the minimum sizes. Name gutters by what they resize with `labels.gutterAriaLabel` on the splitter or on the panel before the gutter.
- Arrow keys move a gutter by `step` percent (Left/Right when horizontal, mirrored in RTL; Up/Down when vertical), Shift by five steps, Home and End to the least and greatest size; Enter collapses and restores a collapsible panel next to it.
- Pointer drags use pointer capture; only the gutter blocks touch scrolling (`touch-action: none`). Its hit area is 24px wide, 44px on coarse pointers, and reaches into the panel after it only, so the scrollbar of the panel before it stays grabbable. The gutter's handle is drawn with borders, so it stays visible in forced-colors mode.
- A collapsed panel is hidden (`visibility: hidden`), so its content leaves the tab order and the accessibility tree; focus inside it moves to the gutter next to it.

## API

### Splitter

Selector: `vig-splitter` · Kind: component

Lays out its `vig-splitter-panel`s side by side (`horizontal`, mirrored in RTL) or stacked (`vertical`) with a draggable gutter between each two. Sizes are percentages of the space the panels share, two-way bound with `[(sizes)]`; without them the panels share it equally.

**Example :**

```html
<vig-splitter [(sizes)]="sizes" style="height: 20rem">
  <vig-splitter-panel [minSize]="20">Navigation</vig-splitter-panel>
  <vig-splitter-panel>Content</vig-splitter-panel>
</vig-splitter>
```

Each gutter is an APG window splitter (`role="separator"` with its value the size of the panel before it): arrows move it by `step` (Shift: five steps), Home and End move it to the least and the greatest size, Enter collapses and restores a `collapsible` panel next to it. Pointers drag it (pointer capture; a 24px hit area, 44px on coarse pointers); dragging a collapsible panel below half its `minSize` collapses it. Sizes render from the inputs, identically on the server; a `state` key restores the user's sizes in the browser after the first render.

Panels must be the splitter's own content (or content of it in `` and `` blocks), not elements of another component's template. When panels come and go, the remaining ones keep their share and a new one takes its `minSize` (or an equal share); `minSize` holds for bound, restored and initial sizes too.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `labels` | `Partial<SplitterLabels>` | — | Texts (i18n): the gutters' accessible name; a panel's `labels` win for the gutter after it. |
| `orientation` | `SplitterOrientation` | `'horizontal'` | `horizontal` (default): panels side by side; `vertical`: stacked (give the splitter a height). |
| `sizes` (model) | `readonly number[]` | `[]` | Panel sizes in percent; two-way bindable. Equal shares while it does not match the panels. |
| `state` | `SplitterStateOptions \| undefined` | `undefined` | Keeps the sizes the user set across visits: a storage key (`localStorage`), or `{ key, storage }`. Restored in the browser right after the first render, so server and hydrated markup match. |
| `step` | `number` | `2` | Percent an arrow key moves a gutter (Shift: five times as much). |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `resized` | `SplitterResizeEvent` | A drag or key press moved a gutter, or Enter collapsed or restored a panel. |
| `sizesChange` | `readonly number[]` | Panel sizes in percent; two-way bindable. Equal shares while it does not match the panels. |

### SplitterPanel

Selector: `vig-splitter-panel` · Kind: component

One panel of a `vig-splitter`. Its size comes from the splitter's `sizes`; `minSize` (percent) is the least the user can resize it to and `collapsible` lets Enter on the gutter (or a drag below half the minimum) collapse it to 0. Each panel after the first renders the gutter before it; the panel scrolls its own overflow.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `collapsible` | `boolean` | `false` | Enter on the gutter, or a drag below half `minSize`, collapses the panel to 0. |
| `labels` | `Partial<SplitterPanelLabels>` | — | Texts (i18n): the name of the gutter after this panel, e.g. "Resize navigation". |
| `minSize` | `number` | `0` | Least size in percent while expanded (default 0). |
