@aleph-alpha/ui-library 7.2.0 → 7.3.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 (32) hide show
  1. package/config.js +6 -0
  2. package/dist/system/components/UiFileUploadBox/UiFileUploadBox.vue.d.ts +2 -2
  3. package/dist/system/components/UiSpinner/UiSpinner.vue.d.ts +1 -3
  4. package/dist/system/components/UiSpinner/index.d.ts +1 -1
  5. package/dist/system/components/UiSpinner/types.d.ts +19 -6
  6. package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
  7. package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
  8. package/dist/system/icons/story-sidebar-icons.json +2 -0
  9. package/dist/system/lib.js +7081 -6851
  10. package/dist/system/patterns/UiFileUpload/UiFileUpload.vue.d.ts +2 -2
  11. package/dist/system/patterns/UiHeadline/UiHeadline.vue.d.ts +204 -0
  12. package/dist/system/patterns/UiHeadline/index.d.ts +2 -0
  13. package/dist/system/patterns/UiHeadline/types.d.ts +64 -0
  14. package/dist/system/patterns/index.d.ts +1 -0
  15. package/dist/system/story-sidebar-icons.json +2 -0
  16. package/package.json +10 -5
  17. package/src/components/UiField/UiField.stories.ts +302 -0
  18. package/src/components/UiSpinner/UiSpinner.stories.ts +579 -6
  19. package/src/components/UiSpinner/UiSpinner.vue +76 -4
  20. package/src/components/UiSpinner/__tests__/UiSpinner.test.ts +79 -0
  21. package/src/components/UiSpinner/index.ts +1 -1
  22. package/src/components/UiSpinner/types.ts +21 -6
  23. package/src/components/core/input/Input.vue +1 -1
  24. package/src/components/core/input-group/InputGroupInput.vue +1 -1
  25. package/src/components/core/native-select/NativeSelect.vue +1 -1
  26. package/src/patterns/UiHeadline/UiHeadline.guidance.md +53 -0
  27. package/src/patterns/UiHeadline/UiHeadline.stories.ts +915 -0
  28. package/src/patterns/UiHeadline/UiHeadline.vue +203 -0
  29. package/src/patterns/UiHeadline/__tests__/UiHeadline.test.ts +204 -0
  30. package/src/patterns/UiHeadline/index.ts +2 -0
  31. package/src/patterns/UiHeadline/types.ts +73 -0
  32. package/src/patterns/index.ts +1 -0
@@ -0,0 +1,915 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { expect, fn, userEvent, waitFor, within } from 'storybook/test';
3
+ import { onMounted, ref, watch } from 'vue';
4
+ import UiHeadline from './UiHeadline.vue';
5
+ import { UiDropdownMenuItem } from '@/components/UiDropdownMenu';
6
+ import { UiIcon } from '@/components/UiIcon';
7
+ import { CopyIcon, Trash2Icon } from '@/icons/lucide';
8
+ import {
9
+ accessibilityStory,
10
+ designNotesStory,
11
+ guidanceLinks,
12
+ parseGuidance,
13
+ } from '../../../.storybook/guidance';
14
+ import guidanceMarkdown from './UiHeadline.guidance.md?raw';
15
+
16
+ const guidance = parseGuidance(guidanceMarkdown);
17
+
18
+ const TITLE = 'This is a headline';
19
+ const DESCRIPTION = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
20
+
21
+ /**
22
+ * UiHeadline titles a page or section, with an optional description below.
23
+ * Where the user owns the name, "Edit name" in its menu turns the title into
24
+ * a field with the current name selected: Enter or the check saves, Escape or
25
+ * the cross cancels. Bind the title with `v-model` and listen to `save` to
26
+ * persist it.
27
+ */
28
+ const meta: Meta<typeof UiHeadline> = {
29
+ title: 'Patterns/UiHeadline',
30
+ component: UiHeadline,
31
+ tags: ['autodocs'],
32
+ argTypes: {
33
+ modelValue: {
34
+ control: 'text',
35
+ description: 'The title. Bind with `v-model`.',
36
+ },
37
+ description: {
38
+ control: 'text',
39
+ description: 'Supporting text shown below the title.',
40
+ },
41
+ as: {
42
+ control: 'select',
43
+ options: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
44
+ description:
45
+ 'Heading element the title renders as. The visual size is the same at every level.',
46
+ },
47
+ editable: {
48
+ control: 'boolean',
49
+ description: 'Shows the menu with "Edit name".',
50
+ },
51
+ editLabel: {
52
+ control: 'text',
53
+ description: 'Label of the menu item that starts editing.',
54
+ },
55
+ menuLabel: {
56
+ control: 'text',
57
+ description: 'Accessible name of the menu button.',
58
+ },
59
+ inputLabel: {
60
+ control: 'text',
61
+ description: 'Accessible name of the title field while editing.',
62
+ },
63
+ saveLabel: {
64
+ control: 'text',
65
+ description: 'Accessible name of the save button.',
66
+ },
67
+ cancelLabel: {
68
+ control: 'text',
69
+ description: 'Accessible name of the cancel button.',
70
+ },
71
+ onSave: { table: { disable: true } },
72
+ onCancel: { table: { disable: true } },
73
+ },
74
+ args: {
75
+ modelValue: TITLE,
76
+ description: DESCRIPTION,
77
+ as: 'h2',
78
+ editable: true,
79
+ onSave: fn(),
80
+ onCancel: fn(),
81
+ },
82
+ parameters: {
83
+ docs: { links: guidanceLinks(guidance) },
84
+ },
85
+ };
86
+
87
+ export default meta;
88
+
89
+ type Story = StoryObj<typeof UiHeadline>;
90
+
91
+ const defaultTemplateSource = `<script setup lang="ts">
92
+ import { ref } from 'vue'
93
+ import { UiHeadline } from '@aleph-alpha/ui-library'
94
+
95
+ const title = ref('${TITLE}')
96
+
97
+ function rename(name: string) {
98
+ // Persist the new name.
99
+ }
100
+ </script>
101
+
102
+ <template>
103
+ <UiHeadline
104
+ v-model="title"
105
+ description="${DESCRIPTION}"
106
+ @save="rename"
107
+ />
108
+ </template>`;
109
+
110
+ /**
111
+ * A title with its menu and description. Choose "Edit name" in the menu to
112
+ * rename it in place.
113
+ */
114
+ export const Default: Story = {
115
+ render: (args) => ({
116
+ components: { UiHeadline },
117
+ setup() {
118
+ const title = ref(args.modelValue ?? TITLE);
119
+ watch(
120
+ () => args.modelValue,
121
+ (value) => {
122
+ title.value = value ?? TITLE;
123
+ },
124
+ );
125
+ return { args, title };
126
+ },
127
+ template: '<UiHeadline v-bind="args" v-model="title" />',
128
+ }),
129
+ parameters: {
130
+ docs: {
131
+ source: {
132
+ code: defaultTemplateSource,
133
+ },
134
+ },
135
+ },
136
+ play: async ({ args, canvasElement, step }) => {
137
+ // Scenario (UiHeadline): a user renames the page from the headline menu
138
+ // and the new title replaces the old one.
139
+ const canvas = within(canvasElement);
140
+ const body = within(document.body);
141
+
142
+ await step('interaction: the title is a heading', async () => {
143
+ await expect(
144
+ canvas.getByRole('heading', { level: 2, name: TITLE }),
145
+ ).toBeVisible();
146
+ await expect(canvas.getByText(DESCRIPTION)).toBeVisible();
147
+ });
148
+
149
+ await step(
150
+ 'interaction: "Edit name" opens the field with the title selected',
151
+ async () => {
152
+ await userEvent.click(
153
+ canvas.getByRole('button', { name: 'More actions' }),
154
+ );
155
+ await userEvent.click(
156
+ await body.findByRole('menuitem', { name: 'Edit name' }),
157
+ );
158
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
159
+ await waitFor(() => expect(input).toHaveFocus());
160
+ await expect(input).toHaveValue(TITLE);
161
+ await expect(input.selectionStart).toBe(0);
162
+ await expect(input.selectionEnd).toBe(TITLE.length);
163
+ },
164
+ );
165
+
166
+ await step('interaction: the check saves the new title', async () => {
167
+ await userEvent.keyboard('Q3 planning');
168
+ await userEvent.click(canvas.getByRole('button', { name: 'Save' }));
169
+ await expect(
170
+ canvas.getByRole('heading', { name: 'Q3 planning' }),
171
+ ).toBeVisible();
172
+ await expect(args.onSave).toHaveBeenCalledWith('Q3 planning');
173
+ });
174
+ },
175
+ };
176
+
177
+ /**
178
+ * Usage, guidelines, and do's and don'ts.
179
+ */
180
+ export const DesignNotes: Story = {
181
+ ...designNotesStory(guidance),
182
+ tags: ['!autodocs'],
183
+ };
184
+
185
+ /**
186
+ * Accessibility guidance, with the component in the default and accessible
187
+ * themes.
188
+ */
189
+ export const Accessibility: Story = {
190
+ ...accessibilityStory({
191
+ requirements: guidance.accessibility,
192
+ implementation: [
193
+ 'The title renders as a heading. Set as to the level that fits the page outline (default h2).',
194
+ 'The menu button is named by menuLabel ("More actions"). The menu opens with Enter, Space or the arrow keys.',
195
+ 'The field is named by inputLabel and described by the description. It opens focused with the current title selected.',
196
+ 'Enter saves and Escape cancels. The check and cross are named by saveLabel and cancelLabel. Save is disabled while the field is empty.',
197
+ 'Focus returns to the menu button when editing ends.',
198
+ ],
199
+ example: {
200
+ components: { UiHeadline },
201
+ setup() {
202
+ return { title: ref(TITLE), description: DESCRIPTION };
203
+ },
204
+ template: '<UiHeadline v-model="title" :description="description" />',
205
+ },
206
+ }),
207
+ tags: ['!autodocs'],
208
+ };
209
+
210
+ const menuOpenTemplateSource = `<script setup lang="ts">
211
+ import { ref } from 'vue'
212
+ import { UiHeadline } from '@aleph-alpha/ui-library'
213
+
214
+ const title = ref('${TITLE}')
215
+ </script>
216
+
217
+ <template>
218
+ <UiHeadline v-model="title" description="${DESCRIPTION}" />
219
+ </template>`;
220
+
221
+ /**
222
+ * The menu next to the title, open on "Edit name".
223
+ */
224
+ export const MenuOpen: Story = {
225
+ render: (args) => ({
226
+ components: { UiHeadline },
227
+ setup() {
228
+ const title = ref(args.modelValue ?? TITLE);
229
+ watch(
230
+ () => args.modelValue,
231
+ (value) => {
232
+ title.value = value ?? TITLE;
233
+ },
234
+ );
235
+ return { args, title };
236
+ },
237
+ template: '<UiHeadline v-bind="args" v-model="title" />',
238
+ }),
239
+ parameters: {
240
+ a11y: {
241
+ // The story ends with the menu open. A modal menu marks the rest of the
242
+ // page `aria-hidden` while its trigger stays focusable, so axe would
243
+ // report that background instead of the open menu.
244
+ config: { rules: [{ id: 'aria-hidden-focus', enabled: false }] },
245
+ },
246
+ docs: {
247
+ source: {
248
+ code: menuOpenTemplateSource,
249
+ },
250
+ },
251
+ },
252
+ play: async ({ canvasElement, step }) => {
253
+ // Scenario (UiHeadline): the menu button opens a menu whose first item
254
+ // starts renaming.
255
+ const canvas = within(canvasElement);
256
+ const body = within(document.body);
257
+
258
+ await step('interaction: the menu opens on "Edit name"', async () => {
259
+ const trigger = canvas.getByRole('button', { name: 'More actions' });
260
+ await userEvent.click(trigger);
261
+ const item = await body.findByRole('menuitem', { name: 'Edit name' });
262
+ await waitFor(() => expect(item).toBeVisible());
263
+ await expect(trigger).toHaveAttribute('aria-expanded', 'true');
264
+ });
265
+ },
266
+ };
267
+
268
+ const editingTemplateSource = `<script setup lang="ts">
269
+ import { onMounted, ref } from 'vue'
270
+ import { UiHeadline } from '@aleph-alpha/ui-library'
271
+
272
+ const title = ref('${TITLE}')
273
+ const headline = ref<InstanceType<typeof UiHeadline>>()
274
+
275
+ // edit() opens the field without the menu, e.g. right after creating an item.
276
+ onMounted(() => headline.value?.edit())
277
+ </script>
278
+
279
+ <template>
280
+ <UiHeadline
281
+ ref="headline"
282
+ v-model="title"
283
+ description="${DESCRIPTION}"
284
+ />
285
+ </template>`;
286
+
287
+ /**
288
+ * Editing: the title becomes a field with the current name selected, so
289
+ * typing replaces it. The check saves and the cross cancels. Call the exposed
290
+ * `edit()` to open it without the menu.
291
+ */
292
+ export const Editing: Story = {
293
+ render: (args) => ({
294
+ components: { UiHeadline },
295
+ setup() {
296
+ const title = ref(args.modelValue ?? TITLE);
297
+ watch(
298
+ () => args.modelValue,
299
+ (value) => {
300
+ title.value = value ?? TITLE;
301
+ },
302
+ );
303
+ const headline = ref<{ edit: () => Promise<void> } | null>(null);
304
+ onMounted(() => headline.value?.edit());
305
+ return { args, title, headline };
306
+ },
307
+ template: '<UiHeadline ref="headline" v-bind="args" v-model="title" />',
308
+ }),
309
+ parameters: {
310
+ // The field takes focus when it opens.
311
+ chromatic: { disableSnapshot: true },
312
+ docs: {
313
+ source: {
314
+ code: editingTemplateSource,
315
+ },
316
+ },
317
+ },
318
+ play: async ({ args, canvasElement, step }) => {
319
+ // Scenario (UiHeadline): the user changes their mind while renaming and
320
+ // the original title stays.
321
+ const canvas = within(canvasElement);
322
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
323
+
324
+ await step(
325
+ 'interaction: the field opens focused with the title selected',
326
+ async () => {
327
+ await waitFor(() => expect(input).toHaveFocus());
328
+ await expect(input.selectionEnd).toBe(TITLE.length);
329
+ await expect(
330
+ canvas.getByRole('button', { name: 'Save' }),
331
+ ).toBeEnabled();
332
+ await expect(
333
+ canvas.getByRole('button', { name: 'Cancel' }),
334
+ ).toBeVisible();
335
+ },
336
+ );
337
+
338
+ await step('interaction: the cross keeps the original title', async () => {
339
+ await userEvent.keyboard('Draft');
340
+ await userEvent.click(canvas.getByRole('button', { name: 'Cancel' }));
341
+ await expect(canvas.getByRole('heading', { name: TITLE })).toBeVisible();
342
+ await expect(args.onCancel).toHaveBeenCalled();
343
+ await expect(args.onSave).not.toHaveBeenCalled();
344
+ });
345
+ },
346
+ };
347
+
348
+ /**
349
+ * An empty or blank name can't be saved: the check is disabled and Enter
350
+ * keeps the field open, so the title is never cleared by accident.
351
+ */
352
+ export const EmptyName: Story = {
353
+ render: (args) => ({
354
+ components: { UiHeadline },
355
+ setup() {
356
+ const title = ref(args.modelValue ?? TITLE);
357
+ watch(
358
+ () => args.modelValue,
359
+ (value) => {
360
+ title.value = value ?? TITLE;
361
+ },
362
+ );
363
+ const headline = ref<{ edit: () => Promise<void> } | null>(null);
364
+ onMounted(() => headline.value?.edit());
365
+ return { args, title, headline };
366
+ },
367
+ template: '<UiHeadline ref="headline" v-bind="args" v-model="title" />',
368
+ }),
369
+ parameters: {
370
+ // The field takes focus when it opens.
371
+ chromatic: { disableSnapshot: true },
372
+ docs: {
373
+ source: {
374
+ code: editingTemplateSource,
375
+ },
376
+ },
377
+ },
378
+ play: async ({ args, canvasElement, step }) => {
379
+ // Scenario (UiHeadline): the user deletes the name, or leaves only spaces,
380
+ // and tries to save.
381
+ const canvas = within(canvasElement);
382
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
383
+ const save = canvas.getByRole('button', { name: 'Save' });
384
+ await waitFor(() => expect(input).toHaveFocus());
385
+
386
+ await step(
387
+ 'interaction: an empty name disables Save and Enter keeps editing',
388
+ async () => {
389
+ await userEvent.clear(input);
390
+ await expect(save).toBeDisabled();
391
+ await userEvent.keyboard('{Enter}');
392
+ await expect(input).toBeInTheDocument();
393
+ await expect(input).toHaveFocus();
394
+ await expect(args.onSave).not.toHaveBeenCalled();
395
+ },
396
+ );
397
+
398
+ await step(
399
+ 'interaction: a name of only spaces disables Save and Enter keeps editing',
400
+ async () => {
401
+ await userEvent.keyboard(' ');
402
+ await expect(input).toHaveValue(' ');
403
+ await expect(save).toBeDisabled();
404
+ await userEvent.keyboard('{Enter}');
405
+ await expect(input).toBeInTheDocument();
406
+ await expect(args.onSave).not.toHaveBeenCalled();
407
+ await expect(
408
+ canvas.queryByRole('heading', { name: TITLE }),
409
+ ).not.toBeInTheDocument();
410
+ },
411
+ );
412
+
413
+ await step('interaction: typing a name enables Save again', async () => {
414
+ await userEvent.keyboard('Q3');
415
+ await expect(save).toBeEnabled();
416
+ });
417
+ },
418
+ };
419
+
420
+ const readOnlyTemplateSource = `<script setup lang="ts">
421
+ import { UiHeadline } from '@aleph-alpha/ui-library'
422
+ </script>
423
+
424
+ <template>
425
+ <UiHeadline
426
+ model-value="${TITLE}"
427
+ description="${DESCRIPTION}"
428
+ :editable="false"
429
+ />
430
+ </template>`;
431
+
432
+ /**
433
+ * Without the menu, for names the user cannot change.
434
+ */
435
+ export const ReadOnly: Story = {
436
+ args: { editable: false },
437
+ render: (args) => ({
438
+ components: { UiHeadline },
439
+ setup() {
440
+ return { args };
441
+ },
442
+ template: '<UiHeadline v-bind="args" />',
443
+ }),
444
+ parameters: {
445
+ docs: {
446
+ source: {
447
+ code: readOnlyTemplateSource,
448
+ },
449
+ },
450
+ },
451
+ play: async ({ canvasElement, step }) => {
452
+ // Scenario (UiHeadline): a read-only title offers no rename action.
453
+ const canvas = within(canvasElement);
454
+
455
+ await step('interaction: no menu button is rendered', async () => {
456
+ await expect(canvas.getByRole('heading', { name: TITLE })).toBeVisible();
457
+ await expect(canvas.queryByRole('button')).not.toBeInTheDocument();
458
+ });
459
+ },
460
+ };
461
+
462
+ const menuItemsTemplateSource = `<script setup lang="ts">
463
+ import { ref } from 'vue'
464
+ import {
465
+ UiDropdownMenuItem,
466
+ UiHeadline,
467
+ UiIcon,
468
+ } from '@aleph-alpha/ui-library'
469
+ import { CopyIcon, Trash2Icon } from '@aleph-alpha/ui-library/icons'
470
+
471
+ const title = ref('${TITLE}')
472
+ </script>
473
+
474
+ <template>
475
+ <UiHeadline v-model="title" description="${DESCRIPTION}">
476
+ <template #menu>
477
+ <UiDropdownMenuItem>
478
+ <UiIcon :name="CopyIcon" size="s" />
479
+ Duplicate
480
+ </UiDropdownMenuItem>
481
+ <UiDropdownMenuItem variant="destructive">
482
+ <UiIcon :name="Trash2Icon" size="s" />
483
+ Delete
484
+ </UiDropdownMenuItem>
485
+ </template>
486
+ </UiHeadline>
487
+ </template>`;
488
+
489
+ /**
490
+ * Further actions for the same object go in the `menu` slot, after
491
+ * "Edit name".
492
+ */
493
+ export const WithMenuItems: Story = {
494
+ render: (args) => ({
495
+ components: { UiHeadline, UiDropdownMenuItem, UiIcon },
496
+ setup() {
497
+ const title = ref(args.modelValue ?? TITLE);
498
+ watch(
499
+ () => args.modelValue,
500
+ (value) => {
501
+ title.value = value ?? TITLE;
502
+ },
503
+ );
504
+ return { args, title, CopyIcon, Trash2Icon };
505
+ },
506
+ template: `
507
+ <UiHeadline v-bind="args" v-model="title">
508
+ <template #menu>
509
+ <UiDropdownMenuItem>
510
+ <UiIcon :name="CopyIcon" size="s" />
511
+ Duplicate
512
+ </UiDropdownMenuItem>
513
+ <UiDropdownMenuItem variant="destructive">
514
+ <UiIcon :name="Trash2Icon" size="s" />
515
+ Delete
516
+ </UiDropdownMenuItem>
517
+ </template>
518
+ </UiHeadline>
519
+ `,
520
+ }),
521
+ parameters: {
522
+ docs: {
523
+ source: {
524
+ code: menuItemsTemplateSource,
525
+ },
526
+ },
527
+ },
528
+ play: async ({ canvasElement, step }) => {
529
+ // Scenario (UiHeadline): the menu lists "Edit name" first, then the
530
+ // consumer's actions.
531
+ const canvas = within(canvasElement);
532
+ const body = within(document.body);
533
+
534
+ await step('interaction: slot items follow "Edit name"', async () => {
535
+ await userEvent.click(
536
+ canvas.getByRole('button', { name: 'More actions' }),
537
+ );
538
+ await body.findByRole('menuitem', { name: 'Edit name' });
539
+ const names = body
540
+ .getAllByRole('menuitem')
541
+ .map((item) => item.textContent?.trim());
542
+ await expect(names).toEqual(['Edit name', 'Duplicate', 'Delete']);
543
+ });
544
+
545
+ await step(
546
+ 'interaction: Escape closes the menu and returns focus to its button',
547
+ async () => {
548
+ await userEvent.keyboard('{Escape}');
549
+ // The modal menu hides the page from assistive tech until it has
550
+ // fully closed; the axe audit after the play must see it closed.
551
+ await waitFor(() =>
552
+ expect(body.queryByRole('menu')).not.toBeInTheDocument(),
553
+ );
554
+ await waitFor(() =>
555
+ expect(
556
+ canvas.getByRole('button', { name: 'More actions' }),
557
+ ).toHaveFocus(),
558
+ );
559
+ await waitFor(() =>
560
+ expect(canvasElement.closest('[aria-hidden]')).toBeNull(),
561
+ );
562
+ },
563
+ );
564
+ },
565
+ };
566
+
567
+ const renderTestHeadline = () => ({
568
+ components: { UiHeadline },
569
+ setup() {
570
+ return { title: ref(TITLE), description: DESCRIPTION };
571
+ },
572
+ template: '<UiHeadline v-model="title" :description="description" />',
573
+ });
574
+
575
+ /**
576
+ * Renaming with the keyboard only: open the menu, choose "Edit name", type,
577
+ * and press Enter or Escape. Hidden test-only story.
578
+ */
579
+ export const KeyboardNavigation: Story = {
580
+ name: 'test - Keyboard Navigation',
581
+ tags: ['!dev', '!autodocs'],
582
+ parameters: {
583
+ // Focus moves during the play function.
584
+ chromatic: { disableSnapshot: true },
585
+ docs: {
586
+ description: {
587
+ story:
588
+ 'Press `Tab` to reach the menu button, `Enter` to open the menu and choose "Edit name", type a name, then `Enter` to save or `Escape` to cancel.',
589
+ },
590
+ },
591
+ },
592
+ render: renderTestHeadline,
593
+ play: async ({ canvasElement, step }) => {
594
+ // Scenario (UiHeadline): a keyboard user renames the page and focus comes
595
+ // back to where they started.
596
+ const canvas = within(canvasElement);
597
+ const body = within(document.body);
598
+ const trigger = canvas.getByRole('button', { name: 'More actions' });
599
+
600
+ await step('keyboard: Tab reaches the menu button', async () => {
601
+ await userEvent.tab();
602
+ await expect(trigger).toHaveFocus();
603
+ });
604
+
605
+ await step(
606
+ 'keyboard: Enter opens the menu and Enter on "Edit name" opens the field',
607
+ async () => {
608
+ await userEvent.keyboard('{Enter}');
609
+ const item = await body.findByRole('menuitem', { name: 'Edit name' });
610
+ await waitFor(() => expect(item).toHaveFocus());
611
+ await userEvent.keyboard('{Enter}');
612
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
613
+ await waitFor(() => expect(input).toHaveFocus());
614
+ },
615
+ );
616
+
617
+ await step(
618
+ 'keyboard: Enter saves and focus returns to the menu button',
619
+ async () => {
620
+ await userEvent.keyboard('Renamed{Enter}');
621
+ await expect(
622
+ canvas.getByRole('heading', { name: 'Renamed' }),
623
+ ).toBeVisible();
624
+ await waitFor(() =>
625
+ expect(
626
+ canvas.getByRole('button', { name: 'More actions' }),
627
+ ).toHaveFocus(),
628
+ );
629
+ },
630
+ );
631
+
632
+ await step('keyboard: Escape cancels and keeps the title', async () => {
633
+ await userEvent.keyboard('{Enter}');
634
+ const item = await body.findByRole('menuitem', { name: 'Edit name' });
635
+ await waitFor(() => expect(item).toHaveFocus());
636
+ await userEvent.keyboard('{Enter}');
637
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
638
+ await waitFor(() => expect(input).toHaveFocus());
639
+ await userEvent.keyboard('Draft{Escape}');
640
+ await expect(
641
+ canvas.getByRole('heading', { name: 'Renamed' }),
642
+ ).toBeVisible();
643
+ await waitFor(() =>
644
+ expect(
645
+ canvas.getByRole('button', { name: 'More actions' }),
646
+ ).toHaveFocus(),
647
+ );
648
+ });
649
+ },
650
+ };
651
+
652
+ /**
653
+ * What assistive technology announces: a heading, a named menu button, and a
654
+ * named, described field with named save and cancel buttons. Hidden
655
+ * test-only story.
656
+ */
657
+ export const ScreenReader: Story = {
658
+ name: 'test - Screen Reader',
659
+ tags: ['!dev', '!autodocs'],
660
+ parameters: {
661
+ chromatic: { disableSnapshot: true },
662
+ docs: {
663
+ description: {
664
+ story:
665
+ 'Verifies the heading, the menu button name and state, and the names and description of the editing controls.',
666
+ },
667
+ },
668
+ },
669
+ render: renderTestHeadline,
670
+ play: async ({ canvasElement, step }) => {
671
+ // Scenario (UiHeadline): a screen-reader user finds the title in the
672
+ // heading list and knows what each editing control does.
673
+ const canvas = within(canvasElement);
674
+ const body = within(document.body);
675
+
676
+ await step(
677
+ 'screen-reader: the title is a level 2 heading (WCAG 1.3.1)',
678
+ async () => {
679
+ await expect(
680
+ canvas.getByRole('heading', { level: 2 }),
681
+ ).toHaveAccessibleName(TITLE);
682
+ },
683
+ );
684
+
685
+ await step(
686
+ 'screen-reader: the menu button is named and reports its menu (WCAG 4.1.2)',
687
+ async () => {
688
+ const trigger = canvas.getByRole('button', { name: 'More actions' });
689
+ await expect(trigger).toHaveAttribute('aria-haspopup', 'menu');
690
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false');
691
+ },
692
+ );
693
+
694
+ await step(
695
+ 'screen-reader: the field is named and described, its buttons are named',
696
+ async () => {
697
+ await userEvent.click(
698
+ canvas.getByRole('button', { name: 'More actions' }),
699
+ );
700
+ await userEvent.click(
701
+ await body.findByRole('menuitem', { name: 'Edit name' }),
702
+ );
703
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
704
+ await expect(input).toHaveAccessibleDescription(DESCRIPTION);
705
+ await expect(
706
+ canvas.getByRole('button', { name: 'Save' }),
707
+ ).toBeInTheDocument();
708
+ await expect(
709
+ canvas.getByRole('button', { name: 'Cancel' }),
710
+ ).toBeInTheDocument();
711
+ },
712
+ );
713
+ },
714
+ };
715
+
716
+ const LONG_TITLE =
717
+ 'Quarterly planning for the design system and platform teams';
718
+
719
+ async function expectInViewport(el: Element) {
720
+ await expect(el).toBeVisible();
721
+ const { left, right } = el.getBoundingClientRect();
722
+ await expect(left).toBeGreaterThanOrEqual(0);
723
+ await expect(right).toBeLessThanOrEqual(
724
+ document.documentElement.clientWidth + 1,
725
+ );
726
+ }
727
+
728
+ async function expectNoHorizontalScroll() {
729
+ const { scrollWidth, clientWidth } = document.documentElement;
730
+ await expect(scrollWidth).toBeLessThanOrEqual(clientWidth + 1);
731
+ }
732
+
733
+ async function expectTargetSize(el: Element) {
734
+ const { width, height } = el.getBoundingClientRect();
735
+ await expect(Math.round(width)).toBeGreaterThanOrEqual(24);
736
+ await expect(Math.round(height)).toBeGreaterThanOrEqual(24);
737
+ }
738
+
739
+ async function openEditor(canvasElement: HTMLElement) {
740
+ const canvas = within(canvasElement);
741
+ await userEvent.click(canvas.getByRole('button', { name: 'More actions' }));
742
+ await userEvent.click(
743
+ await within(document.body).findByRole('menuitem', { name: 'Edit name' }),
744
+ );
745
+ const input = await canvas.findByRole('textbox', { name: 'Name' });
746
+ await waitFor(() => expect(input).toHaveFocus());
747
+ return input;
748
+ }
749
+
750
+ const zoomPlay =
751
+ (zoom: '200' | '400', criterion: string): Story['play'] =>
752
+ async ({ canvasElement, step }) => {
753
+ const canvas = within(canvasElement);
754
+
755
+ await step(
756
+ `zoom: preview is magnified to ${zoom}% (${criterion})`,
757
+ async () => {
758
+ await expect(document.documentElement.style.zoom).toBe(`${zoom}%`);
759
+ },
760
+ );
761
+
762
+ await step(
763
+ 'zoom: title, menu button and description stay in the viewport',
764
+ async () => {
765
+ await expectInViewport(canvas.getByRole('heading', { name: TITLE }));
766
+ await expectInViewport(
767
+ canvas.getByRole('button', { name: 'More actions' }),
768
+ );
769
+ await expectInViewport(canvas.getByText(DESCRIPTION));
770
+ await expectNoHorizontalScroll();
771
+ },
772
+ );
773
+
774
+ await step(
775
+ 'zoom: the field, Save and Cancel stay in the viewport and Save works',
776
+ async () => {
777
+ await openEditor(canvasElement);
778
+ await userEvent.keyboard(LONG_TITLE);
779
+ await expectInViewport(canvas.getByRole('textbox', { name: 'Name' }));
780
+ await expectInViewport(canvas.getByRole('button', { name: 'Save' }));
781
+ await expectInViewport(canvas.getByRole('button', { name: 'Cancel' }));
782
+ await expectNoHorizontalScroll();
783
+ await userEvent.click(canvas.getByRole('button', { name: 'Save' }));
784
+ await expect(
785
+ canvas.getByRole('heading', { name: LONG_TITLE }),
786
+ ).toBeVisible();
787
+ await expectNoHorizontalScroll();
788
+ },
789
+ );
790
+
791
+ await step('zoom: Cancel works and keeps the saved title', async () => {
792
+ await openEditor(canvasElement);
793
+ await userEvent.keyboard('Draft');
794
+ await expectInViewport(canvas.getByRole('button', { name: 'Cancel' }));
795
+ await userEvent.click(canvas.getByRole('button', { name: 'Cancel' }));
796
+ await expect(
797
+ canvas.getByRole('heading', { name: LONG_TITLE }),
798
+ ).toBeVisible();
799
+ });
800
+ };
801
+
802
+ /**
803
+ * Rendered at 200% page zoom (WCAG 1.4.4 Resize Text), in read mode and while
804
+ * renaming. Hidden test-only story.
805
+ */
806
+ export const ZoomMagnification: Story = {
807
+ name: 'test - Zoom Magnification',
808
+ tags: ['!dev', '!autodocs'],
809
+ globals: { zoom: '200' },
810
+ parameters: {
811
+ // Zoom changes the layout scale; the accessible-reflow Chromatic mode
812
+ // already covers magnified snapshots, so skip this one.
813
+ chromatic: { disableSnapshot: true },
814
+ docs: {
815
+ description: {
816
+ story:
817
+ 'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
818
+ },
819
+ },
820
+ },
821
+ render: renderTestHeadline,
822
+ // Scenario (UiHeadline): at 200% page zoom the headline and its editor stay
823
+ // inside the viewport and can still save and cancel.
824
+ play: zoomPlay('200', 'WCAG 1.4.4 Resize Text'),
825
+ };
826
+
827
+ /**
828
+ * Rendered at 400% page zoom (WCAG 1.4.10 Reflow), in read mode and while
829
+ * renaming. Hidden test-only story.
830
+ */
831
+ export const ZoomReflow: Story = {
832
+ name: 'test - Zoom Reflow',
833
+ tags: ['!dev', '!autodocs'],
834
+ globals: { zoom: '400' },
835
+ parameters: {
836
+ chromatic: { disableSnapshot: true },
837
+ docs: {
838
+ description: {
839
+ story:
840
+ 'Use the Zoom toolbar at 400% to check that the headline and its editor reflow.',
841
+ },
842
+ },
843
+ },
844
+ render: renderTestHeadline,
845
+ // Scenario (UiHeadline): at 400% page zoom the headline and its editor
846
+ // reflow without sideways scrolling and can still save and cancel.
847
+ play: zoomPlay('400', 'WCAG 1.4.10 Reflow'),
848
+ };
849
+
850
+ /**
851
+ * Rendered at the mobile (320px reflow) breakpoint via the Media breakpoint
852
+ * toolbar. Hidden test-only story.
853
+ */
854
+ export const MobileBreakpoint: Story = {
855
+ name: 'test - Mobile Breakpoint',
856
+ tags: ['!dev', '!autodocs'],
857
+ globals: { mediaBreakpoint: 'reflow' },
858
+ parameters: {
859
+ // Reflow snapshots are already covered by the accessible-reflow mode.
860
+ chromatic: { disableSnapshot: true },
861
+ docs: {
862
+ description: {
863
+ story:
864
+ 'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
865
+ },
866
+ },
867
+ },
868
+ render: renderTestHeadline,
869
+ play: async ({ canvasElement, step }) => {
870
+ // Scenario (UiHeadline): at the 320px mobile breakpoint the headline fits
871
+ // and its buttons are large enough to tap.
872
+ const canvas = within(canvasElement);
873
+
874
+ await step(
875
+ 'mobile: the layout fits without sideways scrolling (WCAG 1.4.10 Reflow)',
876
+ async () => {
877
+ const { scrollWidth, clientWidth } = document.documentElement;
878
+ await expect(scrollWidth).toBeLessThanOrEqual(clientWidth + 1);
879
+ },
880
+ );
881
+
882
+ await step(
883
+ 'mobile: the menu button is at least 24 by 24 pixels (WCAG 2.5.8)',
884
+ async () => {
885
+ await expectTargetSize(
886
+ canvas.getByRole('button', { name: 'More actions' }),
887
+ );
888
+ },
889
+ );
890
+
891
+ await step(
892
+ 'mobile: a long draft fits without sideways scrolling (WCAG 1.4.10 Reflow)',
893
+ async () => {
894
+ await openEditor(canvasElement);
895
+ await userEvent.keyboard(LONG_TITLE);
896
+ await expect(canvas.getByRole('textbox', { name: 'Name' })).toHaveValue(
897
+ LONG_TITLE,
898
+ );
899
+ await expectInViewport(canvas.getByRole('textbox', { name: 'Name' }));
900
+ await expectInViewport(canvas.getByRole('button', { name: 'Save' }));
901
+ await expectInViewport(canvas.getByRole('button', { name: 'Cancel' }));
902
+ await expectNoHorizontalScroll();
903
+ },
904
+ );
905
+
906
+ await step(
907
+ 'mobile: Save and Cancel are at least 24 by 24 pixels (WCAG 2.5.8)',
908
+ async () => {
909
+ await expectTargetSize(canvas.getByRole('button', { name: 'Save' }));
910
+ await expectTargetSize(canvas.getByRole('button', { name: 'Cancel' }));
911
+ await userEvent.keyboard('{Escape}');
912
+ },
913
+ );
914
+ },
915
+ };