# `ixfx-narrowed-text`

A filtered text selection component with inline search. Shows slotted items filtered by a query string as the user types.

```html
<ixfx-narrowed-text selection-mode="single" value="beta">
  <span data-value="alpha">Alpha</span>
  <span data-value="beta">Beta</span>
  <span data-value="gamma">Gamma</span>
</ixfx-narrowed-text>

<ixfx-narrowed-text selection-mode="multiple">
  <span data-value="opt1" data-label="Option One">Option One</span>
  <span data-value="opt2" data-label="Option Two">Option Two</span>
</ixfx-narrowed-text>
```

### Properties / attributes

| Attribute / Property     | Type            | Default     | Description                                    |
|-------------------------|-----------------|-------------|------------------------------------------------|
| `selectionMode`         | `'single' \| 'multiple'` | `'single'` | Number of items that can be selected |
| `requiredSelection`     | `boolean`        | `false`     | Prevent deselecting last item in single mode   |
| `orientation`           | `'horizontal' \| 'vertical'` | `'horizontal'` | Layout direction       |
| `nowrap`               | `boolean`        | `false`     | Prevent items from wrapping                   |
| `separator`            | `string`          | `' '`       | Separator for multi-value string              |
| `width`                | `string \| undefined` | `undefined` | Container width (e.g. `'200px'`)            |
| `height`               | `string \| undefined` | `undefined` | Container height                          |
| `preserveFilter`        | `boolean`        | `false`     | Keep filter query after selection             |
| `value`                | `string`          | `''`        | Selected value(s) as string                  |
| `registry`             | `CommandRegistry<unknown> \| undefined` | — | Read-only. Exposes the element's `@clinth/ui-commands` registry. See *Commands*. |

### CSS parts

| Part       | Description         |
|------------|---------------------|
| `container` | Main container      |
| `items`    | Items container     |
| `item`     | Individual item     |
| `label`    | Item label text     |

### Events

| Event    | Detail                                          | Description                |
|----------|-------------------------------------------------|----------------------------|
| `change` | `{ item, query, selected }`                    | Fired when selection changes. `item` is `undefined` for bulk operations (`selectAll` / `deselectAll`). |

```js
const nt = document.querySelector('ixfx-narrowed-text');
nt.addEventListener('change', e => {
  console.log(e.detail.selected); // array of selected items
});
```

### Commands

The element exposes two commands via the `@clinth/ui-commands` API, accessible through the `registry` property:

| Command id       | Behaviour                                                                 |
|------------------|----------------------------------------------------------------------------|
| `select-all`     | Multi mode: select every currently-filtered item. Single mode: select the first filtered item. No-op if the filter is empty. |
| `deselect-all`   | Clear all selection. No-op if `requiredSelection` is set and only one item remains. |

The element does not bind any keyboard shortcuts on its own. Wire up a key of your choice (e.g. CMD+A) using `@clinth/ui-commands`:

```ts
import { loadKeyBindings, KeyboardManager } from '@clinth/ui-commands';

const keyboard = new KeyboardManager();
loadKeyBindings(keyboard, [
  { key: 'a', modifiers: ['meta'], command: 'select-all', description: 'Select all' },
  { key: 'a', modifiers: ['ctrl'], command: 'select-all', description: 'Select all',
    nonExclusive: true },
]);
keyboard.onKeyEvent(id => el.registry?.invoke(id));
keyboard.attach(document.body);
```

### Methods

```ts
el.selectAll();      // Selects all filtered items
el.deselectAll();    // Clears all selection
el.setContents([     // Replaces all items. Each item can have value (required),
  { value: string,   // label, description, iconName, and selected (boolean)
    label?: string,
    description?: string,
    iconName?: string,
    selected?: boolean
  }
]);
el.deleteItem(value); // Removes item with given data-value. Returns true if found.
```

### Properties of slotted items

Items should have `data-value` attribute. Optional attributes:
- `data-label`: Display label (defaults to textContent)
- `data-description`: Optional description text
- `data-icon-name`: Optional icon name

### CSS custom properties

| Property                                     | Default | Description                          |
|----------------------------------------------|---------|--------------------------------------|
| `--item-padding`                             | `var(--space-xs) var(--space-m)` | Item padding            |
| `--item-border-radius`                       | `var(--radius-s)` | Item border radius             |
| `--item-icon-spacing`                        | `var(--space-xs)` | Gap between icon and label   |
| `--item-height`                              | `28px`  | Minimum item container height        |
| `--item-label-font-style-default`            | `normal` | Default font style                |
| `--item-label-text-decoration-tickled`       | `underline dotted` | Tickled item decoration  |
| `--highlight-background`                     | `var(--accent)` | Query match highlight bg     |
| `--highlight-color`                         | `var(--accent-text)` | Query match highlight color |
## Performance

- **`items` property.** `el.items = [{ value, label, description?, iconName? }, …]` supplies the options as data, with no
  `<span data-value>` element per option (at 100k that's ~100k DOM nodes and an 11 s first render when horizontal). When set it
  wins over any slotted spans; the span API (and `setContents()`) still works unchanged.
- **Progressive rendering.** 100 rows are rendered at first; the next 100 are added as an end-of-list marker scrolls into view
  (an `IntersectionObserver`, so it works in a scrolling container or the page) or as the keyboard cursor approaches the end.
  Every option stays reachable by typing or the arrow keys. The rendered count resets to 100 when the query changes.
- Keyboard and hover cursor moves are O(1); a selection with an empty query re-renders instead of re-running the search; the
  highlighter looks options up through an index instead of scanning the list per row; the search state no longer copies and
  indexes every result per keystroke (`positionsByItem` is built on first use).

Reference, 100k options, Chrome (`pnpm bench:narrowed-text`, needs `pnpm start`):

| | before | after (`items`) | after (spans) |
|---|---|---|---|
| first render, horizontal / vertical | 11.0 s / 1.3 s | 16 ms | 138 ms |
| a keystroke matching most options | seconds, ~100k rows | ~52 ms, 100 rows | ~52 ms |
| arrow key (script + render) | ~24 ms (at 10k) | ~1.6 ms | ~2 ms |
| select an option | ~34 ms (at 20k) | 0.7 ms | 0.8 ms |
| retained heap | 90 MB | ~21 MB | ~19 MB |

