# `ixfx-crumb-navigation`

Breadcrumb navigation with lazy-loading child support. Uses the same `TreeNode` / `TreeItem` data model as `ixfx-tree-list`, allowing easy swapping between the two components.

```html
<ixfx-crumb-navigation></ixfx-crumb-navigation>
```

```js
const nav = document.querySelector('ixfx-crumb-navigation');
nav.model = {
  root: {
    item: { key: 'root', label: 'Home' },
    children: [
      { item: { key: 'docs', label: 'Documentation' }, children: [...] },
      { item: { key: 'api', label: 'API' } }
    ]
  },
  selectedNode: { item: { key: 'docs', label: 'Documentation' } }
};
```

### Properties / attributes

| Attribute / Property   | Type                            | Default     | Description                                      |
|-----------------------|---------------------------------|-------------|--------------------------------------------------|
| `root`                | `TreeNode \| undefined`         | `undefined` | Root node of the tree                            |
| `model`               | `TreeDataModel \| undefined`    | `undefined` | Optional reactive tree data model                 |
| `selectedNode`        | `TreeNode \| undefined`        | `undefined` | Currently selected node                           |
| `loadChildren`        | `LoadChildrenCallback \| undefined` | `undefined` | Callback for lazy-loading children            |
| `filterPredicate`     | `TreeFilterPredicate \| undefined` | `undefined` | Filter function for nodes                  |
| `exclusivity`         | `TreeExclusivity`              | `'none'`    | Selection exclusivity mode                       |
| `selectionFilter`     | `TreeSelectionFilter`          | `'leaf'`    | Which nodes can be selected                      |
| `showSuggestions`     | `boolean`                      | `false`     | Whether to show child suggestions                |
| `leadingCaret`        | `boolean`                      | `false`     | Show caret before label instead of after         |
| `caretEmbedded`       | `boolean`                      | `true`      | Embed carets within segments                    |
| `hideRoot`            | `boolean`                      | `false`     | Hide the root node                               |
| `inputMode`           | `string`                       | `''`        | Input mode for styling (`'keyboard'`, etc.)      |
| `padEnding`           | `boolean`                      | `false`     | Pad trailing space to enable left-align scroll   |
| `withUp`              | `boolean`                      | `false`     | Show an Up button to navigate to parent         |
| `restoreLoads`        | `boolean`                      | `true`      | Let a restored location load unloaded branches via `loadChildren` |

### Performance

- **Menus are built when opened.** Each caret's menu (child menu, or sibling menu with `leading-caret`) has no items in the DOM
  until it opens; only the open one has any. It starts with the first 100 items (label order) and adds 100 more each time the
  menu scrolls near its end or the keyboard cursor reaches the last rendered item.
  Opening a menu over 100k children costs ~4 ms instead of ~1.3 s, and a render or hover no longer builds them.
- **The path to the selection is cached** (re-validated in O(depth) against the tree on every use, so in-place edits to
  `children` are picked up), and the search for it is iterative: no recursion limit, no per-node array copies.
- Sorted copies of a node's children are cached per children array, and the `show-suggestions` strip's options are only
  rebuilt when the selected node's children change, not on every hover.

Reference, 100k children under one node, Chrome (`pnpm bench:crumb-navigation`, needs `pnpm start`):

| | before | after |
|---|---|---|
| first render (default / leading) | 1.44 s / 1.50 s | 2.5 ms / 4.7 ms |
| hover | 53–98 ms | ~0.3 ms |
| navigate to another folder | 1.4–1.5 s | ~1.3 ms |
| open a menu | 1.1–1.3 s | ~4 ms |
| retained heap | 170–180 MB | ~13 MB |

`show-suggestions` mode at 100k children: first render ≈ 113 ms (was 1.7 s), heap ≈ 26 MB (was 286 MB), hover ≈ 1 ms (was ≈ 120 ms),
navigation ≈ 14 ms (was 1.75 s). The strip is an `ixfx-narrowed-text` fed through its `items` property (no span element per child),
which renders the first 100 options and the rest as it scrolls.

### Events

| Event         | Detail                                              | Description                          |
|---------------|-----------------------------------------------------|--------------------------------------|
| `select`       | `{ selected: Set<TreeNode>, previous: Set<TreeNode> \| undefined }` | Fired when selection changes |
| `item-click`   | `{ node: TreeNode, depth: number }`                | Fired when a breadcrumb label is clicked |
| `expand`       | `{ node: TreeNode, depth: number }`                | Fired when a caret is clicked for a node with unloaded children |
| `tickle`       | `{ node: TreeNode, depth: number }`                 | Fired when mouse enters a crumb      |
| `activate`     | `TreeActivateEvent`                                 | Fired when a node is activated       |

### Methods

| Method                  | Returns | Description                                     |
|-------------------------|---------|------------------------------------------------|
| `navigateTo(keys)`      | `boolean` | Navigate to a node by key path. Returns false if not found. |
| `navigateToNode(predicate)` | `boolean` | Navigate to the first node matching a predicate. |
| `getViewState(options?)` | `CrumbNavigationViewState` | Snapshot the location. See [View state](#view-state). |
| `setViewState(state)` | `void` | Go to a saved location, now or as its nodes arrive. |
| `clearPendingViewState()` | `void` | Forget a saved location not reached yet. |

### View state

The current location as plain JSON, for the host to persist (see
[docs/infra-view-state.md](./infra-view-state.md)):

```ts
{ v: 1, kind: 'crumb-navigation', path: ['dev', 'workspace'] } // keys below the root; [] = root
```

The location *is* the selection, so there is no separate `selected` field, and
it is a key path rather than a lone key so a deep saved location can be loaded
down to. The keyboard cursor is transient and not recorded.

- Keys not loaded yet stay pending: the crumb moves to the deepest loaded
  ancestor and continues as nodes arrive (new `root`, or children filled in
  followed by `requestUpdate()`). With `restoreLoads` it calls `loadChildren`
  itself, one branch at a time. Each step fires `select`.
- New `root` data keeps the location by key. If it's gone, the crumb steps
  back to the closest ancestor silently and returns when the key reappears.
- Any explicit navigation — a click, `select()`, `navigateTo()`, or the host
  assigning `selectedNode` — drops a pending location. Set the initial
  `selectedNode` *before* `setViewState()` / `bindViewState()`.

### CSS custom properties

Variables use the bridge pattern: internal `--crumb-*` variables fall back to generic `--item-*` variables when set.

| Property | Default | Description |
|----------|---------|-------------|
| `--crumb-item-bg` | `var(--item-bg, transparent)` | Item background |
| `--crumb-item-bg-tickled` | `var(--item-bg-tickled)` | Item background on hover |
| `--crumb-item-text-tickled` | `var(--item-text-tickled)` | Text color on hover |
| `--crumb-item-border` | `var(--item-border)` | Item border |
| `--crumb-item-border-tickled` | `var(--item-border-tickled)` | Border color on hover |
| `--crumb-item-padding` | `var(--item-padding)` | Item padding |
| `--crumb-item-border-radius` | `var(--item-border-radius)` | Item border radius |
| `--crumb-item-gap` | `var(--space-xs)` | Gap between items |
| `--crumb-separator` | `var(--surface-muted-text)` | Separator/caret color |
| `--crumb-truncate-min-width` | `10ch` | Minimum width for truncatable segments |
| `--crumb-fade-width` | `30px` | Width of edge fade effect |
| `--crumb-fade-color` | `var(--surface-3)` | Color of edge fade effect |

### Attributes

| Attribute | Type | Default | Description |
|-----------|------|---------|-------------|
| `no-truncate` | `boolean` | `false` | When present, disables text truncation. Segments maintain full width and overflow is scrolled. |
| `show-suggestions` | `boolean` | `false` | Whether to show child suggestions for the selected node. |
| `leading-caret` | `boolean` | `false` | Show caret before label instead of after. |
| `caret-embedded` | `boolean` | `true` | Embed carets within segments. |
| `hide-root` | `boolean` | `false` | Hide the root node. |
| `exclusivity` | `TreeExclusivity` | `'none'` | Selection exclusivity mode. |
| `selection-filter` | `TreeSelectionFilter` | `'leaf'` | Which nodes can be selected. |
| `input-mode` | `string` | `''` | Input mode for styling (`'keyboard'`, etc.). |
| `pad-ending` | `boolean` | `false` | Pad trailing space so the selected item can scroll to the far left. |
| `with-up` | `boolean` | `false` | Show an Up button to navigate to the parent node. |

### CSS parts

| Part | Description |
|------|-------------|
| `crumb-segment` | Individual crumb segment |
| `caret` | The dropdown caret button |
| `label` | Clickable label text |