@barqjs/aria 0.0.0-stage → 0.2.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
package/src/command.tsx
ADDED
|
@@ -0,0 +1,1184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A command palette: a text input over a list it filters, ranks and drives.
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* <Command label="Commands">
|
|
6
|
+
* <CommandInput placeholder="Type a command" />
|
|
7
|
+
* <CommandList>
|
|
8
|
+
* <CommandEmpty>No results found.</CommandEmpty>
|
|
9
|
+
* <CommandGroup heading="Settings">
|
|
10
|
+
* <CommandItem onSelect={openProfile}>Profile</CommandItem>
|
|
11
|
+
* <CommandItem keywords={["invoice"]} onSelect={openBilling}>Billing</CommandItem>
|
|
12
|
+
* </CommandGroup>
|
|
13
|
+
* </CommandList>
|
|
14
|
+
* </Command>
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The behaviour is `cmdk`'s, which is what shadcn's `Command` wraps. The items
|
|
18
|
+
* are the ones that RENDER (see `static-collection.ts`); this adds a score per
|
|
19
|
+
* item and everything that follows from one.
|
|
20
|
+
*
|
|
21
|
+
* The decisions:
|
|
22
|
+
*
|
|
23
|
+
* - **Focus never leaves the input.** The highlighted item is named by
|
|
24
|
+
* `aria-activedescendant`, so typing and arrowing are the same gesture, and
|
|
25
|
+
* a press on an item does not take the keyboard away from the search.
|
|
26
|
+
* - **An item that does not match stays mounted, with `hidden`.** Its value
|
|
27
|
+
* defaults to its own text, and unmounting it would lose the thing the next
|
|
28
|
+
* keystroke has to score. `hidden` takes it out of the accessibility tree
|
|
29
|
+
* and out of the keyboard's path.
|
|
30
|
+
* - **The ranked order is drawn with `order`, not by moving nodes.** `cmdk`
|
|
31
|
+
* re-appends elements, which a renderer that tracks its own nodes cannot be
|
|
32
|
+
* handed back. The document keeps the order the items were written in, the
|
|
33
|
+
* keyboard follows the order that is drawn, and `aria-posinset` states that
|
|
34
|
+
* order to a screen reader while a search is in effect.
|
|
35
|
+
* - **Clearing the search restores the written order**, where `cmdk` leaves
|
|
36
|
+
* the last ranking in place.
|
|
37
|
+
* - **A highlight is an item, not a string.** Two items with one value are
|
|
38
|
+
* still two rows, and only the one the arrows reached is highlighted.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import {
|
|
42
|
+
type Accessor,
|
|
43
|
+
type Child,
|
|
44
|
+
createContext,
|
|
45
|
+
createEffect,
|
|
46
|
+
createMemo,
|
|
47
|
+
createSignal,
|
|
48
|
+
getContext,
|
|
49
|
+
type Incoming,
|
|
50
|
+
onCleanup,
|
|
51
|
+
onSettled,
|
|
52
|
+
provide,
|
|
53
|
+
Show,
|
|
54
|
+
untrack,
|
|
55
|
+
} from "@barqjs/core";
|
|
56
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
57
|
+
|
|
58
|
+
import type { Key } from "./collections.ts";
|
|
59
|
+
import { scrollIntoView } from "./dom.ts";
|
|
60
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
61
|
+
import { visuallyHidden } from "./live.ts";
|
|
62
|
+
import {
|
|
63
|
+
provideStaticSection,
|
|
64
|
+
staticCollection,
|
|
65
|
+
staticItem,
|
|
66
|
+
type StaticCollection,
|
|
67
|
+
type StaticEntry,
|
|
68
|
+
} from "./static-collection.ts";
|
|
69
|
+
import {
|
|
70
|
+
access,
|
|
71
|
+
callback,
|
|
72
|
+
type DOMProps,
|
|
73
|
+
filterDOMProps,
|
|
74
|
+
fromProps,
|
|
75
|
+
id,
|
|
76
|
+
type MaybeAccessor,
|
|
77
|
+
mergeProps,
|
|
78
|
+
type StyleProps,
|
|
79
|
+
styleProps,
|
|
80
|
+
} from "./utils.ts";
|
|
81
|
+
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
// Scoring
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
const SCORE_CONTINUE_MATCH = 1;
|
|
87
|
+
const SCORE_SPACE_WORD_JUMP = 0.9;
|
|
88
|
+
const SCORE_NON_SPACE_WORD_JUMP = 0.8;
|
|
89
|
+
const SCORE_CHARACTER_JUMP = 0.17;
|
|
90
|
+
const SCORE_TRANSPOSITION = 0.1;
|
|
91
|
+
const PENALTY_SKIPPED = 0.999;
|
|
92
|
+
const PENALTY_CASE_MISMATCH = 0.9999;
|
|
93
|
+
const PENALTY_NOT_COMPLETE = 0.99;
|
|
94
|
+
|
|
95
|
+
const IS_GAP = /[\\/_+.#"@[({&]/;
|
|
96
|
+
const COUNT_GAPS = /[\\/_+.#"@[({&]/g;
|
|
97
|
+
const IS_SPACE = /[\s-]/;
|
|
98
|
+
const COUNT_SPACE = /[\s-]/g;
|
|
99
|
+
const COMBINING_MARKS = /[̀-ͯ]/g;
|
|
100
|
+
|
|
101
|
+
function scoreFrom(
|
|
102
|
+
value: string,
|
|
103
|
+
search: string,
|
|
104
|
+
lowerValue: string,
|
|
105
|
+
lowerSearch: string,
|
|
106
|
+
valueIndex: number,
|
|
107
|
+
searchIndex: number,
|
|
108
|
+
memo: Map<number, number>,
|
|
109
|
+
): number {
|
|
110
|
+
if (searchIndex === search.length) {
|
|
111
|
+
return valueIndex === value.length ? SCORE_CONTINUE_MATCH : PENALTY_NOT_COMPLETE;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const memoKey = valueIndex * (search.length + 1) + searchIndex;
|
|
115
|
+
const known = memo.get(memoKey);
|
|
116
|
+
if (known !== undefined) return known;
|
|
117
|
+
|
|
118
|
+
const wanted = lowerSearch.charAt(searchIndex);
|
|
119
|
+
let index = lowerValue.indexOf(wanted, valueIndex);
|
|
120
|
+
let best = 0;
|
|
121
|
+
|
|
122
|
+
while (index >= 0) {
|
|
123
|
+
let score = scoreFrom(value, search, lowerValue, lowerSearch, index + 1, searchIndex + 1, memo);
|
|
124
|
+
|
|
125
|
+
if (score > best) {
|
|
126
|
+
if (index === valueIndex) {
|
|
127
|
+
score *= SCORE_CONTINUE_MATCH;
|
|
128
|
+
} else if (IS_GAP.test(value.charAt(index - 1))) {
|
|
129
|
+
score *= SCORE_NON_SPACE_WORD_JUMP;
|
|
130
|
+
const skipped = value.slice(valueIndex, index - 1).match(COUNT_GAPS);
|
|
131
|
+
if (skipped !== null && valueIndex > 0) score *= PENALTY_SKIPPED ** skipped.length;
|
|
132
|
+
} else if (IS_SPACE.test(value.charAt(index - 1))) {
|
|
133
|
+
score *= SCORE_SPACE_WORD_JUMP;
|
|
134
|
+
const skipped = value.slice(valueIndex, index - 1).match(COUNT_SPACE);
|
|
135
|
+
if (skipped !== null && valueIndex > 0) score *= PENALTY_SKIPPED ** skipped.length;
|
|
136
|
+
} else {
|
|
137
|
+
score *= SCORE_CHARACTER_JUMP;
|
|
138
|
+
if (valueIndex > 0) score *= PENALTY_SKIPPED ** (index - valueIndex);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (value.charAt(index) !== search.charAt(searchIndex)) score *= PENALTY_CASE_MISMATCH;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const next = lowerSearch.charAt(searchIndex + 1);
|
|
145
|
+
const swapped = score < SCORE_TRANSPOSITION && lowerValue.charAt(index - 1) === next;
|
|
146
|
+
// A doubled letter in the search is not a swap of itself.
|
|
147
|
+
const doubled = next === wanted && lowerValue.charAt(index - 1) !== wanted;
|
|
148
|
+
if (swapped || doubled) {
|
|
149
|
+
const transposed = scoreFrom(
|
|
150
|
+
value,
|
|
151
|
+
search,
|
|
152
|
+
lowerValue,
|
|
153
|
+
lowerSearch,
|
|
154
|
+
index + 1,
|
|
155
|
+
searchIndex + 2,
|
|
156
|
+
memo,
|
|
157
|
+
);
|
|
158
|
+
if (transposed * SCORE_TRANSPOSITION > score) score = transposed * SCORE_TRANSPOSITION;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (score > best) best = score;
|
|
162
|
+
index = lowerValue.indexOf(wanted, index + 1);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
memo.set(memoKey, best);
|
|
166
|
+
return best;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Every kind of space and the hyphen are one separator, so they match each other. */
|
|
170
|
+
function comparable(text: string): string {
|
|
171
|
+
return text.toLowerCase().replace(COUNT_SPACE, " ");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Without its accents, so a plain letter finds an accented one. */
|
|
175
|
+
function unaccented(text: string): string {
|
|
176
|
+
return text.normalize("NFD").replace(COMBINING_MARKS, "");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* How well `search` abbreviates `value`: 0 for not at all, 1 for exactly.
|
|
181
|
+
*
|
|
182
|
+
* `cmdk`'s `command-score`. The letters of the search must appear in the value
|
|
183
|
+
* in order; what varies is how they were reached. Letters in a row are worth
|
|
184
|
+
* the most, then a jump to the start of a word after a space, then one after
|
|
185
|
+
* punctuation, then a jump into the middle of a word, which costs more the
|
|
186
|
+
* further it skips. A differing case and an unfinished value each cost a
|
|
187
|
+
* sliver, and two adjacent letters may be swapped at a tenth of the score.
|
|
188
|
+
*
|
|
189
|
+
* `keywords` are searched as if they were part of the value.
|
|
190
|
+
*
|
|
191
|
+
* The one addition is that accents are ignored on both sides.
|
|
192
|
+
*/
|
|
193
|
+
export function commandScore(
|
|
194
|
+
value: string,
|
|
195
|
+
search: string,
|
|
196
|
+
keywords: readonly string[] = [],
|
|
197
|
+
): number {
|
|
198
|
+
const text = unaccented(keywords.length > 0 ? `${value} ${keywords.join(" ")}` : value);
|
|
199
|
+
const term = unaccented(search);
|
|
200
|
+
return scoreFrom(text, term, comparable(text), comparable(term), 0, 0, new Map());
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// State
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
/** How well an item matches: 0 hides it, and a higher number is drawn earlier. */
|
|
208
|
+
export type CommandFilter = (value: string, search: string, keywords: string[]) => number;
|
|
209
|
+
|
|
210
|
+
export interface CommandStateOptions {
|
|
211
|
+
/** The highlighted item's value. */
|
|
212
|
+
value?: MaybeAccessor<string | undefined>;
|
|
213
|
+
defaultValue?: MaybeAccessor<string | undefined>;
|
|
214
|
+
onValueChange?: (value: string) => void;
|
|
215
|
+
/** @default {@link commandScore} */
|
|
216
|
+
filter?: MaybeAccessor<CommandFilter | undefined>;
|
|
217
|
+
/** Hide and rank by the search. Turn off to filter the items yourself. @default true */
|
|
218
|
+
shouldFilter?: MaybeAccessor<boolean | undefined>;
|
|
219
|
+
/** Wrap from the last item to the first and back. @default false */
|
|
220
|
+
loop?: MaybeAccessor<boolean | undefined>;
|
|
221
|
+
/** Leave the highlight to the keyboard. @default false */
|
|
222
|
+
disablePointerSelection?: MaybeAccessor<boolean | undefined>;
|
|
223
|
+
/** Ctrl+J/N for down and Ctrl+K/P for up. @default true */
|
|
224
|
+
vimBindings?: MaybeAccessor<boolean | undefined>;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** One rendered item, as the ranking sees it. */
|
|
228
|
+
export interface CommandRow {
|
|
229
|
+
key: Key;
|
|
230
|
+
value: string;
|
|
231
|
+
keywords: string[];
|
|
232
|
+
isDisabled: boolean;
|
|
233
|
+
/** The group it was written in, if any. */
|
|
234
|
+
group: Key | undefined;
|
|
235
|
+
score: number;
|
|
236
|
+
/** Drawn: it matches, or nothing is being filtered, or it insists. */
|
|
237
|
+
isRendered: boolean;
|
|
238
|
+
onSelect: ((value: string) => void) | undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
interface Ranking {
|
|
242
|
+
rows: Map<Key, CommandRow>;
|
|
243
|
+
/** Whether each group is drawn. */
|
|
244
|
+
groups: Map<Key, boolean>;
|
|
245
|
+
/** Where each item and group sits among its siblings, while filtering. */
|
|
246
|
+
order: Map<Key, number>;
|
|
247
|
+
/** The drawn items, in the order they are drawn. */
|
|
248
|
+
visible: Key[];
|
|
249
|
+
/** The drawn groups, in the order they are drawn. */
|
|
250
|
+
visibleGroups: Key[];
|
|
251
|
+
/** How many items match. */
|
|
252
|
+
count: number;
|
|
253
|
+
isFiltering: boolean;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface CommandState {
|
|
257
|
+
/** Where the items and groups register themselves. */
|
|
258
|
+
rows: StaticCollection;
|
|
259
|
+
search: Accessor<string>;
|
|
260
|
+
setSearch: (search: string) => void;
|
|
261
|
+
/**
|
|
262
|
+
* Let something else own the search text: the input, when it is controlled.
|
|
263
|
+
* `null` hands it back.
|
|
264
|
+
*/
|
|
265
|
+
controlSearch: (search: Accessor<string | undefined> | null) => void;
|
|
266
|
+
/** The highlighted item's value, or `""` when nothing is highlighted. */
|
|
267
|
+
value: Accessor<string>;
|
|
268
|
+
setValue: (value: string) => void;
|
|
269
|
+
highlightedKey: Accessor<Key | null>;
|
|
270
|
+
highlight: (key: Key | null) => void;
|
|
271
|
+
/** How many items match the search. */
|
|
272
|
+
count: Accessor<number>;
|
|
273
|
+
/**
|
|
274
|
+
* Nothing matches, and the items have had their chance to register.
|
|
275
|
+
*
|
|
276
|
+
* Not `count() === 0`: an empty state written above the items is built
|
|
277
|
+
* before any of them exists, and on a server it would stay in the markup of
|
|
278
|
+
* a palette that is full.
|
|
279
|
+
*/
|
|
280
|
+
isEmpty: Accessor<boolean>;
|
|
281
|
+
isFiltering: Accessor<boolean>;
|
|
282
|
+
/** The drawn items, in the order they are drawn. */
|
|
283
|
+
visible: Accessor<Key[]>;
|
|
284
|
+
row: (key: Key) => CommandRow | null;
|
|
285
|
+
isRendered: (key: Key) => boolean;
|
|
286
|
+
isGroupRendered: (key: Key) => boolean;
|
|
287
|
+
/** The `order` an item or a group is drawn at, while a search is in effect. */
|
|
288
|
+
orderOf: (key: Key) => number | undefined;
|
|
289
|
+
/**
|
|
290
|
+
* One item along, skipping the disabled, wrapping if `loop`.
|
|
291
|
+
*
|
|
292
|
+
* Each move answers with the item it asked for, or `null` when there was
|
|
293
|
+
* nowhere to go: the highlight itself is not readable until the write has
|
|
294
|
+
* been flushed, and a caller scrolling to it cannot wait for that.
|
|
295
|
+
*/
|
|
296
|
+
move: (by: 1 | -1) => Key | null;
|
|
297
|
+
/** To the first item of the next or previous group that has one. */
|
|
298
|
+
moveToGroup: (by: 1 | -1) => Key | null;
|
|
299
|
+
moveToEdge: (edge: "first" | "last") => Key | null;
|
|
300
|
+
/** Run an item's `onSelect`. The highlighted one when no key is given. */
|
|
301
|
+
run: (key?: Key) => void;
|
|
302
|
+
disablePointerSelection: Accessor<boolean>;
|
|
303
|
+
vimBindings: Accessor<boolean>;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
interface RowData {
|
|
307
|
+
keywords?: string[];
|
|
308
|
+
isDisabled?: boolean;
|
|
309
|
+
forceMount?: boolean;
|
|
310
|
+
onSelect?: (value: string) => void;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function commandState(options: CommandStateOptions = {}): CommandState {
|
|
314
|
+
const rows = staticCollection();
|
|
315
|
+
|
|
316
|
+
const [typed, setTyped] = createSignal("", { ownedWrite: true });
|
|
317
|
+
const [control, setControl] = createSignal<Accessor<string | undefined> | null>(null, {
|
|
318
|
+
ownedWrite: true,
|
|
319
|
+
});
|
|
320
|
+
const search = createMemo((): string => control()?.() ?? typed());
|
|
321
|
+
|
|
322
|
+
const ranking = createMemo((): Ranking => {
|
|
323
|
+
const term = search();
|
|
324
|
+
const isFiltering = access(options.shouldFilter) !== false && term !== "";
|
|
325
|
+
const score = callback(options.filter) ?? commandScore;
|
|
326
|
+
|
|
327
|
+
const rowOf = (entry: StaticEntry, group: Key | undefined): CommandRow => {
|
|
328
|
+
const data = entry.props as RowData;
|
|
329
|
+
const value = entry.textValue.trim();
|
|
330
|
+
const keywords = data.keywords ?? [];
|
|
331
|
+
const matched = isFiltering ? score(value, term, keywords) : 1;
|
|
332
|
+
return {
|
|
333
|
+
key: entry.key,
|
|
334
|
+
value,
|
|
335
|
+
keywords,
|
|
336
|
+
isDisabled: data.isDisabled === true,
|
|
337
|
+
group,
|
|
338
|
+
score: matched,
|
|
339
|
+
isRendered: !isFiltering || matched > 0 || data.forceMount === true,
|
|
340
|
+
onSelect: data.onSelect,
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const byScore = (a: CommandRow, b: CommandRow): number => b.score - a.score;
|
|
345
|
+
const all = new Map<Key, CommandRow>();
|
|
346
|
+
const groups = new Map<Key, boolean>();
|
|
347
|
+
const order = new Map<Key, number>();
|
|
348
|
+
const visible: Key[] = [];
|
|
349
|
+
const visibleGroups: Key[] = [];
|
|
350
|
+
const loose: CommandRow[] = [];
|
|
351
|
+
const sections: { key: Key; rows: CommandRow[]; best: number; isRendered: boolean }[] = [];
|
|
352
|
+
|
|
353
|
+
for (const entry of rows.entries()) {
|
|
354
|
+
if (entry.type === "item") {
|
|
355
|
+
const row = rowOf(entry, undefined);
|
|
356
|
+
all.set(row.key, row);
|
|
357
|
+
loose.push(row);
|
|
358
|
+
if (!isFiltering) visible.push(row.key);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const members = (entry.children ?? []).map((child) => rowOf(child, entry.key));
|
|
363
|
+
for (const row of members) all.set(row.key, row);
|
|
364
|
+
const best = members.reduce((highest, row) => Math.max(highest, row.score), 0);
|
|
365
|
+
const isRendered =
|
|
366
|
+
!isFiltering ||
|
|
367
|
+
(entry.props as RowData).forceMount === true ||
|
|
368
|
+
members.some((row) => row.score > 0);
|
|
369
|
+
groups.set(entry.key, isRendered);
|
|
370
|
+
sections.push({ key: entry.key, rows: members, best, isRendered });
|
|
371
|
+
if (!isFiltering) {
|
|
372
|
+
visibleGroups.push(entry.key);
|
|
373
|
+
for (const row of members) visible.push(row.key);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (isFiltering) {
|
|
378
|
+
// The loose items first and the groups after them, each best match first:
|
|
379
|
+
// the order `cmdk` leaves the document in after re-appending both.
|
|
380
|
+
const ranked = loose.toSorted(byScore);
|
|
381
|
+
for (const [index, row] of ranked.entries()) {
|
|
382
|
+
order.set(row.key, index + 1);
|
|
383
|
+
if (row.isRendered) visible.push(row.key);
|
|
384
|
+
}
|
|
385
|
+
const rankedSections = sections.toSorted((a, b) => b.best - a.best);
|
|
386
|
+
for (const [index, section] of rankedSections.entries()) {
|
|
387
|
+
order.set(section.key, ranked.length + index + 1);
|
|
388
|
+
if (section.isRendered) visibleGroups.push(section.key);
|
|
389
|
+
for (const [position, row] of section.rows.toSorted(byScore).entries()) {
|
|
390
|
+
order.set(row.key, position + 1);
|
|
391
|
+
if (section.isRendered && row.isRendered) visible.push(row.key);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
let count = 0;
|
|
397
|
+
for (const row of all.values()) if (!isFiltering || row.score > 0) count++;
|
|
398
|
+
|
|
399
|
+
return { rows: all, groups, order, visible, visibleGroups, count, isFiltering };
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
// Uncontrolled, the highlight is the item the arrows reached; a value is the
|
|
403
|
+
// way in from outside, for a `defaultValue` and for `setValue`.
|
|
404
|
+
const [picked, setPicked] = createSignal<{ key: Key | null; value: string | undefined }>(
|
|
405
|
+
{ key: null, value: untrack(() => access(options.defaultValue)) },
|
|
406
|
+
{ ownedWrite: true },
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const keyFor = (value: string, ranked: Ranking): Key | null =>
|
|
410
|
+
ranked.visible.find((key) => ranked.rows.get(key)?.value === value) ?? null;
|
|
411
|
+
|
|
412
|
+
const highlightedKey = createMemo((): Key | null => {
|
|
413
|
+
const ranked = ranking();
|
|
414
|
+
const controlled = access(options.value);
|
|
415
|
+
if (controlled !== undefined) return keyFor(controlled, ranked);
|
|
416
|
+
const current = picked();
|
|
417
|
+
if (current.key !== null) return ranked.visible.includes(current.key) ? current.key : null;
|
|
418
|
+
return current.value === undefined ? null : keyFor(current.value, ranked);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const valueOf = (key: Key | null): string =>
|
|
422
|
+
key === null ? "" : (ranking().rows.get(key)?.value ?? "");
|
|
423
|
+
const value = (): string => valueOf(highlightedKey());
|
|
424
|
+
|
|
425
|
+
const highlight = (key: Key | null): void => {
|
|
426
|
+
const previous = untrack(value);
|
|
427
|
+
const next = untrack(() => valueOf(key));
|
|
428
|
+
setPicked({ key, value: undefined });
|
|
429
|
+
if (next !== previous) options.onValueChange?.(next);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const selectable = (): Key[] => {
|
|
433
|
+
const ranked = ranking();
|
|
434
|
+
return ranked.visible.filter((key) => ranked.rows.get(key)?.isDisabled !== true);
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// The first item is highlighted whenever there is nothing better to keep: on
|
|
438
|
+
// arrival, after every change to the search, and when the highlighted item
|
|
439
|
+
// goes away. An effect, because it WRITES the highlight it reads.
|
|
440
|
+
let searched = untrack(search);
|
|
441
|
+
createEffect(
|
|
442
|
+
() => ({ term: search(), key: highlightedKey(), first: selectable()[0] ?? null }),
|
|
443
|
+
(now) => {
|
|
444
|
+
const changed = now.term !== searched;
|
|
445
|
+
searched = now.term;
|
|
446
|
+
if (!changed && now.key !== null) return;
|
|
447
|
+
if (now.first === now.key) return;
|
|
448
|
+
highlight(now.first);
|
|
449
|
+
},
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const go = (key: Key | undefined): Key | null => {
|
|
453
|
+
if (key === undefined) return null;
|
|
454
|
+
highlight(key);
|
|
455
|
+
return key;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const [isSettled, setSettled] = createSignal(false, { ownedWrite: true });
|
|
459
|
+
onSettled(() => {
|
|
460
|
+
setSettled(true);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const move = (by: 1 | -1): Key | null => {
|
|
464
|
+
const keys = untrack(selectable);
|
|
465
|
+
if (keys.length === 0) return null;
|
|
466
|
+
const current = untrack(highlightedKey);
|
|
467
|
+
const index = current === null ? -1 : keys.indexOf(current);
|
|
468
|
+
let next = index + by;
|
|
469
|
+
if (access(options.loop) === true) {
|
|
470
|
+
next = next < 0 ? keys.length - 1 : next >= keys.length ? 0 : next;
|
|
471
|
+
}
|
|
472
|
+
return go(keys[next]);
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const moveToGroup = (by: 1 | -1): Key | null => {
|
|
476
|
+
const ranked = untrack(ranking);
|
|
477
|
+
const keys = untrack(selectable);
|
|
478
|
+
const current = untrack(highlightedKey);
|
|
479
|
+
const group = current === null ? undefined : ranked.rows.get(current)?.group;
|
|
480
|
+
let at = group === undefined ? -1 : ranked.visibleGroups.indexOf(group);
|
|
481
|
+
// An item outside every group has no group to step from, so the jump is an
|
|
482
|
+
// ordinary step.
|
|
483
|
+
if (at < 0) return move(by);
|
|
484
|
+
|
|
485
|
+
for (at += by; at >= 0 && at < ranked.visibleGroups.length; at += by) {
|
|
486
|
+
const first = keys.find((key) => ranked.rows.get(key)?.group === ranked.visibleGroups[at]);
|
|
487
|
+
if (first !== undefined) return go(first);
|
|
488
|
+
}
|
|
489
|
+
return move(by);
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const moveToEdge = (edge: "first" | "last"): Key | null => {
|
|
493
|
+
const keys = untrack(selectable);
|
|
494
|
+
return go(edge === "first" ? keys[0] : keys.at(-1));
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
const run = (key?: Key): void => {
|
|
498
|
+
const target = key ?? untrack(highlightedKey);
|
|
499
|
+
if (target === null) return;
|
|
500
|
+
const row = untrack(ranking).rows.get(target);
|
|
501
|
+
if (row === undefined || row.isDisabled) return;
|
|
502
|
+
row.onSelect?.(row.value);
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
return {
|
|
506
|
+
rows,
|
|
507
|
+
search,
|
|
508
|
+
setSearch: setTyped,
|
|
509
|
+
controlSearch: (owner) => {
|
|
510
|
+
setControl(() => owner);
|
|
511
|
+
},
|
|
512
|
+
value,
|
|
513
|
+
setValue: (next) => {
|
|
514
|
+
const previous = untrack(value);
|
|
515
|
+
setPicked({ key: null, value: next });
|
|
516
|
+
if (next !== previous) options.onValueChange?.(next);
|
|
517
|
+
},
|
|
518
|
+
highlightedKey,
|
|
519
|
+
highlight,
|
|
520
|
+
count: () => ranking().count,
|
|
521
|
+
isEmpty: () => isSettled() && ranking().count === 0,
|
|
522
|
+
isFiltering: () => ranking().isFiltering,
|
|
523
|
+
visible: () => ranking().visible,
|
|
524
|
+
row: (key) => ranking().rows.get(key) ?? null,
|
|
525
|
+
// A row the ranking has not seen yet is one that has only just rendered.
|
|
526
|
+
isRendered: (key) => ranking().rows.get(key)?.isRendered ?? true,
|
|
527
|
+
isGroupRendered: (key) => ranking().groups.get(key) ?? true,
|
|
528
|
+
orderOf: (key) => ranking().order.get(key),
|
|
529
|
+
move,
|
|
530
|
+
moveToGroup,
|
|
531
|
+
moveToEdge,
|
|
532
|
+
run,
|
|
533
|
+
disablePointerSelection: () => access(options.disablePointerSelection) === true,
|
|
534
|
+
vimBindings: () => access(options.vimBindings) !== false,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// ---------------------------------------------------------------------------
|
|
539
|
+
// Hooks
|
|
540
|
+
// ---------------------------------------------------------------------------
|
|
541
|
+
|
|
542
|
+
export interface CommandOptions {
|
|
543
|
+
/** Names the input and the list. */
|
|
544
|
+
label?: MaybeAccessor<string | undefined>;
|
|
545
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
546
|
+
/** The scroller, which is the list. */
|
|
547
|
+
listRef: ElementRef;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export interface CommandResult {
|
|
551
|
+
commandProps: DOMProps;
|
|
552
|
+
/** For a visually hidden `<label>`, rendered only when there is a label. */
|
|
553
|
+
labelProps: DOMProps;
|
|
554
|
+
/** Options for {@link commandInput}, not props for an element. */
|
|
555
|
+
inputProps: DOMProps;
|
|
556
|
+
listProps: DOMProps;
|
|
557
|
+
label: Accessor<string | undefined>;
|
|
558
|
+
/** The id an item with this key renders under. */
|
|
559
|
+
itemId: (key: Key) => string;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function isComposing(event: KeyboardEvent): boolean {
|
|
563
|
+
// 229 is what a keydown carries while an input method is composing in the
|
|
564
|
+
// browsers that do not set `isComposing` on it.
|
|
565
|
+
return event.isComposing || event.keyCode === 229;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function command(options: CommandOptions, state: CommandState): CommandResult {
|
|
569
|
+
const inputId = id();
|
|
570
|
+
const listId = id();
|
|
571
|
+
const labelId = id();
|
|
572
|
+
const baseId = id();
|
|
573
|
+
const label = (): string | undefined => access(options.label) ?? access(options["aria-label"]);
|
|
574
|
+
const itemId = (key: Key): string => `${baseId()}-item-${String(key)}`;
|
|
575
|
+
const activeId = (): string | undefined => {
|
|
576
|
+
const key = state.highlightedKey();
|
|
577
|
+
return key === null ? undefined : itemId(key);
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
/** Scroll to an item, and to its group's heading when it is the group's first. */
|
|
581
|
+
const reveal = (key: Key | null): void => {
|
|
582
|
+
const list = access(options.listRef) as HTMLElement | null;
|
|
583
|
+
if (list === null || key === null) return;
|
|
584
|
+
const element = state.rows.elementOf(key) as HTMLElement | null;
|
|
585
|
+
if (element === null) return;
|
|
586
|
+
|
|
587
|
+
const group = state.row(key)?.group;
|
|
588
|
+
const firstOfGroup = state.visible().find((other) => state.row(other)?.group === group);
|
|
589
|
+
if (group !== undefined && firstOfGroup === key) {
|
|
590
|
+
const heading = state.rows.elementOf(group)?.querySelector("[cmdk-group-heading]");
|
|
591
|
+
if (heading instanceof HTMLElement) scrollIntoView(list, heading);
|
|
592
|
+
}
|
|
593
|
+
scrollIntoView(list, element);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
const step = (event: KeyboardEvent, by: 1 | -1): void => {
|
|
597
|
+
event.preventDefault();
|
|
598
|
+
if (event.metaKey) reveal(state.moveToEdge(by === 1 ? "last" : "first"));
|
|
599
|
+
else if (event.altKey) reveal(state.moveToGroup(by));
|
|
600
|
+
else reveal(state.move(by));
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
604
|
+
if (event.defaultPrevented || isComposing(event)) return;
|
|
605
|
+
|
|
606
|
+
switch (event.key) {
|
|
607
|
+
case "n":
|
|
608
|
+
case "j":
|
|
609
|
+
if (state.vimBindings() && event.ctrlKey) step(event, 1);
|
|
610
|
+
break;
|
|
611
|
+
case "ArrowDown":
|
|
612
|
+
step(event, 1);
|
|
613
|
+
break;
|
|
614
|
+
case "p":
|
|
615
|
+
case "k":
|
|
616
|
+
if (state.vimBindings() && event.ctrlKey) step(event, -1);
|
|
617
|
+
break;
|
|
618
|
+
case "ArrowUp":
|
|
619
|
+
step(event, -1);
|
|
620
|
+
break;
|
|
621
|
+
case "Home":
|
|
622
|
+
event.preventDefault();
|
|
623
|
+
reveal(state.moveToEdge("first"));
|
|
624
|
+
break;
|
|
625
|
+
case "End":
|
|
626
|
+
event.preventDefault();
|
|
627
|
+
reveal(state.moveToEdge("last"));
|
|
628
|
+
break;
|
|
629
|
+
case "Enter":
|
|
630
|
+
event.preventDefault();
|
|
631
|
+
state.run();
|
|
632
|
+
break;
|
|
633
|
+
default:
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// A new search starts again from the top, where its best match is.
|
|
639
|
+
createEffect(state.search, (now, previous) => {
|
|
640
|
+
if (previous === undefined || previous === now) return;
|
|
641
|
+
const list = untrack(() => access(options.listRef)) as HTMLElement | null;
|
|
642
|
+
if (list !== null) list.scrollTop = 0;
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
return {
|
|
646
|
+
label,
|
|
647
|
+
itemId,
|
|
648
|
+
commandProps: { "cmdk-root": "", tabIndex: -1, onKeyDown },
|
|
649
|
+
labelProps: {
|
|
650
|
+
"cmdk-label": "",
|
|
651
|
+
id: labelId,
|
|
652
|
+
for: inputId,
|
|
653
|
+
...visuallyHidden().visuallyHiddenProps,
|
|
654
|
+
},
|
|
655
|
+
inputProps: {
|
|
656
|
+
id: inputId,
|
|
657
|
+
"aria-controls": listId,
|
|
658
|
+
"aria-labelledby": () => (access(options.label) === undefined ? undefined : labelId()),
|
|
659
|
+
"aria-label": () =>
|
|
660
|
+
access(options.label) === undefined ? access(options["aria-label"]) : undefined,
|
|
661
|
+
"aria-activedescendant": activeId,
|
|
662
|
+
},
|
|
663
|
+
listProps: {
|
|
664
|
+
"cmdk-list": "",
|
|
665
|
+
id: listId,
|
|
666
|
+
role: "listbox",
|
|
667
|
+
tabIndex: -1,
|
|
668
|
+
"aria-label": () => label() ?? "Suggestions",
|
|
669
|
+
"aria-activedescendant": activeId,
|
|
670
|
+
},
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export interface CommandInputOptions {
|
|
675
|
+
/** The search text, when the caller owns it. */
|
|
676
|
+
value?: MaybeAccessor<string | undefined>;
|
|
677
|
+
onValueChange?: (search: string) => void;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** The search box: a combobox whose popup is the list, which is always open. */
|
|
681
|
+
export function commandInput(
|
|
682
|
+
options: CommandInputOptions,
|
|
683
|
+
state: CommandState,
|
|
684
|
+
shared: DOMProps,
|
|
685
|
+
): { inputProps: DOMProps } {
|
|
686
|
+
const isControlled = (): boolean => access(options.value) !== undefined;
|
|
687
|
+
state.controlSearch(() => access(options.value));
|
|
688
|
+
onCleanup(() => state.controlSearch(null));
|
|
689
|
+
|
|
690
|
+
const onInput = (event: Event): void => {
|
|
691
|
+
const input = event.target as HTMLInputElement;
|
|
692
|
+
if (!isControlled()) state.setSearch(input.value);
|
|
693
|
+
options.onValueChange?.(input.value);
|
|
694
|
+
// The field has already changed itself. When the caller owns the text and
|
|
695
|
+
// declined, nothing re-renders on a value that did not move, so the DOM
|
|
696
|
+
// has to be put back by hand.
|
|
697
|
+
if (isControlled() && input.value !== state.search()) input.value = state.search();
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
return {
|
|
701
|
+
inputProps: mergeProps(shared, {
|
|
702
|
+
"cmdk-input": "",
|
|
703
|
+
type: "text",
|
|
704
|
+
role: "combobox",
|
|
705
|
+
autocomplete: "off",
|
|
706
|
+
autocorrect: "off",
|
|
707
|
+
spellcheck: false,
|
|
708
|
+
"aria-autocomplete": "list",
|
|
709
|
+
"aria-expanded": true,
|
|
710
|
+
value: state.search,
|
|
711
|
+
onInput,
|
|
712
|
+
}),
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
export interface CommandItemOptions {
|
|
717
|
+
/** What the item is searched and reported by. @default its own text */
|
|
718
|
+
value?: MaybeAccessor<string | undefined>;
|
|
719
|
+
/** More words to find it by, which are not shown. */
|
|
720
|
+
keywords?: MaybeAccessor<string[] | undefined>;
|
|
721
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
722
|
+
/** Draw it whether or not it matches. @default its group's */
|
|
723
|
+
forceMount?: MaybeAccessor<boolean | undefined>;
|
|
724
|
+
onSelect?: (value: string) => void;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export interface CommandItemResult {
|
|
728
|
+
itemProps: DOMProps;
|
|
729
|
+
key: Key;
|
|
730
|
+
/** Goes on the item's element. */
|
|
731
|
+
ref: (element: Element | null) => void;
|
|
732
|
+
isHighlighted: Accessor<boolean>;
|
|
733
|
+
isDisabled: Accessor<boolean>;
|
|
734
|
+
isRendered: Accessor<boolean>;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
interface CommandGroupValue {
|
|
738
|
+
forceMount: Accessor<boolean>;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const CommandGroupContext = createContext<CommandGroupValue | null>(null);
|
|
742
|
+
|
|
743
|
+
export function commandItem(
|
|
744
|
+
options: CommandItemOptions,
|
|
745
|
+
state: CommandState,
|
|
746
|
+
itemId: (key: Key) => string,
|
|
747
|
+
): CommandItemResult {
|
|
748
|
+
const group = getContext(CommandGroupContext) ?? null;
|
|
749
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
750
|
+
|
|
751
|
+
const row = staticItem(state.rows, {
|
|
752
|
+
textValue: () => access(options.value),
|
|
753
|
+
props: () => ({
|
|
754
|
+
keywords: access(options.keywords),
|
|
755
|
+
isDisabled: isDisabled(),
|
|
756
|
+
forceMount: access(options.forceMount) ?? group?.forceMount() ?? false,
|
|
757
|
+
onSelect: options.onSelect,
|
|
758
|
+
}),
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
const isHighlighted = (): boolean => state.highlightedKey() === row.key;
|
|
762
|
+
const isRendered = (): boolean => state.isRendered(row.key);
|
|
763
|
+
const position = (): number => state.visible().indexOf(row.key);
|
|
764
|
+
|
|
765
|
+
return {
|
|
766
|
+
key: row.key,
|
|
767
|
+
ref: row.ref,
|
|
768
|
+
isHighlighted,
|
|
769
|
+
isDisabled,
|
|
770
|
+
isRendered,
|
|
771
|
+
itemProps: {
|
|
772
|
+
"cmdk-item": "",
|
|
773
|
+
id: () => itemId(row.key),
|
|
774
|
+
role: "option",
|
|
775
|
+
"aria-selected": isHighlighted,
|
|
776
|
+
"aria-disabled": () => isDisabled() || undefined,
|
|
777
|
+
// The document keeps the written order, so the drawn one is stated.
|
|
778
|
+
"aria-posinset": () => (state.isFiltering() && position() >= 0 ? position() + 1 : undefined),
|
|
779
|
+
"aria-setsize": () =>
|
|
780
|
+
state.isFiltering() && position() >= 0 ? state.visible().length : undefined,
|
|
781
|
+
"data-value": () => state.row(row.key)?.value,
|
|
782
|
+
"data-selected": () => (isHighlighted() ? "true" : undefined),
|
|
783
|
+
"data-disabled": () => (isDisabled() ? "true" : undefined),
|
|
784
|
+
hidden: () => !isRendered() || undefined,
|
|
785
|
+
style: () => {
|
|
786
|
+
const order = state.orderOf(row.key);
|
|
787
|
+
return order === undefined ? undefined : { order };
|
|
788
|
+
},
|
|
789
|
+
// A press must not take the focus out of the input.
|
|
790
|
+
onMouseDown: (event: MouseEvent) => event.preventDefault(),
|
|
791
|
+
onPointerMove: () => {
|
|
792
|
+
if (isDisabled() || state.disablePointerSelection() || isHighlighted()) return;
|
|
793
|
+
state.highlight(row.key);
|
|
794
|
+
},
|
|
795
|
+
onClick: () => {
|
|
796
|
+
if (isDisabled()) return;
|
|
797
|
+
state.highlight(row.key);
|
|
798
|
+
state.run(row.key);
|
|
799
|
+
},
|
|
800
|
+
},
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
export interface CommandGroupOptions {
|
|
805
|
+
heading?: MaybeAccessor<unknown>;
|
|
806
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
807
|
+
/** Draw the group and everything in it whatever the search says. */
|
|
808
|
+
forceMount?: MaybeAccessor<boolean | undefined>;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
export interface CommandGroupResult {
|
|
812
|
+
/** The wrapper: presentational, and `hidden` when nothing in it matches. */
|
|
813
|
+
groupProps: DOMProps;
|
|
814
|
+
headingProps: DOMProps;
|
|
815
|
+
/** The `role="group"` element holding the items. */
|
|
816
|
+
itemsProps: DOMProps;
|
|
817
|
+
key: Key;
|
|
818
|
+
ref: (element: Element | null) => void;
|
|
819
|
+
/** Build the group's children inside it. */
|
|
820
|
+
provide: (children: () => unknown) => unknown;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
const COLUMN = { display: "flex", flexDirection: "column" } as const;
|
|
824
|
+
|
|
825
|
+
export function commandGroup(
|
|
826
|
+
options: CommandGroupOptions,
|
|
827
|
+
state: CommandState,
|
|
828
|
+
): CommandGroupResult {
|
|
829
|
+
const headingId = id();
|
|
830
|
+
const forceMount = (): boolean => access(options.forceMount) === true;
|
|
831
|
+
const hasHeading = (): boolean => {
|
|
832
|
+
const heading = access(options.heading);
|
|
833
|
+
return heading !== undefined && heading !== null && heading !== "";
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
const section = staticItem(state.rows, {
|
|
837
|
+
type: "section",
|
|
838
|
+
textValue: "",
|
|
839
|
+
props: () => ({ forceMount: forceMount() }),
|
|
840
|
+
});
|
|
841
|
+
const value: CommandGroupValue = { forceMount };
|
|
842
|
+
|
|
843
|
+
return {
|
|
844
|
+
key: section.key,
|
|
845
|
+
ref: section.ref,
|
|
846
|
+
provide: (children) =>
|
|
847
|
+
provideStaticSection(section.key, () => provide(CommandGroupContext, () => value, children)),
|
|
848
|
+
groupProps: {
|
|
849
|
+
"cmdk-group": "",
|
|
850
|
+
role: "presentation",
|
|
851
|
+
hidden: () => !state.isGroupRendered(section.key) || undefined,
|
|
852
|
+
style: () => {
|
|
853
|
+
const order = state.orderOf(section.key);
|
|
854
|
+
return order === undefined ? undefined : { order };
|
|
855
|
+
},
|
|
856
|
+
},
|
|
857
|
+
headingProps: {
|
|
858
|
+
"cmdk-group-heading": "",
|
|
859
|
+
id: headingId,
|
|
860
|
+
// The heading names the group; it is not one of its children.
|
|
861
|
+
"aria-hidden": true,
|
|
862
|
+
},
|
|
863
|
+
itemsProps: {
|
|
864
|
+
"cmdk-group-items": "",
|
|
865
|
+
role: "group",
|
|
866
|
+
"aria-label": () => access(options["aria-label"]),
|
|
867
|
+
"aria-labelledby": () => (hasHeading() ? headingId() : undefined),
|
|
868
|
+
// `order` is how a ranking is drawn, and it only means something to a
|
|
869
|
+
// flex or grid child.
|
|
870
|
+
style: COLUMN,
|
|
871
|
+
},
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// ---------------------------------------------------------------------------
|
|
876
|
+
// Components
|
|
877
|
+
// ---------------------------------------------------------------------------
|
|
878
|
+
|
|
879
|
+
export interface CommandContextValue {
|
|
880
|
+
state: CommandState;
|
|
881
|
+
inputProps: DOMProps;
|
|
882
|
+
listProps: DOMProps;
|
|
883
|
+
listRef: ReturnType<typeof makeRef<HTMLDivElement>>;
|
|
884
|
+
itemId: (key: Key) => string;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const CommandContext = createContext<CommandContextValue | null>(null);
|
|
888
|
+
|
|
889
|
+
/** The enclosing {@link Command}, or `null` outside one. */
|
|
890
|
+
export function useCommandContext(): CommandContextValue | null {
|
|
891
|
+
return getContext(CommandContext) ?? null;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
export function useCommand(): CommandContextValue {
|
|
895
|
+
const value = useCommandContext();
|
|
896
|
+
if (value === null) throw new Error("This must be rendered inside a Command.");
|
|
897
|
+
return value;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function CommandProvider(props: Incoming<{ value: CommandContextValue; children?: Child }>) {
|
|
901
|
+
return provide(
|
|
902
|
+
CommandContext,
|
|
903
|
+
() => props.value(),
|
|
904
|
+
() => props.children as unknown,
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
export interface CommandComponentProps extends StyleProps {
|
|
909
|
+
children?: Child;
|
|
910
|
+
/** Names the input and the list. Rendered, and visually hidden. */
|
|
911
|
+
label?: string;
|
|
912
|
+
"aria-label"?: string;
|
|
913
|
+
/** The highlighted item's value. */
|
|
914
|
+
value?: string;
|
|
915
|
+
defaultValue?: string;
|
|
916
|
+
filter?: CommandFilter;
|
|
917
|
+
/** @default true */
|
|
918
|
+
shouldFilter?: boolean;
|
|
919
|
+
/** @default false */
|
|
920
|
+
loop?: boolean;
|
|
921
|
+
/** @default false */
|
|
922
|
+
disablePointerSelection?: boolean;
|
|
923
|
+
/** @default true */
|
|
924
|
+
vimBindings?: boolean;
|
|
925
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
926
|
+
onValueChange?: (value: string) => void;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
export function Command(props: Incoming<CommandComponentProps>) {
|
|
930
|
+
const listRef = makeRef<HTMLDivElement>();
|
|
931
|
+
const options = fromProps(props);
|
|
932
|
+
const state = commandState(options);
|
|
933
|
+
const { commandProps, labelProps, inputProps, listProps, label, itemId } = command(
|
|
934
|
+
{ label: options.label, "aria-label": options["aria-label"], listRef },
|
|
935
|
+
state,
|
|
936
|
+
);
|
|
937
|
+
|
|
938
|
+
const value: CommandContextValue = { state, inputProps, listProps, listRef, itemId };
|
|
939
|
+
const elementProps = mergeProps(
|
|
940
|
+
commandProps,
|
|
941
|
+
filterDOMProps(options, { global: true }),
|
|
942
|
+
styleProps(props),
|
|
943
|
+
{ "data-testid": () => props["data-testid"]?.() },
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
return (
|
|
947
|
+
<div {...elementProps} ref={mergeRefs(props.ref?.())}>
|
|
948
|
+
<Show when={props.label?.() !== undefined}>
|
|
949
|
+
<label {...labelProps}>{label()}</label>
|
|
950
|
+
</Show>
|
|
951
|
+
<CommandProvider value={value}>{props.children}</CommandProvider>
|
|
952
|
+
</div>
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
export interface CommandInputComponentProps extends StyleProps {
|
|
957
|
+
/** The search text, when the caller owns it. */
|
|
958
|
+
value?: string;
|
|
959
|
+
placeholder?: string;
|
|
960
|
+
disabled?: boolean;
|
|
961
|
+
autoFocus?: boolean;
|
|
962
|
+
"aria-label"?: string;
|
|
963
|
+
ref?: RefTarget<HTMLInputElement>;
|
|
964
|
+
onValueChange?: (search: string) => void;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
export function CommandInput(props: Incoming<CommandInputComponentProps>) {
|
|
968
|
+
const held = useCommand();
|
|
969
|
+
const options = fromProps(props);
|
|
970
|
+
const { inputProps } = commandInput(options, held.state, held.inputProps);
|
|
971
|
+
|
|
972
|
+
const elementProps = mergeProps(
|
|
973
|
+
inputProps,
|
|
974
|
+
filterDOMProps(options, { global: true, propNames: INPUT_PROPS }),
|
|
975
|
+
styleProps(props),
|
|
976
|
+
{
|
|
977
|
+
"aria-label": () =>
|
|
978
|
+
props["aria-label"]?.() ??
|
|
979
|
+
access(held.inputProps["aria-label"] as MaybeAccessor<string | undefined>),
|
|
980
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
981
|
+
},
|
|
982
|
+
);
|
|
983
|
+
|
|
984
|
+
return <input {...elementProps} ref={mergeRefs(props.ref?.())} />;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
const INPUT_PROPS = new Set(["placeholder", "disabled", "autoFocus", "onKeyDown", "onKeyUp"]);
|
|
988
|
+
|
|
989
|
+
export interface CommandListComponentProps extends StyleProps {
|
|
990
|
+
children?: Child;
|
|
991
|
+
"aria-label"?: string;
|
|
992
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
export function CommandList(props: Incoming<CommandListComponentProps>) {
|
|
996
|
+
const held = useCommand();
|
|
997
|
+
const elementProps = mergeProps(
|
|
998
|
+
held.listProps,
|
|
999
|
+
filterDOMProps(fromProps(props), { global: true }),
|
|
1000
|
+
styleProps(props),
|
|
1001
|
+
{
|
|
1002
|
+
"aria-label": () =>
|
|
1003
|
+
props["aria-label"]?.() ??
|
|
1004
|
+
access(held.listProps["aria-label"] as MaybeAccessor<string | undefined>),
|
|
1005
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
1006
|
+
},
|
|
1007
|
+
);
|
|
1008
|
+
|
|
1009
|
+
return (
|
|
1010
|
+
<div {...elementProps} ref={mergeRefs(held.listRef.set, props.ref?.())}>
|
|
1011
|
+
{/* Presentational, or it would stand between the listbox and its options. */}
|
|
1012
|
+
<div cmdk-list-sizer="" role="presentation" style={COLUMN}>
|
|
1013
|
+
{props.children}
|
|
1014
|
+
</div>
|
|
1015
|
+
</div>
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
export interface CommandEmptyComponentProps extends StyleProps {
|
|
1020
|
+
children?: Child;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/** Rendered while the search matches nothing, and not otherwise. */
|
|
1024
|
+
export function CommandEmpty(props: Incoming<CommandEmptyComponentProps>) {
|
|
1025
|
+
const held = useCommand();
|
|
1026
|
+
return (
|
|
1027
|
+
<Show when={held.state.isEmpty()}>
|
|
1028
|
+
<div
|
|
1029
|
+
cmdk-empty=""
|
|
1030
|
+
role="presentation"
|
|
1031
|
+
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1032
|
+
{...styleProps(props)}
|
|
1033
|
+
>
|
|
1034
|
+
{props.children}
|
|
1035
|
+
</div>
|
|
1036
|
+
</Show>
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
export interface CommandGroupComponentProps extends StyleProps {
|
|
1041
|
+
children?: Child;
|
|
1042
|
+
/** Draws the heading and names the group. */
|
|
1043
|
+
heading?: Child;
|
|
1044
|
+
"aria-label"?: string;
|
|
1045
|
+
forceMount?: boolean;
|
|
1046
|
+
/** The heading's own class and slot, since `class` styles the group. */
|
|
1047
|
+
headingClass?: string;
|
|
1048
|
+
headingSlot?: string;
|
|
1049
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
export function CommandGroup(props: Incoming<CommandGroupComponentProps>) {
|
|
1053
|
+
const held = useCommand();
|
|
1054
|
+
const group = commandGroup(
|
|
1055
|
+
{
|
|
1056
|
+
heading: () => props.heading?.(),
|
|
1057
|
+
"aria-label": () => props["aria-label"]?.(),
|
|
1058
|
+
forceMount: () => props.forceMount?.(),
|
|
1059
|
+
},
|
|
1060
|
+
held.state,
|
|
1061
|
+
);
|
|
1062
|
+
|
|
1063
|
+
const elementProps = mergeProps(
|
|
1064
|
+
group.groupProps,
|
|
1065
|
+
filterDOMProps(fromProps(props), { global: true }),
|
|
1066
|
+
styleProps(props),
|
|
1067
|
+
{
|
|
1068
|
+
// The order comes last, so a caller's own `style` does not drop it.
|
|
1069
|
+
style: () => ({
|
|
1070
|
+
...props.style?.(),
|
|
1071
|
+
...access(group.groupProps.style as MaybeAccessor<Record<string, number> | undefined>),
|
|
1072
|
+
}),
|
|
1073
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
1074
|
+
},
|
|
1075
|
+
);
|
|
1076
|
+
|
|
1077
|
+
return (
|
|
1078
|
+
<div {...elementProps} ref={mergeRefs(group.ref, props.ref?.())}>
|
|
1079
|
+
<Show when={props.heading?.() !== undefined}>
|
|
1080
|
+
<div
|
|
1081
|
+
{...group.headingProps}
|
|
1082
|
+
class={props.headingClass?.()}
|
|
1083
|
+
data-slot={props.headingSlot?.()}
|
|
1084
|
+
>
|
|
1085
|
+
{props.heading}
|
|
1086
|
+
</div>
|
|
1087
|
+
</Show>
|
|
1088
|
+
<div {...group.itemsProps}>{group.provide(() => props.children) as Child}</div>
|
|
1089
|
+
</div>
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
export interface CommandItemComponentProps extends StyleProps {
|
|
1094
|
+
children?: Child;
|
|
1095
|
+
/** What the item is searched and reported by. @default its own text */
|
|
1096
|
+
value?: string;
|
|
1097
|
+
keywords?: string[];
|
|
1098
|
+
disabled?: boolean;
|
|
1099
|
+
forceMount?: boolean;
|
|
1100
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1101
|
+
onSelect?: (value: string) => void;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
export function CommandItem(props: Incoming<CommandItemComponentProps>) {
|
|
1105
|
+
const held = useCommand();
|
|
1106
|
+
const options = fromProps(props);
|
|
1107
|
+
const item = commandItem(
|
|
1108
|
+
{
|
|
1109
|
+
value: options.value,
|
|
1110
|
+
keywords: options.keywords,
|
|
1111
|
+
isDisabled: options.disabled,
|
|
1112
|
+
forceMount: options.forceMount,
|
|
1113
|
+
onSelect: options.onSelect,
|
|
1114
|
+
},
|
|
1115
|
+
held.state,
|
|
1116
|
+
held.itemId,
|
|
1117
|
+
);
|
|
1118
|
+
|
|
1119
|
+
const elementProps = mergeProps(
|
|
1120
|
+
item.itemProps,
|
|
1121
|
+
filterDOMProps(options, { global: true, labelable: true }),
|
|
1122
|
+
styleProps(props),
|
|
1123
|
+
{
|
|
1124
|
+
// The order comes last, so a caller's own `style` does not drop it.
|
|
1125
|
+
style: () => ({
|
|
1126
|
+
...props.style?.(),
|
|
1127
|
+
...access(item.itemProps.style as MaybeAccessor<Record<string, number> | undefined>),
|
|
1128
|
+
}),
|
|
1129
|
+
"data-focused": item.isHighlighted,
|
|
1130
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
1131
|
+
},
|
|
1132
|
+
);
|
|
1133
|
+
|
|
1134
|
+
return (
|
|
1135
|
+
<div {...elementProps} ref={mergeRefs(item.ref, props.ref?.())}>
|
|
1136
|
+
{props.children}
|
|
1137
|
+
</div>
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
export interface CommandSeparatorComponentProps extends StyleProps {
|
|
1142
|
+
/** Stay through a search, where a separator otherwise divides nothing. */
|
|
1143
|
+
alwaysRender?: boolean;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
export function CommandSeparator(props: Incoming<CommandSeparatorComponentProps>) {
|
|
1147
|
+
const held = useCommand();
|
|
1148
|
+
return (
|
|
1149
|
+
<Show when={props.alwaysRender?.() === true || held.state.search() === ""}>
|
|
1150
|
+
<div
|
|
1151
|
+
cmdk-separator=""
|
|
1152
|
+
role="separator"
|
|
1153
|
+
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1154
|
+
{...styleProps(props)}
|
|
1155
|
+
/>
|
|
1156
|
+
</Show>
|
|
1157
|
+
);
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
export interface CommandLoadingComponentProps extends StyleProps {
|
|
1161
|
+
children?: Child;
|
|
1162
|
+
/** How far along, from 0 to 100. */
|
|
1163
|
+
progress?: number;
|
|
1164
|
+
/** @default "Loading..." */
|
|
1165
|
+
label?: string;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/** Shown, by the caller's own condition, while the items are being fetched. */
|
|
1169
|
+
export function CommandLoading(props: Incoming<CommandLoadingComponentProps>) {
|
|
1170
|
+
return (
|
|
1171
|
+
<div
|
|
1172
|
+
cmdk-loading=""
|
|
1173
|
+
role="progressbar"
|
|
1174
|
+
aria-valuenow={props.progress?.()}
|
|
1175
|
+
aria-valuemin={0}
|
|
1176
|
+
aria-valuemax={100}
|
|
1177
|
+
aria-label={props.label?.() ?? "Loading..."}
|
|
1178
|
+
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1179
|
+
{...styleProps(props)}
|
|
1180
|
+
>
|
|
1181
|
+
<div aria-hidden="true">{props.children}</div>
|
|
1182
|
+
</div>
|
|
1183
|
+
);
|
|
1184
|
+
}
|