@aleph-alpha/ui-library 5.6.0 → 5.8.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 (79) hide show
  1. package/README.md +4 -0
  2. package/config.d.ts +5 -1
  3. package/config.js +23 -1
  4. package/dist/system/components/UiBreadcrumb/UiBreadcrumb.vue.d.ts +17 -0
  5. package/dist/system/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue.d.ts +17 -0
  6. package/dist/system/components/UiBreadcrumb/UiBreadcrumbItem.vue.d.ts +17 -0
  7. package/dist/system/components/UiBreadcrumb/UiBreadcrumbLink.vue.d.ts +18 -0
  8. package/dist/system/components/UiBreadcrumb/UiBreadcrumbList.vue.d.ts +17 -0
  9. package/dist/system/components/UiBreadcrumb/UiBreadcrumbPage.vue.d.ts +17 -0
  10. package/dist/system/components/UiBreadcrumb/UiBreadcrumbSeparator.vue.d.ts +17 -0
  11. package/dist/system/components/UiBreadcrumb/index.d.ts +8 -0
  12. package/dist/system/components/UiBreadcrumb/types.d.ts +22 -0
  13. package/dist/system/components/UiField/types.d.ts +2 -1
  14. package/dist/system/components/UiToggleGroup/types.d.ts +0 -5
  15. package/dist/system/components/core/breadcrumb/Breadcrumb.vue.d.ts +21 -0
  16. package/dist/system/components/core/breadcrumb/BreadcrumbEllipsis.vue.d.ts +21 -0
  17. package/dist/system/components/core/breadcrumb/BreadcrumbItem.vue.d.ts +21 -0
  18. package/dist/system/components/core/breadcrumb/BreadcrumbLink.vue.d.ts +24 -0
  19. package/dist/system/components/core/breadcrumb/BreadcrumbList.vue.d.ts +21 -0
  20. package/dist/system/components/core/breadcrumb/BreadcrumbPage.vue.d.ts +21 -0
  21. package/dist/system/components/core/breadcrumb/BreadcrumbSeparator.vue.d.ts +21 -0
  22. package/dist/system/components/core/breadcrumb/index.d.ts +7 -0
  23. package/dist/system/components/core/listbox/Listbox.vue.d.ts +28 -2
  24. package/dist/system/components/core/listbox/context.d.ts +21 -0
  25. package/dist/system/components/index.d.ts +1 -0
  26. package/dist/system/lib.js +10062 -9711
  27. package/dist/system/patterns/UiDatePicker/UiDatePicker.vue.d.ts +1 -1
  28. package/dist/system/story-sidebar-icons.json +6 -0
  29. package/package.json +5 -5
  30. package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +1 -1
  31. package/src/components/UiBreadcrumb/UiBreadcrumb.stories.ts +1419 -0
  32. package/src/components/UiBreadcrumb/UiBreadcrumb.vue +19 -0
  33. package/src/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue +22 -0
  34. package/src/components/UiBreadcrumb/UiBreadcrumbItem.vue +19 -0
  35. package/src/components/UiBreadcrumb/UiBreadcrumbLink.vue +26 -0
  36. package/src/components/UiBreadcrumb/UiBreadcrumbList.vue +19 -0
  37. package/src/components/UiBreadcrumb/UiBreadcrumbPage.vue +19 -0
  38. package/src/components/UiBreadcrumb/UiBreadcrumbSeparator.vue +22 -0
  39. package/src/components/UiBreadcrumb/index.ts +8 -0
  40. package/src/components/UiBreadcrumb/types.ts +28 -0
  41. package/src/components/UiCheckbox/UiCheckbox.stories.ts +625 -74
  42. package/src/components/UiDropdownMenu/UiDropdownMenuTrigger.vue +2 -0
  43. package/src/components/UiField/UiField.stories.ts +1 -1
  44. package/src/components/UiField/types.ts +2 -1
  45. package/src/components/UiListbox/UiListbox.stories.ts +1283 -30
  46. package/src/components/UiListbox/__tests__/UiListbox.test.ts +70 -35
  47. package/src/components/UiRadioGroup/UiRadioGroup.stories.ts +995 -0
  48. package/src/components/UiSelect/UiSelect.stories.ts +1 -1
  49. package/src/components/UiSwitch/UiSwitch.stories.ts +706 -9
  50. package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +2 -6
  51. package/src/components/UiToggle/UiToggle.stories.ts +961 -130
  52. package/src/components/UiToggle/UiToggle.vue +8 -1
  53. package/src/components/UiToggle/__tests__/UiToggle.test.ts +55 -65
  54. package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +1060 -173
  55. package/src/components/UiToggleGroup/UiToggleGroup.vue +4 -2
  56. package/src/components/UiToggleGroup/__tests__/UiToggleGroup.test.ts +117 -325
  57. package/src/components/UiToggleGroup/types.ts +0 -5
  58. package/src/components/core/breadcrumb/Breadcrumb.vue +13 -0
  59. package/src/components/core/breadcrumb/BreadcrumbEllipsis.vue +23 -0
  60. package/src/components/core/breadcrumb/BreadcrumbItem.vue +17 -0
  61. package/src/components/core/breadcrumb/BreadcrumbLink.vue +29 -0
  62. package/src/components/core/breadcrumb/BreadcrumbList.vue +22 -0
  63. package/src/components/core/breadcrumb/BreadcrumbPage.vue +20 -0
  64. package/src/components/core/breadcrumb/BreadcrumbSeparator.vue +22 -0
  65. package/src/components/core/breadcrumb/index.ts +7 -0
  66. package/src/components/core/field/FieldDescription.vue +1 -1
  67. package/src/components/core/field/FieldLabel.vue +3 -2
  68. package/src/components/core/field/FieldTitle.vue +1 -1
  69. package/src/components/core/listbox/Listbox.vue +14 -2
  70. package/src/components/core/listbox/ListboxContent.vue +61 -3
  71. package/src/components/core/listbox/ListboxItem.vue +6 -1
  72. package/src/components/core/listbox/context.ts +34 -0
  73. package/src/components/core/toggle/Toggle.vue +15 -1
  74. package/src/components/core/toggle/index.ts +2 -2
  75. package/src/components/core/toggle-group/ToggleGroup.vue +1 -1
  76. package/src/components/core/toggle-group/index.ts +5 -2
  77. package/src/components/index.ts +1 -0
  78. package/src/components/UiCheckbox/__tests__/UiCheckbox.test.ts +0 -14
  79. package/src/components/UiRadioGroup/__tests__/UiRadioGroup.test.ts +0 -412
@@ -1,4 +1,5 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { expect, userEvent, waitFor, within } from 'storybook/test';
2
3
  import { ref, computed } from 'vue';
3
4
  import { useFilter } from 'reka-ui';
4
5
  import UiListbox from './UiListbox.vue';
@@ -29,7 +30,8 @@ const meta: Meta<typeof UiListbox> = {
29
30
  },
30
31
  highlightOnHover: {
31
32
  control: 'boolean',
32
- description: 'Highlight items on hover',
33
+ description:
34
+ 'Highlight items on hover. Only applies when a `UiListboxFilter` holds the focus.',
33
35
  },
34
36
  },
35
37
  args: {
@@ -70,7 +72,7 @@ const fruits = [
70
72
 
71
73
  <template>
72
74
  <UiListbox v-model="selected" class="w-[200px] p-1">
73
- <UiListboxContent>
75
+ <UiListboxContent aria-label="Fruits">
74
76
  <UiListboxItem
75
77
  v-for="fruit in fruits"
76
78
  :key="fruit.value"
@@ -84,7 +86,8 @@ const fruits = [
84
86
  </template>`;
85
87
 
86
88
  /**
87
- * Default single-selection listbox.
89
+ * Default single-selection listbox. Selecting another option replaces the
90
+ * current one; selecting the active option again clears the selection.
88
91
  */
89
92
  export const Default: Story = {
90
93
  render: (args) => ({
@@ -105,7 +108,7 @@ export const Default: Story = {
105
108
  :disabled="args.disabled"
106
109
  :highlight-on-hover="args.highlightOnHover"
107
110
  >
108
- <UiListboxContent>
111
+ <UiListboxContent aria-label="Fruits">
109
112
  <UiListboxItem
110
113
  v-for="fruit in fruits"
111
114
  :key="fruit.value"
@@ -146,7 +149,7 @@ const fruits = [
146
149
 
147
150
  <template>
148
151
  <UiListbox v-model="selected" multiple class="w-[200px] p-1">
149
- <UiListboxContent>
152
+ <UiListboxContent aria-label="Fruits">
150
153
  <UiListboxItem
151
154
  v-for="fruit in fruits"
152
155
  :key="fruit.value"
@@ -160,7 +163,9 @@ const fruits = [
160
163
  </template>`;
161
164
 
162
165
  /**
163
- * Multiple selection listbox.
166
+ * Multiple selection listbox. Each option toggles independently, and the list
167
+ * reports `aria-multiselectable` so assistive technology announces that more
168
+ * than one option can be chosen.
164
169
  */
165
170
  export const Multiple: Story = {
166
171
  render: () => ({
@@ -176,7 +181,7 @@ export const Multiple: Story = {
176
181
  },
177
182
  template: `
178
183
  <UiListbox v-model="selected" multiple class="w-[200px] p-1">
179
- <UiListboxContent>
184
+ <UiListboxContent aria-label="Fruits">
180
185
  <UiListboxItem
181
186
  v-for="fruit in fruits"
182
187
  :key="fruit.value"
@@ -227,8 +232,13 @@ const filteredFruits = computed(() =>
227
232
 
228
233
  <template>
229
234
  <UiListbox v-model="selected" class="w-[200px]">
230
- <UiListboxFilter v-model="searchTerm" placeholder="Search fruits..." class="border-b" />
231
- <UiListboxContent class="p-1">
235
+ <UiListboxFilter
236
+ v-model="searchTerm"
237
+ placeholder="Search fruits..."
238
+ aria-label="Search fruits"
239
+ class="border-b"
240
+ />
241
+ <UiListboxContent aria-label="Fruits" class="p-1">
232
242
  <UiListboxItem
233
243
  v-for="fruit in filteredFruits"
234
244
  :key="fruit.value"
@@ -238,11 +248,21 @@ const filteredFruits = computed(() =>
238
248
  <UiListboxItemIndicator />
239
249
  </UiListboxItem>
240
250
  </UiListboxContent>
251
+ <p
252
+ v-if="filteredFruits.length === 0"
253
+ role="status"
254
+ class="px-3 py-6 text-center label-14-regular text-content-on-surface-muted"
255
+ >
256
+ No fruits match "{{ searchTerm }}"
257
+ </p>
241
258
  </UiListbox>
242
259
  </template>`;
243
260
 
244
261
  /**
245
- * Listbox with filter/search functionality.
262
+ * Listbox with filter/search functionality. The filter owns the keyboard
263
+ * focus, so the arrow keys move a virtual highlight (`aria-activedescendant`)
264
+ * instead of moving focus. Always render a no-results message so the empty
265
+ * outcome is announced rather than leaving a silent, blank list.
246
266
  */
247
267
  export const WithFilter: Story = {
248
268
  render: () => ({
@@ -268,8 +288,13 @@ export const WithFilter: Story = {
268
288
  },
269
289
  template: `
270
290
  <UiListbox v-model="selected" class="w-[200px]">
271
- <UiListboxFilter v-model="searchTerm" placeholder="Search fruits..." class="border-b" />
272
- <UiListboxContent class="p-1">
291
+ <UiListboxFilter
292
+ v-model="searchTerm"
293
+ placeholder="Search fruits..."
294
+ aria-label="Search fruits"
295
+ class="border-b"
296
+ />
297
+ <UiListboxContent aria-label="Fruits" class="p-1">
273
298
  <UiListboxItem
274
299
  v-for="fruit in filteredFruits"
275
300
  :key="fruit.value"
@@ -279,6 +304,13 @@ export const WithFilter: Story = {
279
304
  <UiListboxItemIndicator />
280
305
  </UiListboxItem>
281
306
  </UiListboxContent>
307
+ <p
308
+ v-if="filteredFruits.length === 0"
309
+ role="status"
310
+ class="px-3 py-6 text-center label-14-regular text-content-on-surface-muted"
311
+ >
312
+ No fruits match "{{ searchTerm }}"
313
+ </p>
282
314
  </UiListbox>
283
315
  `,
284
316
  }),
@@ -307,7 +339,7 @@ const selected = ref('apple')
307
339
 
308
340
  <template>
309
341
  <UiListbox v-model="selected" class="w-[200px] p-1">
310
- <UiListboxContent>
342
+ <UiListboxContent aria-label="Ingredients">
311
343
  <UiListboxGroup>
312
344
  <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
313
345
  <UiListboxItem value="apple">
@@ -335,7 +367,9 @@ const selected = ref('apple')
335
367
  </template>`;
336
368
 
337
369
  /**
338
- * Listbox with grouped items.
370
+ * Listbox with grouped items. `UiListboxGroup` wires its `UiListboxGroupLabel`
371
+ * up as the group's accessible name, so screen readers announce "Fruits" and
372
+ * "Vegetables" while moving through the options.
339
373
  */
340
374
  export const WithGroups: Story = {
341
375
  render: () => ({
@@ -353,7 +387,7 @@ export const WithGroups: Story = {
353
387
  },
354
388
  template: `
355
389
  <UiListbox v-model="selected" class="w-[200px] p-1">
356
- <UiListboxContent>
390
+ <UiListboxContent aria-label="Ingredients">
357
391
  <UiListboxGroup>
358
392
  <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
359
393
  <UiListboxItem value="apple">
@@ -389,6 +423,179 @@ export const WithGroups: Story = {
389
423
  },
390
424
  };
391
425
 
426
+ const scrollableTemplateSource = `<script setup lang="ts">
427
+ import { ref } from 'vue'
428
+ import {
429
+ UiListbox,
430
+ UiListboxContent,
431
+ UiListboxItem,
432
+ UiListboxItemIndicator
433
+ } from '@aleph-alpha/ui-library'
434
+
435
+ const selected = ref('option-1')
436
+ const options = Array.from({ length: 30 }, (_, index) => ({
437
+ value: \`option-\${index + 1}\`,
438
+ label: \`Option \${index + 1}\`,
439
+ }))
440
+ </script>
441
+
442
+ <template>
443
+ <UiListbox v-model="selected" class="w-[220px] p-1">
444
+ <UiListboxContent aria-label="Options">
445
+ <UiListboxItem
446
+ v-for="option in options"
447
+ :key="option.value"
448
+ :value="option.value"
449
+ >
450
+ <span>{{ option.label }}</span>
451
+ <UiListboxItemIndicator />
452
+ </UiListboxItem>
453
+ </UiListboxContent>
454
+ </UiListbox>
455
+ </template>`;
456
+
457
+ /**
458
+ * `UiListboxContent` caps its height and scrolls once the options overflow, so
459
+ * a long list never pushes the surrounding layout. Keyboard navigation scrolls
460
+ * the highlighted option into view automatically.
461
+ */
462
+ export const Scrollable: Story = {
463
+ render: () => ({
464
+ components: {
465
+ UiListbox,
466
+ UiListboxContent,
467
+ UiListboxItem,
468
+ UiListboxItemIndicator,
469
+ },
470
+ setup() {
471
+ const selected = ref('option-1');
472
+ const options = Array.from({ length: 30 }, (_, index) => ({
473
+ value: `option-${index + 1}`,
474
+ label: `Option ${index + 1}`,
475
+ }));
476
+ return { selected, options };
477
+ },
478
+ template: `
479
+ <UiListbox v-model="selected" class="w-[220px] p-1">
480
+ <UiListboxContent aria-label="Options">
481
+ <UiListboxItem
482
+ v-for="option in options"
483
+ :key="option.value"
484
+ :value="option.value"
485
+ >
486
+ <span>{{ option.label }}</span>
487
+ <UiListboxItemIndicator />
488
+ </UiListboxItem>
489
+ </UiListboxContent>
490
+ </UiListbox>
491
+ `,
492
+ }),
493
+ parameters: {
494
+ docs: {
495
+ source: {
496
+ code: scrollableTemplateSource,
497
+ },
498
+ },
499
+ },
500
+ play: async ({ canvasElement, step }) => {
501
+ // Scenario (List content): a long option list stays inside a fixed-height
502
+ // scroll container, and scrolling reveals the options further down.
503
+ const canvas = within(canvasElement);
504
+ const list = canvas.getByRole('listbox', { name: 'Options' });
505
+
506
+ await step(
507
+ 'interaction: the list keeps every option reachable in one scroll container',
508
+ async () => {
509
+ await expect(canvas.getAllByRole('option')).toHaveLength(30);
510
+ await expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
511
+ },
512
+ );
513
+
514
+ await step(
515
+ 'interaction: scrolling moves down the list without clipping options',
516
+ async () => {
517
+ list.scrollTop = list.scrollHeight;
518
+ await waitFor(async () => {
519
+ await expect(list.scrollTop).toBeGreaterThan(0);
520
+ });
521
+ await expect(
522
+ canvas.getByRole('option', { name: 'Option 30' }),
523
+ ).toBeVisible();
524
+ // Restore the resting state so the visual snapshot stays stable.
525
+ list.scrollTop = 0;
526
+ },
527
+ );
528
+ },
529
+ };
530
+
531
+ const emptyStateTemplateSource = `<script setup lang="ts">
532
+ import { ref } from 'vue'
533
+ import { UiListbox, UiListboxContent } from '@aleph-alpha/ui-library'
534
+
535
+ const selected = ref<string>()
536
+ const savedFilters: { value: string; label: string }[] = []
537
+ </script>
538
+
539
+ <template>
540
+ <UiListbox v-model="selected" class="w-[220px] p-1">
541
+ <UiListboxContent aria-label="Saved filters" />
542
+ <p class="px-3 py-6 text-center label-14-regular text-content-on-surface-muted">
543
+ No saved filters yet
544
+ </p>
545
+ </UiListbox>
546
+ </template>`;
547
+
548
+ /**
549
+ * An empty listbox still exposes the list to assistive technology, so pair it
550
+ * with a short message that explains why there is nothing to choose from.
551
+ */
552
+ export const EmptyState: Story = {
553
+ render: () => ({
554
+ components: { UiListbox, UiListboxContent },
555
+ setup() {
556
+ const selected = ref<string>();
557
+ return { selected };
558
+ },
559
+ template: `
560
+ <UiListbox v-model="selected" class="w-[220px] p-1">
561
+ <UiListboxContent aria-label="Saved filters" />
562
+ <p class="px-3 py-6 text-center label-14-regular text-content-on-surface-muted">
563
+ No saved filters yet
564
+ </p>
565
+ </UiListbox>
566
+ `,
567
+ }),
568
+ parameters: {
569
+ docs: {
570
+ source: {
571
+ code: emptyStateTemplateSource,
572
+ },
573
+ },
574
+ },
575
+ play: async ({ canvasElement, step }) => {
576
+ // Scenario (List content): with nothing to choose from, the list is still
577
+ // exposed but empty, and the reason is spelled out in visible text.
578
+ const canvas = within(canvasElement);
579
+
580
+ await step(
581
+ 'interaction: the empty list exposes no selectable options',
582
+ async () => {
583
+ await expect(
584
+ canvas.getByRole('listbox', { name: 'Saved filters' }),
585
+ ).toBeVisible();
586
+ await expect(canvas.queryAllByRole('option')).toHaveLength(0);
587
+ },
588
+ );
589
+
590
+ await step(
591
+ 'interaction: the empty state explains why the list is empty',
592
+ async () => {
593
+ await expect(canvas.getByText('No saved filters yet')).toBeVisible();
594
+ },
595
+ );
596
+ },
597
+ };
598
+
392
599
  const disabledTemplateSource = `<script setup lang="ts">
393
600
  import { ref } from 'vue'
394
601
  import {
@@ -408,7 +615,7 @@ const fruits = [
408
615
 
409
616
  <template>
410
617
  <UiListbox v-model="selected" disabled class="w-[200px] p-1">
411
- <UiListboxContent>
618
+ <UiListboxContent aria-label="Fruits">
412
619
  <UiListboxItem
413
620
  v-for="fruit in fruits"
414
621
  :key="fruit.value"
@@ -422,7 +629,8 @@ const fruits = [
422
629
  </template>`;
423
630
 
424
631
  /**
425
- * Disabled listbox.
632
+ * Disabled listbox. The list and every option report `aria-disabled`, so the
633
+ * state is announced instead of being conveyed by dimming alone.
426
634
  */
427
635
  export const Disabled: Story = {
428
636
  args: {
@@ -441,7 +649,7 @@ export const Disabled: Story = {
441
649
  },
442
650
  template: `
443
651
  <UiListbox v-model="selected" disabled class="w-[200px] p-1">
444
- <UiListboxContent>
652
+ <UiListboxContent aria-label="Fruits">
445
653
  <UiListboxItem
446
654
  v-for="fruit in fruits"
447
655
  :key="fruit.value"
@@ -461,30 +669,76 @@ export const Disabled: Story = {
461
669
  },
462
670
  },
463
671
  },
672
+ play: async ({ canvasElement, step }) => {
673
+ // Scenario (Listbox): the whole list is turned off — the state is exposed
674
+ // to assistive technology and clicks cannot change the selection.
675
+ const canvas = within(canvasElement);
676
+ const list = canvas.getByRole('listbox', { name: 'Fruits' });
677
+
678
+ await step(
679
+ 'interaction: the list reports its disabled state (WCAG 4.1.2)',
680
+ async () => {
681
+ await expect(list).toHaveAttribute('aria-disabled', 'true');
682
+ for (const option of canvas.getAllByRole('option')) {
683
+ await expect(option).toHaveAttribute('aria-disabled', 'true');
684
+ }
685
+ },
686
+ );
687
+
688
+ await step(
689
+ 'interaction: clicking an option in a disabled list does not change the selection',
690
+ async () => {
691
+ const banana = canvas.getByRole('option', { name: 'Banana' });
692
+ // Disabled options render with pointer-events: none, so bypass the
693
+ // check and assert the click is still a no-op.
694
+ await userEvent.click(banana, { pointerEventsCheck: 0 });
695
+ await expect(banana).toHaveAttribute('aria-selected', 'false');
696
+ await expect(
697
+ canvas.getByRole('option', { name: 'Apple' }),
698
+ ).toHaveAttribute('aria-selected', 'true');
699
+ },
700
+ );
701
+ },
464
702
  };
465
703
 
466
704
  const highlightOnHoverTemplateSource = `<script setup lang="ts">
467
- import { ref } from 'vue'
705
+ import { ref, computed } from 'vue'
706
+ import { useFilter } from 'reka-ui'
468
707
  import {
469
708
  UiListbox,
470
709
  UiListboxContent,
710
+ UiListboxFilter,
471
711
  UiListboxItem,
472
712
  UiListboxItemIndicator
473
713
  } from '@aleph-alpha/ui-library'
474
714
 
475
715
  const selected = ref('apple')
716
+ const searchTerm = ref('')
476
717
  const fruits = [
477
718
  { value: 'apple', label: 'Apple' },
478
719
  { value: 'banana', label: 'Banana' },
479
720
  { value: 'cherry', label: 'Cherry' },
480
721
  ]
722
+
723
+ const { contains } = useFilter({ sensitivity: 'base' })
724
+ const filteredFruits = computed(() =>
725
+ searchTerm.value === ''
726
+ ? fruits
727
+ : fruits.filter(fruit => contains(fruit.label, searchTerm.value))
728
+ )
481
729
  </script>
482
730
 
483
731
  <template>
484
- <UiListbox v-model="selected" highlight-on-hover class="w-[200px] p-1">
485
- <UiListboxContent>
732
+ <UiListbox v-model="selected" highlight-on-hover class="w-[200px]">
733
+ <UiListboxFilter
734
+ v-model="searchTerm"
735
+ placeholder="Search fruits..."
736
+ aria-label="Search fruits"
737
+ class="border-b"
738
+ />
739
+ <UiListboxContent aria-label="Fruits" class="p-1">
486
740
  <UiListboxItem
487
- v-for="fruit in fruits"
741
+ v-for="fruit in filteredFruits"
488
742
  :key="fruit.value"
489
743
  :value="fruit.value"
490
744
  >
@@ -496,7 +750,10 @@ const fruits = [
496
750
  </template>`;
497
751
 
498
752
  /**
499
- * Listbox with highlight on hover enabled.
753
+ * `highlightOnHover` keeps the pointer and the keyboard on the same active
754
+ * option. It only applies while a `UiListboxFilter` holds the focus — without
755
+ * a filter the options are focusable themselves and the browser's own focus
756
+ * already tracks the active option.
500
757
  */
501
758
  export const HighlightOnHover: Story = {
502
759
  args: {
@@ -506,18 +763,34 @@ export const HighlightOnHover: Story = {
506
763
  components: {
507
764
  UiListbox,
508
765
  UiListboxContent,
766
+ UiListboxFilter,
509
767
  UiListboxItem,
510
768
  UiListboxItemIndicator,
511
769
  },
512
770
  setup() {
513
771
  const selected = ref('apple');
514
- return { selected, fruits };
772
+ const searchTerm = ref('');
773
+ const { contains } = useFilter({ sensitivity: 'base' });
774
+
775
+ const filteredFruits = computed(() =>
776
+ searchTerm.value === ''
777
+ ? fruits
778
+ : fruits.filter((fruit) => contains(fruit.label, searchTerm.value)),
779
+ );
780
+
781
+ return { selected, searchTerm, filteredFruits };
515
782
  },
516
783
  template: `
517
- <UiListbox v-model="selected" highlight-on-hover class="w-[200px] p-1">
518
- <UiListboxContent>
784
+ <UiListbox v-model="selected" highlight-on-hover class="w-[200px]">
785
+ <UiListboxFilter
786
+ v-model="searchTerm"
787
+ placeholder="Search fruits..."
788
+ aria-label="Search fruits"
789
+ class="border-b"
790
+ />
791
+ <UiListboxContent aria-label="Fruits" class="p-1">
519
792
  <UiListboxItem
520
- v-for="fruit in fruits"
793
+ v-for="fruit in filteredFruits"
521
794
  :key="fruit.value"
522
795
  :value="fruit.value"
523
796
  >
@@ -551,7 +824,7 @@ const selected = ref('apple')
551
824
 
552
825
  <template>
553
826
  <UiListbox v-model="selected" class="w-[200px] p-1">
554
- <UiListboxContent>
827
+ <UiListboxContent aria-label="Fruits">
555
828
  <UiListboxItem value="apple">
556
829
  <span>Apple</span>
557
830
  <UiListboxItemIndicator />
@@ -569,7 +842,8 @@ const selected = ref('apple')
569
842
  </template>`;
570
843
 
571
844
  /**
572
- * Listbox with some disabled items.
845
+ * Listbox with some disabled items. Keep unavailable options in the list so
846
+ * the full set of choices stays visible, and say why in the label.
573
847
  */
574
848
  export const WithDisabledItems: Story = {
575
849
  render: () => ({
@@ -585,7 +859,7 @@ export const WithDisabledItems: Story = {
585
859
  },
586
860
  template: `
587
861
  <UiListbox v-model="selected" class="w-[200px] p-1">
588
- <UiListboxContent>
862
+ <UiListboxContent aria-label="Fruits">
589
863
  <UiListboxItem value="apple">
590
864
  <span>Apple</span>
591
865
  <UiListboxItemIndicator />
@@ -609,4 +883,983 @@ export const WithDisabledItems: Story = {
609
883
  },
610
884
  },
611
885
  },
886
+ play: async ({ canvasElement, step }) => {
887
+ // Scenario (Option): a single unavailable option stays listed and
888
+ // announced, but cannot be chosen.
889
+ const canvas = within(canvasElement);
890
+ const banana = canvas.getByRole('option', { name: 'Banana (unavailable)' });
891
+
892
+ await step(
893
+ 'interaction: the unavailable option stays in the list and reports its state (WCAG 4.1.2)',
894
+ async () => {
895
+ await expect(banana).toBeVisible();
896
+ await expect(banana).toHaveAttribute('aria-disabled', 'true');
897
+ await expect(
898
+ canvas.getByRole('option', { name: 'Cherry' }),
899
+ ).not.toHaveAttribute('aria-disabled');
900
+ },
901
+ );
902
+
903
+ await step(
904
+ 'interaction: a disabled option cannot be selected',
905
+ async () => {
906
+ // Disabled options render with pointer-events: none, so bypass the
907
+ // check and assert the click is still a no-op.
908
+ await userEvent.click(banana, { pointerEventsCheck: 0 });
909
+ await expect(banana).toHaveAttribute('aria-selected', 'false');
910
+ await expect(
911
+ canvas.getByRole('option', { name: 'Apple' }),
912
+ ).toHaveAttribute('aria-selected', 'true');
913
+ },
914
+ );
915
+ },
916
+ };
917
+
918
+ /**
919
+ * Accessibility guidance for listboxes: a meaningful list name, labelled
920
+ * groups, an announced selection, and a spoken empty state — shown side by
921
+ * side in the default and accessible themes.
922
+ */
923
+ export const Accessibility: Story = {
924
+ tags: ['!autodocs'],
925
+ parameters: {
926
+ a11y: { test: 'off' },
927
+ chromatic: { disableSnapshot: true },
928
+ docs: {
929
+ canvas: {
930
+ sourceState: 'none',
931
+ },
932
+ },
933
+ },
934
+ render: () => ({
935
+ components: {
936
+ UiListbox,
937
+ UiListboxContent,
938
+ UiListboxFilter,
939
+ UiListboxItem,
940
+ UiListboxItemIndicator,
941
+ UiListboxGroup,
942
+ UiListboxGroupLabel,
943
+ },
944
+ setup() {
945
+ const defaultValue = ref('apple');
946
+ const accessibleValue = ref('apple');
947
+ const defaultTerm = ref('');
948
+ const accessibleTerm = ref('');
949
+ return {
950
+ defaultValue,
951
+ accessibleValue,
952
+ defaultTerm,
953
+ accessibleTerm,
954
+ };
955
+ },
956
+ template: `
957
+ <section class="flex flex-col gap-6 p-4">
958
+ <div>
959
+ <h2 class="heading-m font-bold text-content-on-surface-primary">
960
+ Accessibility
961
+ </h2>
962
+ </div>
963
+
964
+ <div class="flex flex-col gap-6">
965
+ <div class="flex flex-col gap-2">
966
+ <h3 class="body-16-medium text-content-on-surface-primary">
967
+ Give the list a meaningful name
968
+ </h3>
969
+ <p class="body-16-regular text-content-on-surface-primary">
970
+ <code>UiListboxContent</code> falls back to the generic name
971
+ "Options". Set <code>aria-label</code> (or point
972
+ <code>aria-labelledby</code> at a visible heading) so the list is
973
+ announced as what it actually contains.
974
+ </p>
975
+ </div>
976
+
977
+ <div class="flex flex-col gap-2">
978
+ <h3 class="body-16-medium text-content-on-surface-primary">
979
+ Label the filter input
980
+ </h3>
981
+ <p class="body-16-regular text-content-on-surface-primary">
982
+ The placeholder disappears as soon as the user types, so it is not
983
+ a label. Give <code>UiListboxFilter</code> its own
984
+ <code>aria-label</code>.
985
+ </p>
986
+ </div>
987
+
988
+ <div class="flex flex-col gap-2">
989
+ <h3 class="body-16-medium text-content-on-surface-primary">
990
+ Group related options and label the groups
991
+ </h3>
992
+ <p class="body-16-regular text-content-on-surface-primary">
993
+ Long flat lists are hard to scan and to listen to. Wrap related
994
+ options in <code>UiListboxGroup</code> with a
995
+ <code>UiListboxGroupLabel</code> so each group is announced with
996
+ its name.
997
+ </p>
998
+ </div>
999
+
1000
+ <div class="flex flex-col gap-2">
1001
+ <h3 class="body-16-medium text-content-on-surface-primary">
1002
+ Do not rely on the indicator alone
1003
+ </h3>
1004
+ <p class="body-16-regular text-content-on-surface-primary">
1005
+ <code>UiListboxItemIndicator</code> is decorative and hidden from
1006
+ assistive technology. The selected state is carried by
1007
+ <code>aria-selected</code> on the option, which the component sets
1008
+ for you — keep the indicator as the visible counterpart.
1009
+ </p>
1010
+ </div>
1011
+
1012
+ <div class="flex flex-col gap-2">
1013
+ <h3 class="body-16-medium text-content-on-surface-primary">
1014
+ Announce the empty and no-results states
1015
+ </h3>
1016
+ <p class="body-16-regular text-content-on-surface-primary">
1017
+ A list that filters down to nothing is silent. Render a short
1018
+ message in a <code>role="status"</code> region so the outcome is
1019
+ spoken instead of leaving a blank panel.
1020
+ </p>
1021
+ </div>
1022
+
1023
+ <div class="flex flex-col gap-2">
1024
+ <h3 class="body-16-medium text-content-on-surface-primary">
1025
+ Enable accessibility theme
1026
+ </h3>
1027
+ <p class="body-16-regular text-content-on-surface-primary">
1028
+ When accessibility is required, enable our accessibility theme.
1029
+ This increases contrast and makes sure there are no color
1030
+ contrast issues.
1031
+ </p>
1032
+ </div>
1033
+ </div>
1034
+
1035
+ <div class="grid gap-4 md:grid-cols-2">
1036
+ <div class="flex flex-col gap-3">
1037
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
1038
+ Default
1039
+ </h3>
1040
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
1041
+ <UiListbox v-model="defaultValue" class="w-[200px]">
1042
+ <UiListboxFilter
1043
+ v-model="defaultTerm"
1044
+ placeholder="Search food..."
1045
+ aria-label="Search food"
1046
+ class="border-b"
1047
+ />
1048
+ <UiListboxContent aria-label="Food" class="p-1">
1049
+ <UiListboxGroup>
1050
+ <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
1051
+ <UiListboxItem value="apple">
1052
+ <span>Apple</span>
1053
+ <UiListboxItemIndicator />
1054
+ </UiListboxItem>
1055
+ <UiListboxItem value="mango" disabled>
1056
+ <span>Mango (sold out)</span>
1057
+ <UiListboxItemIndicator />
1058
+ </UiListboxItem>
1059
+ </UiListboxGroup>
1060
+ <UiListboxGroup>
1061
+ <UiListboxGroupLabel>Vegetables</UiListboxGroupLabel>
1062
+ <UiListboxItem value="carrot">
1063
+ <span>Carrot</span>
1064
+ <UiListboxItemIndicator />
1065
+ </UiListboxItem>
1066
+ </UiListboxGroup>
1067
+ </UiListboxContent>
1068
+ </UiListbox>
1069
+ </div>
1070
+ </div>
1071
+
1072
+ <div class="accessible-theme flex flex-col gap-3">
1073
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
1074
+ Accessible
1075
+ </h3>
1076
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
1077
+ <UiListbox v-model="accessibleValue" class="w-[200px]">
1078
+ <UiListboxFilter
1079
+ v-model="accessibleTerm"
1080
+ placeholder="Search food..."
1081
+ aria-label="Search food"
1082
+ class="border-b"
1083
+ />
1084
+ <UiListboxContent aria-label="Food" class="p-1">
1085
+ <UiListboxGroup>
1086
+ <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
1087
+ <UiListboxItem value="apple">
1088
+ <span>Apple</span>
1089
+ <UiListboxItemIndicator />
1090
+ </UiListboxItem>
1091
+ <UiListboxItem value="mango" disabled>
1092
+ <span>Mango (sold out)</span>
1093
+ <UiListboxItemIndicator />
1094
+ </UiListboxItem>
1095
+ </UiListboxGroup>
1096
+ <UiListboxGroup>
1097
+ <UiListboxGroupLabel>Vegetables</UiListboxGroupLabel>
1098
+ <UiListboxItem value="carrot">
1099
+ <span>Carrot</span>
1100
+ <UiListboxItemIndicator />
1101
+ </UiListboxItem>
1102
+ </UiListboxGroup>
1103
+ </UiListboxContent>
1104
+ </UiListbox>
1105
+ </div>
1106
+ </div>
1107
+ </div>
1108
+ </section>
1109
+ `,
1110
+ }),
1111
+ };
1112
+
1113
+ // Test-only stories drive the test-runner (interactive/a11y checks) but are
1114
+ // hidden from the Storybook sidebar and docs via the `!dev` / `!autodocs`
1115
+ // tags. They keep the implicit `test` tag, so they still run in CI and appear
1116
+ // in the automated report under their `test - …` name. `name`/`tags` must be
1117
+ // literal properties so Storybook's static CSF indexer can read them.
1118
+
1119
+ // Shared render for the keyboard scenario: no filter, so the options own the
1120
+ // focus via a roving tabindex. The leading button is the anchor the test tabs
1121
+ // from, since the list highlights its first option as soon as it mounts.
1122
+ function renderKeyboardListbox() {
1123
+ return {
1124
+ components: {
1125
+ UiListbox,
1126
+ UiListboxContent,
1127
+ UiListboxItem,
1128
+ UiListboxItemIndicator,
1129
+ UiListboxGroup,
1130
+ UiListboxGroupLabel,
1131
+ },
1132
+ setup() {
1133
+ const selected = ref<string>();
1134
+ return { selected };
1135
+ },
1136
+ template: `
1137
+ <div class="flex flex-col items-start gap-3">
1138
+ <button type="button" class="rounded-md border border-border-surface-default px-3 py-1.5 label-14-regular">
1139
+ Before the list
1140
+ </button>
1141
+ <UiListbox v-model="selected" class="w-[220px] p-1">
1142
+ <UiListboxContent aria-label="Food">
1143
+ <UiListboxGroup>
1144
+ <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
1145
+ <UiListboxItem value="apple"><span>Apple</span><UiListboxItemIndicator /></UiListboxItem>
1146
+ <UiListboxItem value="mango" disabled><span>Mango (sold out)</span><UiListboxItemIndicator /></UiListboxItem>
1147
+ <UiListboxItem value="banana"><span>Banana</span><UiListboxItemIndicator /></UiListboxItem>
1148
+ </UiListboxGroup>
1149
+ <UiListboxGroup>
1150
+ <UiListboxGroupLabel>Vegetables</UiListboxGroupLabel>
1151
+ <UiListboxItem value="carrot"><span>Carrot</span><UiListboxItemIndicator /></UiListboxItem>
1152
+ </UiListboxGroup>
1153
+ </UiListboxContent>
1154
+ </UiListbox>
1155
+ </div>
1156
+ `,
1157
+ };
1158
+ }
1159
+
1160
+ // Shared render for the filter scenarios: the filter owns the focus, the
1161
+ // options are highlighted virtually, and an empty result set is announced.
1162
+ function renderFilterListbox() {
1163
+ return {
1164
+ components: {
1165
+ UiListbox,
1166
+ UiListboxContent,
1167
+ UiListboxFilter,
1168
+ UiListboxItem,
1169
+ UiListboxItemIndicator,
1170
+ UiListboxGroup,
1171
+ UiListboxGroupLabel,
1172
+ },
1173
+ setup() {
1174
+ const selected = ref<string>();
1175
+ const searchTerm = ref('');
1176
+ const { contains } = useFilter({ sensitivity: 'base' });
1177
+ const food = [
1178
+ { value: 'apple', label: 'Apple', group: 'Fruits' },
1179
+ { value: 'mango', label: 'Mango (sold out)', group: 'Fruits' },
1180
+ { value: 'banana', label: 'Banana', group: 'Fruits' },
1181
+ { value: 'carrot', label: 'Carrot', group: 'Vegetables' },
1182
+ ];
1183
+ const matches = computed(() =>
1184
+ searchTerm.value === ''
1185
+ ? food
1186
+ : food.filter((item) => contains(item.label, searchTerm.value)),
1187
+ );
1188
+ const fruitMatches = computed(() =>
1189
+ matches.value.filter((item) => item.group === 'Fruits'),
1190
+ );
1191
+ const vegetableMatches = computed(() =>
1192
+ matches.value.filter((item) => item.group === 'Vegetables'),
1193
+ );
1194
+ return { selected, searchTerm, matches, fruitMatches, vegetableMatches };
1195
+ },
1196
+ template: `
1197
+ <UiListbox v-model="selected" highlight-on-hover class="w-[220px]">
1198
+ <UiListboxFilter
1199
+ v-model="searchTerm"
1200
+ placeholder="Search food..."
1201
+ aria-label="Search food"
1202
+ class="border-b"
1203
+ />
1204
+ <UiListboxContent aria-label="Food" class="p-1">
1205
+ <UiListboxGroup v-if="fruitMatches.length">
1206
+ <UiListboxGroupLabel>Fruits</UiListboxGroupLabel>
1207
+ <UiListboxItem
1208
+ v-for="item in fruitMatches"
1209
+ :key="item.value"
1210
+ :value="item.value"
1211
+ :disabled="item.value === 'mango'"
1212
+ >
1213
+ <span>{{ item.label }}</span>
1214
+ <UiListboxItemIndicator />
1215
+ </UiListboxItem>
1216
+ </UiListboxGroup>
1217
+ <UiListboxGroup v-if="vegetableMatches.length">
1218
+ <UiListboxGroupLabel>Vegetables</UiListboxGroupLabel>
1219
+ <UiListboxItem
1220
+ v-for="item in vegetableMatches"
1221
+ :key="item.value"
1222
+ :value="item.value"
1223
+ >
1224
+ <span>{{ item.label }}</span>
1225
+ <UiListboxItemIndicator />
1226
+ </UiListboxItem>
1227
+ </UiListboxGroup>
1228
+ </UiListboxContent>
1229
+ <p
1230
+ v-if="matches.length === 0"
1231
+ role="status"
1232
+ class="px-3 py-6 text-center label-14-regular text-content-on-surface-muted"
1233
+ >
1234
+ No food matches "{{ searchTerm }}"
1235
+ </p>
1236
+ </UiListbox>
1237
+ `,
1238
+ };
1239
+ }
1240
+
1241
+ /**
1242
+ * Single selection: choosing an option marks it, marks it with the indicator,
1243
+ * replaces any earlier choice, and choosing it again clears the selection.
1244
+ */
1245
+ export const SingleSelection: Story = {
1246
+ name: 'test - Single Selection',
1247
+ tags: ['!dev', '!autodocs'],
1248
+ parameters: {
1249
+ // The play changes the selection and moves focus into the list.
1250
+ chromatic: { disableSnapshot: true },
1251
+ docs: {
1252
+ description: {
1253
+ story:
1254
+ 'Select an option, replace it with another, and clear it by selecting it again.',
1255
+ },
1256
+ },
1257
+ },
1258
+ render: () => ({
1259
+ components: {
1260
+ UiListbox,
1261
+ UiListboxContent,
1262
+ UiListboxItem,
1263
+ UiListboxItemIndicator,
1264
+ },
1265
+ setup() {
1266
+ const selected = ref('apple');
1267
+ return { selected, fruits };
1268
+ },
1269
+ template: `
1270
+ <UiListbox v-model="selected" class="w-[200px] p-1">
1271
+ <UiListboxContent aria-label="Fruits">
1272
+ <UiListboxItem
1273
+ v-for="fruit in fruits"
1274
+ :key="fruit.value"
1275
+ :value="fruit.value"
1276
+ >
1277
+ <span>{{ fruit.label }}</span>
1278
+ <UiListboxItemIndicator />
1279
+ </UiListboxItem>
1280
+ </UiListboxContent>
1281
+ </UiListbox>
1282
+ `,
1283
+ }),
1284
+ play: async ({ canvasElement, step }) => {
1285
+ // Scenario (Listbox / Option / Selected indicator): a pointer user picks a
1286
+ // different fruit, sees the indicator follow the selection, and clears it
1287
+ // again by re-selecting the same option.
1288
+ const canvas = within(canvasElement);
1289
+ const apple = canvas.getByRole('option', { name: 'Apple' });
1290
+ const banana = canvas.getByRole('option', { name: 'Banana' });
1291
+ const indicator = (option: HTMLElement) =>
1292
+ option.querySelector('[data-slot="listbox-item-indicator"]');
1293
+
1294
+ await step(
1295
+ 'interaction: only the selected option is marked as selected',
1296
+ async () => {
1297
+ await expect(apple).toHaveAttribute('aria-selected', 'true');
1298
+ await expect(banana).toHaveAttribute('aria-selected', 'false');
1299
+ },
1300
+ );
1301
+
1302
+ await step(
1303
+ 'interaction: the selected indicator marks the current selection',
1304
+ async () => {
1305
+ await expect(indicator(apple)).toBeVisible();
1306
+ await expect(indicator(banana)).toBeNull();
1307
+ },
1308
+ );
1309
+
1310
+ await step(
1311
+ 'interaction: selecting another option replaces the previous selection',
1312
+ async () => {
1313
+ await userEvent.click(banana);
1314
+ await waitFor(async () => {
1315
+ await expect(banana).toHaveAttribute('aria-selected', 'true');
1316
+ });
1317
+ await expect(apple).toHaveAttribute('aria-selected', 'false');
1318
+ await expect(indicator(banana)).toBeVisible();
1319
+ await expect(indicator(apple)).toBeNull();
1320
+ },
1321
+ );
1322
+
1323
+ await step(
1324
+ 'interaction: selecting the active option again deselects it',
1325
+ async () => {
1326
+ await userEvent.click(banana);
1327
+ await waitFor(async () => {
1328
+ await expect(banana).toHaveAttribute('aria-selected', 'false');
1329
+ });
1330
+ await expect(indicator(banana)).toBeNull();
1331
+ await expect(
1332
+ canvas.queryAllByRole('option', { selected: true }),
1333
+ ).toHaveLength(0);
1334
+ },
1335
+ );
1336
+ },
1337
+ };
1338
+
1339
+ /**
1340
+ * Multiple selection: options toggle independently, the list advertises
1341
+ * `aria-multiselectable`, and every chosen option keeps its indicator.
1342
+ */
1343
+ export const MultipleSelection: Story = {
1344
+ name: 'test - Multiple Selection',
1345
+ tags: ['!dev', '!autodocs'],
1346
+ parameters: {
1347
+ // The play changes the selection and moves focus into the list.
1348
+ chromatic: { disableSnapshot: true },
1349
+ docs: {
1350
+ description: {
1351
+ story:
1352
+ 'Add and remove options from a multi-select list and check the indicators keep up.',
1353
+ },
1354
+ },
1355
+ },
1356
+ render: () => ({
1357
+ components: {
1358
+ UiListbox,
1359
+ UiListboxContent,
1360
+ UiListboxItem,
1361
+ UiListboxItemIndicator,
1362
+ },
1363
+ setup() {
1364
+ const selected = ref(['apple', 'cherry']);
1365
+ return { selected, fruits };
1366
+ },
1367
+ template: `
1368
+ <UiListbox v-model="selected" multiple class="w-[200px] p-1">
1369
+ <UiListboxContent aria-label="Fruits">
1370
+ <UiListboxItem
1371
+ v-for="fruit in fruits"
1372
+ :key="fruit.value"
1373
+ :value="fruit.value"
1374
+ >
1375
+ <span>{{ fruit.label }}</span>
1376
+ <UiListboxItemIndicator />
1377
+ </UiListboxItem>
1378
+ </UiListboxContent>
1379
+ </UiListbox>
1380
+ `,
1381
+ }),
1382
+ play: async ({ canvasElement, step }) => {
1383
+ // Scenario (Listbox / Selected indicator): several fruits are selected at
1384
+ // once; adding a third keeps the first two, and clicking a selected fruit
1385
+ // removes only that one.
1386
+ const canvas = within(canvasElement);
1387
+ const list = canvas.getByRole('listbox', { name: 'Fruits' });
1388
+ const banana = canvas.getByRole('option', { name: 'Banana' });
1389
+ const cherry = canvas.getByRole('option', { name: 'Cherry' });
1390
+
1391
+ await step(
1392
+ 'interaction: the list allows more than one selection at a time',
1393
+ async () => {
1394
+ await expect(list).toHaveAttribute('aria-multiselectable', 'true');
1395
+ await expect(
1396
+ canvas
1397
+ .getAllByRole('option', { selected: true })
1398
+ .map((o) => o.textContent?.trim()),
1399
+ ).toEqual(['Apple', 'Cherry']);
1400
+ },
1401
+ );
1402
+
1403
+ await step(
1404
+ 'interaction: selecting another option adds it to the selection',
1405
+ async () => {
1406
+ await userEvent.click(banana);
1407
+ await waitFor(async () => {
1408
+ await expect(banana).toHaveAttribute('aria-selected', 'true');
1409
+ });
1410
+ await expect(
1411
+ canvas.getAllByRole('option', { selected: true }),
1412
+ ).toHaveLength(3);
1413
+ await expect(
1414
+ banana.querySelector('[data-slot="listbox-item-indicator"]'),
1415
+ ).toBeVisible();
1416
+ },
1417
+ );
1418
+
1419
+ await step(
1420
+ 'interaction: clicking a selected option removes only that option',
1421
+ async () => {
1422
+ await userEvent.click(cherry);
1423
+ await waitFor(async () => {
1424
+ await expect(cherry).toHaveAttribute('aria-selected', 'false');
1425
+ });
1426
+ await expect(
1427
+ canvas
1428
+ .getAllByRole('option', { selected: true })
1429
+ .map((o) => o.textContent?.trim()),
1430
+ ).toEqual(['Apple', 'Banana']);
1431
+ },
1432
+ );
1433
+ },
1434
+ };
1435
+
1436
+ /**
1437
+ * Filtering: typing narrows the list, an empty result set is announced, and
1438
+ * clearing the filter brings every option back. The filter is fully operable
1439
+ * from the keyboard.
1440
+ */
1441
+ export const Filtering: Story = {
1442
+ name: 'test - Filtering',
1443
+ tags: ['!dev', '!autodocs'],
1444
+ parameters: {
1445
+ // The play types into the filter and moves focus.
1446
+ chromatic: { disableSnapshot: true },
1447
+ docs: {
1448
+ description: {
1449
+ story:
1450
+ 'Type to narrow the list, reach the no-results state, and clear the filter again.',
1451
+ },
1452
+ },
1453
+ },
1454
+ render: renderFilterListbox,
1455
+ play: async ({ canvasElement, step }) => {
1456
+ // Scenario (Filter / Option): a user tabs to the filter, narrows the list
1457
+ // by typing, hits a no-results state, clears it, and confirms the
1458
+ // highlighted option with the keyboard.
1459
+ const canvas = within(canvasElement);
1460
+ const filter = canvas.getByRole('textbox', { name: 'Search food' });
1461
+
1462
+ await step(
1463
+ 'keyboard: the filter is reachable via Tab (WCAG 2.1.1 Keyboard)',
1464
+ async () => {
1465
+ await userEvent.tab();
1466
+ await expect(filter).toHaveFocus();
1467
+ },
1468
+ );
1469
+
1470
+ await step('interaction: typing narrows the list to matches', async () => {
1471
+ await userEvent.type(filter, 'an');
1472
+ await waitFor(async () => {
1473
+ await expect(
1474
+ canvas.getAllByRole('option').map((o) => o.textContent?.trim()),
1475
+ ).toEqual(['Mango (sold out)', 'Banana']);
1476
+ });
1477
+ });
1478
+
1479
+ await step(
1480
+ 'interaction: a search without matches announces the no-results state',
1481
+ async () => {
1482
+ await userEvent.clear(filter);
1483
+ await userEvent.type(filter, 'zzz');
1484
+ await waitFor(async () => {
1485
+ await expect(canvas.queryAllByRole('option')).toHaveLength(0);
1486
+ });
1487
+ await expect(canvas.getByRole('status')).toHaveTextContent(
1488
+ 'No food matches "zzz"',
1489
+ );
1490
+ },
1491
+ );
1492
+
1493
+ await step(
1494
+ 'screen-reader: an empty result set leaves no dangling active-descendant reference (WCAG 4.1.2)',
1495
+ async () => {
1496
+ // The highlighted option was filtered away, so the filter must stop
1497
+ // pointing at it rather than naming an option that no longer exists.
1498
+ await expect(filter).not.toHaveAttribute('aria-activedescendant');
1499
+ },
1500
+ );
1501
+
1502
+ await step(
1503
+ 'interaction: clearing the filter restores every option',
1504
+ async () => {
1505
+ await userEvent.clear(filter);
1506
+ await waitFor(async () => {
1507
+ await expect(canvas.getAllByRole('option')).toHaveLength(4);
1508
+ });
1509
+ await expect(canvas.queryByRole('status')).not.toBeInTheDocument();
1510
+ },
1511
+ );
1512
+
1513
+ await step(
1514
+ 'keyboard: the arrow keys move the highlight while focus stays in the filter',
1515
+ async () => {
1516
+ await userEvent.keyboard('{ArrowDown}');
1517
+ await waitFor(async () => {
1518
+ await expect(filter).toHaveAttribute(
1519
+ 'aria-activedescendant',
1520
+ canvas.getByRole('option', { name: 'Banana' }).id,
1521
+ );
1522
+ });
1523
+ await expect(filter).toHaveFocus();
1524
+ },
1525
+ );
1526
+
1527
+ await step(
1528
+ 'keyboard: Enter selects the highlighted option from the filter',
1529
+ async () => {
1530
+ await userEvent.keyboard('{Enter}');
1531
+ await waitFor(async () => {
1532
+ await expect(
1533
+ canvas.getByRole('option', { name: 'Banana' }),
1534
+ ).toHaveAttribute('aria-selected', 'true');
1535
+ });
1536
+ },
1537
+ );
1538
+
1539
+ await step(
1540
+ 'interaction: hovering an option moves the highlight to it',
1541
+ async () => {
1542
+ const carrot = canvas.getByRole('option', { name: 'Carrot' });
1543
+ await userEvent.hover(carrot);
1544
+ await waitFor(async () => {
1545
+ await expect(carrot).toHaveAttribute('data-highlighted');
1546
+ });
1547
+ },
1548
+ );
1549
+ },
1550
+ };
1551
+
1552
+ /**
1553
+ * `UiListbox` is fully keyboard operable: `Tab` reaches the list, the arrow
1554
+ * keys move through the enabled options, `Enter` and `Space` confirm the
1555
+ * selection, and `Home`/`End` jump to the ends.
1556
+ */
1557
+ export const KeyboardNavigation: Story = {
1558
+ name: 'test - Keyboard Navigation',
1559
+ tags: ['!dev', '!autodocs'],
1560
+ parameters: {
1561
+ // Focus moves during the play function; skip the visual snapshot.
1562
+ chromatic: { disableSnapshot: true },
1563
+ docs: {
1564
+ description: {
1565
+ story:
1566
+ 'Press `Tab` to reach the list, the arrow keys to move, and `Enter` or `Space` to select.',
1567
+ },
1568
+ },
1569
+ },
1570
+ render: renderKeyboardListbox,
1571
+ play: async ({ canvasElement, step }) => {
1572
+ // Scenario (Listbox / Option): a keyboard user tabs into the list, arrows
1573
+ // past the sold-out option, selects with Enter and Space, and jumps to the
1574
+ // ends of the list with Home and End.
1575
+ const canvas = within(canvasElement);
1576
+ const anchor = canvas.getByRole('button', { name: 'Before the list' });
1577
+ const apple = canvas.getByRole('option', { name: 'Apple' });
1578
+ const banana = canvas.getByRole('option', { name: 'Banana' });
1579
+ const carrot = canvas.getByRole('option', { name: 'Carrot' });
1580
+
1581
+ await step(
1582
+ 'keyboard: Tab reaches the list at its active option (WCAG 2.1.1 Keyboard)',
1583
+ async () => {
1584
+ anchor.focus();
1585
+ await userEvent.tab();
1586
+ await expect(apple).toHaveFocus();
1587
+ // The list is a single tab stop: only the active option is tabbable.
1588
+ await expect(apple).toHaveAttribute('tabindex', '0');
1589
+ await expect(banana).toHaveAttribute('tabindex', '-1');
1590
+ },
1591
+ );
1592
+
1593
+ await step(
1594
+ 'keyboard: the arrow keys move through the options and skip disabled ones',
1595
+ async () => {
1596
+ await userEvent.keyboard('{ArrowDown}');
1597
+ await expect(banana).toHaveFocus();
1598
+ await expect(
1599
+ canvas.getByRole('option', { name: 'Mango (sold out)' }),
1600
+ ).not.toHaveFocus();
1601
+ await userEvent.keyboard('{ArrowUp}');
1602
+ await expect(apple).toHaveFocus();
1603
+ },
1604
+ );
1605
+
1606
+ await step('keyboard: Enter selects the active option', async () => {
1607
+ await userEvent.keyboard('{Enter}');
1608
+ await waitFor(async () => {
1609
+ await expect(apple).toHaveAttribute('aria-selected', 'true');
1610
+ });
1611
+ });
1612
+
1613
+ await step('keyboard: Space toggles the active option', async () => {
1614
+ await userEvent.keyboard(' ');
1615
+ await waitFor(async () => {
1616
+ await expect(apple).toHaveAttribute('aria-selected', 'false');
1617
+ });
1618
+ });
1619
+
1620
+ await step(
1621
+ 'keyboard: End and Home jump to the last and first option (WCAG 2.4.3)',
1622
+ async () => {
1623
+ await userEvent.keyboard('{End}');
1624
+ await expect(carrot).toHaveFocus();
1625
+ await userEvent.keyboard('{Home}');
1626
+ await expect(apple).toHaveFocus();
1627
+ },
1628
+ );
1629
+ },
1630
+ };
1631
+
1632
+ /**
1633
+ * Screen-reader semantics: the list exposes a named `listbox`, the options
1634
+ * sit in labelled groups, and each option announces its selected and disabled
1635
+ * state.
1636
+ */
1637
+ export const ScreenReader: Story = {
1638
+ name: 'test - Screen Reader',
1639
+ tags: ['!dev', '!autodocs'],
1640
+ parameters: {
1641
+ // The play changes the selection.
1642
+ chromatic: { disableSnapshot: true },
1643
+ docs: {
1644
+ description: {
1645
+ story:
1646
+ 'Verifies the roles, names, grouping, and states that assistive technology announces.',
1647
+ },
1648
+ },
1649
+ },
1650
+ render: renderFilterListbox,
1651
+ play: async ({ canvasElement, step }) => {
1652
+ // Scenario (Listbox / Group / Option / Filter): assistive tech reads a
1653
+ // named list, finds the options under labelled groups, and hears the
1654
+ // selected and disabled states of individual options.
1655
+ const canvas = within(canvasElement);
1656
+ const list = canvas.getByRole('listbox', { name: 'Food' });
1657
+
1658
+ await step(
1659
+ 'screen-reader: the list exposes a listbox role with a meaningful name (WCAG 4.1.2)',
1660
+ async () => {
1661
+ await expect(list).toHaveAccessibleName('Food');
1662
+ await expect(list).toHaveAttribute('aria-orientation', 'vertical');
1663
+ },
1664
+ );
1665
+
1666
+ await step(
1667
+ 'screen-reader: the filter exposes a named text input (WCAG 3.3.2)',
1668
+ async () => {
1669
+ await expect(
1670
+ canvas.getByRole('textbox', { name: 'Search food' }),
1671
+ ).toHaveAccessibleName('Search food');
1672
+ },
1673
+ );
1674
+
1675
+ await step(
1676
+ 'screen-reader: options are organised under labelled groups (WCAG 1.3.1)',
1677
+ async () => {
1678
+ const fruitsGroup = canvas.getByRole('group', { name: 'Fruits' });
1679
+ await expect(
1680
+ within(fruitsGroup).getByRole('option', { name: 'Apple' }),
1681
+ ).toBeVisible();
1682
+ const vegetablesGroup = canvas.getByRole('group', {
1683
+ name: 'Vegetables',
1684
+ });
1685
+ await expect(
1686
+ within(vegetablesGroup).getByRole('option', { name: 'Carrot' }),
1687
+ ).toBeVisible();
1688
+ },
1689
+ );
1690
+
1691
+ await step(
1692
+ 'screen-reader: every option exposes an option role and its own name',
1693
+ async () => {
1694
+ await expect(
1695
+ canvas.getAllByRole('option').map((o) => o.textContent?.trim()),
1696
+ ).toEqual(['Apple', 'Mango (sold out)', 'Banana', 'Carrot']);
1697
+ },
1698
+ );
1699
+
1700
+ await step(
1701
+ 'screen-reader: an unavailable option announces its disabled state (WCAG 4.1.2)',
1702
+ async () => {
1703
+ await expect(
1704
+ canvas.getByRole('option', { name: 'Mango (sold out)' }),
1705
+ ).toHaveAttribute('aria-disabled', 'true');
1706
+ },
1707
+ );
1708
+
1709
+ await step(
1710
+ 'screen-reader: the chosen option announces its selected state (WCAG 4.1.2)',
1711
+ async () => {
1712
+ const apple = canvas.getByRole('option', { name: 'Apple' });
1713
+ await expect(apple).toHaveAttribute('aria-selected', 'false');
1714
+ await userEvent.click(apple);
1715
+ await waitFor(async () => {
1716
+ await expect(apple).toHaveAttribute('aria-selected', 'true');
1717
+ });
1718
+ await expect(
1719
+ canvas.getByRole('option', { name: 'Carrot' }),
1720
+ ).toHaveAttribute('aria-selected', 'false');
1721
+ },
1722
+ );
1723
+
1724
+ await step(
1725
+ 'screen-reader: the selected indicator is decorative and not announced twice',
1726
+ async () => {
1727
+ const indicator = canvas
1728
+ .getByRole('option', { name: 'Apple' })
1729
+ .querySelector('[data-slot="listbox-item-indicator"]');
1730
+ await expect(indicator).toHaveAttribute('aria-hidden', 'true');
1731
+ },
1732
+ );
1733
+ },
1734
+ };
1735
+
1736
+ /**
1737
+ * Rendered at 200% page zoom (WCAG 1.4.4 Resize Text). Use the Zoom toolbar to
1738
+ * try 400% reflow (WCAG 1.4.10); the filter and the list must stay visible and
1739
+ * operable.
1740
+ */
1741
+ export const ZoomMagnification: Story = {
1742
+ name: 'test - Zoom Magnification',
1743
+ tags: ['!dev', '!autodocs'],
1744
+ globals: { zoom: '200' },
1745
+ parameters: {
1746
+ // The accessible-reflow Chromatic mode already covers magnified snapshots.
1747
+ chromatic: { disableSnapshot: true },
1748
+ docs: {
1749
+ description: {
1750
+ story:
1751
+ 'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
1752
+ },
1753
+ },
1754
+ },
1755
+ render: renderFilterListbox,
1756
+ play: async ({ canvasElement, step }) => {
1757
+ // Scenario (Listbox / Filter): at 200% page zoom the filter and the list
1758
+ // stay on screen, the options are still readable, and both filtering and
1759
+ // selecting keep working.
1760
+ const canvas = within(canvasElement);
1761
+ const filter = canvas.getByRole('textbox', { name: 'Search food' });
1762
+ const list = canvas.getByRole('listbox', { name: 'Food' });
1763
+
1764
+ await step(
1765
+ 'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
1766
+ async () => {
1767
+ await expect(document.documentElement.style.zoom).toBe('200%');
1768
+ },
1769
+ );
1770
+
1771
+ await step(
1772
+ 'zoom: the filter and the list stay visible when magnified',
1773
+ async () => {
1774
+ await expect(filter).toBeVisible();
1775
+ await expect(list).toBeVisible();
1776
+ await expect(
1777
+ canvas.getByRole('option', { name: 'Apple' }),
1778
+ ).toBeVisible();
1779
+ },
1780
+ );
1781
+
1782
+ await step('zoom: the list stays operable when magnified', async () => {
1783
+ await userEvent.type(filter, 'car');
1784
+ await waitFor(async () => {
1785
+ await expect(canvas.getAllByRole('option')).toHaveLength(1);
1786
+ });
1787
+ const carrot = canvas.getByRole('option', { name: 'Carrot' });
1788
+ await userEvent.click(carrot);
1789
+ await waitFor(async () => {
1790
+ await expect(carrot).toHaveAttribute('aria-selected', 'true');
1791
+ });
1792
+ });
1793
+ },
1794
+ };
1795
+
1796
+ /**
1797
+ * Rendered at the mobile (320px reflow) breakpoint via the Media breakpoint
1798
+ * toolbar. The list and its options stay operable and meet the minimum
1799
+ * touch-target size (WCAG 2.5.8).
1800
+ */
1801
+ export const MobileBreakpoint: Story = {
1802
+ name: 'test - Mobile Breakpoint',
1803
+ tags: ['!dev', '!autodocs'],
1804
+ globals: { mediaBreakpoint: 'reflow' },
1805
+ parameters: {
1806
+ // Reflow snapshots are already covered by the accessible-reflow mode.
1807
+ chromatic: { disableSnapshot: true },
1808
+ docs: {
1809
+ description: {
1810
+ story:
1811
+ 'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
1812
+ },
1813
+ },
1814
+ },
1815
+ render: renderFilterListbox,
1816
+ play: async ({ canvasElement, step }) => {
1817
+ // Scenario (Listbox / Filter / Option): at the 320px mobile breakpoint the
1818
+ // list fits the viewport, the filter and options meet the minimum
1819
+ // touch-target size, and tapping an option still selects it.
1820
+ const canvas = within(canvasElement);
1821
+ const filter = canvas.getByRole('textbox', { name: 'Search food' });
1822
+ const list = canvas.getByRole('listbox', { name: 'Food' });
1823
+
1824
+ await step(
1825
+ 'mobile: the list fits the 320px viewport without clipping (WCAG 1.4.10 Reflow)',
1826
+ async () => {
1827
+ await expect(list).toBeVisible();
1828
+ await expect(list.getBoundingClientRect().width).toBeLessThanOrEqual(
1829
+ 320,
1830
+ );
1831
+ },
1832
+ );
1833
+
1834
+ await step(
1835
+ 'mobile: the filter meets the minimum touch-target size (WCAG 2.5.8)',
1836
+ async () => {
1837
+ const { width, height } = filter.getBoundingClientRect();
1838
+ await expect(width).toBeGreaterThanOrEqual(24);
1839
+ await expect(height).toBeGreaterThanOrEqual(24);
1840
+ },
1841
+ );
1842
+
1843
+ await step(
1844
+ 'mobile: options meet the minimum touch-target size (WCAG 2.5.8)',
1845
+ async () => {
1846
+ for (const option of canvas.getAllByRole('option')) {
1847
+ const { width, height } = option.getBoundingClientRect();
1848
+ await expect(width).toBeGreaterThanOrEqual(24);
1849
+ await expect(height).toBeGreaterThanOrEqual(24);
1850
+ }
1851
+ },
1852
+ );
1853
+
1854
+ await step(
1855
+ 'mobile: tapping an option still selects it at the 320px breakpoint',
1856
+ async () => {
1857
+ const carrot = canvas.getByRole('option', { name: 'Carrot' });
1858
+ await userEvent.click(carrot);
1859
+ await waitFor(async () => {
1860
+ await expect(carrot).toHaveAttribute('aria-selected', 'true');
1861
+ });
1862
+ },
1863
+ );
1864
+ },
612
1865
  };