@charcuterie/ui 4.28.4 → 4.29.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.
Files changed (49) hide show
  1. package/dist/CatalogPicker/CatalogPicker.d.ts +105 -0
  2. package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
  3. package/dist/CatalogPicker/CatalogPicker.js +152 -0
  4. package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
  5. package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
  6. package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
  7. package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
  8. package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
  9. package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
  10. package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
  11. package/dist/CatalogPicker/CatalogPickerOption.js +42 -0
  12. package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
  13. package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
  14. package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
  15. package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
  16. package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
  17. package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
  18. package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
  19. package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
  20. package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
  21. package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
  22. package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
  23. package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
  24. package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
  25. package/dist/CatalogPicker/catalogPickerView.d.ts +173 -0
  26. package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
  27. package/dist/CatalogPicker/catalogPickerView.js +235 -0
  28. package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
  29. package/dist/index.d.ts +2 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -0
  32. package/dist/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/CatalogPicker/CatalogPicker.mdx +157 -0
  35. package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
  36. package/src/CatalogPicker/CatalogPicker.test.tsx +1352 -0
  37. package/src/CatalogPicker/CatalogPicker.tsx +376 -0
  38. package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
  39. package/src/CatalogPicker/CatalogPickerOption.tsx +138 -0
  40. package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
  41. package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
  42. package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
  43. package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
  44. package/src/CatalogPicker/catalogPickerView.test.ts +346 -0
  45. package/src/CatalogPicker/catalogPickerView.ts +457 -0
  46. package/src/Dialog/Dialog.test.tsx +20 -289
  47. package/src/Field/Field.test.tsx +11 -405
  48. package/src/index.ts +6 -0
  49. package/src/sourceRules.test.ts +5 -1
@@ -0,0 +1,1352 @@
1
+ import { composeStories } from "@storybook/react"
2
+ import type { Root } from "react-dom/client"
3
+ import { createRoot } from "react-dom/client"
4
+ import {
5
+ expect,
6
+ userEvent,
7
+ waitFor,
8
+ within,
9
+ } from "storybook/test"
10
+ import { afterEach, test, vi } from "vitest"
11
+
12
+ import { Dialog } from "../Dialog/Dialog.tsx"
13
+ import { expectNoAxeViolations } from "../expectNoAxeViolations.testHelpers.ts"
14
+ import {
15
+ finishMotion,
16
+ HELD_MOTION,
17
+ } from "../motion.testHelpers.ts"
18
+ import { mountStory } from "../mountStory.testHelpers.ts"
19
+ import { expectAgentDrivable } from "../testing/index.ts"
20
+ import * as stories from "./CatalogPicker.stories.tsx"
21
+ import {
22
+ CatalogPicker,
23
+ type CatalogPickerGroup,
24
+ type CatalogPickerItem,
25
+ type CatalogPickerProps,
26
+ } from "./CatalogPicker.tsx"
27
+ import { ClearIcon } from "./catalogPicker.storyHelpers.tsx"
28
+
29
+ const {
30
+ AllStates,
31
+ Interactive,
32
+ Playground,
33
+ Responsive,
34
+ WithoutFavorites,
35
+ } = composeStories(stories)
36
+
37
+ type MountableStory = Parameters<typeof mountStory>[0]
38
+
39
+ /** Open a story's picker by its trigger name and wait for the search field to take focus. */
40
+ const openPicker = async (
41
+ story: MountableStory,
42
+ triggerName: string,
43
+ ) => {
44
+ const mounted = await mountStory(story)
45
+
46
+ const trigger = expectAgentDrivable(mounted.canvas, {
47
+ name: triggerName,
48
+ role: "button",
49
+ })
50
+
51
+ await userEvent.click(trigger)
52
+
53
+ const input = expectAgentDrivable(mounted.body, {
54
+ role: "combobox",
55
+ })
56
+
57
+ await waitFor(() => {
58
+ expect(input).toHaveFocus()
59
+ })
60
+
61
+ const dialog = expectAgentDrivable(mounted.body, {
62
+ role: "dialog",
63
+ })
64
+
65
+ return { ...mounted, dialog, input, trigger }
66
+ }
67
+
68
+ const optionNames = (listbox: HTMLElement) =>
69
+ Array.from(
70
+ listbox.querySelectorAll<HTMLElement>("[role=option]"),
71
+ ).map((option) => (option.textContent ?? "").trim())
72
+
73
+ const railButton = (
74
+ body: Parameters<typeof expectAgentDrivable>[0],
75
+ name: string,
76
+ ) =>
77
+ expectAgentDrivable(body, {
78
+ name,
79
+ role: "button",
80
+ })
81
+
82
+ test("the trigger is named by its label and the chosen item, and opens a dialog", async () => {
83
+ const { canvas, canvasElement } =
84
+ await mountStory(Playground)
85
+
86
+ const trigger = expectAgentDrivable(canvas, {
87
+ name: "Agent: Lumen 4",
88
+ role: "button",
89
+ })
90
+
91
+ await expect(trigger).toHaveAttribute(
92
+ "aria-haspopup",
93
+ "dialog",
94
+ )
95
+ await expect(trigger).toHaveAttribute(
96
+ "aria-expanded",
97
+ "false",
98
+ )
99
+
100
+ await expectNoAxeViolations(canvasElement)
101
+ })
102
+
103
+ test("opening on a favorite lands on Favorites with the chosen row active", async () => {
104
+ const { body, dialog, input } = await openPicker(
105
+ Playground,
106
+ "Agent: Lumen 4",
107
+ )
108
+
109
+ await expect(dialog).toHaveAccessibleName(
110
+ "Agent: Lumen 4",
111
+ )
112
+
113
+ await expect(
114
+ railButton(body, "Favorites"),
115
+ ).toHaveAttribute("aria-pressed", "true")
116
+
117
+ const listbox = expectAgentDrivable(body, {
118
+ name: "Agent",
119
+ role: "listbox",
120
+ })
121
+
122
+ await expect(input).toHaveAttribute(
123
+ "aria-controls",
124
+ listbox.id,
125
+ )
126
+
127
+ // Favorites order, framed by the pinned rows, and the Contoso
128
+ // (Work) favorite is its own row.
129
+ await expect(optionNames(listbox)).toEqual([
130
+ "DefaultWhatever the app picks",
131
+ "Lumen 4ct-lumen-4",
132
+ "Swift 3nw-swift-3",
133
+ "Lumen 4Work account",
134
+ // "TL" is Tailspin Labs' initials badge: decorative, so in the
135
+ // text but not in the accessible name.
136
+ "TLGlide 2ts-glide-2",
137
+ "Choose per taskAsk every time",
138
+ ])
139
+
140
+ const chosen = body.getAllByRole("option", {
141
+ selected: true,
142
+ })
143
+
144
+ await expect(chosen).toHaveLength(1)
145
+ await expect(input).toHaveAttribute(
146
+ "aria-activedescendant",
147
+ chosen[0]?.id,
148
+ )
149
+
150
+ await expectNoAxeViolations(dialog)
151
+ })
152
+
153
+ test("opening on a non-favorite lands on its group and centres the chosen row", async () => {
154
+ const { body, input } = await openPicker(
155
+ Interactive,
156
+ "Agent: Harbor 4",
157
+ )
158
+
159
+ await expect(
160
+ railButton(body, "Northwind"),
161
+ ).toHaveAttribute("aria-pressed", "true")
162
+ await expect(
163
+ railButton(body, "Favorites"),
164
+ ).toHaveAttribute("aria-pressed", "false")
165
+
166
+ const chosen = body.getByRole("option", {
167
+ selected: true,
168
+ })
169
+
170
+ await expect(chosen).toHaveTextContent("Harbor 4")
171
+ await expect(input).toHaveAttribute(
172
+ "aria-activedescendant",
173
+ chosen.id,
174
+ )
175
+
176
+ // Highlighting it is not showing it: the row must be inside the
177
+ // list's visible box once the panel's height cap has landed.
178
+ await waitFor(() => {
179
+ const listBox = (
180
+ chosen.closest("[role=listbox]") as HTMLElement
181
+ ).getBoundingClientRect()
182
+ const rowBox = chosen.getBoundingClientRect()
183
+
184
+ expect(rowBox.top).toBeGreaterThanOrEqual(
185
+ listBox.top - 1,
186
+ )
187
+ expect(rowBox.bottom).toBeLessThanOrEqual(
188
+ listBox.bottom + 1,
189
+ )
190
+ })
191
+ })
192
+
193
+ test("without favorites there is no Favorites tab, and a group with no rows is left off", async () => {
194
+ const { body } = await openPicker(
195
+ WithoutFavorites,
196
+ "Agent without favorites: Margin 2",
197
+ )
198
+
199
+ await expect(
200
+ body.queryByRole("button", { name: "Favorites" }),
201
+ ).toBeNull()
202
+ await expect(
203
+ body.queryByRole("button", { name: "Adatum" }),
204
+ ).toBeNull()
205
+
206
+ await expect(railButton(body, "Litware")).toHaveAttribute(
207
+ "aria-pressed",
208
+ "true",
209
+ )
210
+
211
+ const toolbar = expectAgentDrivable(body, {
212
+ name: "Agent without favorites groups",
213
+ role: "toolbar",
214
+ })
215
+
216
+ await expect(toolbar).toHaveAttribute(
217
+ "aria-orientation",
218
+ "vertical",
219
+ )
220
+ await expect(
221
+ Array.from(
222
+ toolbar.querySelectorAll("button"),
223
+ (button) => button.getAttribute("aria-label"),
224
+ ),
225
+ ).toEqual([
226
+ "Northwind",
227
+ "Contoso",
228
+ "Contoso (Work)",
229
+ "Fabrikam",
230
+ "Tailspin Labs",
231
+ "Litware",
232
+ ])
233
+ })
234
+
235
+ test("a rail click shows that group, pinned rows included, and keeps focus in the search", async () => {
236
+ const { body, input } = await openPicker(
237
+ Playground,
238
+ "Agent: Lumen 4",
239
+ )
240
+
241
+ await userEvent.click(railButton(body, "Fabrikam"))
242
+
243
+ await waitFor(() => {
244
+ expect(railButton(body, "Fabrikam")).toHaveAttribute(
245
+ "aria-pressed",
246
+ "true",
247
+ )
248
+ })
249
+
250
+ await expect(input).toHaveFocus()
251
+
252
+ const names = optionNames(body.getByRole("listbox"))
253
+
254
+ await expect(names[0]).toMatch(/^Default/u)
255
+ await expect(names.at(-1)).toMatch(/^Choose per task/u)
256
+ await expect(names).toHaveLength(9)
257
+ await expect(
258
+ body.getByRole("option", {
259
+ name: /Loom 5 \(retired\)/u,
260
+ }),
261
+ ).toBeDisabled()
262
+ })
263
+
264
+ test("typing searches every group under headings, and no tab is current", async () => {
265
+ const { body, input } = await openPicker(
266
+ Playground,
267
+ "Agent: Lumen 4",
268
+ )
269
+
270
+ await userEvent.keyboard("LUMEN")
271
+
272
+ await waitFor(() => {
273
+ expect(body.getAllByRole("group")).toHaveLength(2)
274
+ })
275
+
276
+ const groups = body.getAllByRole("group")
277
+
278
+ await expect(groups[0]).toHaveAccessibleName("Contoso")
279
+ await expect(groups[1]).toHaveAccessibleName(
280
+ "Contoso (Work)",
281
+ )
282
+ await expect(body.getAllByRole("option")).toHaveLength(6)
283
+
284
+ for (const button of Array.from(
285
+ body.getByRole("toolbar").querySelectorAll("button"),
286
+ )) {
287
+ await expect(button).toHaveAttribute(
288
+ "aria-pressed",
289
+ "false",
290
+ )
291
+ }
292
+
293
+ // A live query owns the highlight: the top match, not the chosen row.
294
+ await expect(input).toHaveAttribute(
295
+ "aria-activedescendant",
296
+ body.getAllByRole("option")[0]?.id,
297
+ )
298
+
299
+ await expectNoAxeViolations(body.getByRole("dialog"))
300
+ })
301
+
302
+ test("every word must match, against the label, the description, or the group", async () => {
303
+ const { body } = await openPicker(
304
+ Playground,
305
+ "Agent: Lumen 4",
306
+ )
307
+
308
+ // "work" is the Work account's description and group label; the
309
+ // plain Contoso rows have neither.
310
+ await userEvent.keyboard("work lumen")
311
+
312
+ await waitFor(() => {
313
+ expect(body.getAllByRole("option")).toHaveLength(3)
314
+ })
315
+
316
+ await expect(
317
+ body.getByRole("group"),
318
+ ).toHaveAccessibleName("Contoso (Work)")
319
+
320
+ await userEvent.clear(body.getByRole("combobox"))
321
+ await userEvent.keyboard("litware")
322
+
323
+ await waitFor(() => {
324
+ expect(body.getAllByRole("option")).toHaveLength(5)
325
+ })
326
+
327
+ await userEvent.clear(body.getByRole("combobox"))
328
+ await userEvent.keyboard("default")
329
+
330
+ // A pinned row matches like any other, outside every group.
331
+ await waitFor(() => {
332
+ expect(optionNames(body.getByRole("listbox"))).toEqual([
333
+ "DefaultWhatever the app picks",
334
+ ])
335
+ })
336
+ await expect(body.queryAllByRole("group")).toHaveLength(0)
337
+ })
338
+
339
+ test("a search with no match shows the empty label", async () => {
340
+ const { body, input } = await openPicker(
341
+ Playground,
342
+ "Agent: Lumen 4",
343
+ )
344
+
345
+ await userEvent.keyboard("zzz")
346
+
347
+ await waitFor(() => {
348
+ expect(body.getByText("No matches")).toBeVisible()
349
+ })
350
+
351
+ await expect(body.queryByRole("listbox")).toBeNull()
352
+ await expect(input).toHaveAttribute(
353
+ "aria-expanded",
354
+ "false",
355
+ )
356
+ await expect(input).not.toHaveAttribute(
357
+ "aria-activedescendant",
358
+ )
359
+ })
360
+
361
+ test("the arrows skip a disabled row, and stop at either end", async () => {
362
+ const { body, input } = await openPicker(
363
+ Playground,
364
+ "Agent: Lumen 4",
365
+ )
366
+
367
+ await userEvent.click(railButton(body, "Fabrikam"))
368
+
369
+ const option = (name: RegExp) =>
370
+ body.getByRole("option", { name })
371
+
372
+ // Far more presses than rows: the last one holds.
373
+ await userEvent.keyboard("{ArrowDown>30/}")
374
+
375
+ await waitFor(() => {
376
+ expect(input).toHaveAttribute(
377
+ "aria-activedescendant",
378
+ option(/^Choose per task/u).id,
379
+ )
380
+ })
381
+
382
+ // Up from the last row passes over the disabled "Loom 5 (retired)".
383
+ await userEvent.keyboard("{ArrowUp}")
384
+
385
+ await waitFor(() => {
386
+ expect(input).toHaveAttribute(
387
+ "aria-activedescendant",
388
+ option(/^Spindle Vision/u).id,
389
+ )
390
+ })
391
+
392
+ await userEvent.keyboard("{ArrowDown}")
393
+
394
+ await waitFor(() => {
395
+ expect(input).toHaveAttribute(
396
+ "aria-activedescendant",
397
+ option(/^Choose per task/u).id,
398
+ )
399
+ })
400
+
401
+ await userEvent.keyboard("{ArrowUp>30/}")
402
+
403
+ await waitFor(() => {
404
+ expect(input).toHaveAttribute(
405
+ "aria-activedescendant",
406
+ option(/^Default/u).id,
407
+ )
408
+ })
409
+ })
410
+
411
+ test("Enter chooses the active row, closes, and returns focus to the trigger", async () => {
412
+ const { body, canvas, input } = await openPicker(
413
+ Interactive,
414
+ "Agent: Harbor 4",
415
+ )
416
+
417
+ await userEvent.keyboard("weave 1")
418
+
419
+ await waitFor(() => {
420
+ expect(input).toHaveAttribute(
421
+ "aria-activedescendant",
422
+ body.getByRole("option", { name: /^Weave Code 1/u })
423
+ .id,
424
+ )
425
+ })
426
+
427
+ await userEvent.keyboard("{Enter}")
428
+
429
+ await waitFor(() => {
430
+ expect(body.queryByRole("dialog")).toBeNull()
431
+ })
432
+
433
+ const trigger = expectAgentDrivable(canvas, {
434
+ name: "Agent: Weave Code 1",
435
+ role: "button",
436
+ })
437
+
438
+ await waitFor(() => {
439
+ expect(trigger).toHaveFocus()
440
+ })
441
+ })
442
+
443
+ test("a click on a row chooses it and closes", async () => {
444
+ const { body, canvas } = await openPicker(
445
+ Interactive,
446
+ "Agent: Harbor 4",
447
+ )
448
+
449
+ await userEvent.click(
450
+ body.getByRole("option", {
451
+ name: /^Atlas 2 Reasoning/u,
452
+ }),
453
+ )
454
+
455
+ await waitFor(() => {
456
+ expect(body.queryByRole("dialog")).toBeNull()
457
+ })
458
+
459
+ expectAgentDrivable(canvas, {
460
+ name: "Agent: Atlas 2 Reasoning",
461
+ role: "button",
462
+ })
463
+ })
464
+
465
+ test("Escape clears the query first, then closes and returns focus to the trigger", async () => {
466
+ const { body, input, trigger } = await openPicker(
467
+ Interactive,
468
+ "Agent: Harbor 4",
469
+ )
470
+
471
+ await userEvent.keyboard("glide")
472
+
473
+ await expect(input).toHaveValue("glide")
474
+
475
+ await userEvent.keyboard("{Escape}")
476
+
477
+ await expect(input).toHaveValue("")
478
+ await expect(body.getByRole("dialog")).toBeInTheDocument()
479
+
480
+ // The view is the tab again, not the search.
481
+ await waitFor(() => {
482
+ expect(railButton(body, "Northwind")).toHaveAttribute(
483
+ "aria-pressed",
484
+ "true",
485
+ )
486
+ })
487
+
488
+ await userEvent.keyboard("{Escape}")
489
+
490
+ await waitFor(() => {
491
+ expect(body.queryByRole("dialog")).toBeNull()
492
+ })
493
+
494
+ await waitFor(() => {
495
+ expect(trigger).toHaveFocus()
496
+ })
497
+ })
498
+
499
+ test("Tab moves between the search and the rail, where the arrows choose a group", async () => {
500
+ const { body, input } = await openPicker(
501
+ Interactive,
502
+ "Agent: Harbor 4",
503
+ )
504
+
505
+ await userEvent.keyboard("{Tab}")
506
+
507
+ // The rail is one tab stop, and it is the current tab.
508
+ await waitFor(() => {
509
+ expect(railButton(body, "Northwind")).toHaveFocus()
510
+ })
511
+
512
+ await userEvent.keyboard("{ArrowDown}")
513
+
514
+ await waitFor(() => {
515
+ expect(railButton(body, "Contoso")).toHaveFocus()
516
+ })
517
+
518
+ // Moving focus does not press it; Enter does.
519
+ await expect(railButton(body, "Contoso")).toHaveAttribute(
520
+ "aria-pressed",
521
+ "false",
522
+ )
523
+
524
+ await userEvent.keyboard("{End}")
525
+
526
+ await waitFor(() => {
527
+ expect(railButton(body, "Litware")).toHaveFocus()
528
+ })
529
+
530
+ await userEvent.keyboard("{ArrowDown}")
531
+
532
+ // Wraps.
533
+ await waitFor(() => {
534
+ expect(railButton(body, "Favorites")).toHaveFocus()
535
+ })
536
+
537
+ await userEvent.keyboard("{ArrowDown}{ArrowDown}{Enter}")
538
+
539
+ await waitFor(() => {
540
+ expect(railButton(body, "Contoso")).toHaveAttribute(
541
+ "aria-pressed",
542
+ "true",
543
+ )
544
+ })
545
+
546
+ await expect(
547
+ optionNames(body.getByRole("listbox")),
548
+ ).toContain("Prism 2 Fastct-prism-2-fast")
549
+
550
+ await userEvent.keyboard("{Tab}")
551
+
552
+ await waitFor(() => {
553
+ expect(input).toHaveFocus()
554
+ })
555
+
556
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
557
+
558
+ await waitFor(() => {
559
+ expect(railButton(body, "Contoso")).toHaveFocus()
560
+ })
561
+
562
+ // Escape from the rail closes too.
563
+ await userEvent.keyboard("{Escape}")
564
+
565
+ await waitFor(() => {
566
+ expect(body.queryByRole("dialog")).toBeNull()
567
+ })
568
+ })
569
+
570
+ test("the panel fits the window, rail and all", async () => {
571
+ const { body, dialog } = await openPicker(
572
+ Interactive,
573
+ "Agent: Harbor 4",
574
+ )
575
+
576
+ await waitFor(() => {
577
+ const box = dialog.getBoundingClientRect()
578
+
579
+ expect(box.left).toBeGreaterThanOrEqual(0)
580
+ expect(box.right).toBeLessThanOrEqual(
581
+ document.documentElement.clientWidth,
582
+ )
583
+ expect(box.bottom).toBeLessThanOrEqual(
584
+ window.innerHeight,
585
+ )
586
+ })
587
+
588
+ const toolbarBox = body
589
+ .getByRole("toolbar")
590
+ .getBoundingClientRect()
591
+ const listBox = body
592
+ .getByRole("listbox")
593
+ .getBoundingClientRect()
594
+
595
+ await expect(toolbarBox.width).toBeGreaterThan(0)
596
+ // The list keeps a usable width beside the rail.
597
+ await expect(listBox.width).toBeGreaterThan(200)
598
+ await expect(listBox.left).toBeGreaterThanOrEqual(
599
+ toolbarBox.right - 1,
600
+ )
601
+ })
602
+
603
+ test("a disabled picker does not open", async () => {
604
+ const { body, canvas } = await mountStory(AllStates)
605
+
606
+ const trigger = expectAgentDrivable(canvas, {
607
+ name: "Disabled agent: Lumen 4",
608
+ role: "button",
609
+ })
610
+
611
+ await expect(trigger).toBeDisabled()
612
+
613
+ await userEvent.click(trigger, { pointerEventsCheck: 0 })
614
+
615
+ await expect(body.queryAllByRole("dialog")).toHaveLength(
616
+ 0,
617
+ )
618
+ })
619
+
620
+ test("the inline board names each panel and has no axe issues", async () => {
621
+ const { canvas, canvasElement } =
622
+ await mountStory(AllStates)
623
+
624
+ expectAgentDrivable(canvas, {
625
+ name: "Search panel",
626
+ role: "combobox",
627
+ })
628
+ expectAgentDrivable(canvas, {
629
+ name: "Empty panel groups",
630
+ role: "toolbar",
631
+ })
632
+
633
+ await expectNoAxeViolations(canvasElement)
634
+ })
635
+
636
+ test("a stretched trigger keeps the chevron inside and ends a long name in an ellipsis", async () => {
637
+ const { canvas } = await mountStory(Responsive)
638
+
639
+ const trigger = expectAgentDrivable(canvas, {
640
+ name: "Long agent at 15rem: Tailwind Research Preview With A Very Long Name",
641
+ role: "button",
642
+ })
643
+
644
+ const triggerBox = trigger.getBoundingClientRect()
645
+ const chevronBox = (
646
+ trigger.querySelector("svg:last-of-type") as SVGElement
647
+ ).getBoundingClientRect()
648
+
649
+ await expect(trigger.scrollWidth).toBe(
650
+ trigger.clientWidth,
651
+ )
652
+ await expect(chevronBox.right).toBeLessThanOrEqual(
653
+ triggerBox.right,
654
+ )
655
+ })
656
+
657
+ // ---------------------------------------------------------------------
658
+ // A picker whose props change while it is open — a catalog refresh, a
659
+ // disable — which a composed story cannot do. Mounted on its own root,
660
+ // as `overlayReturnFocus.test.tsx` does, and taken down after each test.
661
+ // ---------------------------------------------------------------------
662
+
663
+ const roots: { host: HTMLElement; root: Root }[] = []
664
+
665
+ afterEach(() => {
666
+ for (const { host, root } of roots.splice(0)) {
667
+ root.unmount()
668
+
669
+ host.remove()
670
+ }
671
+ })
672
+
673
+ // Invented. The first id has a space in it on purpose.
674
+ const SMALL_GROUPS: readonly CatalogPickerGroup[] = [
675
+ { id: "provider one", label: "Provider One" },
676
+ { id: "second", label: "Second Source" },
677
+ ]
678
+
679
+ const ALPHA: CatalogPickerItem = {
680
+ groupId: "provider one",
681
+ label: "Alpha",
682
+ value: "alpha",
683
+ }
684
+
685
+ const BETA: CatalogPickerItem = {
686
+ groupId: "provider one",
687
+ label: "Beta",
688
+ value: "beta",
689
+ }
690
+
691
+ const GAMMA: CatalogPickerItem = {
692
+ groupId: "second",
693
+ label: "Gamma",
694
+ value: "gamma",
695
+ }
696
+
697
+ const SMALL_ITEMS: readonly CatalogPickerItem[] = [
698
+ ALPHA,
699
+ BETA,
700
+ GAMMA,
701
+ ]
702
+
703
+ type SmallPickerProps = Partial<CatalogPickerProps>
704
+
705
+ /** Mount a small picker, and hand back a re-render with new props. */
706
+ const mountPicker = (props: SmallPickerProps = {}) => {
707
+ const host = document.createElement("div")
708
+
709
+ document.body.append(host)
710
+
711
+ const root = createRoot(host)
712
+
713
+ roots.push({ host, root })
714
+
715
+ const chosen: string[] = []
716
+
717
+ const render = (nextProps: SmallPickerProps = {}) => {
718
+ root.render(
719
+ <CatalogPicker
720
+ groups={SMALL_GROUPS}
721
+ items={SMALL_ITEMS}
722
+ label="Model"
723
+ onChange={(value) => {
724
+ chosen.push(value)
725
+ }}
726
+ placeholder="Pick one"
727
+ {...props}
728
+ {...nextProps}
729
+ />,
730
+ )
731
+ }
732
+
733
+ render()
734
+
735
+ return { body: within(document.body), chosen, render }
736
+ }
737
+
738
+ const openSmallPicker = async (
739
+ props: SmallPickerProps = {},
740
+ triggerName = "Model: Pick one",
741
+ ) => {
742
+ const mounted = mountPicker(props)
743
+
744
+ const trigger = await waitFor(() =>
745
+ mounted.body.getByRole("button", { name: triggerName }),
746
+ )
747
+
748
+ await userEvent.click(trigger)
749
+
750
+ const input = mounted.body.getByRole("combobox")
751
+
752
+ await waitFor(() => {
753
+ expect(input).toHaveFocus()
754
+ })
755
+
756
+ return { ...mounted, input, trigger }
757
+ }
758
+
759
+ /** A row's text without its decorative mark (an initials badge is `aria-hidden`). */
760
+ const visibleName = (element: Element) => {
761
+ const copy = element.cloneNode(true) as Element
762
+
763
+ for (const hidden of copy.querySelectorAll(
764
+ '[aria-hidden="true"]',
765
+ )) {
766
+ hidden.remove()
767
+ }
768
+
769
+ return copy.textContent?.trim()
770
+ }
771
+
772
+ const activeOptionName = (input: HTMLElement) => {
773
+ const id = input.getAttribute("aria-activedescendant")
774
+
775
+ const option =
776
+ id === null ? null : document.getElementById(id)
777
+
778
+ return option === null ? undefined : visibleName(option)
779
+ }
780
+
781
+ test("a catalog refresh keeps the highlight on the same row, and Enter chooses it", async () => {
782
+ const { chosen, input, render } = await openSmallPicker()
783
+
784
+ await expect(activeOptionName(input)).toBe("Alpha")
785
+
786
+ await userEvent.keyboard("{ArrowDown}")
787
+
788
+ await waitFor(() => {
789
+ expect(activeOptionName(input)).toBe("Beta")
790
+ })
791
+
792
+ // The rows swap places under the highlight.
793
+ render({ items: [BETA, ALPHA, GAMMA] })
794
+
795
+ await waitFor(() => {
796
+ expect(activeOptionName(input)).toBe("Beta")
797
+ })
798
+
799
+ await userEvent.keyboard("{Enter}")
800
+
801
+ await expect(chosen).toEqual(["beta"])
802
+ })
803
+
804
+ test("a highlighted row that leaves, or is disabled, reseeds the highlight", async () => {
805
+ const { input, render } = await openSmallPicker({
806
+ items: [
807
+ ALPHA,
808
+ BETA,
809
+ { ...ALPHA, label: "Alpha Two", value: "alpha-2" },
810
+ GAMMA,
811
+ ],
812
+ })
813
+
814
+ await userEvent.keyboard("{ArrowDown}")
815
+
816
+ await waitFor(() => {
817
+ expect(activeOptionName(input)).toBe("Beta")
818
+ })
819
+
820
+ // Disabled under the highlight: back to the first enabled row.
821
+ render({
822
+ items: [
823
+ { ...ALPHA, isDisabled: true },
824
+ { ...BETA, isDisabled: true },
825
+ { ...ALPHA, label: "Alpha Two", value: "alpha-2" },
826
+ GAMMA,
827
+ ],
828
+ })
829
+
830
+ await waitFor(() => {
831
+ expect(activeOptionName(input)).toBe("Alpha Two")
832
+ })
833
+
834
+ // Gone from the view: the same.
835
+ render({ items: [ALPHA, BETA, GAMMA] })
836
+
837
+ await waitFor(() => {
838
+ expect(activeOptionName(input)).toBe("Alpha")
839
+ })
840
+ })
841
+
842
+ test("a catalog refresh keeps each row's button, so a half-done click stays on its row", async () => {
843
+ const { body, render } = await openSmallPicker()
844
+
845
+ const alphaButton = body.getByRole("option", {
846
+ name: "Alpha",
847
+ })
848
+
849
+ // A pointer pressed on Alpha, then the rows swap under it: the
850
+ // pressed button must still be Alpha's, not Beta's.
851
+ render({ items: [BETA, ALPHA, GAMMA] })
852
+
853
+ await waitFor(() => {
854
+ expect(
855
+ body.getAllByRole("option").map(visibleName),
856
+ ).toEqual(["Beta", "Alpha"])
857
+ })
858
+
859
+ await expect(visibleName(alphaButton)).toBe("Alpha")
860
+ await expect(alphaButton.isConnected).toBe(true)
861
+ })
862
+
863
+ test("a refresh during a search reseeds to the chosen row when it is shown", async () => {
864
+ const { chosen, input, render } = await openSmallPicker(
865
+ { value: "gamma" },
866
+ "Model: Gamma",
867
+ )
868
+
869
+ // Every label has an "a": a live query takes the top match.
870
+ await userEvent.keyboard("a")
871
+
872
+ await waitFor(() => {
873
+ expect(activeOptionName(input)).toBe("Alpha")
874
+ })
875
+
876
+ await userEvent.keyboard("{ArrowDown}")
877
+
878
+ await waitFor(() => {
879
+ expect(activeOptionName(input)).toBe("Beta")
880
+ })
881
+
882
+ // Reordered and disabled under the highlight in one update.
883
+ render({
884
+ items: [{ ...BETA, isDisabled: true }, ALPHA, GAMMA],
885
+ })
886
+
887
+ await waitFor(() => {
888
+ expect(activeOptionName(input)).toBe("Gamma")
889
+ })
890
+
891
+ await userEvent.keyboard("{Enter}")
892
+
893
+ await expect(chosen).toEqual(["gamma"])
894
+ })
895
+
896
+ test("with no rail, Tab cycles between the search field and its clear button", async () => {
897
+ const { body, input } = await openSmallPicker({
898
+ clearIcon: <ClearIcon />,
899
+ groups: [],
900
+ items: [
901
+ { groupId: null, label: "Alpha", value: "alpha" },
902
+ { groupId: null, label: "Beta", value: "beta" },
903
+ ],
904
+ })
905
+
906
+ // The field alone: Tab keeps focus in the picker.
907
+ await userEvent.keyboard("{Tab}")
908
+
909
+ await expect(input).toHaveFocus()
910
+
911
+ await userEvent.keyboard("a")
912
+
913
+ const clear = body.getByRole("button", {
914
+ name: "Clear search",
915
+ })
916
+
917
+ await userEvent.keyboard("{Tab}")
918
+
919
+ await waitFor(() => {
920
+ expect(clear).toHaveFocus()
921
+ })
922
+
923
+ await userEvent.keyboard("{Tab}")
924
+
925
+ await waitFor(() => {
926
+ expect(input).toHaveFocus()
927
+ })
928
+
929
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
930
+
931
+ await waitFor(() => {
932
+ expect(clear).toHaveFocus()
933
+ })
934
+
935
+ await expect(
936
+ body.getByRole("combobox"),
937
+ ).toBeInTheDocument()
938
+ })
939
+
940
+ test("Escape inside a Dialog clears, then closes the picker, and never closes the dialog", async () => {
941
+ const host = document.createElement("div")
942
+
943
+ document.body.append(host)
944
+
945
+ const root = createRoot(host)
946
+
947
+ roots.push({ host, root })
948
+
949
+ const onClose = vi.fn()
950
+
951
+ root.render(
952
+ <Dialog heading="Settings" isVisible onClose={onClose}>
953
+ <CatalogPicker
954
+ clearIcon={<ClearIcon />}
955
+ groups={SMALL_GROUPS}
956
+ items={SMALL_ITEMS}
957
+ label="Model"
958
+ onChange={() => undefined}
959
+ placeholder="Pick one"
960
+ />
961
+ </Dialog>,
962
+ )
963
+
964
+ const body = within(document.body)
965
+
966
+ const trigger = await waitFor(() =>
967
+ body.getByRole("button", { name: "Model: Pick one" }),
968
+ )
969
+
970
+ await userEvent.click(trigger)
971
+
972
+ const input = body.getByRole("combobox")
973
+
974
+ await waitFor(() => {
975
+ expect(input).toHaveFocus()
976
+ })
977
+
978
+ await userEvent.keyboard("al")
979
+
980
+ await userEvent.keyboard("{Escape}")
981
+
982
+ await expect(input).toHaveValue("")
983
+ await expect(
984
+ body.getByRole("dialog", { name: "Model: Pick one" }),
985
+ ).toBeInTheDocument()
986
+
987
+ await userEvent.keyboard("{Escape}")
988
+
989
+ await waitFor(() => {
990
+ expect(
991
+ body.queryByRole("dialog", {
992
+ name: "Model: Pick one",
993
+ }),
994
+ ).toBeNull()
995
+ })
996
+
997
+ await waitFor(() => {
998
+ expect(trigger).toHaveFocus()
999
+ })
1000
+
1001
+ await expect(
1002
+ body.getByRole("dialog", { name: "Settings" }),
1003
+ ).toBeInTheDocument()
1004
+ await expect(onClose).not.toHaveBeenCalled()
1005
+
1006
+ // The same from the clear button: it clears, and the dialog stays.
1007
+ await userEvent.click(trigger)
1008
+
1009
+ await waitFor(() => {
1010
+ expect(body.getByRole("combobox")).toHaveFocus()
1011
+ })
1012
+
1013
+ await userEvent.keyboard("al{Tab}")
1014
+
1015
+ await waitFor(() => {
1016
+ expect(
1017
+ body.getByRole("button", { name: "Clear search" }),
1018
+ ).toHaveFocus()
1019
+ })
1020
+
1021
+ await userEvent.keyboard("{Escape}")
1022
+
1023
+ await waitFor(() => {
1024
+ expect(body.getByRole("combobox")).toHaveFocus()
1025
+ })
1026
+
1027
+ await expect(body.getByRole("combobox")).toHaveValue("")
1028
+ await expect(onClose).not.toHaveBeenCalled()
1029
+ })
1030
+
1031
+ test("a rail button that leaves while it has focus hands focus to the rail that is left", async () => {
1032
+ const { body, render } = await openSmallPicker(
1033
+ { value: "gamma" },
1034
+ "Model: Gamma",
1035
+ )
1036
+
1037
+ await userEvent.keyboard("{Tab}")
1038
+
1039
+ const second = body.getByRole("button", {
1040
+ name: "Second Source",
1041
+ })
1042
+
1043
+ await waitFor(() => {
1044
+ expect(second).toHaveFocus()
1045
+ })
1046
+
1047
+ // Its last row goes, and the button with it.
1048
+ render({ items: [ALPHA, BETA] })
1049
+
1050
+ await waitFor(() => {
1051
+ expect(second.isConnected).toBe(false)
1052
+ })
1053
+
1054
+ await expect(
1055
+ body.getByRole("button", { name: "Provider One" }),
1056
+ ).toHaveFocus()
1057
+ await expect(body.getByRole("dialog")).toBeInTheDocument()
1058
+ })
1059
+
1060
+ test("a repeated value is one row, the first wins everywhere, and the picker warns", async () => {
1061
+ const warn = vi
1062
+ .spyOn(console, "warn")
1063
+ .mockImplementation(() => undefined)
1064
+
1065
+ try {
1066
+ const { body, input } = await openSmallPicker(
1067
+ {
1068
+ favorites: ["alpha"],
1069
+ items: [
1070
+ ...SMALL_ITEMS,
1071
+ {
1072
+ groupId: "second",
1073
+ label: "Alpha Copy",
1074
+ value: "alpha",
1075
+ },
1076
+ ],
1077
+ value: "alpha",
1078
+ },
1079
+ "Model: Alpha",
1080
+ )
1081
+
1082
+ await expect(warn).toHaveBeenCalledWith(
1083
+ expect.stringContaining('item values ["alpha"]'),
1084
+ )
1085
+
1086
+ // The Favorites tab, the list and the highlight agree on the first.
1087
+ await expect(
1088
+ body.getAllByRole("option", { selected: true }),
1089
+ ).toHaveLength(1)
1090
+ await expect(activeOptionName(input)).toBe("Alpha")
1091
+
1092
+ await userEvent.keyboard("alpha")
1093
+
1094
+ await waitFor(() => {
1095
+ expect(
1096
+ body.getAllByRole("option").map(visibleName),
1097
+ ).toEqual(["Alpha"])
1098
+ })
1099
+ } finally {
1100
+ warn.mockRestore()
1101
+ }
1102
+ })
1103
+
1104
+ test("a search result group is named by its heading, even when its id has a space", async () => {
1105
+ const { body } = await openSmallPicker()
1106
+
1107
+ await userEvent.keyboard("a")
1108
+
1109
+ const group = await waitFor(() =>
1110
+ body.getByRole("group", { name: "Provider One" }),
1111
+ )
1112
+
1113
+ await expect(
1114
+ within(group).getAllByRole("option"),
1115
+ ).toHaveLength(2)
1116
+ await expect(
1117
+ body.getByRole("group", { name: "Second Source" }),
1118
+ ).toBeInTheDocument()
1119
+ })
1120
+
1121
+ test("Tab reaches the clear button between the search field and the rail, both ways", async () => {
1122
+ const { body, input } = await openSmallPicker({
1123
+ clearIcon: <ClearIcon />,
1124
+ })
1125
+
1126
+ // No query, no clear button: the field and the rail.
1127
+ await userEvent.keyboard("{Tab}")
1128
+
1129
+ const rail = body.getByRole("button", {
1130
+ name: "Provider One",
1131
+ })
1132
+
1133
+ await waitFor(() => {
1134
+ expect(rail).toHaveFocus()
1135
+ })
1136
+
1137
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
1138
+
1139
+ await waitFor(() => {
1140
+ expect(input).toHaveFocus()
1141
+ })
1142
+
1143
+ await userEvent.keyboard("be")
1144
+
1145
+ const clear = body.getByRole("button", {
1146
+ name: "Clear search",
1147
+ })
1148
+
1149
+ await userEvent.keyboard("{Tab}")
1150
+
1151
+ await waitFor(() => {
1152
+ expect(clear).toHaveFocus()
1153
+ })
1154
+
1155
+ await userEvent.keyboard("{Tab}")
1156
+
1157
+ await waitFor(() => {
1158
+ expect(rail).toHaveFocus()
1159
+ })
1160
+
1161
+ await userEvent.keyboard("{Tab}")
1162
+
1163
+ await waitFor(() => {
1164
+ expect(input).toHaveFocus()
1165
+ })
1166
+
1167
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
1168
+
1169
+ await waitFor(() => {
1170
+ expect(rail).toHaveFocus()
1171
+ })
1172
+
1173
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
1174
+
1175
+ await waitFor(() => {
1176
+ expect(clear).toHaveFocus()
1177
+ })
1178
+
1179
+ await userEvent.keyboard("{Shift>}{Tab}{/Shift}")
1180
+
1181
+ await waitFor(() => {
1182
+ expect(input).toHaveFocus()
1183
+ })
1184
+ })
1185
+
1186
+ test("keys that end an IME composition are not picker shortcuts, and Home and End move the caret", async () => {
1187
+ const { body, chosen, input } = await openSmallPicker()
1188
+
1189
+ await userEvent.keyboard("alp")
1190
+
1191
+ const dispatchComposing = (key: string) => {
1192
+ input.dispatchEvent(
1193
+ new KeyboardEvent("keydown", {
1194
+ bubbles: true,
1195
+ cancelable: true,
1196
+ isComposing: true,
1197
+ key,
1198
+ }),
1199
+ )
1200
+ }
1201
+
1202
+ dispatchComposing("Enter")
1203
+ dispatchComposing("Escape")
1204
+ dispatchComposing("ArrowDown")
1205
+
1206
+ await expect(chosen).toEqual([])
1207
+ await expect(input).toHaveValue("alp")
1208
+ await expect(body.getByRole("dialog")).toBeInTheDocument()
1209
+ await expect(activeOptionName(input)).toBe("Alpha")
1210
+
1211
+ await userEvent.keyboard("{Home}")
1212
+
1213
+ await expect(
1214
+ (input as HTMLInputElement).selectionStart,
1215
+ ).toBe(0)
1216
+ await expect(activeOptionName(input)).toBe("Alpha")
1217
+
1218
+ await userEvent.keyboard("{End}")
1219
+
1220
+ await expect(
1221
+ (input as HTMLInputElement).selectionStart,
1222
+ ).toBe(3)
1223
+ })
1224
+
1225
+ test("a picker reopened while it is still fading out starts a fresh search", async () => {
1226
+ // The suite freezes motion; this is about the exit, so it is held
1227
+ // instead, as `usePresence.test.tsx` does.
1228
+ const frozenStyle = document.getElementById(
1229
+ "charcuterie-freeze-motion",
1230
+ )
1231
+ const tokens = [
1232
+ "--duration-fast",
1233
+ "--duration-normal",
1234
+ "--duration-slow",
1235
+ ]
1236
+
1237
+ frozenStyle?.remove()
1238
+
1239
+ for (const token of tokens) {
1240
+ document.body.style.setProperty(token, HELD_MOTION)
1241
+ }
1242
+
1243
+ try {
1244
+ const { body, input, trigger } = await openSmallPicker(
1245
+ {
1246
+ value: "gamma",
1247
+ },
1248
+ "Model: Gamma",
1249
+ )
1250
+
1251
+ await userEvent.keyboard("al")
1252
+
1253
+ await expect(input).toHaveValue("al")
1254
+
1255
+ await userEvent.click(trigger)
1256
+
1257
+ // Still on the page, leaving.
1258
+ await expect(
1259
+ document.querySelector('[role="dialog"][inert]'),
1260
+ ).not.toBeNull()
1261
+
1262
+ await userEvent.click(trigger)
1263
+
1264
+ const reopened = await waitFor(() => {
1265
+ const field = body.getByRole("combobox")
1266
+
1267
+ expect(field).toHaveFocus()
1268
+
1269
+ return field
1270
+ })
1271
+
1272
+ await expect(reopened).toHaveValue("")
1273
+ // The opening rule again: the chosen row's group.
1274
+ await expect(
1275
+ body.getByRole("button", { name: "Second Source" }),
1276
+ ).toHaveAttribute("aria-pressed", "true")
1277
+
1278
+ finishMotion(document.body)
1279
+ } finally {
1280
+ for (const token of tokens) {
1281
+ document.body.style.removeProperty(token)
1282
+ }
1283
+
1284
+ if (frozenStyle !== null) {
1285
+ document.head.append(frozenStyle)
1286
+ }
1287
+ }
1288
+ })
1289
+
1290
+ test("disabling an open picker closes it, and enabling it again does not reopen it", async () => {
1291
+ const { body, render, trigger } = await openSmallPicker()
1292
+
1293
+ render({ isDisabled: true })
1294
+
1295
+ await waitFor(() => {
1296
+ expect(body.queryByRole("dialog")).toBeNull()
1297
+ })
1298
+
1299
+ render({ isDisabled: false })
1300
+
1301
+ await waitFor(() => {
1302
+ expect(trigger).toBeEnabled()
1303
+ })
1304
+ await expect(trigger).toHaveAttribute(
1305
+ "aria-expanded",
1306
+ "false",
1307
+ )
1308
+ await expect(body.queryByRole("dialog")).toBeNull()
1309
+ })
1310
+
1311
+ test("an ungrouped chosen row is in the opening view, and a pinned favorite is a Favorites tab", async () => {
1312
+ const { body, input } = await openSmallPicker(
1313
+ {
1314
+ favorites: ["default"],
1315
+ items: [
1316
+ {
1317
+ groupId: null,
1318
+ label: "Default",
1319
+ pinned: "start",
1320
+ value: "default",
1321
+ },
1322
+ ...SMALL_ITEMS,
1323
+ {
1324
+ groupId: null,
1325
+ label: "Loose End",
1326
+ value: "loose",
1327
+ },
1328
+ ],
1329
+ value: "loose",
1330
+ },
1331
+ "Model: Loose End",
1332
+ )
1333
+
1334
+ await expect(
1335
+ body.getByRole("button", { name: "Favorites" }),
1336
+ ).toHaveAttribute("aria-pressed", "true")
1337
+
1338
+ const selected = body.getByRole("option", {
1339
+ selected: true,
1340
+ })
1341
+
1342
+ await expect(selected).toHaveTextContent("Loose End")
1343
+ await expect(input).toHaveAttribute(
1344
+ "aria-activedescendant",
1345
+ selected.id,
1346
+ )
1347
+
1348
+ // The pinned favorite is listed once.
1349
+ await expect(
1350
+ body.getAllByRole("option", { name: /^Default/u }),
1351
+ ).toHaveLength(1)
1352
+ })