# N-Way Split Layout

A Lit web component that lays out any number of panels along one axis with draggable gutters between them.

---

## `ixfx-split-layout`

### Attributes

| Attribute | Type | Default | Description |
|-----------|------|---------|-------------|
| `direction` | `row` \| `column` | `row` | Split direction. `row` creates side-by-side panels, `column` creates stacked panels. |
| `sizes` | string | `''` | Positional weight shorthand, e.g. `"1 2 1"`, applied to children lacking an explicit `size`. |
| `forget-absent` | boolean | `false` | Don't keep the saved size of children that leave (see [View state](#view-state)). |
| `hideGrip` | boolean (`hide-grip`) | `false` | Hide the gutter grip; gutters stay draggable. |
| `gutterSize` | string | — | Convenience mirror of `--split-gutter-size`. |
| `snap` | string | — | Convenience mirror of `--split-snap`. |

---

## `ixfx-split-panel`

The `ixfx-split-panel` element wraps panel content and provides sizing constraints.

### Attributes

| Attribute | Type | Default | Description |
|-----------|------|---------|-------------|
| `id` | string | generated | Identity for persistence + events. |
| `size` / `weight` | number | `1` | Flex weight. |
| `fixed` | boolean \| length | `false` | `fixed` or `fixed="200px"` → non-flexible px track. |
| `min` | length (`px`/`%`) | `0` | Resolved to px against the group's container. |
| `max` | length | — | |
| `when-small` | `prevent` \| `collapse` \| `close` | `prevent` | Behaviour past `min`. |
| `collapsed` | boolean | `false` | Reflected; toggled by the controller. |
| `snap` | length | `--split-snap` | Per-panel hysteresis override. |

---

## Basic Usage

### Three-Panel Row

```html
<ixfx-split-layout direction="row" sizes="1 2 1" style="width: 100%; height: 400px;">
  <ixfx-split-panel>Left Panel</ixfx-split-panel>
  <ixfx-split-panel>Center Panel</ixfx-split-panel>
  <ixfx-split-panel>Right Panel</ixfx-split-panel>
</ixfx-split-layout>
```

### Column Layout

```html
<ixfx-split-layout direction="column" style="width: 100%; height: 400px;">
  <ixfx-split-panel>Top Panel</ixfx-split-panel>
  <ixfx-split-panel>Bottom Panel</ixfx-split-panel>
</ixfx-split-layout>
```

### With Fixed Panel

```html
<ixfx-split-layout direction="row" style="width: 100%; height: 400px;">
  <ixfx-split-panel fixed="200px">Left (200px)</ixfx-split-panel>
  <ixfx-split-panel>Right (flexible)</ixfx-split-panel>
</ixfx-split-layout>
```

### With Min/Max Constraints

```html
<ixfx-split-layout direction="row" style="width: 100%; height: 400px;">
  <ixfx-split-panel min="100px" max="70%">Left</ixfx-split-panel>
  <ixfx-split-panel min="50px">Right</ixfx-split-panel>
</ixfx-split-layout>
```

---

## When-Small Behaviour

The `when-small` attribute controls what happens when a panel is dragged past its minimum size:

- `prevent` (default) — Stop at `min`. Cascade continues to next panel.
- `collapse` — If pointer travels past threshold, collapse the panel. Drag back to restore.
- `close` — If pointer travels past threshold, show closing affordance. On release, emit cancelable `ixfx-split-close`.

```html
<ixfx-split-panel when-small="collapse">Collapsible Panel</ixfx-split-panel>
```

---

## Nested Splits

Nested `<ixfx-split-layout>` elements create sub-groups:

```html
<ixfx-split-layout direction="row" style="width: 100%; height: 400px;">
  <ixfx-split-layout direction="column" style="width: 100%; height: 100%;">
    <ixfx-split-panel>Editor</ixfx-split-panel>
    <ixfx-split-panel>Terminal</ixfx-split-panel>
  </ixfx-split-layout>
  <ixfx-split-panel>Sidebar</ixfx-split-panel>
</ixfx-split-layout>
```

---

## Events

| Name | Detail | Cancelable | When |
|---|---|---|---|
| `ixfx-split-resize` | `{ sizes: Record<string, number>; groupId: string }` | no | Drag end, `setSizes`, `collapse`/`expand`. |
| `ixfx-split-collapse` | `{ id: string; index: number }` | no | Panel collapsed (drag or API). |
| `ixfx-split-expand` | `{ id: string; index: number }` | no | Panel expanded. |
| `ixfx-split-close` | `{ id: string; index: number }` | **yes** | Before a panel is removed; `preventDefault()` vetoes. |
| `ixfx-split-change` | `{ layout: LayoutSnapshot }` | no | After any size/structure change. For persistence use [view state](#view-state). |
| `view-state-change` | `{ state: SplitLayoutViewState; reason }` | no | Debounced; see [View state](#view-state). |

---

## CSS Custom Properties

| Variable | Default | Description |
|----------|---------|-------------|
| `--split-gutter-size` | `6px` | Width/height of gutters |
| `--split-gutter-color` | `transparent` | Gutter background |
| `--split-gutter-hover-color` | `var(--surface-6)` | Gutter on hover |
| `--split-gutter-active-color` | `var(--accent)` | Gutter while dragging |
| `--split-collapsed-size` | `0px` | Size of collapsed panels |
| `--split-snap` | `24px` | Snap/hysteresis distance |
The gutter grip is the shared `dimpled` handle — theme it with the
`--ixfx-handle-*` variables (see [docs/infra-handle.md](./infra-handle.md)).

---

## Programmatic Control

### Getting/Setting Sizes

```javascript
const split = document.querySelector('ixfx-split-layout');

// Get all panel sizes
console.log(split.getSizes());
// { "p-1": 0.25, "p-2": 0.5, "p-3": 0.25 }

// Set sizes by array (positional)
split.setSizes([1, 2, 1]);

// Set sizes by object (id -> weight)
split.setSizes({ "my-panel": 0.3 });
```

### Collapse/Expand

```javascript
// Collapse a panel
split.collapse('panel-id');

// Expand a panel
split.expand('panel-id');

// Toggle
split.toggle('panel-id');
```

### Close Panel (Cancelable)

```javascript
split.addEventListener('ixfx-split-close', (e) => {
  if (someCondition) {
    e.preventDefault(); // Veto the close
  }
});

split.closePanel('panel-id');
```

### Manual Gutter Drags

```javascript
// Bounding rects of the gutter elements, in gutter index order
split.getGutterRects();

// Start dragging gutter 0 as if the user pressed at (x, y); the drag then
// follows pointer `pointerId` until pointerup. Returns false when the index
// is out of range or the layout isn't ready.
split.beginGutterDrag(0, event.pointerId, event.clientX, event.clientY);
```

`beginGutterDrag` is meant for composite resize affordances — e.g. a corner
handle that starts this drag alongside perpendicular drags on other nested
layouts, resizing several splits with one gesture.

---

## View state

`getViewState()` / `setViewState()` snapshot and restore the size and
collapse of each direct child that has an `id`, as plain JSON (`kind:
'split-layout'`). The component never persists anything itself — see
[docs/infra-view-state.md](./infra-view-state.md) and
`bindViewState()`.

```ts
import { bindViewState } from '@ixfx/components';

bindViewState(split, {
  load: () => JSON.parse(localStorage.getItem(`editor-split`) ?? `null`),
  save: s => localStorage.setItem(`editor-split`, JSON.stringify(s)),
});

split.getViewState();
// { v: 1, kind: 'split-layout',
//   layout: { editor: { weight: 0.7 }, terminal: { weight: 0.3, collapsed: true } } }
```

| Field | Type | Meaning |
|---|---|---|
| `layout` | `Record<id, { weight: number; collapsed?: boolean }>` | `weight` is the child's share (0..1) of the weighted flow; for a collapsed child, the share it expands back to. `collapsed` absent ⇒ expanded. |

- **Keyed by `id`.** Children without an `id` (and nested layouts without
  one) are not saved — generated ids are positional. A nested
  `ixfx-split-layout` with an `id` is saved as one entry (its share in this
  layout); its own children are in *its* view state.
- **Pending.** An entry whose child isn't slotted yet is kept and applied when
  a child with that id appears; a child that leaves goes back to pending, so
  removing and re-adding it restores its size. `getViewState()` includes
  pending entries (pass `includePending: false` to leave them out);
  `clearPendingViewState()` drops them.
- **Partial.** Children without an entry keep their relative size and share
  what the restored children leave.
- **Events.** `view-state-change` fires (debounced) after a gutter drag ends,
  collapse / expand, close, `setSizes()`, and when pending entries are applied
  (`reason: 'restore'`).
- `forget-absent` (boolean attribute) turns off the pending behaviour for
  children that leave — for hosts that drive child sizes themselves (used
  by `ixfx-dock-host`).

A rebuild caused by children being added / removed keeps the live (e.g.
dragged) sizes of children that stayed; a changed `size` / `weight` /
`sizes` / `fixed` / `collapsed` attribute on a direct child or the layout
makes the DOM the source of truth again.
