# Tickled items

An item is **tickled** when it is under active attention — pointer hovering over it, or keyboard cursor on it. Only one item is tickled at a time — a single logical cursor regardless of input mechanism.

**Tickled state is controlled via the `.tickled` CSS class** (applied by `TickledController`). The `tickledItemStyles` provides base styling that all tickled components should use.

---

## TickledController

`TickledController` is a Lit `ReactiveController` from `src/interaction/index.js` that manages this state for you.

### Direct-render setup (items in your template)

```ts
import { TickledController, tickledStyles, tickledItemStyles } from '../interaction/index.js';

class MyList extends LitElement {
  static override styles = [
    tickledStyles,
    tickledItemStyles,
    css`
      .item.selected { background: var(--item-bg-selected); }
    `
  ];

  private tickled = new TickledController(this, {
    getItems: () => this.items.map((_, i) => ({ id: i })),
  });

  protected override render() {
    return html`
      <div
        data-input-mode=${this.tickled.inputMode}
        @pointerleave=${() => this.tickled.clearTickled()}
        @keydown=${this.#handleKeyDown}
      >
        ${this.items.map((item, i) => html`
          <div
            class="item ${this.tickled.isTickled(i) ? 'tickled' : ''}"
            @pointerenter=${() => this.tickled.handlePointerEnter(i)}
          >${item}</div>
        `)}
      </div>
    `;
  }

  #handleKeyDown(e: KeyboardEvent) {
    if (e.key === 'ArrowDown') { e.preventDefault(); this.tickled.handleKeyboardNavigation('next'); }
    else if (e.key === 'ArrowUp') { e.preventDefault(); this.tickled.handleKeyboardNavigation('previous'); }
    else if (e.key === 'Home') { e.preventDefault(); this.tickled.handleKeyboardNavigation('first'); }
    else if (e.key === 'End') { e.preventDefault(); this.tickled.handleKeyboardNavigation('last'); }
  }
}
```

### Slotted-content setup

Lit's reactive rendering doesn't touch slotted elements, so you must sync DOM classes manually via `onStateChange`.

```ts
private tickled = new TickledController<number>(this, {
  getItems: () => this._slottedItems().map((_, i) => ({ id: i })),
  onStateChange: () => this._syncTickled(),
});

// Must run in willUpdate so :host gets data-input-mode before paint
override willUpdate(changed: Map<PropertyKey, unknown>) {
  super.willUpdate(changed);
  this.setAttribute('data-input-mode', this.tickled.inputMode);
}

private _slottedItems(): HTMLElement[] {
  const slot = this.shadowRoot?.querySelector('slot');
  return Array.from(slot?.assignedElements() ?? []) as HTMLElement[];
}

private _syncTickled() {
  this._slottedItems().forEach((item, i) => {
    const isTickled = this.tickled.isTickled(i);
    if (isTickled && this.tickled.inputMode === 'keyboard') {
      item.classList.add('tickled');
    } else {
      item.classList.remove('tickled');
    }
    item.tabIndex = isTickled ? 0 : -1;
  });
}
```

For slotted content, set `data-input-mode` on `:host` (not on an inner div) so `tickledStyles` CSS variables cascade through the shadow boundary to slotted elements.

---

## CSS variable contract

All `--item-*` state variables are defined in `src/style.css` (the theme). `tickledItemStyles` applies them to `.item` elements — it no longer sets defaults of its own for the colour/state variables.

See [infra-theming.md](./infra-theming.md#item-states) for the full variable listing and their theme values.

The layout variables (`--item-padding`, `--item-border-radius`, `--item-icon-spacing`, `--item-bg`) are also defined in `style.css` with defaults derived from the spacing/radius tokens.

## Base item styles

`tickledItemStyles` provides these base styles:

```css
.item {
  display: inline-flex;
  align-items: center;
  gap: var(--item-icon-spacing);
  padding: var(--item-padding);
  background: var(--item-bg);
  border: var(--item-border);
  border-radius: var(--item-border-radius);
  transition: background var(--transition), border-color var(--transition);
}

.item.tickled {
  background: var(--item-bg-tickled);
  border-color: var(--item-border-tickled);
  color: var(--item-text-tickled);
}

.item.selected {
  background: var(--item-bg-selected);
  border-color: var(--item-border-selected);
  color: var(--item-text-selected);
}

.item.selected.tickled {
  background: var(--item-bg-selected-tickled);
  outline: 2px dashed var(--surface-3);
  outline-offset: -2px;
}
```

---

## Controller API

**Constructor:** `new TickledController<T>(host, config)`

Config:
- `getItems: () => readonly { id: T; enabled?: boolean }[]` — called on each render
- `onStateChange?: () => void` — fired after every state mutation (required for slotted content)
- `onTickle?: (id: T | undefined) => void` — called only when the tickled id *changes* (useful for opening submenus)
- `initialInputMode?: 'keyboard' | 'mouse'`
- `itemWrap?: 'stop' | 'wrap'` — whether keyboard nav wraps past the first/last item (default `stop`)
- `hoverDisabled?: () => boolean` — read on every hover event; when it returns `true`, `handlePointerEnter` / `handlePointerLeave` are no-ops and hover never switches the input mode to `mouse`. Keyboard nav, `setTickled()` / `focus()`, and clicks are unaffected. Wire it to a reactive host property (e.g. `disable-hover-tickle`) so consumers can turn hover cursoring off.

Properties: `inputMode`, `tickledId`

Methods: `isTickled(id)`, `handlePointerEnter(id)`, `handleKeyboardNavigation('next'|'previous'|'first'|'last'|T)`, `clearTickled()`, `setTickled(id)`, `setInputMode(mode)`

---

## Checklist

- [ ] `tickledStyles` and `tickledItemStyles` included in `static styles`
- [ ] Components use `tickledItemStyles` base styles, override only for component-specific needs
- [ ] `data-input-mode` set on the correct element (container div for direct-render; `:host` via `willUpdate` for slotted)
- [ ] `@pointerenter` on each item, `@pointerleave` on container
- [ ] Keyboard navigation calls `handleKeyboardNavigation`
- [ ] Slotted components: `onStateChange` + `_syncTickled()` implemented; `.tickled` class only added in keyboard mode