/** * Creates a listbox handle on a dedicated control. * @param config */ declare function createListbox(config: ListboxConfig): ListboxComponent; /** The lean selection popup's public types. */ /** One option of a listbox. */ interface ListboxOption { /** The value the option commits. */ id: string; /** The row's text, set as `textContent` -- never HTML. */ text: string; /** A disabled option is shown and read, but navigation and activation skip it. */ disabled?: boolean; /** Returned to the application with the option; never rendered. */ data?: T; } /** What the `open` and `close` events carry. */ interface ListboxEventDetail { /** The listbox that emitted the event. */ listbox: ListboxComponent; /** The event that caused it, when there was one. */ originalEvent?: Event; } /** What the `change` event carries: the value a user choice committed. */ interface ListboxChangeDetail extends ListboxEventDetail { /** The committed option's id. */ value: string; /** The value before this choice. */ previousValue: string | null; /** The committed option, as a snapshot. */ option: ListboxOption; } /** The listbox's typed events, by name. */ interface ListboxEvents { open: ListboxEventDetail; change: ListboxChangeDetail; close: ListboxEventDetail; } /** The handler each typed event takes, by event name. */ type ListboxEventHandlers = { [K in keyof ListboxEvents]: (detail: ListboxEvents[K]) => void; }; /** Configuration for `createListbox`. */ interface ListboxConfig { /** The dedicated value control. */ control: HTMLDivElement; /** The nonempty accessible name, distinct from the value text. */ ariaLabel: string; /** The options, in display order. */ options: readonly ListboxOption[]; /** The initial selected option's id, or `null` for none. */ value?: string | null; /** The control's text while no option is selected. */ placeholder?: string; /** A disabled control stays in the tab order but cannot open or change. */ disabled?: boolean; /** Event handlers, installed before the first interaction. */ on?: Partial>; } /** The handle `createListbox` returns. */ interface ListboxComponent { /** The popup root (`role="listbox"`), stable until `destroy()`. */ element: HTMLElement; /** Opens the popup, focusing the control. */ open: (event?: Event) => ListboxComponent; /** Closes the popup without committing. */ close: (event?: Event) => ListboxComponent; /** Opens a closed popup, closes an open one. */ toggle: (event?: Event) => ListboxComponent; /** Whether the popup is showing. */ isOpen: () => boolean; /** The committed option's id, or `null`. */ getValue: () => string | null; /** Selects an option by id, or clears the selection with `null`. */ setValue: (value: string | null) => ListboxComponent; /** The options, as snapshots: mutating them cannot bypass rendering. */ getOptions: () => ListboxOption[]; /** Replaces the options, validating all of them before changing anything. */ setOptions: (options: readonly ListboxOption[]) => ListboxComponent; /** Disables or enables the control; disabling closes without committing. */ setDisabled: (disabled: boolean) => ListboxComponent; /** Whether the control is disabled. */ isDisabled: () => boolean; /** Subscribes to an event. */ on: >(type: K, handler: (detail: ListboxEvents[K]) => void) => ListboxComponent; /** Removes the exact subscription. */ off: >(type: K, handler: (detail: ListboxEvents[K]) => void) => ListboxComponent; /** Idempotent and terminal. */ destroy: () => void; } export type { ListboxChangeDetail, ListboxComponent, ListboxConfig, ListboxEventDetail, ListboxEventHandlers, ListboxEvents, ListboxOption }; export { createListbox, createListbox as default };