/** * References: * - https://www.w3.org/WAI/ARIA/apg/patterns/combobox/ * - https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-activedescendant * * A combobox (the input) over a listbox (the results). * * Focus never leaves the input; `aria-activedescendant` is what moves. This is * why there is no tabster mover here: the arrow keys must not move focus, or * the user stops being able to type. Tabster still does the finding. * * Filtering is not this component's job. Render the results you want, in the * order you want. */ import Component from "@glimmer/component"; import { type Signature as LinkSignature } from './link'; import type { TOC } from "@ember/component/template-only"; import type Owner from "@ember/owner"; import type { ModifierLike, WithBoundArgs } from "@glint/template"; export interface ItemSignature { Element: HTMLDivElement; Blocks: { default: []; }; } interface PrivateItemSignature { Element: ItemSignature["Element"]; Args: { activeId: string | undefined; }; Blocks: ItemSignature["Blocks"]; } declare class Item extends Component { id: string; get isActive(): boolean; } export interface LinkItemSignature { Element: HTMLAnchorElement; Args: LinkSignature["Args"]; Blocks: { default: []; }; } interface PrivateLinkItemSignature { Element: LinkItemSignature["Element"]; Args: LinkItemSignature["Args"] & { activeId: string | undefined; }; Blocks: LinkItemSignature["Blocks"]; } /** * An option that is also a link. Enter dispatches a real click on * the anchor, so the router navigates exactly as it would have for a mouse. */ declare class LinkItem extends Component { id: string; get isActive(): boolean; } export interface ListSignature { Element: HTMLDivElement; Blocks: { default: [ { Item: WithBoundArgs; LinkItem: WithBoundArgs; } ]; }; } interface PrivateListSignature { Element: ListSignature["Element"]; Args: { id: string; register: ModifierLike<{ Element: HTMLElement; }>; onPointerMove: (event: PointerEvent) => void; onClick: (event: MouseEvent) => void; item: ListSignature["Blocks"]["default"][0]["Item"]; linkItem: ListSignature["Blocks"]["default"][0]["LinkItem"]; }; Blocks: ListSignature["Blocks"]; } /** * The pointer is handled here rather than on each option, and on * `pointermove` rather than `pointerenter`, so that an option under a resting * cursor re-activates when the cursor moves after the keyboard has activated * something else. Native menus behave this way. * * `:hover` cannot do this job. There is one active option, it is what * Enter chooses, and it is what `aria-activedescendant` reports. * Hovering while the keyboard has a different option active would light two * rows and tell a screen reader about neither, so the pointer sets the same * state the arrow keys do instead of painting its own. */ declare const List: TOC; export interface InputSignature { Element: HTMLInputElement; } interface PrivateInputSignature { Element: InputSignature["Element"]; Args: { listId: string; activeId: string | undefined; query: string; onInput: (event: Event) => void; onKeydown: (event: KeyboardEvent) => void; }; } declare const Input: TOC; /** * One entry in the default layout. A bare string is the label. */ export type PaletteItem = string | { label: string; description?: string; icon?: string; }; /** * The default layout: hand it the rows and it renders the whole palette. */ export interface ItemsSignature { Args: { /** * The text in the input. * * The state is managed internally, so this does not need to be a * maintained value, but whenever it changes, the input reflects it. Pair * it with `@onQueryChange` to keep the query somewhere else, such as a * query param. */ query?: string; /** * Called with the input's text every time it changes. */ onQueryChange?: (query: string) => void; /** * A key combination that calls `@onOpen` from anywhere on the page, such * as `"mod+k"`. `mod` is Meta on macOS and Control * everywhere else. * * Needs `@onOpen` to have anything to do. No listener is installed * without both. */ hotkey?: string; /** * Called when `@hotkey` is pressed. Hand it the `open` of whatever the * palette is in: * * ```hbs * * ``` */ onOpen?: () => void; /** * The entries to render. Each is a string, or an object with a `label` * and optionally a `description` and an `icon`. */ items: PaletteItem[]; /** * Called every time a row is chosen, with the entry that was chosen. * This is where a modal palette closes itself: * * ```hbs * * * * ``` * * Also where a row's own action goes, since the entry it is handed says * which row was chosen. */ onSelect?: (item: PaletteItem, event: Event) => void; /** * The input's placeholder, and its accessible name. * * Defaults to "Search". */ placeholder?: string; /** * What a screen reader announces when the number of results changes. * * Defaults to "1 result" and "3 results". */ resultsText?: (count: number) => string; }; /** * No blocks: this form renders the rows. Passing one is an error. */ Blocks: Record; } /** * The composed form: you render the rows. */ export interface BlockSignature { Args: { /** * The text in the input. * * The state is managed internally, so this does not need to be a * maintained value, but whenever it changes, the input reflects it. Pair * it with `@onQueryChange` to keep the query somewhere else, such as a * query param. */ query?: string; /** * Called with the input's text every time it changes. */ onQueryChange?: (query: string) => void; /** * A key combination that calls `@onOpen` from anywhere on the page, such * as `"mod+k"`. `mod` is Meta on macOS and Control * everywhere else. * * Needs `@onOpen` to have anything to do. No listener is installed * without both. */ hotkey?: string; /** * Called when `@hotkey` is pressed. Hand it the `open` of whatever the * palette is in: * * ```hbs * * ``` */ onOpen?: () => void; /** * Not for this form: the rows come from the block. */ items?: never; /** * Not for this form: set it on `Input` yourself. */ placeholder?: never; /** * Not for this form: the rows are yours, and so is announcing them. */ resultsText?: never; /** * Called every time a row is chosen. Hand it `close` to make a modal * palette close itself. * * The rows are yours here, so there is no entry to hand back. Which row * was chosen is `event.target.closest("[role=option]")`. */ onSelect?: (event: Event) => void; }; Blocks: { default: [ { /** * The current text of the input. */ query: string; /** * Sets the text of the input, for a "clear" button or a suggestion. */ setQuery: (query: string) => void; /** * The ``, wired as a combobox over `List`. * * It needs a name: pass `aria-label`, or an `id` and a `