# Selection intents, partial rows, unloaded keys

Shared by every list-like component: `ixfx-vertical-list`, `ixfx-flat-list`, `ixfx-detail-list`, `ixfx-grid-list`, `ixfx-data-grid`, `ixfx-miller-list`, `ixfx-tree-list` and `ixfx-multicol-list`. For a caller that owns the selection itself (e.g. a selection that is an expression or a set of rules, not a key set), so a list shows what it is given and reports what the user did.

---

## `list-select-intent` (cancelable)

Fired **before** a component changes its selection, in every interaction mode (including `manual`, where nothing is selected by default). `preventDefault()` means "I apply this myself": the component changes nothing. Without it, behaviour is unchanged.

```ts
type ListSelectIntentDetail = {
  readonly kind:
    | `replace`   // plain click / Enter: select only `keys`
    | `toggle`    // Cmd/Ctrl-click, Space, sticky tap, checkbox: flip `keys`
    | `extend`    // Shift-click / Shift+Arrow: range from `anchorKey` to `targetKey`
    | `all`       // Cmd/Ctrl+A or selectAll(): "everything in this view", not a key list
    | `marquee`   // drag-select: `keys` covered; `mode` says how they combine with the selection
    | `match`     // incremental search in `select` mode / "Select matches"
    | `clear`;    // Escape / click on empty space / back to "All" in a multicol column
  readonly keys: readonly string[];  // keys directly affected (empty for `all` / `clear`)
  readonly anchorKey?: string;       // `extend`
  readonly targetKey?: string;       // `extend`
  readonly mode?: `replace` | `add` | `toggle`;  // `marquee`
  readonly additive?: boolean;       // `marquee`: `mode` is not `replace`
  readonly query?: string;           // `match`: the search text
  readonly groupKey?: string | null; // `all`: the group the select-all is confined to
  readonly descendants?: boolean;    // `toggle` on a tree branch checkbox: node + loaded descendants
  readonly checked?: boolean;        // ...and the state it is being set to
  readonly columnId?: string;        // multicol-list: the column
  readonly source: `pointer` | `keyboard` | `touch` | `api`;
};
```

```ts
list.addEventListener('list-select-intent', (e) => {
  e.preventDefault();
  const { kind, keys, anchorKey, targetKey } = e.detail;
  // ...compute the new selection from your own model, then:
  list.selectKeys(newKeys);
});
```

- **`extend` gives endpoints.** `keys` also holds the range as loaded here, but rows in between may not be loaded (virtualised / progressive listings): compute the range from your own order using `anchorKey` / `targetKey`.
- **`all` is a meaning, not a list.** Turn it into your own scope rule instead of enumerating keys. In grouped lists with `cross-group-selection` off, select-all is confined to one group (the anchor's group, else the cursor's): `groupKey` names it, so a caller that takes over does the same. It is absent when select-all is not confined.
- **`marquee` `mode`.** `replace`: the selection becomes the covered `keys` (plain drag). `add`: covered keys are added (Shift-drag). `toggle`: each covered key flips, so a Cmd/Ctrl-drag can deselect. `additive` is just `mode !== 'replace'`; use `mode`.
- **`match` is a search.** Incremental search in `select` mode (typing, and `selectMatches()` / the "Select matches" button) reports `{ kind: 'match', query, keys }` — `keys` are the current matches, a convenience; `query` is what to keep as a live rule. Cancelling it leaves the selection untouched while the popover keeps showing the matches. Source is `keyboard` while typing, `pointer` for the button, `api` for `selectMatches()`.
- **Tree branch checkboxes.** In `ixfx-tree-list` `checked` mode, checking a branch is one `toggle` with `descendants: true` and `checked` (the state the branch is being set to); `keys` lists the loaded selectable nodes under it, but express it as a rule.
- **Cursor and anchor keep moving when cancelled.** They are view state, not selection; the next `extend` still has its `anchorKey`.
- **Who fires it.** Gestures (click, Space, Enter, double-click, checkbox, Shift+Arrow, Cmd/Ctrl+A, marquee, Escape, click on empty space) and `selectAll()`. The imperative `select()` / `selectKeys()` / `clearSelection()` family never fires it, so applying your decision through them cannot re-enter your handler.
- **`manual` mode.** The intent is reported and nothing else happens, so the caller never re-implements modifier handling. (`ixfx-data-grid` keeps Space toggling in `manual`, as before.)
- **Not covered.** Right-click auto-select and the drag-origin auto-select of native drag-out still apply directly. `ixfx-miller-list` reports click / checkbox / Shift+Arrow; it has no select-all, and Escape navigates rather than clears.

## `list-select` (consistent)

Every list-like component fires `list-select` with the same key-based detail:

```ts
{ selectedKeys: ReadonlySet<string>, previousKeys: ReadonlySet<string> /* , plus component-specific extras */ }
```

Vertical-list, detail-list and grid-list keep their element-based `selected` / `previous`; data-grid keeps `datagrid-select` (and adds `list-select` with `previous` as keys); miller keeps `select` (and adds `list-select`). One adapter can read any of them through `selectedKeys` / `previousKeys`. Miller now also fires for Cmd-click and Shift-click selection changes.

## `partialKeys`

A row whose resource is only partly selected (a time range of an audio file, a region of an image):

```ts
list.partialKeys = ['k1', 'k2'];   // ReadonlySet<string> getter; replace-on-set
```

- Independent of `selectedKeys`; a key in both counts as selected.
- Light-DOM lists (vertical, detail): `data-partial` on the row; grid-list cell and data-grid row: `data-partial` plus `.partial` (`part="item partial"` on grid-list); miller: `.partial`.
- Accessibility: in `checked` mode the checkbox is indeterminate with `aria-checked="mixed"`; otherwise the row gets `aria-description="Partially selected"` (an author-set `aria-description` is never overwritten).
- Style hooks: `--item-bg-partial`, `--item-border-partial` (`--grid-list-partial-outline` on grid-list).
- `ixfx-flat-list` forwards `partialKeys` to its inner list. In `checked` mode (tree-list and miller-list alike) indeterminate comes from loaded descendants or `partialKeys`, whichever says so.

## Selected keys that aren't loaded yet

`selectKeys()` on vertical-list, flat-list (`flat.list`), detail-list and grid-list keeps keys with no row yet as *pending* and selects them when the row appears (`selectedKeys` lists only present rows). `addSelectedKeys()` does the same; `removeSelectedKeys()` also drops pending keys. Any explicit gesture-driven selection change supersedes pending keys, in single-select only the last unresolved key is kept, and pending keys are capped (2000, oldest dropped). `ixfx-data-grid` already behaved this way. A key that is selected and then leaves the data is reselected if it returns (view-state pending).

## Per-component notes

- **`ixfx-tree-list`** — also fires `list-select` next to `select`. `partialKeys` combines with the checkbox state derived from loaded descendants: indeterminate if either says so; selected wins.
- **`ixfx-multicol-list`** — the intent is re-fired on the host with `columnId`; the "All" row is a `clear`. `partialKeys` applies to every column with a matching item key; `setColumnPartialKeys(columnId, keys)` targets one column.
- **`ixfx-miller-list`** — same contract as the tree: a branch checkbox is one `toggle` with `descendants: true` and `checked`, `keys` = the node and its loaded descendants; `partialKeys` combines with the derived checkbox state (indeterminate if either says so; selected wins).
