@aleph-alpha/ui-library 5.7.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 (67) hide show
  1. package/README.md +4 -0
  2. package/config.d.ts +5 -1
  3. package/config.js +18 -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/UiToggleGroup/types.d.ts +0 -5
  14. package/dist/system/components/core/breadcrumb/Breadcrumb.vue.d.ts +21 -0
  15. package/dist/system/components/core/breadcrumb/BreadcrumbEllipsis.vue.d.ts +21 -0
  16. package/dist/system/components/core/breadcrumb/BreadcrumbItem.vue.d.ts +21 -0
  17. package/dist/system/components/core/breadcrumb/BreadcrumbLink.vue.d.ts +24 -0
  18. package/dist/system/components/core/breadcrumb/BreadcrumbList.vue.d.ts +21 -0
  19. package/dist/system/components/core/breadcrumb/BreadcrumbPage.vue.d.ts +21 -0
  20. package/dist/system/components/core/breadcrumb/BreadcrumbSeparator.vue.d.ts +21 -0
  21. package/dist/system/components/core/breadcrumb/index.d.ts +7 -0
  22. package/dist/system/components/core/listbox/Listbox.vue.d.ts +28 -2
  23. package/dist/system/components/core/listbox/context.d.ts +21 -0
  24. package/dist/system/components/index.d.ts +1 -0
  25. package/dist/system/lib.js +10057 -9707
  26. package/dist/system/patterns/UiDatePicker/UiDatePicker.vue.d.ts +1 -1
  27. package/dist/system/story-sidebar-icons.json +6 -0
  28. package/package.json +2 -2
  29. package/src/components/UiBreadcrumb/UiBreadcrumb.stories.ts +1419 -0
  30. package/src/components/UiBreadcrumb/UiBreadcrumb.vue +19 -0
  31. package/src/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue +22 -0
  32. package/src/components/UiBreadcrumb/UiBreadcrumbItem.vue +19 -0
  33. package/src/components/UiBreadcrumb/UiBreadcrumbLink.vue +26 -0
  34. package/src/components/UiBreadcrumb/UiBreadcrumbList.vue +19 -0
  35. package/src/components/UiBreadcrumb/UiBreadcrumbPage.vue +19 -0
  36. package/src/components/UiBreadcrumb/UiBreadcrumbSeparator.vue +22 -0
  37. package/src/components/UiBreadcrumb/index.ts +8 -0
  38. package/src/components/UiBreadcrumb/types.ts +28 -0
  39. package/src/components/UiDropdownMenu/UiDropdownMenuTrigger.vue +2 -0
  40. package/src/components/UiListbox/UiListbox.stories.ts +1283 -30
  41. package/src/components/UiListbox/__tests__/UiListbox.test.ts +70 -35
  42. package/src/components/UiSwitch/UiSwitch.stories.ts +706 -9
  43. package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +2 -6
  44. package/src/components/UiToggle/UiToggle.stories.ts +961 -130
  45. package/src/components/UiToggle/UiToggle.vue +8 -1
  46. package/src/components/UiToggle/__tests__/UiToggle.test.ts +55 -65
  47. package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +1060 -173
  48. package/src/components/UiToggleGroup/UiToggleGroup.vue +4 -2
  49. package/src/components/UiToggleGroup/__tests__/UiToggleGroup.test.ts +117 -325
  50. package/src/components/UiToggleGroup/types.ts +0 -5
  51. package/src/components/core/breadcrumb/Breadcrumb.vue +13 -0
  52. package/src/components/core/breadcrumb/BreadcrumbEllipsis.vue +23 -0
  53. package/src/components/core/breadcrumb/BreadcrumbItem.vue +17 -0
  54. package/src/components/core/breadcrumb/BreadcrumbLink.vue +29 -0
  55. package/src/components/core/breadcrumb/BreadcrumbList.vue +22 -0
  56. package/src/components/core/breadcrumb/BreadcrumbPage.vue +20 -0
  57. package/src/components/core/breadcrumb/BreadcrumbSeparator.vue +22 -0
  58. package/src/components/core/breadcrumb/index.ts +7 -0
  59. package/src/components/core/listbox/Listbox.vue +14 -2
  60. package/src/components/core/listbox/ListboxContent.vue +61 -3
  61. package/src/components/core/listbox/ListboxItem.vue +6 -1
  62. package/src/components/core/listbox/context.ts +34 -0
  63. package/src/components/core/toggle/Toggle.vue +15 -1
  64. package/src/components/core/toggle/index.ts +2 -2
  65. package/src/components/core/toggle-group/ToggleGroup.vue +1 -1
  66. package/src/components/core/toggle-group/index.ts +5 -2
  67. package/src/components/index.ts +1 -0
@@ -1,4 +1,5 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { expect, fn, userEvent, within } from 'storybook/test';
2
3
  import UiSwitch from './UiSwitch.vue';
3
4
  import { ref, watch } from 'vue';
4
5
 
@@ -31,6 +32,11 @@ export default meta;
31
32
 
32
33
  type Story = StoryObj<typeof UiSwitch>;
33
34
 
35
+ // `UiSwitch` forwards `defineModel`, so it emits `update:modelValue`. Play
36
+ // functions that need to assert the emit bind this module-level spy via
37
+ // `@update:model-value` and `mockClear()` it first.
38
+ const onUpdate = fn();
39
+
34
40
  const defaultTemplateSource = `<script setup lang="ts">
35
41
  import { ref } from 'vue'
36
42
  import { UiSwitch } from '@aleph-alpha/ui-library'
@@ -46,7 +52,9 @@ const enabled = ref(false)
46
52
  </template>`;
47
53
 
48
54
  /**
49
- * Default switch with a label.
55
+ * Default switch, resting in the off state. Always pair the switch with a
56
+ * visible `<label for="…">`: it names the control for assistive technology and
57
+ * extends the tap target, which the 36×20px switch needs on touch screens.
50
58
  */
51
59
  export const Default: Story = {
52
60
  render: (args) => ({
@@ -75,25 +83,48 @@ export const Default: Story = {
75
83
  },
76
84
  },
77
85
  },
86
+ play: async ({ canvasElement, step }) => {
87
+ // Scenario (Switch): the resting off state, paired with a visible label.
88
+ // Nothing is toggled here, so the visual snapshot stays stable.
89
+ const canvas = within(canvasElement);
90
+ const toggle = canvas.getByRole('switch', { name: 'Airplane Mode' });
91
+
92
+ await step(
93
+ 'interaction: the visible label names the switch (WCAG 1.3.1 / 4.1.2)',
94
+ async () => {
95
+ await expect(toggle).toHaveAccessibleName('Airplane Mode');
96
+ await expect(canvas.getByText('Airplane Mode')).toBeVisible();
97
+ },
98
+ );
99
+
100
+ await step('interaction: the switch rests in the off state', async () => {
101
+ await expect(toggle).toHaveAttribute('aria-checked', 'false');
102
+ await expect(toggle).toHaveAttribute('data-state', 'unchecked');
103
+ });
104
+ },
78
105
  };
79
106
 
80
- const disabledTemplateSource = `<script setup lang="ts">
107
+ const checkedTemplateSource = `<script setup lang="ts">
108
+ import { ref } from 'vue'
81
109
  import { UiSwitch } from '@aleph-alpha/ui-library'
110
+
111
+ const enabled = ref(true)
82
112
  </script>
83
113
 
84
114
  <template>
85
115
  <div class="flex items-center gap-x-3">
86
- <UiSwitch id="airplane-mode" name="airplane-mode" disabled />
87
- <label for="airplane-mode" class="label-14-medium text-content-on-surface-muted">Airplane Mode</label>
116
+ <UiSwitch id="airplane-mode-on" v-model="enabled" name="airplane-mode" />
117
+ <label for="airplane-mode-on" class="label-14-medium">Airplane Mode</label>
88
118
  </div>
89
119
  </template>`;
90
120
 
91
121
  /**
92
- * Switch in disabled state.
122
+ * The on resting state. The state is carried by `aria-checked` and mirrored in
123
+ * `data-state`, so it is never signalled by the track colour alone (WCAG 1.4.1).
93
124
  */
94
- export const Disabled: Story = {
125
+ export const Checked: Story = {
95
126
  args: {
96
- disabled: true,
127
+ modelValue: true,
97
128
  },
98
129
  render: (args) => ({
99
130
  components: { UiSwitch },
@@ -109,16 +140,682 @@ export const Disabled: Story = {
109
140
  },
110
141
  template: `
111
142
  <div class="flex items-center gap-x-3">
112
- <UiSwitch id="airplane-mode" v-model="enabled" name="airplane-mode" :disabled="args.disabled" />
113
- <label for="airplane-mode" class="label-14-medium text-content-on-surface-muted">Airplane Mode</label>
143
+ <UiSwitch id="airplane-mode-on" v-model="enabled" name="airplane-mode" :disabled="args.disabled" />
144
+ <label for="airplane-mode-on" class="label-14-medium">Airplane Mode</label>
145
+ </div>
146
+ `,
147
+ }),
148
+ parameters: {
149
+ docs: {
150
+ source: {
151
+ code: checkedTemplateSource,
152
+ },
153
+ },
154
+ },
155
+ play: async ({ canvasElement, step }) => {
156
+ // Scenario (Switch): the resting on state. Non-mutating, so the visual
157
+ // snapshot stays stable.
158
+ const canvas = within(canvasElement);
159
+ const toggle = canvas.getByRole('switch', { name: 'Airplane Mode' });
160
+
161
+ await step('interaction: the switch rests in the on state', async () => {
162
+ await expect(toggle).toBeChecked();
163
+ await expect(toggle).toHaveAttribute('aria-checked', 'true');
164
+ });
165
+
166
+ await step(
167
+ 'interaction: the on state is exposed beyond the track colour (WCAG 1.4.1)',
168
+ async () => {
169
+ await expect(toggle).toHaveAttribute('data-state', 'checked');
170
+ },
171
+ );
172
+ },
173
+ };
174
+
175
+ const disabledTemplateSource = `<script setup lang="ts">
176
+ import { UiSwitch } from '@aleph-alpha/ui-library'
177
+ </script>
178
+
179
+ <template>
180
+ <div class="flex flex-col gap-y-3">
181
+ <div class="flex items-center gap-x-3">
182
+ <UiSwitch id="disabled-off" name="airplane-mode" disabled />
183
+ <label for="disabled-off" class="label-14-medium text-content-on-surface-muted">
184
+ Airplane Mode
185
+ </label>
186
+ </div>
187
+ <div class="flex items-center gap-x-3">
188
+ <UiSwitch id="disabled-on" name="wifi" :model-value="true" disabled />
189
+ <label for="disabled-on" class="label-14-medium text-content-on-surface-muted">
190
+ Wi-Fi
191
+ </label>
192
+ </div>
193
+ </div>
194
+ </template>`;
195
+
196
+ /**
197
+ * Disabled in both resting states. `UiSwitch` renders a native `button`, so a
198
+ * disabled switch reports `disabled` to assistive technology and drops out of
199
+ * the tab order — the state is never conveyed by dimming alone.
200
+ */
201
+ export const Disabled: Story = {
202
+ args: {
203
+ disabled: true,
204
+ },
205
+ render: () => ({
206
+ components: { UiSwitch },
207
+ setup() {
208
+ return { onUpdate };
209
+ },
210
+ template: `
211
+ <div class="flex flex-col gap-y-3">
212
+ <div class="flex items-center gap-x-3">
213
+ <UiSwitch id="disabled-off" name="airplane-mode" disabled @update:model-value="onUpdate" />
214
+ <label for="disabled-off" class="label-14-medium text-content-on-surface-muted">
215
+ Airplane Mode
216
+ </label>
217
+ </div>
218
+ <div class="flex items-center gap-x-3">
219
+ <UiSwitch id="disabled-on" name="wifi" :model-value="true" disabled @update:model-value="onUpdate" />
220
+ <label for="disabled-on" class="label-14-medium text-content-on-surface-muted">
221
+ Wi-Fi
222
+ </label>
223
+ </div>
114
224
  </div>
115
225
  `,
116
226
  }),
117
227
  parameters: {
228
+ controls: { disable: true },
118
229
  docs: {
119
230
  source: {
120
231
  code: disabledTemplateSource,
121
232
  },
122
233
  },
123
234
  },
235
+ play: async ({ canvasElement, step }) => {
236
+ // Scenario (Switch): a disabled switch keeps its on/off state readable,
237
+ // refuses activation, and is skipped by Tab. The forced click is a no-op,
238
+ // so the snapshot stays stable.
239
+ onUpdate.mockClear();
240
+ const canvas = within(canvasElement);
241
+ const off = canvas.getByRole('switch', { name: 'Airplane Mode' });
242
+ const on = canvas.getByRole('switch', { name: 'Wi-Fi' });
243
+
244
+ await step(
245
+ 'interaction: both switches report the disabled state (WCAG 4.1.2)',
246
+ async () => {
247
+ await expect(off).toBeDisabled();
248
+ await expect(on).toBeDisabled();
249
+ },
250
+ );
251
+
252
+ await step(
253
+ 'interaction: a disabled switch still announces whether it is on or off',
254
+ async () => {
255
+ await expect(off).toHaveAttribute('aria-checked', 'false');
256
+ await expect(on).toHaveAttribute('aria-checked', 'true');
257
+ },
258
+ );
259
+
260
+ await step('interaction: a disabled switch cannot be toggled', async () => {
261
+ // Disabled controls render with pointer-events: none, so bypass the
262
+ // check and assert the click is still a no-op.
263
+ await userEvent.click(off, { pointerEventsCheck: 0 });
264
+ await expect(off).toHaveAttribute('aria-checked', 'false');
265
+ await expect(onUpdate).not.toHaveBeenCalled();
266
+ });
267
+
268
+ await step(
269
+ 'keyboard: a disabled switch is skipped by Tab (WCAG 2.1.1)',
270
+ async () => {
271
+ await userEvent.tab();
272
+ await expect(off).not.toHaveFocus();
273
+ await expect(on).not.toHaveFocus();
274
+ },
275
+ );
276
+ },
277
+ };
278
+
279
+ /**
280
+ * Accessibility guidance for switches: a visible label that also enlarges the
281
+ * tap target, state that survives without colour, and an explicit name when the
282
+ * label text is dynamic — shown side by side in the default and accessible
283
+ * themes.
284
+ */
285
+ export const Accessibility: Story = {
286
+ tags: ['!autodocs'],
287
+ parameters: {
288
+ a11y: { test: 'off' },
289
+ chromatic: { disableSnapshot: true },
290
+ controls: { disable: true },
291
+ docs: {
292
+ canvas: {
293
+ sourceState: 'none',
294
+ },
295
+ },
296
+ },
297
+ render: () => ({
298
+ components: { UiSwitch },
299
+ setup() {
300
+ const defaultEnabled = ref(true);
301
+ const accessibleEnabled = ref(true);
302
+ return { defaultEnabled, accessibleEnabled };
303
+ },
304
+ template: `
305
+ <section class="flex flex-col gap-6 p-4">
306
+ <div>
307
+ <h2 class="heading-m font-bold text-content-on-surface-primary">
308
+ Accessibility
309
+ </h2>
310
+ </div>
311
+
312
+ <div class="flex flex-col gap-6">
313
+ <div class="flex flex-col gap-2">
314
+ <h3 class="body-16-medium text-content-on-surface-primary">
315
+ Always pair the switch with a visible label
316
+ </h3>
317
+ <p class="body-16-regular text-content-on-surface-primary">
318
+ Give <code>UiSwitch</code> an <code>id</code> and point a
319
+ <code>&lt;label for="…"&gt;</code> at it. The label becomes the
320
+ accessible name and makes the text part of the activation target.
321
+ </p>
322
+ </div>
323
+
324
+ <div class="flex flex-col gap-2">
325
+ <h3 class="body-16-medium text-content-on-surface-primary">
326
+ Give the switch a big enough target
327
+ </h3>
328
+ <p class="body-16-regular text-content-on-surface-primary">
329
+ The track alone is 36×20px, which is under the 24px minimum height
330
+ in WCAG 2.5.8. Rely on the label to extend the target, and give the
331
+ row some vertical padding so the whole row is comfortably tappable.
332
+ </p>
333
+ </div>
334
+
335
+ <div class="flex flex-col gap-2">
336
+ <h3 class="body-16-medium text-content-on-surface-primary">
337
+ Name the state in the label, not in the track colour
338
+ </h3>
339
+ <p class="body-16-regular text-content-on-surface-primary">
340
+ On and off are announced through <code>aria-checked</code>, which
341
+ the component sets for you. Keep the label describing what the
342
+ setting does ("Airplane Mode"), not its current value ("On"), so it
343
+ stays correct in both states (WCAG 1.4.1).
344
+ </p>
345
+ </div>
346
+
347
+ <div class="flex flex-col gap-2">
348
+ <h3 class="body-16-medium text-content-on-surface-primary">
349
+ Set an explicit name when the label text changes
350
+ </h3>
351
+ <p class="body-16-regular text-content-on-surface-primary">
352
+ The name is read from the associated label once, when the switch
353
+ mounts. If the label text can change at runtime, pass your own
354
+ <code>aria-label</code> or <code>aria-labelledby</code> so the name
355
+ stays in sync.
356
+ </p>
357
+ </div>
358
+
359
+ <div class="flex flex-col gap-2">
360
+ <h3 class="body-16-medium text-content-on-surface-primary">
361
+ Use a switch only for instant changes
362
+ </h3>
363
+ <p class="body-16-regular text-content-on-surface-primary">
364
+ A switch applies immediately and has no "off" value to submit. For
365
+ a value that is saved with a form, use <code>UiCheckbox</code>.
366
+ </p>
367
+ </div>
368
+
369
+ <div class="flex flex-col gap-2">
370
+ <h3 class="body-16-medium text-content-on-surface-primary">
371
+ Enable accessibility theme
372
+ </h3>
373
+ <p class="body-16-regular text-content-on-surface-primary">
374
+ When accessibility is required, enable our accessibility theme.
375
+ This increases contrast and makes sure there are no color contrast
376
+ issues.
377
+ </p>
378
+ </div>
379
+ </div>
380
+
381
+ <div class="grid gap-4 md:grid-cols-2">
382
+ <div class="flex flex-col gap-3">
383
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
384
+ Default
385
+ </h3>
386
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
387
+ <label
388
+ for="switch-accessibility-default"
389
+ class="flex min-h-6 cursor-pointer items-center gap-x-3 py-1 label-14-medium text-content-on-surface-primary"
390
+ >
391
+ <UiSwitch
392
+ id="switch-accessibility-default"
393
+ v-model="defaultEnabled"
394
+ name="switch-accessibility-default"
395
+ />
396
+ <span>Airplane Mode</span>
397
+ </label>
398
+ </div>
399
+ </div>
400
+
401
+ <div class="accessible-theme flex flex-col gap-3">
402
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
403
+ Accessible
404
+ </h3>
405
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
406
+ <label
407
+ for="switch-accessibility-accessible"
408
+ class="flex min-h-6 cursor-pointer items-center gap-x-3 py-1 label-14-medium text-content-on-surface-primary"
409
+ >
410
+ <UiSwitch
411
+ id="switch-accessibility-accessible"
412
+ v-model="accessibleEnabled"
413
+ name="switch-accessibility-accessible"
414
+ />
415
+ <span>Airplane Mode</span>
416
+ </label>
417
+ </div>
418
+ </div>
419
+ </div>
420
+ </section>
421
+ `,
422
+ }),
423
+ };
424
+
425
+ // Test-only stories drive the test-runner (interactive/a11y checks) but are
426
+ // hidden from the Storybook sidebar and docs via the `!dev` / `!autodocs`
427
+ // tags. They keep the implicit `test` tag, so they still run in CI and appear
428
+ // in the automated report under their `test - …` name. `name`/`tags` must be
429
+ // literal properties so Storybook's static CSF indexer can read them.
430
+
431
+ // Shared render for the test scenarios: an enabled switch that starts off, a
432
+ // second one that starts on, and a disabled one — a single fixture covering
433
+ // naming, both resting states, activation, and the disabled guard. Each row is
434
+ // a `min-h-6` label so the whole row is a WCAG 2.5.8 sized target.
435
+ function renderTestSwitches() {
436
+ return {
437
+ components: { UiSwitch },
438
+ setup() {
439
+ const airplane = ref(false);
440
+ const wifi = ref(true);
441
+ const bluetooth = ref(false);
442
+ return { airplane, wifi, bluetooth, onUpdate };
443
+ },
444
+ template: `
445
+ <div class="flex flex-col gap-y-2">
446
+ <label
447
+ for="t-airplane"
448
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
449
+ >
450
+ <UiSwitch
451
+ id="t-airplane"
452
+ v-model="airplane"
453
+ name="airplane"
454
+ @update:model-value="onUpdate"
455
+ />
456
+ <span>Airplane Mode</span>
457
+ </label>
458
+
459
+ <label
460
+ for="t-wifi"
461
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
462
+ >
463
+ <UiSwitch id="t-wifi" v-model="wifi" name="wifi" />
464
+ <span>Wi-Fi</span>
465
+ </label>
466
+
467
+ <label
468
+ for="t-bluetooth"
469
+ class="flex min-h-6 w-fit items-center gap-x-3 py-1 label-14-medium text-content-on-surface-muted"
470
+ >
471
+ <UiSwitch id="t-bluetooth" v-model="bluetooth" name="bluetooth" disabled />
472
+ <span>Bluetooth</span>
473
+ </label>
474
+ </div>
475
+ `,
476
+ };
477
+ }
478
+
479
+ /**
480
+ * Toggle behaviour: activating the switch flips it on and off and emits the new
481
+ * value, the label is part of the hit area, and a disabled switch refuses to
482
+ * change state.
483
+ */
484
+ export const ToggleBehavior: Story = {
485
+ name: 'test - Toggle Behavior',
486
+ tags: ['!dev', '!autodocs'],
487
+ render: renderTestSwitches,
488
+ parameters: {
489
+ // The play toggles state; skip the visual snapshot.
490
+ chromatic: { disableSnapshot: true },
491
+ docs: {
492
+ description: {
493
+ story:
494
+ 'Turn the switch on and off, activate it through its label, and confirm the disabled guard holds.',
495
+ },
496
+ },
497
+ },
498
+ play: async ({ canvasElement, step }) => {
499
+ // Scenario (Switch): a pointer user turns a switch on and back off,
500
+ // activates another through its label text, and finds the disabled one
501
+ // unresponsive.
502
+ onUpdate.mockClear();
503
+ const canvas = within(canvasElement);
504
+ const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
505
+ const wifi = canvas.getByRole('switch', { name: 'Wi-Fi' });
506
+
507
+ await step(
508
+ 'interaction: activating the switch turns it on and emits the new value',
509
+ async () => {
510
+ await expect(airplane).toHaveAttribute('aria-checked', 'false');
511
+ await userEvent.click(airplane);
512
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
513
+ await expect(onUpdate).toHaveBeenCalledWith(true);
514
+ },
515
+ );
516
+
517
+ await step(
518
+ 'interaction: activating it again turns it back off',
519
+ async () => {
520
+ await userEvent.click(airplane);
521
+ await expect(airplane).toHaveAttribute('aria-checked', 'false');
522
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
523
+ },
524
+ );
525
+
526
+ await step(
527
+ 'interaction: the visible label is part of the activation target (WCAG 2.5.8)',
528
+ async () => {
529
+ await userEvent.click(canvas.getByText('Airplane Mode'));
530
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
531
+ },
532
+ );
533
+
534
+ await step(
535
+ 'interaction: a switch that starts on toggles off independently',
536
+ async () => {
537
+ await expect(wifi).toHaveAttribute('aria-checked', 'true');
538
+ await userEvent.click(wifi);
539
+ await expect(wifi).toHaveAttribute('aria-checked', 'false');
540
+ // The other switch is untouched.
541
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
542
+ },
543
+ );
544
+
545
+ await step(
546
+ 'interaction: a disabled switch does not change state (WCAG 4.1.2)',
547
+ async () => {
548
+ const bluetooth = canvas.getByRole('switch', { name: 'Bluetooth' });
549
+ await expect(bluetooth).toBeDisabled();
550
+ // Disabled controls render with pointer-events: none, so bypass the
551
+ // check and assert the click is still a no-op.
552
+ await userEvent.click(bluetooth, { pointerEventsCheck: 0 });
553
+ await expect(bluetooth).toHaveAttribute('aria-checked', 'false');
554
+ },
555
+ );
556
+ },
557
+ };
558
+
559
+ /**
560
+ * `UiSwitch` is keyboard operable: `Tab` reaches it, and both `Space` and
561
+ * `Enter` flip the state. A disabled switch is skipped entirely.
562
+ */
563
+ export const KeyboardNavigation: Story = {
564
+ name: 'test - Keyboard Navigation',
565
+ tags: ['!dev', '!autodocs'],
566
+ render: renderTestSwitches,
567
+ parameters: {
568
+ // Focus moves during the play function; skip the visual snapshot so the
569
+ // focus ring doesn't register as a Chromatic diff.
570
+ chromatic: { disableSnapshot: true },
571
+ docs: {
572
+ description: {
573
+ story:
574
+ 'Press `Tab` to reach the switch, then `Space` or `Enter` to flip it.',
575
+ },
576
+ },
577
+ },
578
+ play: async ({ canvasElement, step }) => {
579
+ // Scenario (Switch): a keyboard user tabs to the first switch, toggles it
580
+ // with both Space and Enter, moves on to the next one, and never lands on
581
+ // the disabled switch.
582
+ onUpdate.mockClear();
583
+ const canvas = within(canvasElement);
584
+ const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
585
+ const wifi = canvas.getByRole('switch', { name: 'Wi-Fi' });
586
+ const bluetooth = canvas.getByRole('switch', { name: 'Bluetooth' });
587
+
588
+ await step(
589
+ 'keyboard: the switch is reachable with Tab (WCAG 2.1.1 Keyboard)',
590
+ async () => {
591
+ await userEvent.tab();
592
+ await expect(airplane).toHaveFocus();
593
+ },
594
+ );
595
+
596
+ await step(
597
+ 'keyboard: the focused switch shows a visible focus indicator (WCAG 2.4.7)',
598
+ async () => {
599
+ // `ui-focus-ring` replaces the browser default with the library ring.
600
+ const { boxShadow, outlineStyle } = getComputedStyle(airplane);
601
+ await expect(outlineStyle).toBe('none');
602
+ await expect(boxShadow).not.toBe('none');
603
+ },
604
+ );
605
+
606
+ await step('keyboard: Space turns the switch on', async () => {
607
+ await userEvent.keyboard(' ');
608
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
609
+ await expect(onUpdate).toHaveBeenCalledWith(true);
610
+ });
611
+
612
+ await step('keyboard: Enter turns it back off', async () => {
613
+ await userEvent.keyboard('{Enter}');
614
+ await expect(airplane).toHaveAttribute('aria-checked', 'false');
615
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
616
+ });
617
+
618
+ await step(
619
+ 'keyboard: Tab follows DOM order and skips the disabled switch (WCAG 2.4.3 Focus Order)',
620
+ async () => {
621
+ await userEvent.tab();
622
+ await expect(wifi).toHaveFocus();
623
+ await userEvent.tab();
624
+ await expect(bluetooth).not.toHaveFocus();
625
+ },
626
+ );
627
+ },
628
+ };
629
+
630
+ /**
631
+ * Screen-reader semantics: the switch exposes a `switch` role, takes its
632
+ * accessible name from the visible label, and announces its on, off, and
633
+ * disabled states.
634
+ */
635
+ export const ScreenReader: Story = {
636
+ name: 'test - Screen Reader',
637
+ tags: ['!dev', '!autodocs'],
638
+ render: renderTestSwitches,
639
+ parameters: {
640
+ // The play toggles state; skip the visual snapshot.
641
+ chromatic: { disableSnapshot: true },
642
+ docs: {
643
+ description: {
644
+ story:
645
+ 'Verifies the role, name, and on/off/disabled states that assistive technology announces.',
646
+ },
647
+ },
648
+ },
649
+ play: async ({ canvasElement, step }) => {
650
+ // Scenario (Switch): assistive tech reads each control as a switch named by
651
+ // its visible label, and is told which are on, off, and unavailable.
652
+ const canvas = within(canvasElement);
653
+ const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
654
+ const wifi = canvas.getByRole('switch', { name: 'Wi-Fi' });
655
+ const bluetooth = canvas.getByRole('switch', { name: 'Bluetooth' });
656
+
657
+ await step(
658
+ 'screen-reader: every control exposes a switch role (WCAG 4.1.2)',
659
+ async () => {
660
+ await expect(canvas.getAllByRole('switch')).toHaveLength(3);
661
+ await expect(airplane).toHaveAttribute('role', 'switch');
662
+ },
663
+ );
664
+
665
+ await step(
666
+ 'screen-reader: the visible label supplies the accessible name (WCAG 1.3.1)',
667
+ async () => {
668
+ await expect(airplane).toHaveAccessibleName('Airplane Mode');
669
+ await expect(wifi).toHaveAccessibleName('Wi-Fi');
670
+ await expect(bluetooth).toHaveAccessibleName('Bluetooth');
671
+ },
672
+ );
673
+
674
+ await step(
675
+ 'screen-reader: the on and off states are announced (WCAG 4.1.2)',
676
+ async () => {
677
+ await expect(airplane).toHaveAttribute('aria-checked', 'false');
678
+ await expect(wifi).toHaveAttribute('aria-checked', 'true');
679
+ },
680
+ );
681
+
682
+ await step(
683
+ 'screen-reader: an unavailable switch announces its disabled state (WCAG 4.1.2)',
684
+ async () => {
685
+ await expect(bluetooth).toBeDisabled();
686
+ },
687
+ );
688
+
689
+ await step(
690
+ 'screen-reader: the announced state follows the interaction (WCAG 4.1.2)',
691
+ async () => {
692
+ await userEvent.click(airplane);
693
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
694
+ },
695
+ );
696
+ },
697
+ };
698
+
699
+ /**
700
+ * Rendered at 200% page zoom (WCAG 1.4.4 Resize Text). Use the Zoom toolbar to
701
+ * try 400% reflow (WCAG 1.4.10); the switch and its label must stay visible,
702
+ * readable, and operable.
703
+ */
704
+ export const ZoomMagnification: Story = {
705
+ name: 'test - Zoom Magnification',
706
+ tags: ['!dev', '!autodocs'],
707
+ globals: { zoom: '200' },
708
+ render: renderTestSwitches,
709
+ parameters: {
710
+ // The accessible-reflow Chromatic mode already covers magnified snapshots.
711
+ chromatic: { disableSnapshot: true },
712
+ docs: {
713
+ description: {
714
+ story:
715
+ 'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
716
+ },
717
+ },
718
+ },
719
+ play: async ({ canvasElement, step }) => {
720
+ // Scenario (Switch): at 200% page zoom every switch keeps its label on
721
+ // screen and still toggles.
722
+ const canvas = within(canvasElement);
723
+ const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
724
+
725
+ await step(
726
+ 'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
727
+ async () => {
728
+ await expect(document.documentElement.style.zoom).toBe('200%');
729
+ },
730
+ );
731
+
732
+ await step(
733
+ 'zoom: every switch stays visible with a readable label when magnified',
734
+ async () => {
735
+ for (const toggle of canvas.getAllByRole('switch')) {
736
+ await expect(toggle).toBeVisible();
737
+ await expect(toggle).toHaveAccessibleName();
738
+ }
739
+ await expect(canvas.getByText('Airplane Mode')).toBeVisible();
740
+ },
741
+ );
742
+
743
+ await step('zoom: the switch stays operable when magnified', async () => {
744
+ await userEvent.click(airplane);
745
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
746
+ });
747
+ },
748
+ };
749
+
750
+ /**
751
+ * Rendered at the mobile (320px reflow) breakpoint via the Media breakpoint
752
+ * toolbar. The switch stays operable and, together with its label, meets the
753
+ * minimum touch-target size (WCAG 2.5.8).
754
+ */
755
+ export const MobileBreakpoint: Story = {
756
+ name: 'test - Mobile Breakpoint',
757
+ tags: ['!dev', '!autodocs'],
758
+ globals: { mediaBreakpoint: 'reflow' },
759
+ render: renderTestSwitches,
760
+ parameters: {
761
+ // Focus moves and state toggles during the play function; skip the snapshot.
762
+ chromatic: { disableSnapshot: true },
763
+ docs: {
764
+ description: {
765
+ story:
766
+ 'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
767
+ },
768
+ },
769
+ },
770
+ play: async ({ canvasElement, step }) => {
771
+ // Scenario (Switch): at the 320px mobile breakpoint the switch stays
772
+ // labelled and operable, and the label row provides the touch target the
773
+ // 20px-tall track cannot supply on its own.
774
+ onUpdate.mockClear();
775
+ const canvas = within(canvasElement);
776
+ const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
777
+
778
+ await step(
779
+ 'mobile: the switch fits the 320px viewport without clipping (WCAG 1.4.10 Reflow)',
780
+ async () => {
781
+ await expect(airplane).toBeVisible();
782
+ await expect(
783
+ airplane.getBoundingClientRect().width,
784
+ ).toBeLessThanOrEqual(320);
785
+ await expect(canvas.getByText('Airplane Mode')).toBeVisible();
786
+ },
787
+ );
788
+
789
+ await step(
790
+ 'mobile: the label row meets the minimum touch-target size (WCAG 2.5.8)',
791
+ async () => {
792
+ // The track alone is 36×20, so the row it shares with its label is what
793
+ // has to clear 24×24 — which is why the row carries `min-h-6`.
794
+ const row = airplane.closest('label') as HTMLElement;
795
+ const { width, height } = row.getBoundingClientRect();
796
+ await expect(width).toBeGreaterThanOrEqual(24);
797
+ await expect(height).toBeGreaterThanOrEqual(24);
798
+ },
799
+ );
800
+
801
+ await step(
802
+ 'mobile: stays keyboard operable — Tab focuses and Space toggles (WCAG 2.1.1)',
803
+ async () => {
804
+ await userEvent.tab();
805
+ await expect(airplane).toHaveFocus();
806
+ await userEvent.keyboard(' ');
807
+ await expect(airplane).toHaveAttribute('aria-checked', 'true');
808
+ await expect(onUpdate).toHaveBeenCalledWith(true);
809
+ },
810
+ );
811
+
812
+ await step(
813
+ 'mobile: tapping the label toggles the switch at the 320px breakpoint',
814
+ async () => {
815
+ await userEvent.click(canvas.getByText('Airplane Mode'));
816
+ await expect(airplane).toHaveAttribute('aria-checked', 'false');
817
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
818
+ },
819
+ );
820
+ },
124
821
  };