# Two-Way Split Layout

A Lit web component that lays out two elements within a container with a split bar to adjust their proportions.

---

## `ixfx-twosplit-layout`

### Attributes

| Attribute | Type | Default | Description |
|-----------|------|---------|-------------|
| `layout` | `vertical` \| `horizontal` | `vertical` | Split orientation. `vertical` creates left/right panels, `horizontal` creates top/bottom panels. |
| `sizeA` | `String` | `""` | CSS size for panel A (slot `a`). Can use relative (`50%`) or absolute (`100px`) values. Empty string defaults to `auto`. |
| `sizeB` | `String` | `""` | CSS size for panel B (slot `b`). Same format as `sizeA`. |
| `fixedA` | `Boolean` | `false` | If `true`, `sizeA` is calculated in pixels rather than percentage of whole. |
| `fixedB` | `Boolean` | `false` | If `true`, `sizeB` is calculated in pixels rather than percentage of whole. |
| `collapsible` | `Boolean` | `false` | If `true`, panels can be collapsed by dragging the handle to the edge. The other panel expands to fill the space. |
| `minSizeA` | `String` | `""` | Minimum size for panel A (e.g., `"100px"` or `"20%"`). Empty means no minimum. |
| `maxSizeA` | `String` | `""` | Maximum size for panel A. Empty means no maximum. |
| `minSizeB` | `String` | `""` | Minimum size for panel B. |
| `maxSizeB` | `String` | `""` | Maximum size for panel B. |

### Properties

| Property | Type | Description |
|----------|------|-------------|
| `collapsedA` | `Boolean` (reflects attribute) | `true` if panel A is currently collapsed. |
| `collapsedB` | `Boolean` (reflects attribute) | `true` if panel B is currently collapsed. |

### CSS Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `--handle-size` | `2px` | Width/height of the split bar |
| `--handle-colour` | `transparent` | Colour of the split bar |
| `--handle-hover-colour` | `var(--surface-6)` | Split bar on hover |
| `--handle-active-colour` | `var(--accent)` | Split bar while dragging |

When `--handle-size` exceeds `5px` the split bar shows the shared `dimpled` grip —
theme it with the `--ixfx-handle-*` variables (see
[docs/infra-handle.md](./infra-handle.md)).

### Events

| Event | Detail | Description |
|-------|--------|-------------|
| `ixfx-split-collapsed` | `{ panel: 'A' \| 'B', collapsed: boolean }` | Fired when a panel is collapsed or uncollapsed via drag. |
| `view-state-change` | `{ state: TwoSplitLayoutViewState, reason }` | Debounced, after a drag that changed the sizes and after `setViewState()`. See [View state](#view-state). |

### View state

`getViewState()` / `setViewState()` snapshot and restore both sides as plain
JSON (`kind: 'twosplit-layout'`), keyed by slot name. The component never
persists anything itself — see
[docs/infra-view-state.md](./infra-view-state.md) and `bindViewState()`.

```ts
split.getViewState();
// { v: 1, kind: 'twosplit-layout', layout: { a: { size: '30%' }, b: { size: '70%' } } }
split.setViewState({ layout: { a: { size: '0px', collapsed: true }, b: { size: '100%' } } });
```

| Field | Type | Meaning |
|---|---|---|
| `layout` | `{ a?, b?: { size: string; collapsed?: boolean } }` | `size` as in `sizeA` / `sizeB`; `collapsed` absent ⇒ expanded. A side missing from a restored `layout` is left as it is. |

Both slots always exist, so nothing is ever pending (`clearPendingViewState()`
is a no-op). Note the `layout` *field* is unrelated to the `layout`
(orientation) attribute.

---

## Basic Usage

### Vertical Split (Left/Right)

```html
<ixfx-twosplit-layout layout="vertical" style="width: 100%; height: 400px;">
  <div slot="a" style="background: var(--surface-3);">Left Panel</div>
  <div slot="b" style="background: var(--surface-5);">Right Panel</div>
</ixfx-twosplit-layout>
```

### Horizontal Split (Top/Bottom)

```html
<ixfx-twosplit-layout layout="horizontal" style="width: 100%; height: 400px;">
  <div slot="a" style="background: var(--surface-3);">Top Panel</div>
  <div slot="b" style="background: var(--surface-5);">Bottom Panel</div>
</ixfx-twosplit-layout>
```

### With Fixed Panel

```html
<!-- Left panel fixed at 200px, right panel takes remaining space -->
<ixfx-twosplit-layout fixedA layout="vertical" style="width: 100%; height: 400px;">
  <div slot="a" style="background: var(--surface-3);">Left Panel (200px)</div>
  <div slot="b" style="background: var(--surface-5);">Right Panel (flexible)</div>
</ixfx-twosplit-layout>
```

---

## Collapsible Panels

When `collapsible` is set, dragging a split bar to the edge collapses that panel. The other panel expands to fill the space. Dragging away from the edge uncollapses the panel.

```html
<ixfx-twosplit-layout collapsible layout="horizontal" style="width: 100%; height: 400px;">
  <div slot="a" style="background: var(--surface-3);">Top Panel</div>
  <div slot="b" style="background: var(--surface-5);">Bottom Panel</div>
</ixfx-twosplit-layout>
```

### Collapse Events

```javascript
const split = document.querySelector('ixfx-twosplit-layout[collapsible]');

split.addEventListener('ixfx-split-collapsed', ({ detail }) => {
  console.log(`Panel ${detail.panel} is now ${detail.collapsed ? 'collapsed' : 'uncollapsed'}`);
});
```

---

## Min/Max Size Constraints

Use `minSizeA`, `maxSizeA`, `minSizeB`, and `maxSizeB` to constrain panel sizes:

```html
<ixfx-twosplit-layout
  minSizeA="100px"
  maxSizeA="70%"
  minSizeB="50px"
  layout="horizontal"
  style="width: 100%; height: 400px;">
  <div slot="a" style="background: var(--surface-3);">Top Panel</div>
  <div slot="b" style="background: var(--surface-5);">Bottom Panel</div>
</ixfx-twosplit-layout>
```

**Constraint rules:**
- **Min constraints take precedence**: If both panels' minimums exceed 100% combined, they're scaled proportionally
- **Max constraints are secondary**: If a max constraint conflicts with a min constraint, the max is ignored to satisfy the min
- **The constrained panel gets clamped**: The other panel receives the remaining space

---

## Nested Splits with Two-Way Corner Resize

When a `ixfx-twosplit-layout` is nested inside slot A of an outer split, a corner zone appears at the intersection of the two split bars. This enables simultaneous resize of both splits.

```
┌─────────────────┬────────────┐
│                 │            │
│   ┌─────────────┤            │
│   │   Editor    │            │
│   │             │            │
│   ├─────────────┤  Sidebar   │
│   │  Terminal   │            │
│   │             │            │
│   └─────────────┘            │
└─────────────────┴────────────┘
         ↑
    Corner Zone
    (drag here for two-way resize)
```

### Structure

```html
<ixfx-twosplit-layout layout="vertical" style="width: 100%; height: 400px;">
  <!-- Nested split is a direct child of slot A -->
  <ixfx-twosplit-layout slot="a" layout="horizontal" style="width: 100%; height: 100%;">
    <div slot="a" style="background: var(--surface);">Editor</div>
    <div slot="b" style="background: var(--surface-3);">Terminal</div>
  </ixfx-twosplit-layout>
  <div slot="b" style="background: var(--surface-5);">Sidebar</div>
</ixfx-twosplit-layout>
```

**Note**: For the corner zone to appear, the nested split must be a direct child of slot A (no wrapper elements between the split and slot A).

### Corner Zone Behavior

- **Visual indicator**: A small magenta circle (12px) appears at the intersection point
- **Cursor change**: Shows `nwse-resize` cursor when hovering over the corner zone (for vertical-outer/horizontal-inner splits)
- **Two-way resize**: Dragging the corner zone adjusts both the outer vertical split and the inner horizontal split simultaneously
- **Position tracking**: The corner zone follows the split bars as they move, staying at their intersection

---

## CSS Customisation

### Handle Styling

```css
ixfx-twosplit-layout {
  --handle-size: 4px;
  --handle-colour: var(--surface-4);
  --handle-hover-colour: var(--surface-6);
  --handle-active-colour: var(--accent);
}
```

### Grab Handle

When `--handle-size` is greater than `5px`, a grip appears in the centre of the
split bar — the shared `dimpled` handle (a debossed square grid), oriented along
the split. Theme it with the `--ixfx-handle-*` variables; see
[docs/infra-handle.md](./infra-handle.md).

```css
ixfx-twosplit-layout {
  --ixfx-handle-color: var(--surface-muted-text);
  --ixfx-handle-tile: 4px;
  --ixfx-handle-length: 16px;
  --ixfx-handle-thickness: 8px;
}
```

### Hide Collapsed Panels Completely

```css
ixfx-twosplit-layout[data-collapsed-a] slot[name="a"] {
  display: none;
}
```

---

## Programmatic Control

### Setting Sizes

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

// Set to 30% / 70%
split.sizeA = '30%';
split.sizeB = '70%';

// Set to fixed pixel widths
split.fixedA = true;
split.sizeA = '200px'; // Panel A is 200px
// Panel B automatically gets remaining space

// Collapse a panel programmatically
split.collapsedA = true;
split.collapsedB = false;
```

### Reading State

```javascript
console.log('Panel A size:', split.sizeA);
console.log('Panel B size:', split.sizeB);
console.log('Is A collapsed?', split.collapsedA);
console.log('Is B collapsed?', split.collapsedB);
```

---

## Implementation Notes

- Uses Shadow DOM for style encapsulation
- Slots use `display: block` and `overflow: hidden` by default
- When no size is specified for a panel, it defaults to `auto` (fill remaining space)
- When both sizes are empty, both default to `50%`
- The split bar is `flex-shrink: 0` to prevent it from collapsing
- Nested splits detect their parent through slot assignment and automatically enable corner resize
