@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,6 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { expect, fn, userEvent, within } from 'storybook/test';
3
+ import { ref } from 'vue';
2
4
  import UiToggle from './UiToggle.vue';
3
5
  import { UiIcon } from '../UiIcon';
4
6
 
@@ -37,18 +39,25 @@ export default meta;
37
39
 
38
40
  type Story = StoryObj<typeof UiToggle>;
39
41
 
42
+ // `UiToggle` forwards `defineModel`, so it emits `update:modelValue`. Play
43
+ // functions that need to assert the emit bind this module-level spy via
44
+ // `@update:model-value` and `mockClear()` it first.
45
+ const onUpdate = fn();
46
+
40
47
  const defaultTemplateSource = `<script setup lang="ts">
41
48
  import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
42
49
  </script>
43
50
 
44
51
  <template>
45
- <UiToggle aria-label="Toggle bold">
52
+ <UiToggle aria-label="Bold">
46
53
  <UiIcon name="bold" />
47
54
  </UiToggle>
48
55
  </template>`;
49
56
 
50
57
  /**
51
- * Default toggle button with icon.
58
+ * Default toggle, resting in the unpressed state. The icon is decorative, so an
59
+ * icon-only toggle needs an explicit `aria-label` to have a name at all — and
60
+ * that name describes the control ("Bold"), never its current state.
52
61
  */
53
62
  export const Default: Story = {
54
63
  render: (args) => ({
@@ -56,7 +65,7 @@ export const Default: Story = {
56
65
  setup() {
57
66
  return { args };
58
67
  },
59
- template: `<UiToggle :variant="args.variant" :size="args.size" :disabled="args.disabled" aria-label="Toggle bold">
68
+ template: `<UiToggle :variant="args.variant" :size="args.size" :disabled="args.disabled" aria-label="Bold">
60
69
  <UiIcon name="bold" />
61
70
  </UiToggle>`,
62
71
  }),
@@ -67,6 +76,77 @@ export const Default: Story = {
67
76
  },
68
77
  },
69
78
  },
79
+ play: async ({ canvasElement, step }) => {
80
+ // Scenario (Toggle): the resting unpressed state. Nothing is activated
81
+ // here, so the visual snapshot stays stable.
82
+ const canvas = within(canvasElement);
83
+ const toggle = canvas.getByRole('button', { name: 'Bold' });
84
+
85
+ await step(
86
+ 'interaction: the toggle is a button with an accessible name (WCAG 4.1.2)',
87
+ async () => {
88
+ await expect(toggle).toBeVisible();
89
+ await expect(toggle).toHaveAccessibleName('Bold');
90
+ },
91
+ );
92
+
93
+ await step('interaction: the toggle rests unpressed', async () => {
94
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
95
+ await expect(toggle).toHaveAttribute('data-state', 'off');
96
+ });
97
+ },
98
+ };
99
+
100
+ const pressedTemplateSource = `<script setup lang="ts">
101
+ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
102
+ </script>
103
+
104
+ <template>
105
+ <UiToggle default-value aria-label="Bold">
106
+ <UiIcon name="bold" />
107
+ </UiToggle>
108
+ </template>`;
109
+
110
+ /**
111
+ * The pressed resting state. The state is carried by `aria-pressed` and
112
+ * mirrored in `data-state`, so it is never signalled by the fill alone
113
+ * (WCAG 1.4.1 Use of Color).
114
+ */
115
+ export const Pressed: Story = {
116
+ render: () => ({
117
+ components: { UiToggle, UiIcon },
118
+ template: `<UiToggle default-value aria-label="Bold">
119
+ <UiIcon name="bold" />
120
+ </UiToggle>`,
121
+ }),
122
+ parameters: {
123
+ controls: { disable: true },
124
+ docs: {
125
+ source: {
126
+ code: pressedTemplateSource,
127
+ },
128
+ },
129
+ },
130
+ play: async ({ canvasElement, step }) => {
131
+ // Scenario (Toggle): the resting pressed state. Non-mutating, so the
132
+ // visual snapshot stays stable.
133
+ const canvas = within(canvasElement);
134
+ const toggle = canvas.getByRole('button', { name: 'Bold' });
135
+
136
+ await step(
137
+ 'interaction: the toggle rests in the pressed state',
138
+ async () => {
139
+ await expect(toggle).toHaveAttribute('aria-pressed', 'true');
140
+ },
141
+ );
142
+
143
+ await step(
144
+ 'interaction: the pressed state is exposed beyond the fill (WCAG 1.4.1)',
145
+ async () => {
146
+ await expect(toggle).toHaveAttribute('data-state', 'on');
147
+ },
148
+ );
149
+ },
70
150
  };
71
151
 
72
152
  const outlineTemplateSource = `<script setup lang="ts">
@@ -74,28 +154,45 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
74
154
  </script>
75
155
 
76
156
  <template>
77
- <UiToggle variant="outline" aria-label="Toggle bold">
157
+ <UiToggle variant="outline" aria-label="Bold">
78
158
  <UiIcon name="bold" />
79
159
  </UiToggle>
80
160
  </template>`;
81
161
 
82
162
  /**
83
- * Outline variant toggle.
163
+ * Outline variant. The visible border keeps the unpressed toggle discoverable
164
+ * on its own, which the borderless default variant leaves to the surrounding
165
+ * toolbar.
84
166
  */
85
167
  export const Outline: Story = {
86
168
  render: () => ({
87
169
  components: { UiToggle, UiIcon },
88
- template: `<UiToggle variant="outline" aria-label="Toggle bold">
170
+ template: `<UiToggle variant="outline" aria-label="Bold">
89
171
  <UiIcon name="bold" />
90
172
  </UiToggle>`,
91
173
  }),
92
174
  parameters: {
175
+ controls: { disable: true },
93
176
  docs: {
94
177
  source: {
95
178
  code: outlineTemplateSource,
96
179
  },
97
180
  },
98
181
  },
182
+ play: async ({ canvasElement, step }) => {
183
+ // Scenario (Toggle): a style variation carries the same semantics as the
184
+ // default one. Non-mutating, so the visual snapshot stays stable.
185
+ const canvas = within(canvasElement);
186
+ const toggle = canvas.getByRole('button', { name: 'Bold' });
187
+
188
+ await step(
189
+ 'interaction: the outline variant exposes the same name and state',
190
+ async () => {
191
+ await expect(toggle).toHaveAccessibleName('Bold');
192
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
193
+ },
194
+ );
195
+ },
99
196
  };
100
197
 
101
198
  const softTemplateSource = `<script setup lang="ts">
@@ -103,28 +200,108 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
103
200
  </script>
104
201
 
105
202
  <template>
106
- <UiToggle variant="soft" aria-label="Toggle bold">
203
+ <UiToggle variant="soft" aria-label="Bold">
107
204
  <UiIcon name="bold" />
108
205
  </UiToggle>
109
206
  </template>`;
110
207
 
111
208
  /**
112
- * Soft variant toggle with a subtle accent background when pressed.
209
+ * Soft variant, which fills with a subtle accent instead of the solid one when
210
+ * pressed. The quieter fill is why `aria-pressed` matters here.
113
211
  */
114
212
  export const Soft: Story = {
115
213
  render: () => ({
116
214
  components: { UiToggle, UiIcon },
117
- template: `<UiToggle variant="soft" aria-label="Toggle bold">
215
+ template: `<UiToggle variant="soft" aria-label="Bold">
118
216
  <UiIcon name="bold" />
119
217
  </UiToggle>`,
120
218
  }),
121
219
  parameters: {
220
+ controls: { disable: true },
122
221
  docs: {
123
222
  source: {
124
223
  code: softTemplateSource,
125
224
  },
126
225
  },
127
226
  },
227
+ play: async ({ canvasElement, step }) => {
228
+ // Scenario (Toggle): a style variation carries the same semantics as the
229
+ // default one. Non-mutating, so the visual snapshot stays stable.
230
+ const canvas = within(canvasElement);
231
+ const toggle = canvas.getByRole('button', { name: 'Bold' });
232
+
233
+ await step(
234
+ 'interaction: the soft variant exposes the same name and state',
235
+ async () => {
236
+ await expect(toggle).toHaveAccessibleName('Bold');
237
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
238
+ },
239
+ );
240
+ },
241
+ };
242
+
243
+ const iconOnlyTemplateSource = `<script setup lang="ts">
244
+ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
245
+ </script>
246
+
247
+ <template>
248
+ <div class="flex items-center gap-2">
249
+ <UiToggle aria-label="Bold"><UiIcon name="bold" /></UiToggle>
250
+ <UiToggle aria-label="Italic"><UiIcon name="italic" /></UiToggle>
251
+ <UiToggle aria-label="Underline"><UiIcon name="underline" /></UiToggle>
252
+ </div>
253
+ </template>`;
254
+
255
+ /**
256
+ * Icon-only toggles, as used in a formatting toolbar. `UiIcon` renders
257
+ * `aria-hidden`, so without an `aria-label` these buttons would reach assistive
258
+ * technology with no name at all.
259
+ */
260
+ export const IconOnly: Story = {
261
+ render: () => ({
262
+ components: { UiToggle, UiIcon },
263
+ template: `
264
+ <div class="flex items-center gap-2">
265
+ <UiToggle aria-label="Bold"><UiIcon name="bold" /></UiToggle>
266
+ <UiToggle aria-label="Italic"><UiIcon name="italic" /></UiToggle>
267
+ <UiToggle aria-label="Underline"><UiIcon name="underline" /></UiToggle>
268
+ </div>
269
+ `,
270
+ }),
271
+ parameters: {
272
+ controls: { disable: true },
273
+ docs: {
274
+ source: {
275
+ code: iconOnlyTemplateSource,
276
+ },
277
+ },
278
+ },
279
+ play: async ({ canvasElement, step }) => {
280
+ // Scenario (Toggle): icon-only usage. Non-mutating, so the visual snapshot
281
+ // stays stable.
282
+ const canvas = within(canvasElement);
283
+
284
+ await step(
285
+ 'screen-reader: every icon-only toggle carries a name (WCAG 1.1.1 / 4.1.2)',
286
+ async () => {
287
+ for (const name of ['Bold', 'Italic', 'Underline']) {
288
+ await expect(
289
+ canvas.getByRole('button', { name }),
290
+ ).toHaveAccessibleName(name);
291
+ }
292
+ },
293
+ );
294
+
295
+ await step(
296
+ 'screen-reader: the decorative icon is hidden from assistive technology',
297
+ async () => {
298
+ const icon = canvas
299
+ .getByRole('button', { name: 'Bold' })
300
+ .querySelector('svg');
301
+ await expect(icon).toHaveAttribute('aria-hidden', 'true');
302
+ },
303
+ );
304
+ },
128
305
  };
129
306
 
130
307
  const withTextTemplateSource = `<script setup lang="ts">
@@ -132,30 +309,48 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
132
309
  </script>
133
310
 
134
311
  <template>
135
- <UiToggle aria-label="Toggle italic">
312
+ <UiToggle>
136
313
  <UiIcon name="italic" />
137
314
  Italic
138
315
  </UiToggle>
139
316
  </template>`;
140
317
 
141
318
  /**
142
- * Toggle with text label.
319
+ * Toggle with a text label. The visible text already names the button, so no
320
+ * `aria-label` is needed — and adding one that disagrees with the text would
321
+ * break speech input (WCAG 2.5.3 Label in Name).
143
322
  */
144
323
  export const WithText: Story = {
145
324
  render: () => ({
146
325
  components: { UiToggle, UiIcon },
147
- template: `<UiToggle aria-label="Toggle italic">
326
+ template: `<UiToggle>
148
327
  <UiIcon name="italic" />
149
328
  Italic
150
329
  </UiToggle>`,
151
330
  }),
152
331
  parameters: {
332
+ controls: { disable: true },
153
333
  docs: {
154
334
  source: {
155
335
  code: withTextTemplateSource,
156
336
  },
157
337
  },
158
338
  },
339
+ play: async ({ canvasElement, step }) => {
340
+ // Scenario (Toggle): usage with text. Non-mutating, so the visual snapshot
341
+ // stays stable.
342
+ const canvas = within(canvasElement);
343
+ const toggle = canvas.getByRole('button', { name: 'Italic' });
344
+
345
+ await step(
346
+ 'screen-reader: the visible text is the accessible name (WCAG 2.5.3 Label in Name)',
347
+ async () => {
348
+ await expect(toggle).toHaveAccessibleName('Italic');
349
+ await expect(toggle).not.toHaveAttribute('aria-label');
350
+ await expect(canvas.getByText('Italic')).toBeVisible();
351
+ },
352
+ );
353
+ },
159
354
  };
160
355
 
161
356
  const smallTemplateSource = `<script setup lang="ts">
@@ -163,28 +358,45 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
163
358
  </script>
164
359
 
165
360
  <template>
166
- <UiToggle size="sm" aria-label="Toggle italic">
361
+ <UiToggle size="sm" aria-label="Italic">
167
362
  <UiIcon name="italic" />
168
363
  </UiToggle>
169
364
  </template>`;
170
365
 
171
366
  /**
172
- * Small size toggle.
367
+ * Small size — the tightest of the three, and still 32×32px, comfortably over
368
+ * the 24×24px floor in WCAG 2.5.8 Target Size (Minimum).
173
369
  */
174
370
  export const Small: Story = {
175
371
  render: () => ({
176
372
  components: { UiToggle, UiIcon },
177
- template: `<UiToggle size="sm" aria-label="Toggle italic">
373
+ template: `<UiToggle size="sm" aria-label="Italic">
178
374
  <UiIcon name="italic" />
179
375
  </UiToggle>`,
180
376
  }),
181
377
  parameters: {
378
+ controls: { disable: true },
182
379
  docs: {
183
380
  source: {
184
381
  code: smallTemplateSource,
185
382
  },
186
383
  },
187
384
  },
385
+ play: async ({ canvasElement, step }) => {
386
+ // Scenario (Toggle): the smallest size variation still has to be tappable.
387
+ // Non-mutating, so the visual snapshot stays stable.
388
+ const canvas = within(canvasElement);
389
+ const toggle = canvas.getByRole('button', { name: 'Italic' });
390
+
391
+ await step(
392
+ 'mobile: the small toggle meets the minimum target size (WCAG 2.5.8)',
393
+ async () => {
394
+ const { width, height } = toggle.getBoundingClientRect();
395
+ await expect(width).toBeGreaterThanOrEqual(24);
396
+ await expect(height).toBeGreaterThanOrEqual(24);
397
+ },
398
+ );
399
+ },
188
400
  };
189
401
 
190
402
  const largeTemplateSource = `<script setup lang="ts">
@@ -192,28 +404,44 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
192
404
  </script>
193
405
 
194
406
  <template>
195
- <UiToggle size="lg" aria-label="Toggle italic">
407
+ <UiToggle size="lg" aria-label="Italic">
196
408
  <UiIcon name="italic" />
197
409
  </UiToggle>
198
410
  </template>`;
199
411
 
200
412
  /**
201
- * Large size toggle.
413
+ * Large size, for touch-first surfaces and toolbars that sit on their own row.
202
414
  */
203
415
  export const Large: Story = {
204
416
  render: () => ({
205
417
  components: { UiToggle, UiIcon },
206
- template: `<UiToggle size="lg" aria-label="Toggle italic">
418
+ template: `<UiToggle size="lg" aria-label="Italic">
207
419
  <UiIcon name="italic" />
208
420
  </UiToggle>`,
209
421
  }),
210
422
  parameters: {
423
+ controls: { disable: true },
211
424
  docs: {
212
425
  source: {
213
426
  code: largeTemplateSource,
214
427
  },
215
428
  },
216
429
  },
430
+ play: async ({ canvasElement, step }) => {
431
+ // Scenario (Toggle): the largest size variation. Non-mutating, so the
432
+ // visual snapshot stays stable.
433
+ const canvas = within(canvasElement);
434
+ const toggle = canvas.getByRole('button', { name: 'Italic' });
435
+
436
+ await step(
437
+ 'mobile: the large toggle meets the minimum target size (WCAG 2.5.8)',
438
+ async () => {
439
+ const { width, height } = toggle.getBoundingClientRect();
440
+ await expect(width).toBeGreaterThanOrEqual(24);
441
+ await expect(height).toBeGreaterThanOrEqual(24);
442
+ },
443
+ );
444
+ },
217
445
  };
218
446
 
219
447
  const disabledTemplateSource = `<script setup lang="ts">
@@ -221,28 +449,94 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
221
449
  </script>
222
450
 
223
451
  <template>
224
- <UiToggle disabled aria-label="Toggle underline">
225
- <UiIcon name="underline" />
226
- </UiToggle>
452
+ <div class="flex items-center gap-2">
453
+ <UiToggle disabled aria-label="Underline">
454
+ <UiIcon name="underline" />
455
+ </UiToggle>
456
+ <UiToggle disabled default-value aria-label="Strikethrough">
457
+ <UiIcon name="strikethrough" />
458
+ </UiToggle>
459
+ </div>
227
460
  </template>`;
228
461
 
229
462
  /**
230
- * Disabled toggle.
463
+ * Disabled in both resting states. `UiToggle` renders a native `button`, so a
464
+ * disabled toggle reports `disabled` to assistive technology and drops out of
465
+ * the tab order — the state is never conveyed by dimming alone.
231
466
  */
232
467
  export const Disabled: Story = {
468
+ args: {
469
+ disabled: true,
470
+ },
233
471
  render: () => ({
234
472
  components: { UiToggle, UiIcon },
235
- template: `<UiToggle disabled aria-label="Toggle underline">
236
- <UiIcon name="underline" />
237
- </UiToggle>`,
473
+ setup() {
474
+ return { onUpdate };
475
+ },
476
+ template: `
477
+ <div class="flex items-center gap-2">
478
+ <UiToggle disabled aria-label="Underline" @update:model-value="onUpdate">
479
+ <UiIcon name="underline" />
480
+ </UiToggle>
481
+ <UiToggle disabled default-value aria-label="Strikethrough" @update:model-value="onUpdate">
482
+ <UiIcon name="strikethrough" />
483
+ </UiToggle>
484
+ </div>
485
+ `,
238
486
  }),
239
487
  parameters: {
488
+ controls: { disable: true },
240
489
  docs: {
241
490
  source: {
242
491
  code: disabledTemplateSource,
243
492
  },
244
493
  },
245
494
  },
495
+ play: async ({ canvasElement, step }) => {
496
+ // Scenario (Toggle): a disabled toggle keeps its pressed state readable,
497
+ // refuses activation, and is skipped by Tab. The forced click is a no-op,
498
+ // so the snapshot stays stable.
499
+ onUpdate.mockClear();
500
+ const canvas = within(canvasElement);
501
+ const unpressed = canvas.getByRole('button', { name: 'Underline' });
502
+ const pressed = canvas.getByRole('button', { name: 'Strikethrough' });
503
+
504
+ await step(
505
+ 'interaction: both toggles report the disabled state (WCAG 4.1.2)',
506
+ async () => {
507
+ await expect(unpressed).toBeDisabled();
508
+ await expect(pressed).toBeDisabled();
509
+ },
510
+ );
511
+
512
+ await step(
513
+ 'interaction: a disabled toggle still announces whether it is pressed',
514
+ async () => {
515
+ await expect(unpressed).toHaveAttribute('aria-pressed', 'false');
516
+ await expect(pressed).toHaveAttribute('aria-pressed', 'true');
517
+ },
518
+ );
519
+
520
+ await step(
521
+ 'interaction: a disabled toggle does not change state',
522
+ async () => {
523
+ // Disabled controls render with pointer-events: none, so bypass the
524
+ // check and assert the click is still a no-op.
525
+ await userEvent.click(unpressed, { pointerEventsCheck: 0 });
526
+ await expect(unpressed).toHaveAttribute('aria-pressed', 'false');
527
+ await expect(onUpdate).not.toHaveBeenCalled();
528
+ },
529
+ );
530
+
531
+ await step(
532
+ 'keyboard: a disabled toggle is skipped by Tab (WCAG 2.1.1)',
533
+ async () => {
534
+ await userEvent.tab();
535
+ await expect(unpressed).not.toHaveFocus();
536
+ await expect(pressed).not.toHaveFocus();
537
+ },
538
+ );
539
+ },
246
540
  };
247
541
 
248
542
  const allStatesTemplateSource = `<script setup lang="ts">
@@ -251,28 +545,16 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
251
545
 
252
546
  <template>
253
547
  <div class="flex flex-col gap-6">
254
- <!-- Variants -->
548
+ <!-- Variants, unpressed and pressed -->
255
549
  <div>
256
550
  <p class="body-14-medium mb-2">Variants</p>
257
551
  <div class="flex items-center gap-2">
258
- <UiToggle aria-label="Toggle bold">
259
- <UiIcon name="bold" />
260
- </UiToggle>
261
- <UiToggle default-value aria-label="Toggle bold (pressed)">
262
- <UiIcon name="bold" />
263
- </UiToggle>
264
- <UiToggle variant="outline" aria-label="Toggle italic">
265
- <UiIcon name="italic" />
266
- </UiToggle>
267
- <UiToggle variant="outline" default-value aria-label="Toggle italic (pressed)">
268
- <UiIcon name="italic" />
269
- </UiToggle>
270
- <UiToggle variant="soft" aria-label="Toggle underline">
271
- <UiIcon name="underline" />
272
- </UiToggle>
273
- <UiToggle variant="soft" default-value aria-label="Toggle underline (pressed)">
274
- <UiIcon name="underline" />
275
- </UiToggle>
552
+ <UiToggle aria-label="Bold"><UiIcon name="bold" /></UiToggle>
553
+ <UiToggle default-value aria-label="Bold"><UiIcon name="bold" /></UiToggle>
554
+ <UiToggle variant="outline" aria-label="Italic"><UiIcon name="italic" /></UiToggle>
555
+ <UiToggle variant="outline" default-value aria-label="Italic"><UiIcon name="italic" /></UiToggle>
556
+ <UiToggle variant="soft" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
557
+ <UiToggle variant="soft" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
276
558
  </div>
277
559
  </div>
278
560
 
@@ -280,26 +562,18 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
280
562
  <div>
281
563
  <p class="body-14-medium mb-2">Sizes</p>
282
564
  <div class="flex items-end gap-2">
283
- <UiToggle size="sm" aria-label="Toggle bold">
284
- <UiIcon name="bold" />
285
- </UiToggle>
286
- <UiToggle aria-label="Toggle bold">
287
- <UiIcon name="bold" />
288
- </UiToggle>
289
- <UiToggle size="lg" aria-label="Toggle bold">
290
- <UiIcon name="bold" />
291
- </UiToggle>
565
+ <UiToggle size="sm" aria-label="Align left"><UiIcon name="align-left" /></UiToggle>
566
+ <UiToggle aria-label="Align center"><UiIcon name="align-center" /></UiToggle>
567
+ <UiToggle size="lg" aria-label="Align right"><UiIcon name="align-right" /></UiToggle>
292
568
  </div>
293
569
  </div>
294
570
 
295
- <!-- With Text -->
571
+ <!-- With text -->
296
572
  <div>
297
573
  <p class="body-14-medium mb-2">With Text</p>
298
574
  <div class="flex items-center gap-2">
299
- <UiToggle aria-label="Toggle italic">
300
- <UiIcon name="italic" />
301
- Italic
302
- </UiToggle>
575
+ <UiToggle><UiIcon name="italic" /> Italic</UiToggle>
576
+ <UiToggle default-value><UiIcon name="italic" /> Italic</UiToggle>
303
577
  </div>
304
578
  </div>
305
579
 
@@ -307,121 +581,678 @@ import { UiToggle, UiIcon } from '@aleph-alpha/ui-library'
307
581
  <div>
308
582
  <p class="body-14-medium mb-2">Disabled</p>
309
583
  <div class="flex items-center gap-2">
310
- <UiToggle disabled aria-label="Toggle underline">
311
- <UiIcon name="underline" />
312
- </UiToggle>
313
- <UiToggle disabled default-value aria-label="Toggle underline (pressed)">
314
- <UiIcon name="underline" />
315
- </UiToggle>
316
- <UiToggle disabled variant="outline" aria-label="Toggle underline">
317
- <UiIcon name="underline" />
318
- </UiToggle>
319
- <UiToggle disabled variant="outline" default-value aria-label="Toggle underline (pressed)">
320
- <UiIcon name="underline" />
321
- </UiToggle>
322
- <UiToggle disabled variant="soft" aria-label="Toggle underline">
323
- <UiIcon name="underline" />
324
- </UiToggle>
325
- <UiToggle disabled variant="soft" default-value aria-label="Toggle underline (pressed)">
326
- <UiIcon name="underline" />
327
- </UiToggle>
584
+ <UiToggle disabled aria-label="Underline"><UiIcon name="underline" /></UiToggle>
585
+ <UiToggle disabled default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
586
+ <UiToggle disabled variant="outline" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
587
+ <UiToggle disabled variant="outline" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
588
+ <UiToggle disabled variant="soft" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
589
+ <UiToggle disabled variant="soft" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
328
590
  </div>
329
591
  </div>
330
592
  </div>
331
593
  </template>`;
332
594
 
333
595
  /**
334
- * All toggle states displayed together: variants, sizes, with text, and disabled states.
596
+ * Every combination side by side: the three variants unpressed and pressed, the
597
+ * three sizes, usage with text, and the disabled states. Each pressed toggle
598
+ * sits next to its unpressed twin so the pressed styling can be compared
599
+ * directly.
335
600
  */
336
601
  export const AllStates: Story = {
337
602
  render: () => ({
338
603
  components: { UiToggle, UiIcon },
339
604
  template: `
340
605
  <div class="flex flex-col gap-6">
341
- <!-- Variants -->
342
606
  <div>
343
607
  <p class="body-14-medium mb-2">Variants</p>
344
608
  <div class="flex items-center gap-2">
345
- <UiToggle aria-label="Toggle bold">
346
- <UiIcon name="bold" />
347
- </UiToggle>
348
- <UiToggle default-value aria-label="Toggle bold (pressed)">
349
- <UiIcon name="bold" />
350
- </UiToggle>
351
- <UiToggle variant="outline" aria-label="Toggle italic">
352
- <UiIcon name="italic" />
353
- </UiToggle>
354
- <UiToggle variant="outline" default-value aria-label="Toggle italic (pressed)">
355
- <UiIcon name="italic" />
356
- </UiToggle>
357
- <UiToggle variant="soft" aria-label="Toggle underline">
358
- <UiIcon name="underline" />
359
- </UiToggle>
360
- <UiToggle variant="soft" default-value aria-label="Toggle underline (pressed)">
361
- <UiIcon name="underline" />
362
- </UiToggle>
609
+ <UiToggle aria-label="Bold"><UiIcon name="bold" /></UiToggle>
610
+ <UiToggle default-value aria-label="Bold"><UiIcon name="bold" /></UiToggle>
611
+ <UiToggle variant="outline" aria-label="Italic"><UiIcon name="italic" /></UiToggle>
612
+ <UiToggle variant="outline" default-value aria-label="Italic"><UiIcon name="italic" /></UiToggle>
613
+ <UiToggle variant="soft" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
614
+ <UiToggle variant="soft" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
363
615
  </div>
364
616
  </div>
365
617
 
366
- <!-- Sizes -->
367
618
  <div>
368
619
  <p class="body-14-medium mb-2">Sizes</p>
369
620
  <div class="flex items-end gap-2">
370
- <UiToggle size="sm" aria-label="Toggle bold">
371
- <UiIcon name="bold" />
372
- </UiToggle>
373
- <UiToggle aria-label="Toggle bold">
374
- <UiIcon name="bold" />
375
- </UiToggle>
376
- <UiToggle size="lg" aria-label="Toggle bold">
377
- <UiIcon name="bold" />
378
- </UiToggle>
621
+ <UiToggle size="sm" aria-label="Align left"><UiIcon name="align-left" /></UiToggle>
622
+ <UiToggle aria-label="Align center"><UiIcon name="align-center" /></UiToggle>
623
+ <UiToggle size="lg" aria-label="Align right"><UiIcon name="align-right" /></UiToggle>
379
624
  </div>
380
625
  </div>
381
626
 
382
- <!-- With Text -->
383
627
  <div>
384
628
  <p class="body-14-medium mb-2">With Text</p>
385
629
  <div class="flex items-center gap-2">
386
- <UiToggle aria-label="Toggle italic">
387
- <UiIcon name="italic" />
388
- Italic
389
- </UiToggle>
630
+ <UiToggle><UiIcon name="italic" /> Italic</UiToggle>
631
+ <UiToggle default-value><UiIcon name="italic" /> Italic</UiToggle>
390
632
  </div>
391
633
  </div>
392
634
 
393
- <!-- Disabled -->
394
635
  <div>
395
636
  <p class="body-14-medium mb-2">Disabled</p>
396
637
  <div class="flex items-center gap-2">
397
- <UiToggle disabled aria-label="Toggle underline">
398
- <UiIcon name="underline" />
399
- </UiToggle>
400
- <UiToggle disabled default-value aria-label="Toggle underline (pressed)">
401
- <UiIcon name="underline" />
402
- </UiToggle>
403
- <UiToggle disabled variant="outline" aria-label="Toggle underline">
404
- <UiIcon name="underline" />
405
- </UiToggle>
406
- <UiToggle disabled variant="outline" default-value aria-label="Toggle underline (pressed)">
407
- <UiIcon name="underline" />
408
- </UiToggle>
409
- <UiToggle disabled variant="soft" aria-label="Toggle underline">
410
- <UiIcon name="underline" />
411
- </UiToggle>
412
- <UiToggle disabled variant="soft" default-value aria-label="Toggle underline (pressed)">
413
- <UiIcon name="underline" />
414
- </UiToggle>
638
+ <UiToggle disabled aria-label="Underline"><UiIcon name="underline" /></UiToggle>
639
+ <UiToggle disabled default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
640
+ <UiToggle disabled variant="outline" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
641
+ <UiToggle disabled variant="outline" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
642
+ <UiToggle disabled variant="soft" aria-label="Underline"><UiIcon name="underline" /></UiToggle>
643
+ <UiToggle disabled variant="soft" default-value aria-label="Underline"><UiIcon name="underline" /></UiToggle>
415
644
  </div>
416
645
  </div>
417
646
  </div>
418
647
  `,
419
648
  }),
420
649
  parameters: {
650
+ controls: { disable: true },
421
651
  docs: {
422
652
  source: {
423
653
  code: allStatesTemplateSource,
424
654
  },
425
655
  },
426
656
  },
657
+ play: async ({ canvasElement, step }) => {
658
+ // Scenario (Toggle): the overview grid. Non-mutating, so the visual
659
+ // snapshot stays stable.
660
+ const canvas = within(canvasElement);
661
+
662
+ await step(
663
+ 'interaction: every toggle in the overview exposes a pressed state and a name',
664
+ async () => {
665
+ const toggles = canvas.getAllByRole('button');
666
+ await expect(toggles.length).toBeGreaterThan(0);
667
+ for (const toggle of toggles) {
668
+ await expect(toggle).toHaveAttribute('aria-pressed');
669
+ await expect(toggle).toHaveAccessibleName();
670
+ }
671
+ },
672
+ );
673
+
674
+ await step(
675
+ 'interaction: pressed and unpressed twins differ in state, not in name',
676
+ async () => {
677
+ const [unpressed, pressed] = canvas.getAllByRole('button', {
678
+ name: 'Bold',
679
+ });
680
+ await expect(unpressed).toHaveAttribute('aria-pressed', 'false');
681
+ await expect(pressed).toHaveAttribute('aria-pressed', 'true');
682
+ },
683
+ );
684
+ },
685
+ };
686
+
687
+ /**
688
+ * Accessibility guidance for toggle buttons: an explicit name when the toggle
689
+ * is icon-only, a name that describes the control rather than its state, and a
690
+ * pressed state that survives without colour — shown side by side in the
691
+ * default and accessible themes.
692
+ */
693
+ export const Accessibility: Story = {
694
+ tags: ['!autodocs'],
695
+ parameters: {
696
+ a11y: { test: 'off' },
697
+ chromatic: { disableSnapshot: true },
698
+ controls: { disable: true },
699
+ docs: {
700
+ canvas: {
701
+ sourceState: 'none',
702
+ },
703
+ },
704
+ },
705
+ render: () => ({
706
+ components: { UiToggle, UiIcon },
707
+ setup() {
708
+ const defaultBold = ref(true);
709
+ const accessibleBold = ref(true);
710
+ return { defaultBold, accessibleBold };
711
+ },
712
+ template: `
713
+ <section class="flex flex-col gap-6 p-4">
714
+ <div>
715
+ <h2 class="heading-m font-bold text-content-on-surface-primary">
716
+ Accessibility
717
+ </h2>
718
+ </div>
719
+
720
+ <div class="flex flex-col gap-6">
721
+ <div class="flex flex-col gap-2">
722
+ <h3 class="body-16-medium text-content-on-surface-primary">
723
+ Name an icon-only toggle explicitly
724
+ </h3>
725
+ <p class="body-16-regular text-content-on-surface-primary">
726
+ <code>UiIcon</code> renders <code>aria-hidden</code>, so a toggle
727
+ that contains nothing but an icon has no accessible name. Give it
728
+ an <code>aria-label</code>; without one, screen readers announce
729
+ only "button".
730
+ </p>
731
+ </div>
732
+
733
+ <div class="flex flex-col gap-2">
734
+ <h3 class="body-16-medium text-content-on-surface-primary">
735
+ Name the control, not its state
736
+ </h3>
737
+ <p class="body-16-regular text-content-on-surface-primary">
738
+ Pressed and unpressed are announced through
739
+ <code>aria-pressed</code>, which the component sets for you. Label
740
+ the toggle "Bold", not "Bold (pressed)" or "Turn bold off", so the
741
+ name stays correct in both states.
742
+ </p>
743
+ </div>
744
+
745
+ <div class="flex flex-col gap-2">
746
+ <h3 class="body-16-medium text-content-on-surface-primary">
747
+ Keep the visible text in the accessible name
748
+ </h3>
749
+ <p class="body-16-regular text-content-on-surface-primary">
750
+ When the toggle has text, let that text be the name and skip the
751
+ <code>aria-label</code>. An override that does not contain the
752
+ visible words leaves speech-input users unable to activate the
753
+ control by saying what they see (WCAG 2.5.3 Label in Name).
754
+ </p>
755
+ </div>
756
+
757
+ <div class="flex flex-col gap-2">
758
+ <h3 class="body-16-medium text-content-on-surface-primary">
759
+ Do not rely on the fill alone
760
+ </h3>
761
+ <p class="body-16-regular text-content-on-surface-primary">
762
+ The default variant is borderless until it is pressed, so the only
763
+ visual difference is the accent fill. That is fine for sighted
764
+ users because <code>aria-pressed</code> carries the same
765
+ information to everyone else (WCAG 1.4.1 Use of Color) — but in a
766
+ dense toolbar prefer the <code>outline</code> variant so the
767
+ unpressed buttons stay discoverable.
768
+ </p>
769
+ </div>
770
+
771
+ <div class="flex flex-col gap-2">
772
+ <h3 class="body-16-medium text-content-on-surface-primary">
773
+ All three sizes are large enough to tap
774
+ </h3>
775
+ <p class="body-16-regular text-content-on-surface-primary">
776
+ Small is 32×32px, default 36×36px and large 40×40px, so every size
777
+ clears the 24×24px floor in WCAG 2.5.8 Target Size (Minimum)
778
+ without extra padding.
779
+ </p>
780
+ </div>
781
+
782
+ <div class="flex flex-col gap-2">
783
+ <h3 class="body-16-medium text-content-on-surface-primary">
784
+ Enable accessibility theme
785
+ </h3>
786
+ <p class="body-16-regular text-content-on-surface-primary">
787
+ When accessibility is required, enable our accessibility theme.
788
+ This increases contrast and makes sure there are no color contrast
789
+ issues.
790
+ </p>
791
+ </div>
792
+ </div>
793
+
794
+ <div class="grid gap-4 md:grid-cols-2">
795
+ <div class="flex flex-col gap-3">
796
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
797
+ Default
798
+ </h3>
799
+ <div class="flex min-h-20 items-center justify-center gap-2 border border-border-surface-default p-6">
800
+ <UiToggle v-model="defaultBold" variant="outline" aria-label="Bold">
801
+ <UiIcon name="bold" />
802
+ </UiToggle>
803
+ <UiToggle variant="outline" aria-label="Italic">
804
+ <UiIcon name="italic" />
805
+ </UiToggle>
806
+ <UiToggle variant="outline" aria-label="Underline">
807
+ <UiIcon name="underline" />
808
+ </UiToggle>
809
+ </div>
810
+ </div>
811
+
812
+ <div class="accessible-theme flex flex-col gap-3">
813
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
814
+ Accessible
815
+ </h3>
816
+ <div class="flex min-h-20 items-center justify-center gap-2 border border-border-surface-default p-6">
817
+ <UiToggle v-model="accessibleBold" variant="outline" aria-label="Bold">
818
+ <UiIcon name="bold" />
819
+ </UiToggle>
820
+ <UiToggle variant="outline" aria-label="Italic">
821
+ <UiIcon name="italic" />
822
+ </UiToggle>
823
+ <UiToggle variant="outline" aria-label="Underline">
824
+ <UiIcon name="underline" />
825
+ </UiToggle>
826
+ </div>
827
+ </div>
828
+ </div>
829
+ </section>
830
+ `,
831
+ }),
832
+ };
833
+
834
+ // Test-only stories drive the test-runner (interactive/a11y checks) but are
835
+ // hidden from the Storybook sidebar and docs via the `!dev` / `!autodocs`
836
+ // tags. They keep the implicit `test` tag, so they still run in CI and appear
837
+ // in the automated report under their `test - …` name. `name`/`tags` must be
838
+ // literal properties so Storybook's static CSF indexer can read them.
839
+
840
+ // Shared render for the test scenarios: a formatting toolbar with an icon-only
841
+ // toggle that starts unpressed, one that starts pressed, one labelled by its
842
+ // visible text, and a disabled one — a single fixture covering naming, both
843
+ // resting states, activation, and the disabled guard. The three sizes sit
844
+ // alongside so the target-size checks have something to measure.
845
+ function renderTestToggles() {
846
+ return {
847
+ components: { UiToggle, UiIcon },
848
+ setup() {
849
+ const bold = ref(false);
850
+ return { bold, onUpdate };
851
+ },
852
+ template: `
853
+ <div class="flex flex-col items-start gap-4">
854
+ <div class="flex items-center gap-2">
855
+ <UiToggle
856
+ v-model="bold"
857
+ variant="outline"
858
+ aria-label="Bold"
859
+ @update:model-value="onUpdate"
860
+ >
861
+ <UiIcon name="bold" />
862
+ </UiToggle>
863
+
864
+ <UiToggle variant="outline" default-value aria-label="Italic">
865
+ <UiIcon name="italic" />
866
+ </UiToggle>
867
+
868
+ <UiToggle variant="outline">
869
+ <UiIcon name="underline" />
870
+ Underline
871
+ </UiToggle>
872
+
873
+ <UiToggle variant="outline" disabled default-value aria-label="Strikethrough">
874
+ <UiIcon name="strikethrough" />
875
+ </UiToggle>
876
+ </div>
877
+
878
+ <div class="flex items-end gap-2">
879
+ <UiToggle size="sm" variant="outline" aria-label="Align left">
880
+ <UiIcon name="align-left" />
881
+ </UiToggle>
882
+ <UiToggle variant="outline" aria-label="Align center">
883
+ <UiIcon name="align-center" />
884
+ </UiToggle>
885
+ <UiToggle size="lg" variant="outline" aria-label="Align right">
886
+ <UiIcon name="align-right" />
887
+ </UiToggle>
888
+ </div>
889
+ </div>
890
+ `,
891
+ };
892
+ }
893
+
894
+ /**
895
+ * Press behaviour: activating the toggle switches it between pressed and
896
+ * unpressed and emits the new value, a toggle that starts pressed releases
897
+ * independently, and a disabled toggle refuses to change state.
898
+ */
899
+ export const ToggleBehavior: Story = {
900
+ name: 'test - Toggle Behavior',
901
+ tags: ['!dev', '!autodocs'],
902
+ render: renderTestToggles,
903
+ parameters: {
904
+ // The play changes the pressed state; skip the visual snapshot.
905
+ chromatic: { disableSnapshot: true },
906
+ docs: {
907
+ description: {
908
+ story:
909
+ 'Press and release the toggle, confirm the neighbours are unaffected, and check the disabled guard holds.',
910
+ },
911
+ },
912
+ },
913
+ play: async ({ canvasElement, step }) => {
914
+ // Scenario (Toggle): a pointer user presses a toggle, releases it again,
915
+ // releases a second one that started pressed, and finds the disabled one
916
+ // unresponsive.
917
+ onUpdate.mockClear();
918
+ const canvas = within(canvasElement);
919
+ const bold = canvas.getByRole('button', { name: 'Bold' });
920
+ const italic = canvas.getByRole('button', { name: 'Italic' });
921
+
922
+ await step(
923
+ 'interaction: activating the toggle presses it and emits the new value',
924
+ async () => {
925
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
926
+ await userEvent.click(bold);
927
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
928
+ await expect(onUpdate).toHaveBeenCalledWith(true);
929
+ },
930
+ );
931
+
932
+ await step(
933
+ 'interaction: activating it again releases it back to unpressed',
934
+ async () => {
935
+ await userEvent.click(bold);
936
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
937
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
938
+ },
939
+ );
940
+
941
+ await step(
942
+ 'interaction: a toggle that starts pressed releases independently',
943
+ async () => {
944
+ await expect(italic).toHaveAttribute('aria-pressed', 'true');
945
+ await userEvent.click(italic);
946
+ await expect(italic).toHaveAttribute('aria-pressed', 'false');
947
+ // The neighbour is untouched — these are independent toggles, not a
948
+ // radio group.
949
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
950
+ },
951
+ );
952
+
953
+ await step(
954
+ 'interaction: a toggle with text is activated by clicking its label text',
955
+ async () => {
956
+ const underline = canvas.getByRole('button', { name: 'Underline' });
957
+ await userEvent.click(canvas.getByText('Underline'));
958
+ await expect(underline).toHaveAttribute('aria-pressed', 'true');
959
+ },
960
+ );
961
+
962
+ await step(
963
+ 'interaction: a disabled toggle does not change state (WCAG 4.1.2)',
964
+ async () => {
965
+ const strikethrough = canvas.getByRole('button', {
966
+ name: 'Strikethrough',
967
+ });
968
+ await expect(strikethrough).toBeDisabled();
969
+ // Disabled controls render with pointer-events: none, so bypass the
970
+ // check and assert the click is still a no-op.
971
+ await userEvent.click(strikethrough, { pointerEventsCheck: 0 });
972
+ await expect(strikethrough).toHaveAttribute('aria-pressed', 'true');
973
+ },
974
+ );
975
+ },
976
+ };
977
+
978
+ /**
979
+ * `UiToggle` is keyboard operable: `Tab` reaches it, and both `Space` and
980
+ * `Enter` flip the pressed state. A disabled toggle is skipped entirely.
981
+ */
982
+ export const KeyboardNavigation: Story = {
983
+ name: 'test - Keyboard Navigation',
984
+ tags: ['!dev', '!autodocs'],
985
+ render: renderTestToggles,
986
+ parameters: {
987
+ // Focus moves during the play function; skip the visual snapshot so the
988
+ // focus ring doesn't register as a Chromatic diff.
989
+ chromatic: { disableSnapshot: true },
990
+ docs: {
991
+ description: {
992
+ story:
993
+ 'Press `Tab` to reach the toggle, then `Space` or `Enter` to press and release it.',
994
+ },
995
+ },
996
+ },
997
+ play: async ({ canvasElement, step }) => {
998
+ // Scenario (Toggle): a keyboard user tabs to the first toggle, presses it
999
+ // with both Space and Enter, moves on through the toolbar, and never lands
1000
+ // on the disabled toggle.
1001
+ onUpdate.mockClear();
1002
+ const canvas = within(canvasElement);
1003
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1004
+ const italic = canvas.getByRole('button', { name: 'Italic' });
1005
+ const strikethrough = canvas.getByRole('button', { name: 'Strikethrough' });
1006
+
1007
+ await step(
1008
+ 'keyboard: the toggle is reachable with Tab (WCAG 2.1.1 Keyboard)',
1009
+ async () => {
1010
+ await userEvent.tab();
1011
+ await expect(bold).toHaveFocus();
1012
+ },
1013
+ );
1014
+
1015
+ await step(
1016
+ 'keyboard: the focused toggle shows a visible focus indicator (WCAG 2.4.7)',
1017
+ async () => {
1018
+ // `ui-focus-ring` replaces the browser default with the library ring.
1019
+ const { boxShadow, outlineStyle } = getComputedStyle(bold);
1020
+ await expect(outlineStyle).toBe('none');
1021
+ await expect(boxShadow).not.toBe('none');
1022
+ },
1023
+ );
1024
+
1025
+ await step('keyboard: Space presses the toggle', async () => {
1026
+ await userEvent.keyboard(' ');
1027
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1028
+ await expect(onUpdate).toHaveBeenCalledWith(true);
1029
+ });
1030
+
1031
+ await step('keyboard: Enter releases it again', async () => {
1032
+ await userEvent.keyboard('{Enter}');
1033
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1034
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
1035
+ });
1036
+
1037
+ await step(
1038
+ 'keyboard: Tab follows DOM order and skips the disabled toggle (WCAG 2.4.3 Focus Order)',
1039
+ async () => {
1040
+ await userEvent.tab();
1041
+ await expect(italic).toHaveFocus();
1042
+ await userEvent.tab();
1043
+ await expect(
1044
+ canvas.getByRole('button', { name: 'Underline' }),
1045
+ ).toHaveFocus();
1046
+ await userEvent.tab();
1047
+ await expect(strikethrough).not.toHaveFocus();
1048
+ },
1049
+ );
1050
+ },
1051
+ };
1052
+
1053
+ /**
1054
+ * Screen-reader semantics: the toggle exposes a `button` role, carries a name
1055
+ * even when it is icon-only, and announces its pressed and disabled states.
1056
+ */
1057
+ export const ScreenReader: Story = {
1058
+ name: 'test - Screen Reader',
1059
+ tags: ['!dev', '!autodocs'],
1060
+ render: renderTestToggles,
1061
+ parameters: {
1062
+ // The play changes the pressed state; skip the visual snapshot.
1063
+ chromatic: { disableSnapshot: true },
1064
+ docs: {
1065
+ description: {
1066
+ story:
1067
+ 'Verifies the role, name, and pressed/disabled states that assistive technology announces.',
1068
+ },
1069
+ },
1070
+ },
1071
+ play: async ({ canvasElement, step }) => {
1072
+ // Scenario (Toggle): assistive tech reads each control as a toggle button
1073
+ // with a name, and is told which are pressed and which are unavailable.
1074
+ const canvas = within(canvasElement);
1075
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1076
+ const italic = canvas.getByRole('button', { name: 'Italic' });
1077
+ const underline = canvas.getByRole('button', { name: 'Underline' });
1078
+ const strikethrough = canvas.getByRole('button', { name: 'Strikethrough' });
1079
+
1080
+ await step(
1081
+ 'screen-reader: every control exposes a button role (WCAG 4.1.2)',
1082
+ async () => {
1083
+ for (const toggle of canvas.getAllByRole('button')) {
1084
+ await expect(toggle.tagName).toBe('BUTTON');
1085
+ }
1086
+ },
1087
+ );
1088
+
1089
+ await step(
1090
+ 'screen-reader: an icon-only toggle is named by its aria-label (WCAG 1.1.1)',
1091
+ async () => {
1092
+ await expect(bold).toHaveAccessibleName('Bold');
1093
+ await expect(italic).toHaveAccessibleName('Italic');
1094
+ },
1095
+ );
1096
+
1097
+ await step(
1098
+ 'screen-reader: a toggle with text is named by that text (WCAG 2.5.3)',
1099
+ async () => {
1100
+ await expect(underline).toHaveAccessibleName('Underline');
1101
+ await expect(underline).not.toHaveAttribute('aria-label');
1102
+ },
1103
+ );
1104
+
1105
+ await step(
1106
+ 'screen-reader: the pressed and unpressed states are announced (WCAG 4.1.2)',
1107
+ async () => {
1108
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1109
+ await expect(italic).toHaveAttribute('aria-pressed', 'true');
1110
+ },
1111
+ );
1112
+
1113
+ await step(
1114
+ 'screen-reader: an unavailable toggle announces its disabled state (WCAG 4.1.2)',
1115
+ async () => {
1116
+ await expect(strikethrough).toBeDisabled();
1117
+ await expect(strikethrough).toHaveAttribute('aria-pressed', 'true');
1118
+ },
1119
+ );
1120
+
1121
+ await step(
1122
+ 'screen-reader: the announced state follows the interaction (WCAG 4.1.2)',
1123
+ async () => {
1124
+ await userEvent.click(bold);
1125
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1126
+ },
1127
+ );
1128
+ },
1129
+ };
1130
+
1131
+ /**
1132
+ * Rendered at 200% page zoom (WCAG 1.4.4 Resize Text). Use the Zoom toolbar to
1133
+ * try 400% reflow (WCAG 1.4.10); the toggle must stay visible, readable, and
1134
+ * operable.
1135
+ */
1136
+ export const ZoomMagnification: Story = {
1137
+ name: 'test - Zoom Magnification',
1138
+ tags: ['!dev', '!autodocs'],
1139
+ globals: { zoom: '200' },
1140
+ render: renderTestToggles,
1141
+ parameters: {
1142
+ // The accessible-reflow Chromatic mode already covers magnified snapshots.
1143
+ chromatic: { disableSnapshot: true },
1144
+ docs: {
1145
+ description: {
1146
+ story:
1147
+ 'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
1148
+ },
1149
+ },
1150
+ },
1151
+ play: async ({ canvasElement, step }) => {
1152
+ // Scenario (Toggle): at 200% page zoom every toggle keeps its name and
1153
+ // still presses.
1154
+ const canvas = within(canvasElement);
1155
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1156
+
1157
+ await step(
1158
+ 'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
1159
+ async () => {
1160
+ await expect(document.documentElement.style.zoom).toBe('200%');
1161
+ },
1162
+ );
1163
+
1164
+ await step(
1165
+ 'zoom: every toggle stays visible with a readable name when magnified',
1166
+ async () => {
1167
+ for (const toggle of canvas.getAllByRole('button')) {
1168
+ await expect(toggle).toBeVisible();
1169
+ await expect(toggle).toHaveAccessibleName();
1170
+ }
1171
+ await expect(canvas.getByText('Underline')).toBeVisible();
1172
+ },
1173
+ );
1174
+
1175
+ await step('zoom: the toggle stays operable when magnified', async () => {
1176
+ await userEvent.click(bold);
1177
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1178
+ });
1179
+ },
1180
+ };
1181
+
1182
+ /**
1183
+ * Rendered at the mobile (320px reflow) breakpoint via the Media breakpoint
1184
+ * toolbar. The toggle stays operable and every size meets the minimum
1185
+ * touch-target size (WCAG 2.5.8).
1186
+ */
1187
+ export const MobileBreakpoint: Story = {
1188
+ name: 'test - Mobile Breakpoint',
1189
+ tags: ['!dev', '!autodocs'],
1190
+ globals: { mediaBreakpoint: 'reflow' },
1191
+ render: renderTestToggles,
1192
+ parameters: {
1193
+ // Focus moves and the pressed state changes during the play function; skip
1194
+ // the snapshot.
1195
+ chromatic: { disableSnapshot: true },
1196
+ docs: {
1197
+ description: {
1198
+ story:
1199
+ 'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
1200
+ },
1201
+ },
1202
+ },
1203
+ play: async ({ canvasElement, step }) => {
1204
+ // Scenario (Toggle): at the 320px mobile breakpoint the toolbar stays on
1205
+ // screen, every size is big enough to tap, and the toggle still presses by
1206
+ // touch and by keyboard.
1207
+ onUpdate.mockClear();
1208
+ const canvas = within(canvasElement);
1209
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1210
+
1211
+ await step(
1212
+ 'mobile: the toolbar fits the 320px viewport without clipping (WCAG 1.4.10 Reflow)',
1213
+ async () => {
1214
+ for (const toggle of canvas.getAllByRole('button')) {
1215
+ await expect(toggle).toBeVisible();
1216
+ await expect(
1217
+ toggle.getBoundingClientRect().width,
1218
+ ).toBeLessThanOrEqual(320);
1219
+ }
1220
+ },
1221
+ );
1222
+
1223
+ await step(
1224
+ 'mobile: every size meets the minimum touch-target size (WCAG 2.5.8)',
1225
+ async () => {
1226
+ // sm is 32×32, default 36×36 and lg 40×40, so the toggle carries the
1227
+ // target on its own without help from a surrounding label.
1228
+ for (const name of ['Align left', 'Align center', 'Align right']) {
1229
+ const { width, height } = canvas
1230
+ .getByRole('button', { name })
1231
+ .getBoundingClientRect();
1232
+ await expect(width).toBeGreaterThanOrEqual(24);
1233
+ await expect(height).toBeGreaterThanOrEqual(24);
1234
+ }
1235
+ },
1236
+ );
1237
+
1238
+ await step(
1239
+ 'mobile: stays keyboard operable — Tab focuses and Space presses (WCAG 2.1.1)',
1240
+ async () => {
1241
+ await userEvent.tab();
1242
+ await expect(bold).toHaveFocus();
1243
+ await userEvent.keyboard(' ');
1244
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1245
+ await expect(onUpdate).toHaveBeenCalledWith(true);
1246
+ },
1247
+ );
1248
+
1249
+ await step(
1250
+ 'mobile: tapping the toggle releases it at the 320px breakpoint',
1251
+ async () => {
1252
+ await userEvent.click(bold);
1253
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1254
+ await expect(onUpdate).toHaveBeenLastCalledWith(false);
1255
+ },
1256
+ );
1257
+ },
427
1258
  };