@aleph-alpha/ui-library 5.7.0 → 5.9.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 (124) hide show
  1. package/README.md +12 -7
  2. package/config.d.ts +5 -1
  3. package/config.js +18 -1
  4. package/dist/system/__tests__/test-utils.d.ts +24 -0
  5. package/dist/system/components/UiAutocomplete/UiAutocomplete.vue.d.ts +8 -4
  6. package/dist/system/components/UiAutocomplete/types.d.ts +11 -0
  7. package/dist/system/components/UiBreadcrumb/UiBreadcrumb.vue.d.ts +17 -0
  8. package/dist/system/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue.d.ts +17 -0
  9. package/dist/system/components/UiBreadcrumb/UiBreadcrumbItem.vue.d.ts +17 -0
  10. package/dist/system/components/UiBreadcrumb/UiBreadcrumbLink.vue.d.ts +18 -0
  11. package/dist/system/components/UiBreadcrumb/UiBreadcrumbList.vue.d.ts +17 -0
  12. package/dist/system/components/UiBreadcrumb/UiBreadcrumbPage.vue.d.ts +17 -0
  13. package/dist/system/components/UiBreadcrumb/UiBreadcrumbSeparator.vue.d.ts +17 -0
  14. package/dist/system/components/UiBreadcrumb/index.d.ts +8 -0
  15. package/dist/system/components/UiBreadcrumb/types.d.ts +22 -0
  16. package/dist/system/components/UiFadeMask/UiFadeMask.vue.d.ts +21 -0
  17. package/dist/system/components/UiFadeMask/index.d.ts +2 -0
  18. package/dist/system/components/UiFadeMask/types.d.ts +33 -0
  19. package/dist/system/components/UiListbox/UiListboxVirtualizer.vue.d.ts +21 -0
  20. package/dist/system/components/UiListbox/index.d.ts +1 -0
  21. package/dist/system/components/UiListbox/types.d.ts +36 -0
  22. package/dist/system/components/UiSelect/UiSelectVirtualizer.vue.d.ts +21 -0
  23. package/dist/system/components/UiSelect/index.d.ts +1 -0
  24. package/dist/system/components/UiSelect/types.d.ts +36 -0
  25. package/dist/system/components/UiToggleGroup/types.d.ts +0 -5
  26. package/dist/system/components/core/breadcrumb/Breadcrumb.vue.d.ts +21 -0
  27. package/dist/system/components/core/breadcrumb/BreadcrumbEllipsis.vue.d.ts +21 -0
  28. package/dist/system/components/core/breadcrumb/BreadcrumbItem.vue.d.ts +21 -0
  29. package/dist/system/components/core/breadcrumb/BreadcrumbLink.vue.d.ts +24 -0
  30. package/dist/system/components/core/breadcrumb/BreadcrumbList.vue.d.ts +21 -0
  31. package/dist/system/components/core/breadcrumb/BreadcrumbPage.vue.d.ts +21 -0
  32. package/dist/system/components/core/breadcrumb/BreadcrumbSeparator.vue.d.ts +21 -0
  33. package/dist/system/components/core/breadcrumb/index.d.ts +7 -0
  34. package/dist/system/components/core/listbox/Listbox.vue.d.ts +28 -2
  35. package/dist/system/components/core/listbox/context.d.ts +21 -0
  36. package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
  37. package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
  38. package/dist/system/components/index.d.ts +2 -0
  39. package/dist/system/lib.js +22570 -21061
  40. package/dist/system/mobile-device-frame.png +0 -0
  41. package/dist/system/patterns/UiDatePicker/UiDatePicker.vue.d.ts +1 -1
  42. package/dist/system/patterns/UiFloatActions/types.d.ts +2 -1
  43. package/dist/system/patterns/UiProfileCard/index.d.ts +7 -0
  44. package/dist/system/patterns/UiTopBar/UiTopBar.vue.d.ts +23 -0
  45. package/dist/system/patterns/UiTopBar/index.d.ts +2 -0
  46. package/dist/system/patterns/UiTopBar/types.d.ts +28 -0
  47. package/dist/system/patterns/index.d.ts +2 -0
  48. package/dist/system/story-sidebar-icons.json +10 -1
  49. package/package.json +3 -2
  50. package/src/__tests__/test-utils.ts +74 -0
  51. package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +94 -0
  52. package/src/components/UiAutocomplete/UiAutocomplete.vue +164 -44
  53. package/src/components/UiAutocomplete/__tests__/UiAutocompleteVirtualized.test.ts +107 -0
  54. package/src/components/UiAutocomplete/types.ts +11 -0
  55. package/src/components/UiBreadcrumb/UiBreadcrumb.stories.ts +1419 -0
  56. package/src/components/UiBreadcrumb/UiBreadcrumb.vue +19 -0
  57. package/src/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue +22 -0
  58. package/src/components/UiBreadcrumb/UiBreadcrumbItem.vue +19 -0
  59. package/src/components/UiBreadcrumb/UiBreadcrumbLink.vue +26 -0
  60. package/src/components/UiBreadcrumb/UiBreadcrumbList.vue +19 -0
  61. package/src/components/UiBreadcrumb/UiBreadcrumbPage.vue +19 -0
  62. package/src/components/UiBreadcrumb/UiBreadcrumbSeparator.vue +22 -0
  63. package/src/components/UiBreadcrumb/index.ts +8 -0
  64. package/src/components/UiBreadcrumb/types.ts +28 -0
  65. package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +760 -0
  66. package/src/components/UiDropdownMenu/UiDropdownMenuTrigger.vue +2 -0
  67. package/src/components/UiFadeMask/UiFadeMask.stories.ts +563 -0
  68. package/src/components/UiFadeMask/UiFadeMask.vue +86 -0
  69. package/src/components/UiFadeMask/__tests__/UiFadeMask.test.ts +52 -0
  70. package/src/components/UiFadeMask/index.ts +6 -0
  71. package/src/components/UiFadeMask/types.ts +35 -0
  72. package/src/components/UiListbox/UiListbox.stories.ts +1404 -30
  73. package/src/components/UiListbox/UiListboxVirtualizer.vue +42 -0
  74. package/src/components/UiListbox/__tests__/UiListbox.test.ts +70 -35
  75. package/src/components/UiListbox/__tests__/UiListboxVirtualizer.test.ts +96 -0
  76. package/src/components/UiListbox/index.ts +1 -0
  77. package/src/components/UiListbox/types.ts +42 -0
  78. package/src/components/UiSelect/UiSelect.stories.ts +109 -1
  79. package/src/components/UiSelect/UiSelectVirtualizer.vue +373 -0
  80. package/src/components/UiSelect/__tests__/UiSelectVirtualizer.test.ts +185 -0
  81. package/src/components/UiSelect/index.ts +1 -0
  82. package/src/components/UiSelect/types.ts +38 -0
  83. package/src/components/UiSwitch/UiSwitch.stories.ts +706 -9
  84. package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +2 -6
  85. package/src/components/UiToggle/UiToggle.stories.ts +961 -130
  86. package/src/components/UiToggle/UiToggle.vue +8 -1
  87. package/src/components/UiToggle/__tests__/UiToggle.test.ts +55 -65
  88. package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +1060 -173
  89. package/src/components/UiToggleGroup/UiToggleGroup.vue +4 -2
  90. package/src/components/UiToggleGroup/__tests__/UiToggleGroup.test.ts +117 -325
  91. package/src/components/UiToggleGroup/types.ts +0 -5
  92. package/src/components/core/breadcrumb/Breadcrumb.vue +13 -0
  93. package/src/components/core/breadcrumb/BreadcrumbEllipsis.vue +23 -0
  94. package/src/components/core/breadcrumb/BreadcrumbItem.vue +17 -0
  95. package/src/components/core/breadcrumb/BreadcrumbLink.vue +29 -0
  96. package/src/components/core/breadcrumb/BreadcrumbList.vue +22 -0
  97. package/src/components/core/breadcrumb/BreadcrumbPage.vue +20 -0
  98. package/src/components/core/breadcrumb/BreadcrumbSeparator.vue +22 -0
  99. package/src/components/core/breadcrumb/index.ts +7 -0
  100. package/src/components/core/dropdown-menu/DropdownMenuCheckboxItem.vue +8 -2
  101. package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
  102. package/src/components/core/dropdown-menu/DropdownMenuRadioItem.vue +8 -2
  103. package/src/components/core/dropdown-menu/DropdownMenuSubTrigger.vue +1 -1
  104. package/src/components/core/listbox/Listbox.vue +14 -2
  105. package/src/components/core/listbox/ListboxContent.vue +61 -3
  106. package/src/components/core/listbox/ListboxItem.vue +6 -1
  107. package/src/components/core/listbox/context.ts +34 -0
  108. package/src/components/core/toggle/Toggle.vue +15 -1
  109. package/src/components/core/toggle/index.ts +2 -2
  110. package/src/components/core/toggle-group/ToggleGroup.vue +1 -1
  111. package/src/components/core/toggle-group/index.ts +5 -2
  112. package/src/components/index.ts +2 -0
  113. package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +4 -5
  114. package/src/patterns/UiFloatActions/UiFloatActions.vue +23 -11
  115. package/src/patterns/UiFloatActions/__tests__/UiFloatActions.test.ts +23 -0
  116. package/src/patterns/UiFloatActions/types.ts +2 -1
  117. package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +1064 -0
  118. package/src/patterns/UiProfileCard/index.ts +7 -0
  119. package/src/patterns/UiTopBar/UiTopBar.stories.ts +1121 -0
  120. package/src/patterns/UiTopBar/UiTopBar.vue +166 -0
  121. package/src/patterns/UiTopBar/__tests__/UiTopBar.test.ts +245 -0
  122. package/src/patterns/UiTopBar/index.ts +2 -0
  123. package/src/patterns/UiTopBar/types.ts +29 -0
  124. package/src/patterns/index.ts +2 -0
@@ -1,82 +1,44 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { expect, fn, userEvent, within } from 'storybook/test';
2
3
  import { ref } from 'vue';
3
4
  import { UiToggleGroup, UiToggleGroupItem } from './index';
4
5
  import { UiIcon } from '../UiIcon';
5
6
 
7
+ interface ToggleItemConfig {
8
+ value: string;
9
+ icon: string;
10
+ /** Accessible name for the item. Names the control, never its pressed state. */
11
+ label: string;
12
+ /** Optional visible text. Must appear in `label` to satisfy WCAG 2.5.3. */
13
+ text?: string;
14
+ }
15
+
6
16
  /**
7
17
  * Shared item configurations for consistent stories.
8
18
  */
9
- const formattingItems = [
10
- { value: 'bold', icon: 'bold', label: 'Toggle bold' },
11
- { value: 'italic', icon: 'italic', label: 'Toggle italic' },
12
- { value: 'underline', icon: 'underline', label: 'Toggle underline' },
19
+ const formattingItems: ToggleItemConfig[] = [
20
+ { value: 'bold', icon: 'bold', label: 'Bold' },
21
+ { value: 'italic', icon: 'italic', label: 'Italic' },
22
+ { value: 'underline', icon: 'underline', label: 'Underline' },
13
23
  ];
14
24
 
15
- const alignmentItems = [
16
- { value: 'left', icon: 'align-left', label: 'Align left' },
17
- { value: 'center', icon: 'align-center', label: 'Align center' },
18
- { value: 'right', icon: 'align-right', label: 'Align right' },
25
+ const alignmentItems: ToggleItemConfig[] = [
26
+ { value: 'left', icon: 'align-left', label: 'Align left', text: 'Left' },
27
+ {
28
+ value: 'center',
29
+ icon: 'align-center',
30
+ label: 'Align center',
31
+ text: 'Center',
32
+ },
33
+ { value: 'right', icon: 'align-right', label: 'Align right', text: 'Right' },
19
34
  ];
20
35
 
21
- const viewItems = [
36
+ const viewItems: ToggleItemConfig[] = [
22
37
  { value: 'list', icon: 'list', label: 'List view' },
23
38
  { value: 'grid', icon: 'layout-grid', label: 'Grid view' },
24
39
  { value: 'columns', icon: 'columns-3', label: 'Column view' },
25
40
  ];
26
41
 
27
- /**
28
- * Helper factory to create render functions with consistent setup.
29
- * @param config - Configuration object for the toggle group render.
30
- * @param config.type - The selection type: 'single' or 'multiple'.
31
- * @param config.items - Array of items to render in the toggle group.
32
- * @param [config.initialValue] - Initial selected value(s).
33
- * @param [config.variant] - Visual style variant: 'default' or 'outline'.
34
- * @param [config.size] - Size of the toggle group: 'default', 'sm', or 'lg'.
35
- * @param [config.disabled] - Whether the entire group is disabled.
36
- * @param [config.itemDisabledIndex] - Index of a specific item to disable.
37
- * @param [config.showLabels] - Whether to show text labels alongside icons.
38
- */
39
- const createToggleGroupRender = (config: {
40
- type: 'single' | 'multiple';
41
- items: typeof formattingItems;
42
- initialValue?: string | string[];
43
- variant?: 'default' | 'outline';
44
- size?: 'default' | 'sm' | 'lg';
45
- disabled?: boolean;
46
- itemDisabledIndex?: number;
47
- showLabels?: boolean;
48
- }) => {
49
- return (args: Record<string, unknown>) => ({
50
- components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
51
- setup() {
52
- const value = ref(
53
- config.initialValue ?? (config.type === 'single' ? '' : []),
54
- );
55
- return { args, value, config };
56
- },
57
- template: `
58
- <UiToggleGroup
59
- v-model="value"
60
- :type="config.type"
61
- :variant="args.variant ?? config.variant"
62
- :size="args.size ?? config.size"
63
- :disabled="args.disabled ?? config.disabled"
64
- >
65
- <UiToggleGroupItem
66
- v-for="(item, index) in config.items"
67
- :key="item.value"
68
- :value="item.value"
69
- :aria-label="item.label"
70
- :disabled="config.itemDisabledIndex === index"
71
- >
72
- <UiIcon :name="item.icon" :size="16" />
73
- <span v-if="config.showLabels">{{ item.label.replace('Toggle ', '').replace('Align ', '') }}</span>
74
- </UiToggleGroupItem>
75
- </UiToggleGroup>
76
- `,
77
- });
78
- };
79
-
80
42
  /**
81
43
  * UiToggleGroup is a horizontal group of related toggle buttons supporting single- or multi-selection.
82
44
  * Use it for text-formatting toolbars, view-mode switchers, and feature toggles; for a single standalone toggle, use `UiToggle`.
@@ -103,7 +65,7 @@ const meta: Meta<typeof UiToggleGroup> = {
103
65
  },
104
66
  variant: {
105
67
  control: 'select',
106
- options: ['default', 'outline'],
68
+ options: ['default', 'outline', 'soft'],
107
69
  description: 'The visual style variant applied to all items',
108
70
  },
109
71
  size: {
@@ -115,18 +77,12 @@ const meta: Meta<typeof UiToggleGroup> = {
115
77
  control: 'boolean',
116
78
  description: 'Whether the entire group is disabled',
117
79
  },
118
- orientation: {
119
- control: 'select',
120
- options: ['horizontal', 'vertical'],
121
- description: 'The orientation for keyboard navigation',
122
- },
123
80
  },
124
81
  args: {
125
82
  type: 'multiple',
126
83
  variant: 'default',
127
84
  size: 'default',
128
85
  disabled: false,
129
- orientation: 'horizontal',
130
86
  },
131
87
  };
132
88
 
@@ -134,203 +90,490 @@ export default meta;
134
90
 
135
91
  type Story = StoryObj<typeof UiToggleGroup>;
136
92
 
93
+ // `UiToggleGroup` forwards `defineModel`, so it emits `update:modelValue`. Play
94
+ // functions that need to assert the emit bind this module-level spy via
95
+ // `@update:model-value` and `mockClear()` it first.
96
+ const onUpdate = fn();
97
+
98
+ /**
99
+ * Builds a render function for a single toggle group.
100
+ *
101
+ * Every value is taken straight from `config` rather than merged with the
102
+ * story args: the meta supplies concrete `variant`/`size`/`disabled` args for
103
+ * the `Default` story's controls, and merging them here would override — and
104
+ * silently erase — whatever the individual story is meant to demonstrate.
105
+ * @param config Fixed configuration for the rendered group.
106
+ * @param config.type Selection mode, `'single'` or `'multiple'`.
107
+ * @param config.groupLabel Accessible name for the group as a whole.
108
+ * @param config.items Items to render.
109
+ * @param [config.initialValue] Initially selected value(s).
110
+ * @param [config.variant] Visual style applied to all items.
111
+ * @param [config.size] Size applied to all items.
112
+ * @param [config.disabled] Whether the whole group is disabled.
113
+ * @param [config.disabledValue] Value of a single item to disable.
114
+ * @param [config.showText] Whether items render their text alongside the icon.
115
+ * @returns A Storybook render function.
116
+ */
117
+ function renderGroup(config: {
118
+ type: 'single' | 'multiple';
119
+ groupLabel: string;
120
+ items: ToggleItemConfig[];
121
+ initialValue?: string | string[];
122
+ variant?: 'default' | 'outline' | 'soft';
123
+ size?: 'default' | 'sm' | 'lg';
124
+ disabled?: boolean;
125
+ disabledValue?: string;
126
+ showText?: boolean;
127
+ }) {
128
+ return () => ({
129
+ components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
130
+ setup() {
131
+ const value = ref(
132
+ config.initialValue ?? (config.type === 'single' ? '' : []),
133
+ );
134
+ return { value, config, onUpdate };
135
+ },
136
+ template: `
137
+ <UiToggleGroup
138
+ v-model="value"
139
+ :type="config.type"
140
+ :aria-label="config.groupLabel"
141
+ :variant="config.variant ?? 'default'"
142
+ :size="config.size ?? 'default'"
143
+ :disabled="config.disabled ?? false"
144
+ @update:model-value="onUpdate"
145
+ >
146
+ <UiToggleGroupItem
147
+ v-for="item in config.items"
148
+ :key="item.value"
149
+ :value="item.value"
150
+ :aria-label="item.label"
151
+ :disabled="config.disabledValue === item.value"
152
+ >
153
+ <UiIcon :name="item.icon" :size="16" />
154
+ <span v-if="config.showText">{{ item.text }}</span>
155
+ </UiToggleGroupItem>
156
+ </UiToggleGroup>
157
+ `,
158
+ });
159
+ }
160
+
137
161
  /**
138
- * The default toggle group with multiple selection enabled.
139
- * Users can select multiple options simultaneously, similar to checkboxes.
162
+ * The default toggle group with multiple selection enabled: every item toggles
163
+ * on and off independently, like a row of checkboxes.
140
164
  *
141
- * **Use cases:**
142
- * - Text formatting toolbars (bold, italic, underline)
143
- * - Filter selections
144
- * - Feature toggles
165
+ * The group carries an `aria-label` because `role="group"` is otherwise
166
+ * announced with no name, and each icon-only item carries its own.
145
167
  */
146
168
  export const Default: Story = {
147
- render: createToggleGroupRender({
148
- type: 'multiple',
149
- items: formattingItems,
169
+ render: (args) => ({
170
+ components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
171
+ setup() {
172
+ const value = ref<string[]>([]);
173
+ return { args, value, formattingItems };
174
+ },
175
+ template: `
176
+ <UiToggleGroup
177
+ v-model="value"
178
+ type="multiple"
179
+ aria-label="Text formatting"
180
+ :variant="args.variant"
181
+ :size="args.size"
182
+ :disabled="args.disabled"
183
+ >
184
+ <UiToggleGroupItem
185
+ v-for="item in formattingItems"
186
+ :key="item.value"
187
+ :value="item.value"
188
+ :aria-label="item.label"
189
+ >
190
+ <UiIcon :name="item.icon" :size="16" />
191
+ </UiToggleGroupItem>
192
+ </UiToggleGroup>
193
+ `,
150
194
  }),
195
+ play: async ({ canvasElement, step }) => {
196
+ // Scenario (Toggle group): the resting state of a multiple-choice group.
197
+ // Nothing is pressed here, so the visual snapshot stays stable.
198
+ const canvas = within(canvasElement);
199
+
200
+ await step(
201
+ 'interaction: the group exposes a named group role (WCAG 1.3.1 / 4.1.2)',
202
+ async () => {
203
+ const group = canvas.getByRole('group', { name: 'Text formatting' });
204
+ await expect(group).toBeVisible();
205
+ },
206
+ );
207
+
208
+ await step(
209
+ 'interaction: every item is a named button resting unpressed',
210
+ async () => {
211
+ for (const { label } of formattingItems) {
212
+ const item = canvas.getByRole('button', { name: label });
213
+ await expect(item).toHaveAttribute('aria-pressed', 'false');
214
+ }
215
+ },
216
+ );
217
+ },
151
218
  };
152
219
 
153
220
  /**
154
- * The outline variant provides a bordered appearance with a subtle shadow.
155
- * Use this when you need more visual prominence or when placing on colored backgrounds.
156
- *
157
- * **Best for:**
158
- * - Prominent toolbar actions
159
- * - Standalone toggle groups
160
- * - Higher contrast requirements
221
+ * The outline variant gives every item a visible border, which keeps the
222
+ * unpressed items discoverable when the group sits on its own rather than
223
+ * inside a toolbar.
161
224
  */
162
225
  export const Outline: Story = {
163
- render: createToggleGroupRender({
226
+ render: renderGroup({
164
227
  type: 'multiple',
228
+ groupLabel: 'Text formatting',
165
229
  items: formattingItems,
166
230
  variant: 'outline',
167
231
  }),
232
+ parameters: { controls: { disable: true } },
233
+ play: async ({ canvasElement, step }) => {
234
+ // Scenario (Toggle group): a style variation carries the same semantics.
235
+ // Non-mutating, so the visual snapshot stays stable.
236
+ const canvas = within(canvasElement);
237
+
238
+ await step(
239
+ 'interaction: the outline variant keeps the group and item semantics',
240
+ async () => {
241
+ await expect(
242
+ canvas.getByRole('group', { name: 'Text formatting' }),
243
+ ).toBeVisible();
244
+ await expect(
245
+ canvas.getByRole('button', { name: 'Bold' }),
246
+ ).toHaveAttribute('aria-pressed', 'false');
247
+ },
248
+ );
249
+ },
168
250
  };
169
251
 
170
252
  /**
171
- * When `type="single"`, only one item can be selected at a time.
172
- * Selecting a new item automatically deselects the previous one.
173
- *
174
- * **Use cases:**
175
- * - View mode selectors (list/grid)
176
- * - Alignment options (left/center/right)
177
- * - Exclusive options where only one can be active
253
+ * With `type="single"` only one item can be pressed at a time, and pressing a
254
+ * new one releases the previous. Focus enters the group on the pressed item.
178
255
  */
179
256
  export const Single: Story = {
180
- render: createToggleGroupRender({
257
+ render: renderGroup({
181
258
  type: 'single',
259
+ groupLabel: 'Text alignment',
182
260
  items: alignmentItems,
261
+ variant: 'outline',
183
262
  initialValue: 'center',
184
263
  }),
264
+ parameters: { controls: { disable: true } },
265
+ play: async ({ canvasElement, step }) => {
266
+ // Scenario (Toggle group): single-choice resting state. Non-mutating, so
267
+ // the visual snapshot stays stable.
268
+ const canvas = within(canvasElement);
269
+
270
+ await step(
271
+ 'interaction: exactly one item is pressed in a single-choice group',
272
+ async () => {
273
+ const pressed = canvas
274
+ .getAllByRole('button')
275
+ .filter((item) => item.getAttribute('aria-pressed') === 'true');
276
+ await expect(pressed).toHaveLength(1);
277
+ await expect(pressed[0]).toHaveAccessibleName('Align center');
278
+ },
279
+ );
280
+ },
185
281
  };
186
282
 
187
283
  /**
188
- * The small size variant (`size="sm"`) provides a more compact toggle group.
189
- * Use this when space is limited or for secondary actions.
190
- *
191
- * **Best for:**
192
- * - Compact toolbars
193
- * - Inline controls
194
- * - Dense UI layouts
284
+ * Multiple-choice mode, with more than one item pressed at once. Unlike the
285
+ * single-choice group there is no mutual exclusion, so the state of each item
286
+ * is independent.
287
+ */
288
+ export const Multiple: Story = {
289
+ render: renderGroup({
290
+ type: 'multiple',
291
+ groupLabel: 'Text formatting',
292
+ items: formattingItems,
293
+ variant: 'outline',
294
+ initialValue: ['bold', 'underline'],
295
+ }),
296
+ parameters: { controls: { disable: true } },
297
+ play: async ({ canvasElement, step }) => {
298
+ // Scenario (Toggle group): multiple-choice resting state. Non-mutating, so
299
+ // the visual snapshot stays stable.
300
+ const canvas = within(canvasElement);
301
+
302
+ await step(
303
+ 'interaction: several items are pressed at once in a multiple-choice group',
304
+ async () => {
305
+ await expect(
306
+ canvas.getByRole('button', { name: 'Bold' }),
307
+ ).toHaveAttribute('aria-pressed', 'true');
308
+ await expect(
309
+ canvas.getByRole('button', { name: 'Underline' }),
310
+ ).toHaveAttribute('aria-pressed', 'true');
311
+ await expect(
312
+ canvas.getByRole('button', { name: 'Italic' }),
313
+ ).toHaveAttribute('aria-pressed', 'false');
314
+ },
315
+ );
316
+ },
317
+ };
318
+
319
+ /**
320
+ * The small size (`size="sm"`) for compact toolbars. At 32×32px it still clears
321
+ * the 24×24px floor in WCAG 2.5.8 Target Size (Minimum).
195
322
  */
196
323
  export const Small: Story = {
197
- render: createToggleGroupRender({
324
+ render: renderGroup({
198
325
  type: 'single',
326
+ groupLabel: 'Text alignment',
199
327
  items: alignmentItems,
328
+ variant: 'outline',
200
329
  size: 'sm',
201
330
  initialValue: 'left',
202
331
  }),
332
+ parameters: { controls: { disable: true } },
333
+ play: async ({ canvasElement, step }) => {
334
+ // Scenario (Toggle group): the smallest size still has to be tappable.
335
+ // Non-mutating, so the visual snapshot stays stable.
336
+ const canvas = within(canvasElement);
337
+
338
+ await step(
339
+ 'mobile: every small item meets the minimum target size (WCAG 2.5.8)',
340
+ async () => {
341
+ for (const item of canvas.getAllByRole('button')) {
342
+ const { width, height } = item.getBoundingClientRect();
343
+ await expect(width).toBeGreaterThanOrEqual(24);
344
+ await expect(height).toBeGreaterThanOrEqual(24);
345
+ }
346
+ },
347
+ );
348
+ },
203
349
  };
204
350
 
205
351
  /**
206
- * The large size variant (`size="lg"`) provides bigger touch targets.
207
- * Use this for primary actions or touch-friendly interfaces.
208
- *
209
- * **Best for:**
210
- * - Touch interfaces
211
- * - Primary toolbars
212
- * - Accessibility-focused designs
352
+ * The large size (`size="lg"`) for touch-first surfaces and primary toolbars.
213
353
  */
214
354
  export const Large: Story = {
215
- render: createToggleGroupRender({
355
+ render: renderGroup({
216
356
  type: 'multiple',
357
+ groupLabel: 'Text formatting',
217
358
  items: formattingItems,
359
+ variant: 'outline',
218
360
  size: 'lg',
361
+ initialValue: ['italic'],
219
362
  }),
363
+ parameters: { controls: { disable: true } },
364
+ play: async ({ canvasElement, step }) => {
365
+ // Scenario (Toggle group): the largest size variation. Non-mutating, so
366
+ // the visual snapshot stays stable.
367
+ const canvas = within(canvasElement);
368
+
369
+ await step(
370
+ 'mobile: every large item meets the minimum target size (WCAG 2.5.8)',
371
+ async () => {
372
+ for (const item of canvas.getAllByRole('button')) {
373
+ const { width, height } = item.getBoundingClientRect();
374
+ await expect(width).toBeGreaterThanOrEqual(24);
375
+ await expect(height).toBeGreaterThanOrEqual(24);
376
+ }
377
+ },
378
+ );
379
+ },
220
380
  };
221
381
 
222
382
  /**
223
- * The disabled state prevents all interaction with the toggle group.
224
- * Use this when the options are temporarily unavailable.
225
- *
226
- * **Note:** You can also disable individual items using the `disabled` prop
227
- * on `UiToggleGroupItem`.
383
+ * The whole group disabled. Every item reports `disabled` to assistive
384
+ * technology and the group drops out of the tab order entirely, so keyboard
385
+ * users are never stranded inside a control they cannot operate.
228
386
  */
229
387
  export const Disabled: Story = {
230
- render: createToggleGroupRender({
388
+ render: renderGroup({
231
389
  type: 'multiple',
390
+ groupLabel: 'Text formatting',
232
391
  items: formattingItems,
392
+ variant: 'outline',
233
393
  disabled: true,
234
394
  }),
395
+ parameters: { controls: { disable: true } },
396
+ play: async ({ canvasElement, step }) => {
397
+ // Scenario (Toggle group): a disabled group refuses activation and is
398
+ // skipped by Tab. The forced click is a no-op, so the snapshot stays
399
+ // stable.
400
+ onUpdate.mockClear();
401
+ const canvas = within(canvasElement);
402
+ const items = canvas.getAllByRole('button');
403
+
404
+ await step(
405
+ 'interaction: every item reports the disabled state (WCAG 4.1.2)',
406
+ async () => {
407
+ for (const item of items) {
408
+ await expect(item).toBeDisabled();
409
+ }
410
+ },
411
+ );
412
+
413
+ await step('interaction: a disabled group cannot be pressed', async () => {
414
+ // Disabled controls render with pointer-events: none, so bypass the
415
+ // check and assert the click is still a no-op.
416
+ await userEvent.click(items[0], { pointerEventsCheck: 0 });
417
+ await expect(items[0]).toHaveAttribute('aria-pressed', 'false');
418
+ await expect(onUpdate).not.toHaveBeenCalled();
419
+ });
420
+
421
+ await step(
422
+ 'keyboard: a disabled group is skipped by Tab (WCAG 2.1.1)',
423
+ async () => {
424
+ await userEvent.tab();
425
+ for (const item of items) {
426
+ await expect(item).not.toHaveFocus();
427
+ }
428
+ },
429
+ );
430
+ },
235
431
  };
236
432
 
237
433
  /**
238
- * Toggle group items can include both icons and text labels for better clarity.
239
- * This is useful when the icon alone might not clearly convey the action.
240
- *
241
- * **Best for:**
242
- * - Complex options that need explanation
243
- * - Less frequently used features
244
- * - Accessibility improvements
434
+ * Items can include text next to the icon. The accessible name still comes from
435
+ * `aria-label`, which must contain the visible words so speech-input users can
436
+ * activate the item by saying what they see (WCAG 2.5.3 Label in Name).
245
437
  */
246
438
  export const WithText: Story = {
247
- render: createToggleGroupRender({
439
+ render: renderGroup({
248
440
  type: 'single',
441
+ groupLabel: 'Text alignment',
249
442
  items: alignmentItems,
250
443
  variant: 'outline',
251
444
  initialValue: 'left',
252
- showLabels: true,
445
+ showText: true,
253
446
  }),
447
+ parameters: { controls: { disable: true } },
448
+ play: async ({ canvasElement, step }) => {
449
+ // Scenario (Toggle group): items with text. Non-mutating, so the visual
450
+ // snapshot stays stable.
451
+ const canvas = within(canvasElement);
452
+
453
+ await step(
454
+ 'screen-reader: each name contains the visible text (WCAG 2.5.3 Label in Name)',
455
+ async () => {
456
+ for (const item of canvas.getAllByRole('button')) {
457
+ const name = item.getAttribute('aria-label') ?? '';
458
+ const visible = item.textContent?.trim() ?? '';
459
+ await expect(visible).not.toBe('');
460
+ await expect(name.toLowerCase()).toContain(visible.toLowerCase());
461
+ }
462
+ },
463
+ );
464
+ },
254
465
  };
255
466
 
256
467
  /**
257
- * A common pattern for switching between different view modes.
258
- * This example shows a list/grid/column view selector.
259
- *
260
- * **Use cases:**
261
- * - File browsers
262
- * - Product listings
263
- * - Dashboard layouts
468
+ * A common single-choice pattern: switching between list, grid, and column
469
+ * views.
264
470
  */
265
471
  export const ViewToggle: Story = {
266
- render: createToggleGroupRender({
472
+ render: renderGroup({
267
473
  type: 'single',
474
+ groupLabel: 'View mode',
268
475
  items: viewItems,
269
476
  variant: 'outline',
270
477
  initialValue: 'grid',
271
478
  }),
479
+ parameters: { controls: { disable: true } },
480
+ play: async ({ canvasElement, step }) => {
481
+ // Scenario (Toggle group): a realistic single-choice group. Non-mutating,
482
+ // so the visual snapshot stays stable.
483
+ const canvas = within(canvasElement);
484
+
485
+ await step(
486
+ 'interaction: the active view is the only pressed item',
487
+ async () => {
488
+ await expect(
489
+ canvas.getByRole('button', { name: 'Grid view' }),
490
+ ).toHaveAttribute('aria-pressed', 'true');
491
+ await expect(
492
+ canvas.getByRole('button', { name: 'List view' }),
493
+ ).toHaveAttribute('aria-pressed', 'false');
494
+ },
495
+ );
496
+ },
272
497
  };
273
498
 
274
499
  /**
275
- * Individual items can be disabled while keeping others interactive.
276
- * This is useful when certain options are temporarily unavailable.
277
- *
278
- * **Use cases:**
279
- * - Premium features that require upgrade
280
- * - Context-dependent options
281
- * - Temporarily unavailable actions
500
+ * A single item disabled while the rest stay interactive. Arrow-key navigation
501
+ * steps over the disabled item rather than stopping on something that cannot be
502
+ * activated.
282
503
  */
283
504
  export const IndividualDisabled: Story = {
284
- render: createToggleGroupRender({
505
+ render: renderGroup({
285
506
  type: 'multiple',
507
+ groupLabel: 'Text formatting',
286
508
  items: formattingItems,
287
509
  variant: 'outline',
288
510
  initialValue: ['bold'],
289
- itemDisabledIndex: 1,
511
+ disabledValue: 'italic',
290
512
  }),
513
+ parameters: { controls: { disable: true } },
514
+ play: async ({ canvasElement, step }) => {
515
+ // Scenario (Toggle group): one unavailable item among enabled ones. The
516
+ // arrow key moves focus only, so the snapshot stays stable.
517
+ const canvas = within(canvasElement);
518
+ const bold = canvas.getByRole('button', { name: 'Bold' });
519
+ const italic = canvas.getByRole('button', { name: 'Italic' });
520
+ const underline = canvas.getByRole('button', { name: 'Underline' });
521
+
522
+ await step(
523
+ 'interaction: only the disabled item reports the disabled state',
524
+ async () => {
525
+ await expect(italic).toBeDisabled();
526
+ await expect(bold).toBeEnabled();
527
+ await expect(underline).toBeEnabled();
528
+ },
529
+ );
530
+
531
+ await step(
532
+ 'keyboard: the arrow keys step over the disabled item (WCAG 2.4.3 Focus Order)',
533
+ async () => {
534
+ await userEvent.tab();
535
+ await expect(bold).toHaveFocus();
536
+ await userEvent.keyboard('{ArrowRight}');
537
+ await expect(underline).toHaveFocus();
538
+ await expect(italic).not.toHaveFocus();
539
+ },
540
+ );
541
+ },
291
542
  };
292
543
 
293
544
  /**
294
- * A comprehensive showcase of all toggle group states and variations in a single view.
295
- * This is useful for:
296
- *
297
- * - **Visual QA**: Quickly verify all states look correct
298
- * - **Design review**: See all variations side by side
299
- * - **Documentation**: Reference for available options
300
- *
301
- * ### Variants
302
- * - Default, Outline
303
- *
304
- * ### Sizes
305
- * - Small, Default, Large
306
- *
307
- * ### Types
308
- * - Single selection, Multiple selection
309
- *
310
- * ### States
311
- * - Normal, Disabled, Individual disabled
545
+ * A comprehensive showcase of all toggle group states and variations in a
546
+ * single view: both selection modes, the style variants, every size, items with
547
+ * text, and the disabled states.
312
548
  */
313
549
  export const AllStates: Story = {
550
+ parameters: { controls: { disable: true } },
314
551
  render: () => ({
315
552
  components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
316
553
  setup() {
317
554
  const multipleDefault = ref<string[]>(['bold']);
318
555
  const multipleOutline = ref<string[]>(['italic']);
556
+ const multipleSoft = ref<string[]>(['underline']);
319
557
  const singleValue = ref<string>('center');
558
+ const multipleValue = ref<string[]>(['bold', 'italic']);
320
559
  const smallValue = ref<string>('left');
321
- const defaultSizeValue = ref<string[]>(['bold', 'italic']);
560
+ const defaultSizeValue = ref<string[]>(['bold']);
322
561
  const largeValue = ref<string[]>(['underline']);
323
- const disabledValue = ref<string[]>([]);
562
+ const disabledValue = ref<string[]>(['bold']);
324
563
  const partialDisabled = ref<string[]>(['bold']);
564
+ const textValue = ref<string>('left');
325
565
  return {
326
566
  multipleDefault,
327
567
  multipleOutline,
568
+ multipleSoft,
328
569
  singleValue,
570
+ multipleValue,
329
571
  smallValue,
330
572
  defaultSizeValue,
331
573
  largeValue,
332
574
  disabledValue,
333
575
  partialDisabled,
576
+ textValue,
334
577
  formattingItems,
335
578
  alignmentItems,
336
579
  };
@@ -342,7 +585,7 @@ export const AllStates: Story = {
342
585
  <div class="flex flex-col gap-4">
343
586
  <div class="flex items-center gap-4">
344
587
  <span class="body-14-regular text-content-on-surface-muted w-24">Default:</span>
345
- <UiToggleGroup v-model="multipleDefault" type="multiple">
588
+ <UiToggleGroup v-model="multipleDefault" type="multiple" aria-label="Text formatting, default variant">
346
589
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
347
590
  <UiIcon :name="item.icon" :size="16" />
348
591
  </UiToggleGroupItem>
@@ -350,7 +593,15 @@ export const AllStates: Story = {
350
593
  </div>
351
594
  <div class="flex items-center gap-4">
352
595
  <span class="body-14-regular text-content-on-surface-muted w-24">Outline:</span>
353
- <UiToggleGroup v-model="multipleOutline" type="multiple" variant="outline">
596
+ <UiToggleGroup v-model="multipleOutline" type="multiple" variant="outline" aria-label="Text formatting, outline variant">
597
+ <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
598
+ <UiIcon :name="item.icon" :size="16" />
599
+ </UiToggleGroupItem>
600
+ </UiToggleGroup>
601
+ </div>
602
+ <div class="flex items-center gap-4">
603
+ <span class="body-14-regular text-content-on-surface-muted w-24">Soft:</span>
604
+ <UiToggleGroup v-model="multipleSoft" type="multiple" variant="soft" aria-label="Text formatting, soft variant">
354
605
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
355
606
  <UiIcon :name="item.icon" :size="16" />
356
607
  </UiToggleGroupItem>
@@ -364,7 +615,7 @@ export const AllStates: Story = {
364
615
  <div class="flex flex-col gap-4">
365
616
  <div class="flex items-center gap-4">
366
617
  <span class="body-14-regular text-content-on-surface-muted w-24">Single:</span>
367
- <UiToggleGroup v-model="singleValue" type="single" variant="outline">
618
+ <UiToggleGroup v-model="singleValue" type="single" variant="outline" aria-label="Text alignment, single choice">
368
619
  <UiToggleGroupItem v-for="item in alignmentItems" :key="item.value" :value="item.value" :aria-label="item.label">
369
620
  <UiIcon :name="item.icon" :size="16" />
370
621
  </UiToggleGroupItem>
@@ -372,7 +623,7 @@ export const AllStates: Story = {
372
623
  </div>
373
624
  <div class="flex items-center gap-4">
374
625
  <span class="body-14-regular text-content-on-surface-muted w-24">Multiple:</span>
375
- <UiToggleGroup v-model="defaultSizeValue" type="multiple" variant="outline">
626
+ <UiToggleGroup v-model="multipleValue" type="multiple" variant="outline" aria-label="Text formatting, multiple choice">
376
627
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
377
628
  <UiIcon :name="item.icon" :size="16" />
378
629
  </UiToggleGroupItem>
@@ -386,7 +637,7 @@ export const AllStates: Story = {
386
637
  <div class="flex flex-col gap-4">
387
638
  <div class="flex items-center gap-4">
388
639
  <span class="body-14-regular text-content-on-surface-muted w-24">Small:</span>
389
- <UiToggleGroup v-model="smallValue" type="single" variant="outline" size="sm">
640
+ <UiToggleGroup v-model="smallValue" type="single" variant="outline" size="sm" aria-label="Text alignment, small">
390
641
  <UiToggleGroupItem v-for="item in alignmentItems" :key="item.value" :value="item.value" :aria-label="item.label">
391
642
  <UiIcon :name="item.icon" :size="16" />
392
643
  </UiToggleGroupItem>
@@ -394,7 +645,7 @@ export const AllStates: Story = {
394
645
  </div>
395
646
  <div class="flex items-center gap-4">
396
647
  <span class="body-14-regular text-content-on-surface-muted w-24">Default:</span>
397
- <UiToggleGroup v-model="defaultSizeValue" type="multiple" variant="outline">
648
+ <UiToggleGroup v-model="defaultSizeValue" type="multiple" variant="outline" aria-label="Text formatting, default size">
398
649
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
399
650
  <UiIcon :name="item.icon" :size="16" />
400
651
  </UiToggleGroupItem>
@@ -402,7 +653,7 @@ export const AllStates: Story = {
402
653
  </div>
403
654
  <div class="flex items-center gap-4">
404
655
  <span class="body-14-regular text-content-on-surface-muted w-24">Large:</span>
405
- <UiToggleGroup v-model="largeValue" type="multiple" variant="outline" size="lg">
656
+ <UiToggleGroup v-model="largeValue" type="multiple" variant="outline" size="lg" aria-label="Text formatting, large">
406
657
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
407
658
  <UiIcon :name="item.icon" :size="16" />
408
659
  </UiToggleGroupItem>
@@ -416,7 +667,7 @@ export const AllStates: Story = {
416
667
  <div class="flex flex-col gap-4">
417
668
  <div class="flex items-center gap-4">
418
669
  <span class="body-14-regular text-content-on-surface-muted w-24">All disabled:</span>
419
- <UiToggleGroup v-model="disabledValue" type="multiple" variant="outline" disabled>
670
+ <UiToggleGroup v-model="disabledValue" type="multiple" variant="outline" disabled aria-label="Text formatting, disabled">
420
671
  <UiToggleGroupItem v-for="item in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label">
421
672
  <UiIcon :name="item.icon" :size="16" />
422
673
  </UiToggleGroupItem>
@@ -424,7 +675,7 @@ export const AllStates: Story = {
424
675
  </div>
425
676
  <div class="flex items-center gap-4">
426
677
  <span class="body-14-regular text-content-on-surface-muted w-24">Partial:</span>
427
- <UiToggleGroup v-model="partialDisabled" type="multiple" variant="outline">
678
+ <UiToggleGroup v-model="partialDisabled" type="multiple" variant="outline" aria-label="Text formatting, one item unavailable">
428
679
  <UiToggleGroupItem v-for="(item, index) in formattingItems" :key="item.value" :value="item.value" :aria-label="item.label" :disabled="index === 1">
429
680
  <UiIcon :name="item.icon" :size="16" />
430
681
  </UiToggleGroupItem>
@@ -435,14 +686,650 @@ export const AllStates: Story = {
435
686
 
436
687
  <section>
437
688
  <h3 class="heading-s mb-4">With Text Labels</h3>
438
- <UiToggleGroup v-model="singleValue" type="single" variant="outline">
689
+ <UiToggleGroup v-model="textValue" type="single" variant="outline" aria-label="Text alignment, with labels">
439
690
  <UiToggleGroupItem v-for="item in alignmentItems" :key="item.value" :value="item.value" :aria-label="item.label">
440
691
  <UiIcon :name="item.icon" :size="16" />
441
- {{ item.label.replace('Align ', '') }}
692
+ {{ item.text }}
442
693
  </UiToggleGroupItem>
443
694
  </UiToggleGroup>
444
695
  </section>
445
696
  </div>
446
697
  `,
447
698
  }),
699
+ play: async ({ canvasElement, step }) => {
700
+ // Scenario (Toggle group): the overview grid. Non-mutating, so the visual
701
+ // snapshot stays stable.
702
+ const canvas = within(canvasElement);
703
+
704
+ await step(
705
+ 'interaction: every group in the overview carries an accessible name',
706
+ async () => {
707
+ const groups = canvas.getAllByRole('group');
708
+ await expect(groups.length).toBeGreaterThan(0);
709
+ for (const group of groups) {
710
+ await expect(group).toHaveAccessibleName();
711
+ }
712
+ },
713
+ );
714
+
715
+ await step(
716
+ 'interaction: every item exposes a pressed state and a name',
717
+ async () => {
718
+ for (const item of canvas.getAllByRole('button')) {
719
+ await expect(item).toHaveAttribute('aria-pressed');
720
+ await expect(item).toHaveAccessibleName();
721
+ }
722
+ },
723
+ );
724
+ },
725
+ };
726
+
727
+ /**
728
+ * Accessibility guidance for toggle groups: a named group, named items, a
729
+ * single tab stop with arrow-key navigation inside, and a pressed state that
730
+ * survives without colour — shown side by side in the default and accessible
731
+ * themes.
732
+ */
733
+ export const Accessibility: Story = {
734
+ tags: ['!autodocs'],
735
+ parameters: {
736
+ a11y: { test: 'off' },
737
+ chromatic: { disableSnapshot: true },
738
+ controls: { disable: true },
739
+ docs: {
740
+ canvas: {
741
+ sourceState: 'none',
742
+ },
743
+ },
744
+ },
745
+ render: () => ({
746
+ components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
747
+ setup() {
748
+ const defaultValue = ref<string>('center');
749
+ const accessibleValue = ref<string>('center');
750
+ return { defaultValue, accessibleValue, alignmentItems };
751
+ },
752
+ template: `
753
+ <section class="flex flex-col gap-6 p-4">
754
+ <div>
755
+ <h2 class="heading-m font-bold text-content-on-surface-primary">
756
+ Accessibility
757
+ </h2>
758
+ </div>
759
+
760
+ <div class="flex flex-col gap-6">
761
+ <div class="flex flex-col gap-2">
762
+ <h3 class="body-16-medium text-content-on-surface-primary">
763
+ Give the group an accessible name
764
+ </h3>
765
+ <p class="body-16-regular text-content-on-surface-primary">
766
+ The group renders as <code>role="group"</code>, which screen
767
+ readers announce with no name of its own. Pass an
768
+ <code>aria-label</code> such as "Text formatting" so the items are
769
+ heard in context rather than as three loose buttons.
770
+ </p>
771
+ </div>
772
+
773
+ <div class="flex flex-col gap-2">
774
+ <h3 class="body-16-medium text-content-on-surface-primary">
775
+ Name every item, including icon-only ones
776
+ </h3>
777
+ <p class="body-16-regular text-content-on-surface-primary">
778
+ <code>UiIcon</code> renders <code>aria-hidden</code>, so an item
779
+ containing only an icon has no accessible name. Give each item an
780
+ <code>aria-label</code> that describes the control — "Bold", not
781
+ "Bold (on)" — because <code>aria-pressed</code> already carries
782
+ the state.
783
+ </p>
784
+ </div>
785
+
786
+ <div class="flex flex-col gap-2">
787
+ <h3 class="body-16-medium text-content-on-surface-primary">
788
+ Keep the visible text inside the accessible name
789
+ </h3>
790
+ <p class="body-16-regular text-content-on-surface-primary">
791
+ When an item shows text next to its icon, the
792
+ <code>aria-label</code> must contain those words — "Align left"
793
+ alongside the visible "Left" is fine, "Justify" is not. Otherwise
794
+ speech-input users cannot activate the item by saying what they
795
+ see (WCAG 2.5.3 Label in Name).
796
+ </p>
797
+ </div>
798
+
799
+ <div class="flex flex-col gap-2">
800
+ <h3 class="body-16-medium text-content-on-surface-primary">
801
+ The group is one tab stop
802
+ </h3>
803
+ <p class="body-16-regular text-content-on-surface-primary">
804
+ <code>Tab</code> enters the group once — landing on the pressed
805
+ item, or the first one — and the arrow keys move between items,
806
+ looping at the ends and stepping over disabled ones. A second
807
+ <code>Tab</code> leaves the group. Do not add your own
808
+ <code>tabindex</code>; it breaks this roving-focus contract
809
+ (WCAG 2.1.1 Keyboard, 2.4.3 Focus Order).
810
+ </p>
811
+ </div>
812
+
813
+ <div class="flex flex-col gap-2">
814
+ <h3 class="body-16-medium text-content-on-surface-primary">
815
+ Do not rely on the fill alone
816
+ </h3>
817
+ <p class="body-16-regular text-content-on-surface-primary">
818
+ Pressed items are filled with the accent colour, and the same
819
+ information reaches everyone else through
820
+ <code>aria-pressed</code> (WCAG 1.4.1 Use of Color). In a
821
+ single-choice group this is what tells assistive technology which
822
+ option is active.
823
+ </p>
824
+ </div>
825
+
826
+ <div class="flex flex-col gap-2">
827
+ <h3 class="body-16-medium text-content-on-surface-primary">
828
+ Every size is large enough to tap
829
+ </h3>
830
+ <p class="body-16-regular text-content-on-surface-primary">
831
+ Items are 32×32px at <code>sm</code>, 36×36px at the default size
832
+ and 40×40px at <code>lg</code>, so each one clears the 24×24px
833
+ floor in WCAG 2.5.8 Target Size (Minimum).
834
+ </p>
835
+ </div>
836
+
837
+ <div class="flex flex-col gap-2">
838
+ <h3 class="body-16-medium text-content-on-surface-primary">
839
+ Enable accessibility theme
840
+ </h3>
841
+ <p class="body-16-regular text-content-on-surface-primary">
842
+ When accessibility is required, enable our accessibility theme.
843
+ This increases contrast and makes sure there are no color contrast
844
+ issues.
845
+ </p>
846
+ </div>
847
+ </div>
848
+
849
+ <div class="grid gap-4 md:grid-cols-2">
850
+ <div class="flex flex-col gap-3">
851
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
852
+ Default
853
+ </h3>
854
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
855
+ <UiToggleGroup v-model="defaultValue" type="single" variant="outline" aria-label="Text alignment">
856
+ <UiToggleGroupItem v-for="item in alignmentItems" :key="item.value" :value="item.value" :aria-label="item.label">
857
+ <UiIcon :name="item.icon" :size="16" />
858
+ </UiToggleGroupItem>
859
+ </UiToggleGroup>
860
+ </div>
861
+ </div>
862
+
863
+ <div class="accessible-theme flex flex-col gap-3">
864
+ <h3 class="label-16-medium text-center text-content-on-surface-primary">
865
+ Accessible
866
+ </h3>
867
+ <div class="flex min-h-20 items-center justify-center border border-border-surface-default p-6">
868
+ <UiToggleGroup v-model="accessibleValue" type="single" variant="outline" aria-label="Text alignment">
869
+ <UiToggleGroupItem v-for="item in alignmentItems" :key="item.value" :value="item.value" :aria-label="item.label">
870
+ <UiIcon :name="item.icon" :size="16" />
871
+ </UiToggleGroupItem>
872
+ </UiToggleGroup>
873
+ </div>
874
+ </div>
875
+ </div>
876
+ </section>
877
+ `,
878
+ }),
879
+ };
880
+
881
+ // Test-only stories drive the test-runner (interactive/a11y checks) but are
882
+ // hidden from the Storybook sidebar and docs via the `!dev` / `!autodocs`
883
+ // tags. They keep the implicit `test` tag, so they still run in CI and appear
884
+ // in the automated report under their `test - …` name. `name`/`tags` must be
885
+ // literal properties so Storybook's static CSF indexer can read them.
886
+
887
+ // Shared render for the test scenarios: a multiple-choice formatting group
888
+ // with an icon-only item, an item with text and a disabled item, followed by a
889
+ // single-choice alignment group. Two groups in a row is what makes the
890
+ // tab-stop and arrow-key contract observable.
891
+ function renderTestGroups() {
892
+ return {
893
+ components: { UiToggleGroup, UiToggleGroupItem, UiIcon },
894
+ setup() {
895
+ const formatting = ref<string[]>([]);
896
+ const alignment = ref<string>('center');
897
+ return { formatting, alignment, alignmentItems, onUpdate };
898
+ },
899
+ template: `
900
+ <div class="flex flex-col items-start gap-4">
901
+ <UiToggleGroup
902
+ v-model="formatting"
903
+ type="multiple"
904
+ variant="outline"
905
+ aria-label="Text formatting"
906
+ @update:model-value="onUpdate"
907
+ >
908
+ <UiToggleGroupItem value="bold" aria-label="Bold">
909
+ <UiIcon name="bold" :size="16" />
910
+ </UiToggleGroupItem>
911
+ <UiToggleGroupItem value="italic" aria-label="Italic">
912
+ <UiIcon name="italic" :size="16" />
913
+ </UiToggleGroupItem>
914
+ <UiToggleGroupItem value="underline" aria-label="Underline">
915
+ <UiIcon name="underline" :size="16" />
916
+ <span>Underline</span>
917
+ </UiToggleGroupItem>
918
+ <UiToggleGroupItem value="strikethrough" aria-label="Strikethrough" disabled>
919
+ <UiIcon name="strikethrough" :size="16" />
920
+ </UiToggleGroupItem>
921
+ </UiToggleGroup>
922
+
923
+ <UiToggleGroup
924
+ v-model="alignment"
925
+ type="single"
926
+ variant="outline"
927
+ aria-label="Text alignment"
928
+ >
929
+ <UiToggleGroupItem
930
+ v-for="item in alignmentItems"
931
+ :key="item.value"
932
+ :value="item.value"
933
+ :aria-label="item.label"
934
+ >
935
+ <UiIcon :name="item.icon" :size="16" />
936
+ </UiToggleGroupItem>
937
+ </UiToggleGroup>
938
+ </div>
939
+ `,
940
+ };
941
+ }
942
+
943
+ /**
944
+ * Selection behaviour: multiple-choice items toggle independently,
945
+ * single-choice items replace one another, and a disabled item cannot be
946
+ * pressed.
947
+ */
948
+ export const SelectionBehavior: Story = {
949
+ name: 'test - Selection Behavior',
950
+ tags: ['!dev', '!autodocs'],
951
+ render: renderTestGroups,
952
+ parameters: {
953
+ // The play changes the pressed state; skip the visual snapshot.
954
+ chromatic: { disableSnapshot: true },
955
+ docs: {
956
+ description: {
957
+ story:
958
+ 'Press and release items in both selection modes and confirm the disabled guard holds.',
959
+ },
960
+ },
961
+ },
962
+ play: async ({ canvasElement, step }) => {
963
+ // Scenario (Toggle group): a pointer user presses items in a
964
+ // multiple-choice group, switches the choice in a single-choice group, and
965
+ // finds the disabled item unresponsive.
966
+ onUpdate.mockClear();
967
+ const canvas = within(canvasElement);
968
+ const bold = canvas.getByRole('button', { name: 'Bold' });
969
+ const italic = canvas.getByRole('button', { name: 'Italic' });
970
+ const left = canvas.getByRole('button', { name: 'Align left' });
971
+ const center = canvas.getByRole('button', { name: 'Align center' });
972
+
973
+ await step(
974
+ 'interaction: pressing an item in a multiple-choice group emits the new selection',
975
+ async () => {
976
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
977
+ await userEvent.click(bold);
978
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
979
+ await expect(onUpdate).toHaveBeenCalledWith(['bold']);
980
+ },
981
+ );
982
+
983
+ await step(
984
+ 'interaction: a second item joins the selection rather than replacing it',
985
+ async () => {
986
+ await userEvent.click(italic);
987
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
988
+ await expect(italic).toHaveAttribute('aria-pressed', 'true');
989
+ await expect(onUpdate).toHaveBeenLastCalledWith(['bold', 'italic']);
990
+ },
991
+ );
992
+
993
+ await step('interaction: pressing it again releases it', async () => {
994
+ await userEvent.click(bold);
995
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
996
+ await expect(italic).toHaveAttribute('aria-pressed', 'true');
997
+ await expect(onUpdate).toHaveBeenLastCalledWith(['italic']);
998
+ });
999
+
1000
+ await step(
1001
+ 'interaction: a single-choice group releases the previous item (WCAG 4.1.2)',
1002
+ async () => {
1003
+ await expect(center).toHaveAttribute('aria-pressed', 'true');
1004
+ await userEvent.click(left);
1005
+ await expect(left).toHaveAttribute('aria-pressed', 'true');
1006
+ await expect(center).toHaveAttribute('aria-pressed', 'false');
1007
+ },
1008
+ );
1009
+
1010
+ await step(
1011
+ 'interaction: a disabled item cannot be pressed (WCAG 4.1.2)',
1012
+ async () => {
1013
+ const strikethrough = canvas.getByRole('button', {
1014
+ name: 'Strikethrough',
1015
+ });
1016
+ await expect(strikethrough).toBeDisabled();
1017
+ // Disabled controls render with pointer-events: none, so bypass the
1018
+ // check and assert the click is still a no-op.
1019
+ await userEvent.click(strikethrough, { pointerEventsCheck: 0 });
1020
+ await expect(strikethrough).toHaveAttribute('aria-pressed', 'false');
1021
+ },
1022
+ );
1023
+ },
1024
+ };
1025
+
1026
+ /**
1027
+ * Keyboard operability: the group is a single tab stop, the arrow keys move
1028
+ * between items and loop at the ends, `Space` and `Enter` toggle, and disabled
1029
+ * items are stepped over.
1030
+ */
1031
+ export const KeyboardNavigation: Story = {
1032
+ name: 'test - Keyboard Navigation',
1033
+ tags: ['!dev', '!autodocs'],
1034
+ render: renderTestGroups,
1035
+ parameters: {
1036
+ // Focus moves during the play function; skip the visual snapshot so the
1037
+ // focus ring doesn't register as a Chromatic diff.
1038
+ chromatic: { disableSnapshot: true },
1039
+ docs: {
1040
+ description: {
1041
+ story:
1042
+ 'Press `Tab` to enter the group, arrow keys to move between items, and `Space` or `Enter` to toggle.',
1043
+ },
1044
+ },
1045
+ },
1046
+ play: async ({ canvasElement, step }) => {
1047
+ // Scenario (Toggle group): a keyboard user tabs into the first group, walks
1048
+ // it with the arrow keys, toggles an item, then tabs on to the next group
1049
+ // rather than through every item.
1050
+ onUpdate.mockClear();
1051
+ const canvas = within(canvasElement);
1052
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1053
+ const italic = canvas.getByRole('button', { name: 'Italic' });
1054
+ const underline = canvas.getByRole('button', { name: 'Underline' });
1055
+ const strikethrough = canvas.getByRole('button', { name: 'Strikethrough' });
1056
+ const center = canvas.getByRole('button', { name: 'Align center' });
1057
+
1058
+ await step(
1059
+ 'keyboard: Tab enters the group on its first item (WCAG 2.1.1 Keyboard)',
1060
+ async () => {
1061
+ await userEvent.tab();
1062
+ await expect(bold).toHaveFocus();
1063
+ },
1064
+ );
1065
+
1066
+ await step(
1067
+ 'keyboard: the focused item shows a visible focus indicator (WCAG 2.4.7)',
1068
+ async () => {
1069
+ // `ui-focus-ring` replaces the browser default with the library ring.
1070
+ const { boxShadow, outlineStyle } = getComputedStyle(bold);
1071
+ await expect(outlineStyle).toBe('none');
1072
+ await expect(boxShadow).not.toBe('none');
1073
+ },
1074
+ );
1075
+
1076
+ await step(
1077
+ 'keyboard: the arrow keys move between items (WCAG 2.4.3 Focus Order)',
1078
+ async () => {
1079
+ await userEvent.keyboard('{ArrowRight}');
1080
+ await expect(italic).toHaveFocus();
1081
+ await userEvent.keyboard('{ArrowLeft}');
1082
+ await expect(bold).toHaveFocus();
1083
+ },
1084
+ );
1085
+
1086
+ await step('keyboard: Space toggles the focused item', async () => {
1087
+ await userEvent.keyboard(' ');
1088
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1089
+ await expect(onUpdate).toHaveBeenCalledWith(['bold']);
1090
+ });
1091
+
1092
+ await step('keyboard: Enter releases it again', async () => {
1093
+ await userEvent.keyboard('{Enter}');
1094
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1095
+ await expect(onUpdate).toHaveBeenLastCalledWith([]);
1096
+ });
1097
+
1098
+ await step(
1099
+ 'keyboard: the arrow keys step over the disabled item and loop at the end',
1100
+ async () => {
1101
+ await userEvent.keyboard('{ArrowRight}{ArrowRight}');
1102
+ await expect(underline).toHaveFocus();
1103
+ // Strikethrough is disabled, so the next step wraps to the first item.
1104
+ await userEvent.keyboard('{ArrowRight}');
1105
+ await expect(strikethrough).not.toHaveFocus();
1106
+ await expect(bold).toHaveFocus();
1107
+ },
1108
+ );
1109
+
1110
+ await step(
1111
+ 'keyboard: a second Tab leaves the group instead of walking its items',
1112
+ async () => {
1113
+ await userEvent.tab();
1114
+ await expect(center).toHaveFocus();
1115
+ await expect(italic).not.toHaveFocus();
1116
+ },
1117
+ );
1118
+ },
1119
+ };
1120
+
1121
+ /**
1122
+ * Screen-reader semantics: the group exposes a named `group` role, every item
1123
+ * is a button with its own name, and pressed and disabled states are
1124
+ * announced.
1125
+ */
1126
+ export const ScreenReader: Story = {
1127
+ name: 'test - Screen Reader',
1128
+ tags: ['!dev', '!autodocs'],
1129
+ render: renderTestGroups,
1130
+ parameters: {
1131
+ // The play changes the pressed state; skip the visual snapshot.
1132
+ chromatic: { disableSnapshot: true },
1133
+ docs: {
1134
+ description: {
1135
+ story:
1136
+ 'Verifies the group name, item roles and names, and the pressed/disabled states that assistive technology announces.',
1137
+ },
1138
+ },
1139
+ },
1140
+ play: async ({ canvasElement, step }) => {
1141
+ // Scenario (Toggle group): assistive tech reads a named group of named
1142
+ // toggle buttons, and is told which are pressed and which are unavailable.
1143
+ const canvas = within(canvasElement);
1144
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1145
+ const underline = canvas.getByRole('button', { name: 'Underline' });
1146
+ const strikethrough = canvas.getByRole('button', { name: 'Strikethrough' });
1147
+ const center = canvas.getByRole('button', { name: 'Align center' });
1148
+
1149
+ await step(
1150
+ 'screen-reader: each group is announced with its own name (WCAG 1.3.1)',
1151
+ async () => {
1152
+ await expect(
1153
+ canvas.getByRole('group', { name: 'Text formatting' }),
1154
+ ).toBeVisible();
1155
+ await expect(
1156
+ canvas.getByRole('group', { name: 'Text alignment' }),
1157
+ ).toBeVisible();
1158
+ },
1159
+ );
1160
+
1161
+ await step(
1162
+ 'screen-reader: an icon-only item is named by its aria-label (WCAG 1.1.1)',
1163
+ async () => {
1164
+ await expect(bold).toHaveAccessibleName('Bold');
1165
+ await expect(bold.tagName).toBe('BUTTON');
1166
+ },
1167
+ );
1168
+
1169
+ await step(
1170
+ 'screen-reader: the decorative icon is hidden from assistive technology',
1171
+ async () => {
1172
+ await expect(bold.querySelector('svg')).toHaveAttribute(
1173
+ 'aria-hidden',
1174
+ 'true',
1175
+ );
1176
+ },
1177
+ );
1178
+
1179
+ await step(
1180
+ 'screen-reader: an item with text keeps that text in its name (WCAG 2.5.3)',
1181
+ async () => {
1182
+ await expect(underline).toHaveAccessibleName('Underline');
1183
+ await expect(canvas.getByText('Underline')).toBeVisible();
1184
+ },
1185
+ );
1186
+
1187
+ await step(
1188
+ 'screen-reader: pressed and unpressed states are announced (WCAG 4.1.2)',
1189
+ async () => {
1190
+ await expect(center).toHaveAttribute('aria-pressed', 'true');
1191
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1192
+ },
1193
+ );
1194
+
1195
+ await step(
1196
+ 'screen-reader: an unavailable item announces its disabled state (WCAG 4.1.2)',
1197
+ async () => {
1198
+ await expect(strikethrough).toBeDisabled();
1199
+ },
1200
+ );
1201
+
1202
+ await step(
1203
+ 'screen-reader: the announced state follows the interaction (WCAG 4.1.2)',
1204
+ async () => {
1205
+ await userEvent.click(bold);
1206
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1207
+ },
1208
+ );
1209
+ },
1210
+ };
1211
+
1212
+ /**
1213
+ * Rendered at 200% page zoom (WCAG 1.4.4 Resize Text). Use the Zoom toolbar to
1214
+ * try 400% reflow (WCAG 1.4.10); every item must stay visible, readable, and
1215
+ * operable.
1216
+ */
1217
+ export const ZoomMagnification: Story = {
1218
+ name: 'test - Zoom Magnification',
1219
+ tags: ['!dev', '!autodocs'],
1220
+ globals: { zoom: '200' },
1221
+ render: renderTestGroups,
1222
+ parameters: {
1223
+ // The accessible-reflow Chromatic mode already covers magnified snapshots.
1224
+ chromatic: { disableSnapshot: true },
1225
+ docs: {
1226
+ description: {
1227
+ story:
1228
+ 'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
1229
+ },
1230
+ },
1231
+ },
1232
+ play: async ({ canvasElement, step }) => {
1233
+ // Scenario (Toggle group): at 200% page zoom every item keeps its name and
1234
+ // the group still responds.
1235
+ const canvas = within(canvasElement);
1236
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1237
+
1238
+ await step(
1239
+ 'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
1240
+ async () => {
1241
+ await expect(document.documentElement.style.zoom).toBe('200%');
1242
+ },
1243
+ );
1244
+
1245
+ await step(
1246
+ 'zoom: every item stays visible with a readable name when magnified',
1247
+ async () => {
1248
+ for (const item of canvas.getAllByRole('button')) {
1249
+ await expect(item).toBeVisible();
1250
+ await expect(item).toHaveAccessibleName();
1251
+ }
1252
+ await expect(canvas.getByText('Underline')).toBeVisible();
1253
+ },
1254
+ );
1255
+
1256
+ await step('zoom: the group stays operable when magnified', async () => {
1257
+ await userEvent.click(bold);
1258
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1259
+ });
1260
+ },
1261
+ };
1262
+
1263
+ /**
1264
+ * Rendered at the mobile (320px reflow) breakpoint via the Media breakpoint
1265
+ * toolbar. The group stays operable and every item meets the minimum
1266
+ * touch-target size (WCAG 2.5.8).
1267
+ */
1268
+ export const MobileBreakpoint: Story = {
1269
+ name: 'test - Mobile Breakpoint',
1270
+ tags: ['!dev', '!autodocs'],
1271
+ globals: { mediaBreakpoint: 'reflow' },
1272
+ render: renderTestGroups,
1273
+ parameters: {
1274
+ // Focus moves and the pressed state changes during the play function; skip
1275
+ // the snapshot.
1276
+ chromatic: { disableSnapshot: true },
1277
+ docs: {
1278
+ description: {
1279
+ story:
1280
+ 'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
1281
+ },
1282
+ },
1283
+ },
1284
+ play: async ({ canvasElement, step }) => {
1285
+ // Scenario (Toggle group): at the 320px mobile breakpoint both groups stay
1286
+ // on screen, every item is big enough to tap, and the group still responds
1287
+ // to touch and to the keyboard.
1288
+ onUpdate.mockClear();
1289
+ const canvas = within(canvasElement);
1290
+ const bold = canvas.getByRole('button', { name: 'Bold' });
1291
+
1292
+ await step(
1293
+ 'mobile: the groups fit the 320px viewport without clipping (WCAG 1.4.10 Reflow)',
1294
+ async () => {
1295
+ for (const group of canvas.getAllByRole('group')) {
1296
+ await expect(group).toBeVisible();
1297
+ await expect(group.getBoundingClientRect().width).toBeLessThanOrEqual(
1298
+ 320,
1299
+ );
1300
+ }
1301
+ },
1302
+ );
1303
+
1304
+ await step(
1305
+ 'mobile: every item meets the minimum touch-target size (WCAG 2.5.8)',
1306
+ async () => {
1307
+ for (const item of canvas.getAllByRole('button')) {
1308
+ const { width, height } = item.getBoundingClientRect();
1309
+ await expect(width).toBeGreaterThanOrEqual(24);
1310
+ await expect(height).toBeGreaterThanOrEqual(24);
1311
+ }
1312
+ },
1313
+ );
1314
+
1315
+ await step(
1316
+ 'mobile: stays keyboard operable — Tab enters and Space toggles (WCAG 2.1.1)',
1317
+ async () => {
1318
+ await userEvent.tab();
1319
+ await expect(bold).toHaveFocus();
1320
+ await userEvent.keyboard(' ');
1321
+ await expect(bold).toHaveAttribute('aria-pressed', 'true');
1322
+ await expect(onUpdate).toHaveBeenCalledWith(['bold']);
1323
+ },
1324
+ );
1325
+
1326
+ await step(
1327
+ 'mobile: tapping an item releases it at the 320px breakpoint',
1328
+ async () => {
1329
+ await userEvent.click(bold);
1330
+ await expect(bold).toHaveAttribute('aria-pressed', 'false');
1331
+ await expect(onUpdate).toHaveBeenLastCalledWith([]);
1332
+ },
1333
+ );
1334
+ },
448
1335
  };