# Incremental search

Type-ahead search shared by the list and tree components. One popover, one
`ReactiveController`, three behaviours ("modes"), triggered by
`@clinth/ui-commands` commands. Built on the fuzzy engine in
`src/incr-search/incr-search.ts` (fzf).

Supported today: `ixfx-grid-list`, `ixfx-vertical-list`, `ixfx-detail-list`,
`ixfx-data-grid`, `ixfx-tree-list`, `ixfx-miller-list`.

---

## The three modes

| Mode | As you type… | Enter | On close |
|---|---|---|---|
| **navigate** | the cursor moves to the best match; ↑/↓ cycle between matches | activates the current match, closes | cursor stays |
| **select** | the selection becomes the set of matching items, live | closes | selection stays |
| **filter** | items that don't match are hidden | closes (keeps the filter with `stickyFilter`) | filter is removed |

All modes highlight the matched characters (CSS Custom Highlights) where the
component supports it.

### Conversions

The popover is interactive. While it's open you can, without retyping:

- **switch mode** — buttons for each other enabled mode, or **Tab** /
  **Shift+Tab** in the input to cycle forward / back (`cycleMode(±1)`). The old
  mode's effect is undone first (e.g. leaving *filter* un-hides), then the new
  mode is applied to the current query. Example: start in *filter*, hit
  **navigate** to stop hiding and just move the cursor.
- **re-open** — running a search command while the popover is open keeps the
  query: it switches to that command's mode and refocuses the input with the
  text selected. `focus()` refocuses without changing mode.
- **Select matches** — a one-shot button (shown in *navigate* / *filter* when
  *select* is enabled) that turns the current match set into the selection while
  staying in the current mode.

### Sticky filter

By default a filter lasts only while the popover is open. Set
`list.search.stickyFilter = true` (or the `stickyFilter` controller option) so
that Enter in *filter* mode closes the popover but keeps the filter. A
"Filtered: …" chip (part `search-kept`) stays in the popover's place, and the
filter re-applies as items change. Clear it with the chip's ×, Escape on the
list, or `search.clearFilter()`. Clicking the chip, or running any search
command, reopens the popover with the kept query. `search.keptFilter` is the
kept query (or `undefined`).

### Which modes are offered

The effective set is **component capability ∩ the `search-modes` attribute**:

- **capability** — most components offer all three. A component with no
  selection concept would omit `select`, etc.
- **`search-modes`** attribute — a space-separated subset of
  `navigate select filter`. Unset ⇒ the component default. Empty (`search-modes=""`)
  ⇒ search disabled: no commands registered, no popover.

```html
<ixfx-grid-list search-modes="navigate filter"></ixfx-grid-list>
```

---

## Commands

Each component owns a `@clinth/ui-commands` `CommandRegistry` (exposed as
`el.registry`) and registers one command per enabled mode:

| id | opens |
|---|---|
| `list.search.navigate` | popover in navigate mode |
| `list.search.select` | popover in select mode |
| `list.search.filter` | popover in filter mode |

There is **no default keybinding** — a consumer binds a key or button:

```ts
el.registry.invoke('list.search.filter');
// or via @clinth/ui-commands' InputManager / a data-command button
```

`ixfx-grouped-item-lister.dispatch('list.search.filter')` fans the command out to
every sub-list.

---

## Adding search to a new component

1. **Adapter.** Implement `ListSearchAdapter<TItem>` (`src/incr-search/list-search-controller.ts`):
   `getItems` / `getLabel` / `getKey`, then `focusItem` (navigate),
   `selectItems` (select), `applyFilter` (filter; `undefined` clears).
   Optional: `activateItem`, the `highlight*` hooks, `container` (an element
   whose child-list mutations should re-`refresh()` the item set).
   - Element-based lists: `createElementListSearchAdapter(this, { itemSelector })`.
   - Tree-based: `createTreeListSearchAdapter(this, { labelSelector })`.

2. **Controller.** One field:

   ```ts
   readonly search = new ListSearchController<TItem>(this, {
     adapter: /* from step 1 */,
   });
   ```

3. **Styles + field.** Add `listSearchStyles` to `static styles` and
   `${this.search.renderField()}` to `render()`, inside a `position: relative`
   ancestor that is **not** the scroll container (so the popover doesn't scroll
   away — put `overflow:auto` on an inner element, `position:relative` on `:host`).

4. **Modes + commands.** Keep a `search-modes` reflected property; on connect and
   whenever it changes:

   ```ts
   this.search.setModes(/* capability ∩ search-modes */);
   this.#unregister?.();
   this.#unregister = registerListSearchCommands(this.registry, this.search);
   ```

   Own the registry (`createCommandRegistry()`), expose it as `registry`,
   unregister on disconnect. Route any `dispatch(commandId, args)` to
   `registry.invoke(commandId, args)`.

5. **Keyboard.** `if (this.search.isOpen) return;` at the top of the host's
   keydown handler so list navigation doesn't fight the field.

---

## Notes

- **Virtualised lists** (`ixfx-grid-list`, `ixfx-data-grid`) — filter genuinely
  drops items from layout/virtualisation. Character highlights are best-effort:
  applied to whatever is in the rendered window (data-grid does no highlighting —
  a match position indexes into the whole-row text, which spans cells).
- **Trees** snapshot the whole node set when the popover opens (`getNodes()`
  returns every node regardless of expansion or an active filter), so refining a
  query can always re-surface a node. `navigate` expands the match's ancestors;
  `filter` prunes via `filterPredicate` but does **not** auto-expand collapsed
  ancestors of a deep match.
- **Highlights** painted inside a host's shadow root are styled by
  `listSearchStyles` (keys `incr-search-highlight`, `grid-list-search`,
  `data-grid-search`; tokens `--search-highlight-bg` / `--search-highlight-color`).
  A host using another key must add its own rule. Text in the light DOM (the
  element lists paint onto the item elements) needs a page-level
  `::highlight(<key>)` rule:
  `::highlight(vertical-list-search) { background: var(--accent); color: var(--accent-text); }`.
- Everything is inert when the effective mode set is empty.

## Deprecated

`createElementIncrSearch` and `IncrSearchTreeController` (the external-`<input>`
filter-only controllers) still work but are superseded by the built-in popover.
