# `ixfx-select-horiz`

A horizontally scrolling, snap-to-center single-item picker. Children are ordinary elements you provide — the component doesn't render its own item markup, it just lays them out with scroll-snap, keeps one centered as the current selection, and drives that selection from click, keyboard, and mousewheel.

```html
<ixfx-select-horiz selected-index="1">
  <div>Apple</div>
  <div>Banana</div>
  <div>Cherry</div>
</ixfx-select-horiz>
```

```js
const el = document.querySelector('ixfx-select-horiz');
el.addEventListener('change', ({ detail }) => {
  console.log(detail.selectedIndex, detail.selectedItem, detail.selectedValue);
});
```

Items can be any element type, with any nested markup (icons, spans, whatever) — clicks and hovers resolve up to the top-level child regardless of what's nested inside it.

---

### Properties / attributes

| Attribute / Property | Type | Default | Description |
|---|---|---|---|
| `selected-index` / `selectedIndex` | `number` | `-1` | Index of the selected item among the actual slotted children (internal layout elements are never counted). `-1` means nothing selected. Reflected both ways — set the attribute declaratively or the property programmatically. |
| — / `selectedValue` | `string \| null` | `null` | Get or set selection by matching a child's `value` attribute. |
| — / `selectedItem` | `HTMLElement \| null` | `null` | Read-only. The currently selected child element. |
| — / `items` | `HTMLCollection` | — | Read-only. The slotted children, in order. |
| `lighttouch` | `boolean` | `false` | From the shared wheel-nudge mixin. When present, the mousewheel changes selection just by hovering — no focus required. Without it, the element must be focused first. |

If neither a `selected-index` attribute nor a child `selected` attribute is present, the first item is selected automatically once children are known.

### Declarative selection on a child

Mark any child with a bare `selected` attribute to preselect it:

```html
<ixfx-select-horiz>
  <div>Apple</div>
  <div selected>Banana</div>
  <div>Cherry</div>
</ixfx-select-horiz>
```

A child's `selected` attribute takes priority over `selected-index` if both are present. Once initial selection is resolved, the component keeps `selected` in sync going forward — the attribute always sits on whichever child is currently selected, and `selected-index` on the host tracks the same thing. Both update live as the user clicks, uses the keyboard, or scrolls the wheel.

---

### Events

| Event | Detail type | When fired |
|---|---|---|
| `change` | `SelectHorizChangeDetail` | Selection changes, by any means (click, keyboard, wheel, or setting `selectedIndex`/`selectedValue` programmatically) |

```ts
type SelectHorizChangeDetail = {
  readonly selectedIndex: number;
  readonly selectedItem: HTMLElement | null;
  readonly selectedValue: string | null;
};
```

`change` bubbles and is composed (crosses shadow-DOM boundaries).

---

### Keyboard

Click an item (or Tab to the element) to focus it first — a focus ring appears once focused.

| Key | Action |
|---|---|
| `Arrow Left` | Select the previous item |
| `Arrow Right` | Select the next item |
| `Home` | Select the first item |
| `End` | Select the last item |

### Mouse and wheel

- **Click** an item to select it and focus the component.
- **Hover** highlights an item (the "tickled" state) without changing selection — same visual system used by `ixfx-tree-list` and other list components.
- **Wheel** steps the selection forward/backward, one item per notch or per trackpad gesture (rapid ticks are debounced to a single step). Wheel up/away selects the next item, wheel down/toward selects the previous — the same "up = increase" convention used by every other wheel-enabled ixfx component. Requires focus unless `lighttouch` is set, in which case hovering is enough.

---

### CSS custom properties

| Property | Default | Description |
|---|---|---|
| `--select-horiz-item-width` | `80%` | Flex-basis width of each item |
| `--select-horiz-gap` | `var(--space-s)` | Gap between items |
| `--select-horiz-bg` | `var(--surface-3)` | Host background |
| `--select-horiz-border` | `var(--border)` | Host border |
| `--select-horiz-radius` | `var(--radius-l)` | Host border radius |
| `--select-horiz-spacer-width` | `10%` | Width of the leading/trailing spacer that lets the first/last item scroll to center |
| `--scroll-fade-width` | `30px` | Width of the edge fade and the inner horizontal padding (shared with `scrollFadeStyles`) |

Items are styled with the standard `--item-*` theming tokens (see [`docs/user-theming.md`](./user-theming.md)):

| Property | State |
|---|---|
| `--item-bg`, `--item-border`, `--item-border-radius`, `--item-padding` | Default |
| `--item-bg-tickled`, `--item-border-tickled`, `--item-text-tickled` | Hovered or keyboard-cursored |
| `--item-bg-selected`, `--item-border-selected`, `--item-text-selected` | Selected |
| `--item-bg-selected-tickled` | Selected *and* tickled (also gets a dashed outline) |

### Usage notes

- The selected item is scroll-snapped to the horizontal center, including the first and last items — the leading/trailing spacers exist specifically to make that possible.
- Selection state is exposed as a plain `selected` attribute on the child element, so you can style it directly with light-DOM CSS (`ixfx-select-horiz > [selected] { ... }`) instead of, or alongside, the `--item-*` variables.
- Set a `value` attribute on any child to make it addressable via `selectedValue`.
