/** * Listbox state: the pure selection model behind dap-ds-option-list. * * `reduceListbox(state, action)` returns the next state plus an ordered list * of effects (events to emit, focus/scroll requests, timers). It never touches * the DOM, so every rule — navigation, typeahead, filtering, selection, the * active option — is testable without rendering. * * Behaviour is a faithful transliteration of the option-list element's former * imperative paths (one action per entry point), including its quirks; those * are pinned by the characterization suites and change only deliberately. * * Vocabulary (GLOSSARY.md): the **active option** is the keyboard highlight * (`currentOption`), the **selected value** is `value`, **available options** * are the options left after the **filter**, **typeahead** jumps without * filtering. */ /** What the listbox needs from an option. Option elements satisfy it. */ export interface ListboxOption { value?: any; readonly optionText: string; disabled?: boolean; hasAttribute(name: string): boolean; } export type ListboxSearchMode = 'none' | 'typehead' | 'autocomplete' | 'manual'; export interface ListboxState { value?: string; filterText: string; searchMode: ListboxSearchMode; search: boolean; /** String flag, as on the element: only `'true'` enables value syncing. */ searchOnValueSet: string; disableDelaySet: boolean; focused: boolean; /** Options given through the (internal) `options` property, if any. */ options?: O[]; /** Options found in the slot. */ optionEls: O[]; /** Options left after the filter, in display order. */ availableOptions: O[]; /** Index of the active option in `availableOptions` (undefined until first set). */ selectedIndex: number | undefined; /** The active option (may be outside `availableOptions`). */ currentOption: O | null | undefined; /** The option last marked as selected (drives the option `selected` flag). */ marked: O | null; /** The option carrying the focused flag / tabindex 0, or null when cleared. */ flagged: O | null; /** Typeahead query typed so far. */ query: string; /** * A value the list set itself from a user action, until the element has * synced it. Its attribute/update echoes are skipped so one pick emits one * dds-option-change. Null when there is nothing pending. */ ownValue: { value: unknown; } | null; } /** How an emitted event's detail is built, mirroring the element's calls. */ export type ListboxEmit = { type: 'emit'; name: 'dds-option-change' | 'dds-current-change'; /** `{ ...details(option) }`: undefined option yields `{}`. */ detail: { kind: 'details'; option: O | null | undefined; index: number | undefined; }; } | { type: 'emit'; name: 'dds-current-change'; /** emit(name, null) */ detail: { kind: 'null'; }; } | { type: 'emit'; name: 'dds-keydown'; detail: { kind: 'key'; key: string; code?: string; } | { kind: 'keyDetails'; key: string; code?: string; option: O; index: number | undefined; }; } | { type: 'emit'; name: 'dds-cancel'; detail: { kind: 'none'; }; } | { type: 'emit'; name: 'dds-list-changed'; detail: { kind: 'elements'; elements: O[]; }; } | { type: 'emit'; name: 'dds-option-deselect'; detail: { kind: 'deselect'; option: O; }; }; export type ListboxEffect = ListboxEmit /** Move real focus onto the rendered option for `option`. */ | { type: 'focusOption'; option: O; } /** Scroll `option` (the active option at that moment) into view within the list. */ | { type: 'scrollActive'; option: O; } /** Scroll the active option into view on the next animation frame. */ | { type: 'scrollActiveNextFrame'; } /** Focus the listbox host. */ | { type: 'focusHost'; } | { type: 'preventDefault'; } | { type: 'stopPropagation'; } /** (Re)start the typeahead reset timer; dispatch `typeaheadElapsed` after `ms`. */ | { type: 'scheduleTypeaheadReset'; ms: number; } /** Dispatch `clickSettled` once the element finished its next update. */ | { type: 'afterUpdate'; action: { type: 'clickSettled'; }; }; export type ListboxAction = /** A keydown that reached the list (focus inside it). */ { type: 'keydown'; key: string; code?: string; } /** The list is opened / receives focus, optionally with the key that opened it. */ | { type: 'open'; key?: string; } /** The list is closed. */ | { type: 'close'; } /** A pointer click on a rendered option. */ | { type: 'click'; option: O; /** The clicked option currently shows as selected. */ selected: boolean; /** Ctrl or Cmd was held. */ modifier: boolean; } /** Follow-up of `click` after the element's update settled. */ | { type: 'clickSettled'; } /** `filterText` changed (state already holds the new text). */ | { type: 'filterChanged'; } /** The `value` attribute changed (state already holds the new value). */ | { type: 'valueAttributeChanged'; value: string | null; } /** The `value` property changed in an update in which the active option did not. */ | { type: 'valueUpdated'; previous: unknown; } /** `options` / slotted options changed in an update. */ | { type: 'optionsUpdated'; } /** The slot content changed. */ | { type: 'slotChanged'; options: O[]; } /** The typeahead reset timer elapsed. */ | { type: 'typeaheadElapsed'; } /** Run typeahead for `char` directly (public buildQueryString). */ | { type: 'typeahead'; char: string; }; export interface ListboxResult { state: ListboxState; effects: ListboxEffect[]; /** Fields that changed at any point during the reduction. */ changed: (keyof ListboxState)[]; /** * Which option flags must be re-applied from state: `selected` from * `marked`, `focused` + tabindex from `flagged`. */ flags: { selected: boolean; focus: boolean; }; } export declare const TYPEAHEAD_RESET_MS = 500; export declare function initialListboxState(overrides?: Partial>): ListboxState; export declare const isAutoCompleteMode: (mode: ListboxSearchMode) => mode is "manual" | "autocomplete"; /** Next enabled index from `current` stepping by `step`; `current` if none. */ export declare function findNearestEnabled(options: ListboxOption[], current: number | undefined, step: number): number | undefined; export declare const findIndexByValue: (options: ListboxOption[], value?: unknown) => number; /** Enabled options whose text starts with `filterText` (case-insensitive). */ export declare function filterOptions(options: O[], filterText: string): O[]; export declare function reduceListbox(state: ListboxState, action: ListboxAction): ListboxResult;