# `ixfx-ac-text`

VS Code command-palette style autocomplete text input. Shows a popup with matching results as the user types.

```html
<ixfx-ac-text placeholder="Search commands…"></ixfx-ac-text>
```

```js
const input = document.querySelector('ixfx-ac-text');

input.completionProvider = async (query, signal) => {
  const allCommands = await fetchCommands();
  return allCommands
    .filter(c => c.label.toLowerCase().includes(query.toLowerCase()))
    .map(c => ({ label: c.label, icon: c.icon, key: c.id }));
};

input.addEventListener('change', e => {
  console.log(e.detail.value, e.detail.source); // 'Save', 'autocomplete'
});
```

### Properties / attributes

| Attribute / Property | Type                        | Default  | Description                                      |
|---------------------|-----------------------------|----------|--------------------------------------------------|
| `completionProvider`| `AcTextCompletionProvider \| undefined` | `undefined` | Function returning completion results   |
| `placeholder`       | `string`                    | `''`     | Placeholder text                                |
| `value`             | `string`                    | `''`     | Current text value                              |
| `tabSuffix`         | `string`                    | `''`     | Appended when Tab-accepts a non-leaf item        |
| `cacheSize`         | `number`                    | `20`     | LRU cache size for query results                |
| `debounceMs`        | `number`                    | `150`    | Debounce delay before invoking provider          |

### Events

| Event        | Detail                                                              | Description                              |
|--------------|---------------------------------------------------------------------|------------------------------------------|
| `ac-select`  | `{ item: AcTextItem, query: string }`                              | When an autocomplete item is selected    |
| `value`      | `{ value: string }`                                                 | On every edit (keystroke, paste, Tab-accept), committed or not. Read it to keep text left with a blur |
| `change`     | `{ value, key, data?, source: 'text' \| 'autocomplete' }`          | When value is committed (Enter or select)|

### Keyboard navigation

| Key          | Action                                         |
|--------------|------------------------------------------------|
| `ArrowDown`  | Move highlight to next result                 |
| `ArrowUp`    | Move highlight to previous result             |
| `Enter`      | Commit highlighted item or typed text          |
| `Tab`        | Accept highlight + append `tabSuffix` if not leaf |
| `Escape`     | Close popup without committing                 |

### CSS custom properties

| Property                    | Default                              | Description                  |
|-----------------------------|--------------------------------------|------------------------------|
| `--ac-input-padding`        | `var(--space-m) var(--space-l)`       | Input padding                |
| `--ac-input-background`     | `var(--surface-3)`                    | Input background             |
| `--ac-input-border-radius`   | `var(--radius-m)`                    | Input border radius          |
| `--ac-popup-font-size`       | `var(--text-s)`                      | Popup item font size         |
| `--ac-highlight-background`  | `var(--accent)`                      | Text highlight background    |
| `--ac-highlight-color`       | `var(--accent-text)`                 | Text highlight color         |

### Popup rendering

The suggestions popup uses the native [Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API) with CSS Anchor Positioning. This means:

- The popup renders in the browser's **top layer**, so it is **not clipped** by `overflow: hidden` or other scroll containers that the component might be placed inside.
- The popup **flips above** the input when there is not enough room below in the viewport (`position-try-fallbacks: flip-block`).
- Styles are applied via the `popup-surface` class. If you need to override popup styles, target `.popup.popup-surface` or `.popup-surface` in your global CSS.

### Performance

The popup renders the first 50 results and adds 50 more as it scrolls near the end or the keyboard cursor
approaches it, so a query matching all of 100k items costs ~50 rows, not 100k. Every result stays reachable
(`aria-setsize` / `aria-posinset` report the full list). `ArrowUp` with no cursor jumps to the last result
only for lists up to 1,000; longer lists start at the top. Result lists longer than 5,000 aren't kept in the
LRU cache (recomputing is cheaper than retaining them).

`createAcCompletionProvider(items, { limit })` caps how many best-first matches the provider builds and
returns; leave it unset to return every match.

Reference, 100k items in Chrome (`pnpm bench:ac-text`, needs `pnpm start`): a keystroke matching nearly
every item ≈ 50 ms to rendered results, ArrowDown ≈ 1.8 ms; before this, such a query took ~1.5 s, rendered
100k rows and threw `RangeError: Maximum call stack size exceeded` while highlighting. Heap after a
broad query ≈ 46 MB, down from ≈ 125 MB.

### TypeScript types

```ts
import type { AcTextItem, AcTextCompletionProvider, AcTextChangeDetail, AcTextValueDetail } from 'ixfxfun-components';
```