# Sunburst Tree

`<ixfx-sunbursttree-list>` — a zoomable sunburst over weighted tree data. The zoom node is the centre disc; each ring outward is one generation, and each arc's angle is its share of its parent's value. Every change — data arriving, a different weighting, a zoom — animates from what's on screen to the new layout.

It implements the shared `TreeComponent` interface (see [`src/tree/README.md`](./tree.md)), so `TreeDataModel`, `IncrSearchTreeController`, and `TreeComponent`-typed code work unmodified. Geometrically it is a polar [icicle](./icicle-tree.md), and shares its API shape.

---

## Contents

1. [Differences from `ixfx-tree-list`](#differences-from-ixfx-tree-list)
2. [Data model](#data-model)
3. [Values and weighting](#values-and-weighting)
4. [Progressive data](#progressive-data)
5. [Colour](#colour)
6. [Properties](#properties)
7. [Zoom, selection, navigation](#zoom-selection-navigation)
8. [Events](#events)
9. [Keyboard / pointer interaction](#keyboard--pointer-interaction)
10. [View state](#view-state)
11. [CSS variables and parts](#css-variables-and-parts)

---

## Differences from `ixfx-tree-list`

- **Expand/collapse is reinterpreted as zoom.** `expand` fires on zoom-in, `collapse` on zoom-out.
- **`interactionMode` drops `'checked'`**; **`exclusivity` is accepted but ignored**.
- **`filterPredicate` dims** non-matching arcs instead of hiding them.
- **Reassigning `root` keeps zoom and selection** wherever the same key paths still exist; state whose path has gone stays pending and comes back with it. See [Progressive data](#progressive-data) and [View state](#view-state).
- Drag-and-drop is out of scope.

## Data model

Same `TreeNode` / `TreeItem` shapes as the rest of the tree family, including `root`, `model` and `loadChildren` for lazy loading (children are fetched for every visible arc).

Keys only need to be unique **among siblings**: arcs are identified by their key *path* from the root, so file-system data — where `readme.md` recurs in many folders — works. Lookups that only have a bare key (`revealKey`) match the first node, depth-first, carrying it.

## Values and weighting

Every node — branches included — has a value. A child's arc is its share of its parent's:

```
childAngle = parentAngle × child / max(parent, Σ siblings)
```

- Children summing to **less** than their parent leave an unfilled gap — "unaccounted for" space (unscanned disk, an unknown remainder).
- Children summing to **more** are scaled down to fit.

Give values as a plain number on `item.weight` (the default accessor reads it), or as a function:

```typescript
el.root = {
  item: { key: 'hd', label: 'Disk', weight: 500e9 },
  children: [
    { item: { key: 'apps', label: 'Applications', weight: 120e9 }, children: [] },
    { item: { key: 'users', label: 'Users', weight: 300e9 }, children: [] },
  ],
};

// Swap weighting scheme: no DOM is rebuilt, the chart animates to the new layout.
el.valueAccessor = (item, node) => (item.data as Stats).fileCount;
```

`valueAccessor` is `(item, node) => number | undefined`. When it returns `undefined` (or a negative / non-finite number) the node falls back to the **sum of its loaded children**, or `1` for a leaf or not-yet-loaded node. So unweighted data shows descendant counts, and an accessor can value only leaves and let branches sum:

```typescript
el.valueAccessor = (item, node) => node.children ? undefined : Math.log2(bytes(item) + 1);
```

`valueFormatter` (`(value, node) => string`) formats the value in the centre readout:

```typescript
el.valueFormatter = v => `${(v / 1e9).toFixed(1)} GB`;
```

## Progressive data

Any update re-lays out and animates: new arcs grow from zero width, removed ones shrink away, the rest slide to their new angles. An update arriving mid-animation retargets from wherever things currently are.

Either rebuild the tree immutably and reassign it:

```typescript
el.root = snapshotOfScanSoFar(); // zoom + selection carry over by key path
```

or mutate in place and nudge the element — or let a `TreeDataModel` notify it:

```typescript
folder.children.push(newFile);
el.requestUpdate();
```

## Colour

Hue is set by the ring around the centre: each first-ring arc takes the hue at its angular midpoint (so the ring spans the colour wheel), and every descendant takes a tone of that hue — lighter and less saturated each ring out, with a small per-sibling shift so neighbours stay distinct. Zooming re-anchors hues to the new first ring, cross-fading as it goes. The centre disc mixes towards `--sunbursttree-center-color`.

All of it is computed in CSS from per-arc custom properties, so the palette is themeable:

| Per-arc property | Value |
|---|---|
| `--arc-hue` | Degrees, added to `--sunbursttree-hue-start` |
| `--arc-depth` | Ring index from the centre (`0` = first ring) |
| `--arc-shade` | Sibling-position offset, `-0.5..0.5` |
| `--arc-centre` | `0..1`, how far the arc has become the centre disc |

## Properties

| Property (attribute) | Type | Default | Description |
|---|---|---|---|
| `root` | `TreeNode \| undefined` | `undefined` | Root node of the tree |
| `model` | `TreeDataModel \| undefined` | `undefined` | Shared data model; takes precedence over `loadChildren` |
| `loadChildren` | `LoadChildrenCallback \| undefined` | `undefined` | Callback for lazy child loading |
| `valueAccessor` | `SunburstValueAccessor` | `item => item.weight` | Node value. See [Values and weighting](#values-and-weighting) |
| `valueFormatter` | `SunburstValueFormatter` | locale number | Formats the centre readout's value |
| `interactionMode` (`interaction-mode`) | `'implicit' \| 'standard' \| 'manual'` | `'standard'` | Gesture mapping |
| `selectionFilter` (`selection-filter`) | `'none' \| 'leaf' \| 'branch' \| 'all'` | `'all'` | Which node kinds can be selected |
| `maxDepth` (`max-depth`) | `number` | `5` | Rings drawn around the centre |
| `centerRatio` (`center-ratio`) | `number` | `0.22` | Centre disc radius as a fraction of the overall radius |
| `minArcLength` (`min-arc-length`) | `number` | `1.5` | Arcs whose outer edge is shorter than this (px) aren't drawn — bounds DOM size for big trees |
| `disableLabels` (`disable-labels`) | `boolean` | `false` | Hide in-arc labels (the centre readout remains) |
| `curvedLabels` (`curved-labels`) | `boolean` | `false` | Curve labels along their ring instead of drawing them straight. See below |
| `animationDuration` (`animation-duration`) | `number` | `600` | Transition length in ms; `0` snaps. Respects `prefers-reduced-motion` |
| `filterPredicate` | `TreeFilterPredicate \| undefined` | `undefined` | Non-matching arcs (with no matching descendant) are dimmed |
| `disableKeyboardNavigation` (`disable-keyboard-navigation`) | `boolean` | `false` | Turn off keyboard handling |
| `searchModes` (`search-modes`) | `string \| undefined` | `undefined` | Subset of `navigate select filter` — see [`docs/infra-incremental.md`](./infra-incremental.md) |
| `exclusivity` | `TreeExclusivity` | `'none'` | Accepted for interface compatibility, ignored |
| `selectedNode` / `selectedNodes` | | | Same contract as `ixfx-tree-list` |
| `focusedNode` | `TreeNode \| undefined` (read-only) | `undefined` | Current zoom node; `undefined` when showing the full tree |

In-arc labels run along the ring when the arc is long enough, otherwise radially (truncated to the ring width); both flip to stay upright. With `curved-labels`, ring-following labels bend to the arc (an SVG `<textPath>` along the ring's middle, reversed in the bottom half so it reads upright). Since the whole arc length is usable, long labels are truncated with `…` rather than dropped; arcs too narrow for a few characters still fall back to straight radial text. Arcs too thin for either are unlabelled — hover or keyboard-cursor them and the centre readout shows their label, value and share of the centre.

## Zoom, selection, navigation

```typescript
el.zoomTo(node);   // node becomes the centre
el.zoomUp();       // centre's parent becomes the centre
el.zoomOut();      // back to the full tree

el.select(node); el.deselect(node); el.clearSelection(); el.selectAll(); el.deselectAll();

// Zooms so the target sits in the first ring (its parent at the centre), then selects it.
el.navigateTo(['users', 'me', 'photos']);
el.navigateToNode(n => n.item.key === 'photos');

el.getNodes(); // visible arcs (centre excluded), ring-major
```

## Events

All events **bubble** and are **composed**. `depth` counts from the root's children (`0`).

| Event | Detail | When |
|---|---|---|
| `zoom` | `SunburstZoomDetail` — `{ node: TreeNode \| undefined; depth }` | Zoom node changes (`node` undefined = full tree) |
| `expand` / `collapse` | `{ node; depth }` | Zoomed into a descendant / out of the previous centre |
| `select` | `TreeSelectDetail` | Selection changes |
| `tickle` | `TreeTickleDetail` | Pointer or keyboard cursor enters an arc |
| `activate` | `TreeActivateDetail` | Enter or double-click |
| `item-click` / `item-dblclick` | `{ node; depth }` | Raw pointer clicks on an arc |

## Keyboard / pointer interaction

| Input | Action |
|---|---|
| Click an arc | Select (per `interactionMode`) and zoom in, if it has children |
| Ctrl/⌘-click, Shift-click | Toggle / range-select (`standard` mode), without zooming |
| Click the centre | Zoom up one level |
| `←` / `→` | Previous / next sibling (wraps) |
| `↑` / `↓` | Parent (inward) / first child (outward) |
| `Home` / `End` | First / last visible arc |
| `Enter` | Select, activate and zoom in |
| `Escape` / `Backspace` | Zoom up one level |

## View state

Implements the shared view-state API (`getViewState()` / `setViewState()` / `clearPendingViewState()` / `view-state-change`) — see [`docs/infra-view-state.md`](./infra-view-state.md). `kind` is `sunbursttree-list`.

| Field | Type | Meaning |
|---|---|---|
| `selected` | `string[]` | Selected `item.key`s |
| `zoom` | `string[]` | Key path from the root's children to the centre node; `[]` = full tree |
| `focus` | `string` | Keyboard-cursor `item.key` (recorded only when the cursor was moved by keyboard) |

There is no `expanded` (expansion *is* zoom) and no `scroll`.

```typescript
const stop = bindViewState(el, {
  load: () => JSON.parse(localStorage.getItem(`disk`) ?? `null`),
  save: s => localStorage.setItem(`disk`, JSON.stringify(s)),
});
el.root = await scan(); // zoom + selection apply as their nodes arrive
```

- State for nodes not loaded yet stays pending and applies when they appear — whether the tree is reassigned, grown in place, or lazily loaded. A data change that removes the zoom node or a selected node moves it back to pending (silently), so it returns with its path.
- `zoom` is a path because keys only need to be unique among siblings. While it is pending, the nodes along it are loaded one at a time via `loadChildren` / `model` (set `restoreLoads = false` to only apply it as the host loads them).
- Live selection follows its key path across data changes. A restored `selected` key matches the first node, depth-first, carrying it — so with repeated keys (`readme.md` in many folders) a restored selection can land on a different node.
- Restored selection fires `select`; a restored zoom fires `zoom` (and `expand`). Any explicit selection change drops pending selection; any explicit zoom (`zoomTo()`, clicks, `navigateTo()`…) drops a pending zoom.

| Property | Type | Default | Description |
|---|---|---|---|
| `restoreLoads` | `boolean` | `true` | Load nodes along a pending saved zoom path |

## CSS variables and parts

| Variable | Default | Description |
|---|---|---|
| `--sunbursttree-hue-start` | `250` | Hue at 12 o'clock |
| `--sunbursttree-chroma` | `0.14` | First-ring chroma |
| `--sunbursttree-chroma-step` | `0.015` | Chroma lost per ring outward |
| `--sunbursttree-lightness` | `60%` | First-ring lightness |
| `--sunbursttree-lightness-step` | `5%` | Lightness gained per ring outward |
| `--sunbursttree-shade-step` | `10%` | Lightness spread across siblings |
| `--sunbursttree-center-color` | `var(--surface-4)` | Centre disc fill |
| `--sunbursttree-center-text` | `var(--surface-4-text)` | Centre readout text |
| `--sunbursttree-gap-color` | `var(--surface-2)` | Stroke between arcs — match it to the background |
| `--sunbursttree-gap` | `1px` | Width of that stroke |
| `--sunbursttree-font-size` | `11px` | Label size (px; read for label fitting) |
| `--sunbursttree-label-lightness` | `22%` | Label lightness (hue follows the arc) |

Parts: `arc`, `center`, `label`, `center-readout`.

The host defaults to `aspect-ratio: 1` with a `200px` minimum height; give it a width (or both dimensions) to size it.
