@aleph-alpha/ui-library 5.8.0 → 5.10.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 (111) hide show
  1. package/README.md +8 -7
  2. package/config.js +157 -4
  3. package/dist/system/__tests__/test-utils.d.ts +24 -0
  4. package/dist/system/components/UiAutocomplete/UiAutocomplete.vue.d.ts +8 -4
  5. package/dist/system/components/UiAutocomplete/types.d.ts +11 -0
  6. package/dist/system/components/UiFadeMask/UiFadeMask.vue.d.ts +21 -0
  7. package/dist/system/components/UiFadeMask/index.d.ts +2 -0
  8. package/dist/system/components/UiFadeMask/types.d.ts +33 -0
  9. package/dist/system/components/UiFileItem/UiFileItem.vue.d.ts +3 -1
  10. package/dist/system/components/UiFileType/types.d.ts +2 -1
  11. package/dist/system/components/UiListbox/UiListboxVirtualizer.vue.d.ts +21 -0
  12. package/dist/system/components/UiListbox/index.d.ts +1 -0
  13. package/dist/system/components/UiListbox/types.d.ts +36 -0
  14. package/dist/system/components/UiSelect/UiSelectVirtualizer.vue.d.ts +21 -0
  15. package/dist/system/components/UiSelect/index.d.ts +1 -0
  16. package/dist/system/components/UiSelect/types.d.ts +36 -0
  17. package/dist/system/components/UiSwitch/UiSwitch.vue.d.ts +1 -0
  18. package/dist/system/components/UiSwitch/types.d.ts +12 -0
  19. package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
  20. package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
  21. package/dist/system/components/core/switch/Switch.vue.d.ts +2 -0
  22. package/dist/system/components/core/switch/index.d.ts +8 -0
  23. package/dist/system/components/index.d.ts +1 -0
  24. package/dist/system/index.d.ts +1 -0
  25. package/dist/system/lib/scrollbar.d.ts +37 -0
  26. package/dist/system/lib/useAnchorFloat.d.ts +0 -2
  27. package/dist/system/lib/useIsTruncated.d.ts +15 -0
  28. package/dist/system/lib.js +22660 -21292
  29. package/dist/system/mobile-device-frame.png +0 -0
  30. package/dist/system/patterns/UiFloatActions/types.d.ts +2 -2
  31. package/dist/system/patterns/UiProfileCard/index.d.ts +7 -0
  32. package/dist/system/patterns/UiTopBar/UiTopBar.vue.d.ts +23 -0
  33. package/dist/system/patterns/UiTopBar/index.d.ts +2 -0
  34. package/dist/system/patterns/UiTopBar/types.d.ts +28 -0
  35. package/dist/system/patterns/index.d.ts +2 -0
  36. package/dist/system/story-sidebar-icons.json +7 -1
  37. package/docs/public-docs/foundations.md +17 -15
  38. package/package.json +4 -3
  39. package/src/__tests__/test-utils.ts +74 -0
  40. package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +94 -0
  41. package/src/components/UiAutocomplete/UiAutocomplete.vue +164 -44
  42. package/src/components/UiAutocomplete/__tests__/UiAutocompleteVirtualized.test.ts +107 -0
  43. package/src/components/UiAutocomplete/types.ts +11 -0
  44. package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +54 -0
  45. package/src/components/UiCodeBlock/UiCodeBlock.vue +84 -32
  46. package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +65 -21
  47. package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +760 -0
  48. package/src/components/UiFadeMask/UiFadeMask.stories.ts +563 -0
  49. package/src/components/UiFadeMask/UiFadeMask.vue +86 -0
  50. package/src/components/UiFadeMask/__tests__/UiFadeMask.test.ts +52 -0
  51. package/src/components/UiFadeMask/index.ts +6 -0
  52. package/src/components/UiFadeMask/types.ts +35 -0
  53. package/src/components/UiFileItem/UiFileItem.vue +17 -4
  54. package/src/components/UiFileItem/__tests__/UiFileItem.test.ts +49 -90
  55. package/src/components/UiFileType/UiFileType.stories.ts +13 -2
  56. package/src/components/UiFileType/UiFileType.vue +8 -0
  57. package/src/components/UiFileType/__tests__/UiFileType.test.ts +10 -0
  58. package/src/components/UiFileType/types.ts +3 -1
  59. package/src/components/UiListbox/UiListbox.stories.ts +121 -0
  60. package/src/components/UiListbox/UiListboxVirtualizer.vue +42 -0
  61. package/src/components/UiListbox/__tests__/UiListboxVirtualizer.test.ts +96 -0
  62. package/src/components/UiListbox/index.ts +1 -0
  63. package/src/components/UiListbox/types.ts +42 -0
  64. package/src/components/UiSelect/UiSelect.stories.ts +109 -1
  65. package/src/components/UiSelect/UiSelectVirtualizer.vue +373 -0
  66. package/src/components/UiSelect/__tests__/UiSelectVirtualizer.test.ts +185 -0
  67. package/src/components/UiSelect/index.ts +1 -0
  68. package/src/components/UiSelect/types.ts +38 -0
  69. package/src/components/UiSnackbar/UiSnackbar.stories.ts +4 -5
  70. package/src/components/UiSwitch/UiSwitch.stories.ts +658 -7
  71. package/src/components/UiSwitch/UiSwitch.vue +1 -0
  72. package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +48 -0
  73. package/src/components/UiSwitch/types.ts +13 -0
  74. package/src/components/UiToggleGroup/UiToggleGroup.vue +1 -1
  75. package/src/components/core/dropdown-menu/DropdownMenuCheckboxItem.vue +8 -2
  76. package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
  77. package/src/components/core/dropdown-menu/DropdownMenuRadioItem.vue +8 -2
  78. package/src/components/core/dropdown-menu/DropdownMenuSubTrigger.vue +1 -1
  79. package/src/components/core/field/FieldLabel.vue +1 -1
  80. package/src/components/core/label/Label.vue +1 -1
  81. package/src/components/core/listbox/ListboxItem.vue +1 -1
  82. package/src/components/core/switch/Switch.vue +23 -17
  83. package/src/components/core/switch/index.ts +36 -0
  84. package/src/components/index.ts +1 -0
  85. package/src/index.ts +3 -0
  86. package/src/lib/scrollbar.ts +295 -0
  87. package/src/lib/useAnchorFloat.ts +0 -2
  88. package/src/lib/useIsTruncated.test.ts +174 -0
  89. package/src/lib/useIsTruncated.ts +75 -0
  90. package/src/patterns/UiFileUpload/UiFileItem.stories.ts +1162 -0
  91. package/src/patterns/UiFileUpload/UiFileUpload.stories.ts +552 -1
  92. package/src/patterns/UiFileUpload/__tests__/UiFileUpload.test.ts +5 -21
  93. package/src/patterns/UiFloatActions/UiFloatActionItems.vue +0 -1
  94. package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +4 -6
  95. package/src/patterns/UiFloatActions/UiFloatActions.vue +23 -12
  96. package/src/patterns/UiFloatActions/__tests__/UiFloatActions.test.ts +23 -0
  97. package/src/patterns/UiFloatActions/types.ts +2 -2
  98. package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +1079 -0
  99. package/src/patterns/UiProfileCard/index.ts +7 -0
  100. package/src/patterns/UiTopBar/UiTopBar.stories.ts +1121 -0
  101. package/src/patterns/UiTopBar/UiTopBar.vue +166 -0
  102. package/src/patterns/UiTopBar/__tests__/UiTopBar.test.ts +245 -0
  103. package/src/patterns/UiTopBar/index.ts +2 -0
  104. package/src/patterns/UiTopBar/types.ts +29 -0
  105. package/src/patterns/index.ts +2 -0
  106. package/src/theme/Background.stories.ts +27 -1
  107. package/src/theme/BorderRadius.stories.ts +5 -0
  108. package/src/theme/Overlay.stories.ts +12 -1
  109. package/src/theme/Scrollbar.stories.ts +260 -0
  110. package/tokens.json +224 -2
  111. package/src/components/UiFileItem/UiFileItem.stories.ts +0 -442
@@ -1,7 +1,17 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
2
  import { expect, fn, userEvent, within } from 'storybook/test';
3
3
  import UiSwitch from './UiSwitch.vue';
4
- import { ref, watch } from 'vue';
4
+ import {
5
+ UiField,
6
+ UiFieldContent,
7
+ UiFieldDescription,
8
+ UiFieldGroup,
9
+ UiFieldLabel,
10
+ UiFieldLegend,
11
+ UiFieldSet,
12
+ UiFieldTitle,
13
+ } from '../UiField';
14
+ import { reactive, ref, watch } from 'vue';
5
15
 
6
16
  /**
7
17
  * UiSwitch is a toggle control that flips a single setting between on and off.
@@ -20,11 +30,18 @@ const meta: Meta<typeof UiSwitch> = {
20
30
  control: 'boolean',
21
31
  description: 'Toggle the switch state',
22
32
  },
33
+ size: {
34
+ control: 'select',
35
+ options: ['default', 'sm'],
36
+ description:
37
+ '`default` (36×20px) for standalone settings rows; `sm` (28×16px) for dense rows and table cells. Purely visual — both sizes expose the same role, name, and state.',
38
+ },
23
39
  },
24
40
  args: {
25
41
  name: 'switch',
26
42
  disabled: false,
27
43
  modelValue: false,
44
+ size: 'default',
28
45
  },
29
46
  };
30
47
 
@@ -71,7 +88,13 @@ export const Default: Story = {
71
88
  },
72
89
  template: `
73
90
  <div class="flex items-center gap-x-3">
74
- <UiSwitch id="airplane-mode" v-model="enabled" name="airplane-mode" :disabled="args.disabled" />
91
+ <UiSwitch
92
+ id="airplane-mode"
93
+ v-model="enabled"
94
+ name="airplane-mode"
95
+ :disabled="args.disabled"
96
+ :size="args.size"
97
+ />
75
98
  <label for="airplane-mode" class="label-14-medium">Airplane Mode</label>
76
99
  </div>
77
100
  `,
@@ -276,6 +299,563 @@ export const Disabled: Story = {
276
299
  },
277
300
  };
278
301
 
302
+ const allSizesTemplateSource = `<script setup lang="ts">
303
+ import { ref } from 'vue'
304
+ import { UiSwitch } from '@aleph-alpha/ui-library'
305
+
306
+ const airplane = ref(false)
307
+ const wifi = ref(true)
308
+ </script>
309
+
310
+ <template>
311
+ <!-- 36×20px track with a 16px thumb -->
312
+ <UiSwitch id="airplane" v-model="airplane" name="airplane" />
313
+
314
+ <!-- 28×16px track with a 12px thumb -->
315
+ <UiSwitch id="wifi" v-model="wifi" name="wifi" size="sm" />
316
+ </template>`;
317
+
318
+ /**
319
+ * Both sizes, in each resting state. `default` (36×20px) is the standalone
320
+ * setting; `sm` (28×16px) is for dense rows such as table cells and toolbars,
321
+ * where the full-size track would crowd the row.
322
+ *
323
+ * Size is purely visual: both sizes expose the same `switch` role, name, and
324
+ * `aria-checked` state. Neither track reaches the 24px minimum target height, so
325
+ * the label has to extend the target in both cases (WCAG 2.5.8) — all the more
326
+ * so for `sm`.
327
+ */
328
+ export const AllSizes: Story = {
329
+ render: () => ({
330
+ components: { UiSwitch },
331
+ setup() {
332
+ const airplaneDefault = ref(false);
333
+ const wifiDefault = ref(true);
334
+ const airplaneSmall = ref(false);
335
+ const wifiSmall = ref(true);
336
+ return { airplaneDefault, wifiDefault, airplaneSmall, wifiSmall };
337
+ },
338
+ template: `
339
+ <div class="flex flex-col gap-y-8">
340
+ <section class="flex flex-col gap-y-2">
341
+ <h3 class="label-12-medium uppercase tracking-wide text-content-on-surface-muted">
342
+ Default
343
+ </h3>
344
+ <div class="flex flex-wrap items-center gap-x-8 gap-y-2">
345
+ <label
346
+ for="size-default-airplane"
347
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
348
+ >
349
+ <UiSwitch
350
+ id="size-default-airplane"
351
+ v-model="airplaneDefault"
352
+ name="airplane-default"
353
+ />
354
+ <span>Airplane Mode</span>
355
+ </label>
356
+
357
+ <label
358
+ for="size-default-wifi"
359
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
360
+ >
361
+ <UiSwitch id="size-default-wifi" v-model="wifiDefault" name="wifi-default" />
362
+ <span>Wi-Fi</span>
363
+ </label>
364
+
365
+ <label
366
+ for="size-default-bluetooth"
367
+ class="flex min-h-6 w-fit items-center gap-x-3 py-1 label-14-medium text-content-on-surface-muted"
368
+ >
369
+ <UiSwitch id="size-default-bluetooth" name="bluetooth-default" disabled />
370
+ <span>Bluetooth</span>
371
+ </label>
372
+ </div>
373
+ </section>
374
+
375
+ <section class="flex flex-col gap-y-2">
376
+ <h3 class="label-12-medium uppercase tracking-wide text-content-on-surface-muted">
377
+ Small
378
+ </h3>
379
+ <div class="flex flex-wrap items-center gap-x-8 gap-y-2">
380
+ <label
381
+ for="size-sm-airplane"
382
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
383
+ >
384
+ <UiSwitch
385
+ id="size-sm-airplane"
386
+ v-model="airplaneSmall"
387
+ name="airplane-sm"
388
+ size="sm"
389
+ />
390
+ <span>Airplane Mode</span>
391
+ </label>
392
+
393
+ <label
394
+ for="size-sm-wifi"
395
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
396
+ >
397
+ <UiSwitch id="size-sm-wifi" v-model="wifiSmall" name="wifi-sm" size="sm" />
398
+ <span>Wi-Fi</span>
399
+ </label>
400
+
401
+ <label
402
+ for="size-sm-bluetooth"
403
+ class="flex min-h-6 w-fit items-center gap-x-3 py-1 label-14-medium text-content-on-surface-muted"
404
+ >
405
+ <UiSwitch id="size-sm-bluetooth" name="bluetooth-sm" size="sm" disabled />
406
+ <span>Bluetooth</span>
407
+ </label>
408
+ </div>
409
+ </section>
410
+ </div>
411
+ `,
412
+ }),
413
+ parameters: {
414
+ controls: { disable: true },
415
+ docs: {
416
+ source: {
417
+ code: allSizesTemplateSource,
418
+ },
419
+ },
420
+ },
421
+ play: async ({ canvasElement, step }) => {
422
+ // Scenario (Switch): the same three settings rendered at both sizes.
423
+ // Non-mutating, so the visual snapshot stays stable.
424
+ const canvas = within(canvasElement);
425
+ // Each name appears once per section, in DOM order: default, then small.
426
+ const [airplaneDefault, airplaneSmall] = canvas.getAllByRole('switch', {
427
+ name: 'Airplane Mode',
428
+ });
429
+ const [wifiDefault, wifiSmall] = canvas.getAllByRole('switch', {
430
+ name: 'Wi-Fi',
431
+ });
432
+
433
+ await step(
434
+ 'interaction: the small size renders a smaller track than the default',
435
+ async () => {
436
+ const defaultTrack = airplaneDefault.getBoundingClientRect();
437
+ await expect(defaultTrack.width).toBe(36);
438
+ await expect(defaultTrack.height).toBe(20);
439
+
440
+ const smallTrack = airplaneSmall.getBoundingClientRect();
441
+ await expect(smallTrack.width).toBe(28);
442
+ await expect(smallTrack.height).toBe(16);
443
+ },
444
+ );
445
+
446
+ await step('interaction: the thumb scales with the track', async () => {
447
+ const defaultThumb = airplaneDefault.querySelector(
448
+ '[data-slot="switch-thumb"]',
449
+ ) as HTMLElement;
450
+ const smallThumb = airplaneSmall.querySelector(
451
+ '[data-slot="switch-thumb"]',
452
+ ) as HTMLElement;
453
+
454
+ await expect(defaultThumb.getBoundingClientRect().height).toBe(16);
455
+ await expect(smallThumb.getBoundingClientRect().height).toBe(12);
456
+ });
457
+
458
+ await step(
459
+ 'interaction: the thumb travels the whole track at both sizes',
460
+ async () => {
461
+ // An off thumb rests against the near edge and an on thumb against the
462
+ // far one, so the two insets match at a given size — the check that
463
+ // catches a thumb offset that was tuned for the other size.
464
+ for (const [off, on] of [
465
+ [airplaneDefault, wifiDefault],
466
+ [airplaneSmall, wifiSmall],
467
+ ]) {
468
+ const offTrack = off.getBoundingClientRect();
469
+ const offThumb = (
470
+ off.querySelector('[data-slot="switch-thumb"]') as HTMLElement
471
+ ).getBoundingClientRect();
472
+ const onTrack = on.getBoundingClientRect();
473
+ const onThumb = (
474
+ on.querySelector('[data-slot="switch-thumb"]') as HTMLElement
475
+ ).getBoundingClientRect();
476
+
477
+ await expect(onTrack.right - onThumb.right).toBe(
478
+ offThumb.left - offTrack.left,
479
+ );
480
+ }
481
+ },
482
+ );
483
+
484
+ await step(
485
+ 'screen-reader: size is visual only — both sizes expose the same role, name, and state (WCAG 4.1.2)',
486
+ async () => {
487
+ for (const toggle of [airplaneDefault, airplaneSmall]) {
488
+ await expect(toggle).toHaveAttribute('role', 'switch');
489
+ await expect(toggle).toHaveAccessibleName('Airplane Mode');
490
+ await expect(toggle).toHaveAttribute('aria-checked', 'false');
491
+ }
492
+ for (const toggle of [wifiDefault, wifiSmall]) {
493
+ await expect(toggle).toHaveAttribute('aria-checked', 'true');
494
+ }
495
+ for (const toggle of canvas.getAllByRole('switch', {
496
+ name: 'Bluetooth',
497
+ })) {
498
+ await expect(toggle).toBeDisabled();
499
+ }
500
+ },
501
+ );
502
+ },
503
+ };
504
+
505
+ const withFieldTemplateSource = `<script setup lang="ts">
506
+ import { reactive } from 'vue'
507
+ import {
508
+ UiField,
509
+ UiFieldContent,
510
+ UiFieldDescription,
511
+ UiFieldGroup,
512
+ UiFieldLabel,
513
+ UiFieldLegend,
514
+ UiFieldSet,
515
+ UiFieldTitle,
516
+ UiSwitch,
517
+ } from '@aleph-alpha/ui-library'
518
+
519
+ const state = reactive({ push: true, email: false })
520
+ </script>
521
+
522
+ <template>
523
+ <UiFieldGroup>
524
+ <UiFieldSet>
525
+ <UiFieldLegend variant="label">Notifications</UiFieldLegend>
526
+
527
+ <UiFieldGroup data-slot="checkbox-group">
528
+ <!-- Plain row: the label names the switch, the description sits under it. -->
529
+ <UiField orientation="horizontal">
530
+ <UiFieldContent>
531
+ <UiFieldLabel for="push" class="min-h-6">Push notifications</UiFieldLabel>
532
+ <UiFieldDescription>
533
+ Get alerts on your device as soon as something happens.
534
+ </UiFieldDescription>
535
+ </UiFieldContent>
536
+ <!-- order-last trails the switch; drop it to let the switch lead. -->
537
+ <UiSwitch id="push" v-model="state.push" name="push" class="order-last" />
538
+ </UiField>
539
+
540
+ <!-- Card: wrapping the field in a label makes the whole row the target. -->
541
+ <UiFieldLabel for="email">
542
+ <UiField orientation="horizontal">
543
+ <UiFieldContent>
544
+ <UiFieldTitle>Email digest</UiFieldTitle>
545
+ <UiFieldDescription>
546
+ Receive a single summary of activity every morning.
547
+ </UiFieldDescription>
548
+ </UiFieldContent>
549
+ <UiSwitch
550
+ id="email"
551
+ v-model="state.email"
552
+ name="email"
553
+ size="sm"
554
+ class="order-last"
555
+ />
556
+ </UiField>
557
+ </UiFieldLabel>
558
+ </UiFieldGroup>
559
+ </UiFieldSet>
560
+ </UiFieldGroup>
561
+ </template>`;
562
+
563
+ type WithFieldSize = { key: 'default' | 'sm'; label: string; short: string };
564
+ type WithFieldPlacement = { key: string; label: string; trailing: boolean };
565
+ type WithFieldRow = {
566
+ key: string;
567
+ title: string;
568
+ description: string;
569
+ card: boolean;
570
+ on: boolean;
571
+ };
572
+
573
+ const withFieldSizes: WithFieldSize[] = [
574
+ {
575
+ key: 'default',
576
+ label: 'Default size',
577
+ short: 'default size',
578
+ },
579
+ { key: 'sm', label: 'Small size', short: 'small size' },
580
+ ];
581
+
582
+ const withFieldPlacements: WithFieldPlacement[] = [
583
+ { key: 'trailing', label: 'Switch trailing', trailing: true },
584
+ { key: 'leading', label: 'Switch leading', trailing: false },
585
+ ];
586
+
587
+ const withFieldRows: WithFieldRow[] = [
588
+ {
589
+ key: 'updates',
590
+ title: 'Product updates',
591
+ description: 'Occasional news about new features.',
592
+ card: false,
593
+ on: false,
594
+ },
595
+ {
596
+ key: 'email',
597
+ title: 'Email digest',
598
+ description: 'Receive a single summary of activity every morning.',
599
+ card: true,
600
+ on: false,
601
+ },
602
+ {
603
+ key: 'push',
604
+ title: 'Push notifications',
605
+ description: 'Get alerts on your device as soon as something happens.',
606
+ card: false,
607
+ on: true,
608
+ },
609
+ {
610
+ key: 'weekly',
611
+ title: 'Weekly summary',
612
+ description: 'A recap of everything that happened this week.',
613
+ card: true,
614
+ on: true,
615
+ },
616
+ ];
617
+
618
+ const withFieldRowId = (
619
+ size: WithFieldSize,
620
+ placement: WithFieldPlacement,
621
+ row: WithFieldRow,
622
+ ) => `${size.key}-${placement.key}-${row.key}`;
623
+
624
+ /**
625
+ * Switches composed with `UiField`, laid out as the design matrix: both sizes,
626
+ * both control placements, and each as a plain row and as a card.
627
+ *
628
+ * A plain row keeps the switch on the same line as its `UiFieldLabel` and puts
629
+ * the description underneath. Wrapping the field in a `UiFieldLabel` instead
630
+ * turns the row into a bordered card that highlights while its switch is on, and
631
+ * makes the whole card the activation target — which the 20px-tall track cannot
632
+ * be on its own (WCAG 2.5.8). A plain row needs `min-h-6` on its label to reach
633
+ * the same minimum.
634
+ *
635
+ * A horizontal `UiField` orders `UiFieldContent` after the control, so the switch
636
+ * leads by default; add `class="order-last"` to trail it behind the text, which
637
+ * is the usual reading order for a settings list. Mark the wrapping
638
+ * `UiFieldGroup` with `data-slot="checkbox-group"` so the rows sit at the tighter
639
+ * group spacing.
640
+ */
641
+ export const WithField: Story = {
642
+ render: () => ({
643
+ components: {
644
+ UiField,
645
+ UiFieldContent,
646
+ UiFieldDescription,
647
+ UiFieldGroup,
648
+ UiFieldLabel,
649
+ UiFieldLegend,
650
+ UiFieldSet,
651
+ UiFieldTitle,
652
+ UiSwitch,
653
+ },
654
+ setup() {
655
+ const state = reactive<Record<string, boolean>>({});
656
+ for (const size of withFieldSizes) {
657
+ for (const placement of withFieldPlacements) {
658
+ for (const row of withFieldRows) {
659
+ state[withFieldRowId(size, placement, row)] = row.on;
660
+ }
661
+ }
662
+ }
663
+
664
+ return {
665
+ sizes: withFieldSizes,
666
+ placements: withFieldPlacements,
667
+ rows: withFieldRows,
668
+ rowId: withFieldRowId,
669
+ state,
670
+ };
671
+ },
672
+ template: `
673
+ <div class="flex w-full flex-col gap-y-8">
674
+ <section v-for="size in sizes" :key="size.key" class="flex flex-col gap-y-3">
675
+ <h3 class="label-12-medium uppercase tracking-wide text-content-on-surface-muted">
676
+ {{ size.label }}
677
+ </h3>
678
+
679
+ <div class="grid gap-16 md:grid-cols-2">
680
+ <UiFieldGroup v-for="placement in placements" :key="placement.key">
681
+ <UiFieldSet>
682
+ <UiFieldLegend variant="label">
683
+ {{ placement.label }} — {{ size.short }}
684
+ </UiFieldLegend>
685
+
686
+ <UiFieldGroup data-slot="checkbox-group">
687
+ <template v-for="row in rows" :key="row.key">
688
+ <UiFieldLabel v-if="row.card" :for="rowId(size, placement, row)">
689
+ <UiField orientation="horizontal">
690
+ <UiFieldContent>
691
+ <UiFieldTitle>{{ row.title }}</UiFieldTitle>
692
+ <UiFieldDescription>{{ row.description }}</UiFieldDescription>
693
+ </UiFieldContent>
694
+ <UiSwitch
695
+ :id="rowId(size, placement, row)"
696
+ v-model="state[rowId(size, placement, row)]"
697
+ :name="rowId(size, placement, row)"
698
+ :size="size.key"
699
+ :class="placement.trailing ? 'order-last' : undefined"
700
+ />
701
+ </UiField>
702
+ </UiFieldLabel>
703
+
704
+ <UiField v-else orientation="horizontal">
705
+ <UiFieldContent>
706
+ <UiFieldLabel :for="rowId(size, placement, row)" class="min-h-6">
707
+ {{ row.title }}
708
+ </UiFieldLabel>
709
+ <UiFieldDescription>{{ row.description }}</UiFieldDescription>
710
+ </UiFieldContent>
711
+ <UiSwitch
712
+ :id="rowId(size, placement, row)"
713
+ v-model="state[rowId(size, placement, row)]"
714
+ :name="rowId(size, placement, row)"
715
+ :size="size.key"
716
+ :class="placement.trailing ? 'order-last' : undefined"
717
+ />
718
+ </UiField>
719
+ </template>
720
+ </UiFieldGroup>
721
+ </UiFieldSet>
722
+ </UiFieldGroup>
723
+ </div>
724
+ </section>
725
+ </div>
726
+ `,
727
+ }),
728
+ parameters: {
729
+ controls: { disable: true },
730
+ docs: {
731
+ source: {
732
+ code: withFieldTemplateSource,
733
+ },
734
+ },
735
+ },
736
+ play: async ({ canvasElement, step }) => {
737
+ // Scenario (Switch): the whole field matrix — both sizes, both placements,
738
+ // as plain rows and as cards. Non-mutating, so the snapshot stays stable.
739
+ const canvas = within(canvasElement);
740
+ const sections = [
741
+ {
742
+ group: canvas.getByRole('group', { name: /trailing.*default/ }),
743
+ trailing: true,
744
+ height: 20,
745
+ },
746
+ {
747
+ group: canvas.getByRole('group', { name: /leading.*default/ }),
748
+ trailing: false,
749
+ height: 20,
750
+ },
751
+ {
752
+ group: canvas.getByRole('group', { name: /trailing.*small/ }),
753
+ trailing: true,
754
+ height: 16,
755
+ },
756
+ {
757
+ group: canvas.getByRole('group', { name: /leading.*small/ }),
758
+ trailing: false,
759
+ height: 16,
760
+ },
761
+ ];
762
+
763
+ await step(
764
+ 'screen-reader: every fieldset is a group named by its legend (WCAG 1.3.1)',
765
+ async () => {
766
+ await expect(
767
+ canvas.getAllByRole('group', { name: /Switch (trailing|leading)/ }),
768
+ ).toHaveLength(4);
769
+ },
770
+ );
771
+
772
+ await step(
773
+ 'screen-reader: plain rows are named by their label, cards by the whole card (WCAG 1.3.1 / 4.1.2)',
774
+ async () => {
775
+ for (const { group } of sections) {
776
+ const rows = within(group);
777
+ await expect(rows.getAllByRole('switch')).toHaveLength(4);
778
+ await expect(
779
+ rows.getByRole('switch', { name: 'Product updates' }),
780
+ ).not.toBeChecked();
781
+ await expect(
782
+ rows.getByRole('switch', { name: 'Push notifications' }),
783
+ ).toBeChecked();
784
+ // A card label wraps the title and the description, so both end up in
785
+ // the accessible name.
786
+ await expect(
787
+ rows.getByRole('switch', { name: /Email digest.*single summary/ }),
788
+ ).not.toBeChecked();
789
+ await expect(
790
+ rows.getByRole('switch', { name: /Weekly summary.*recap/ }),
791
+ ).toBeChecked();
792
+ }
793
+ },
794
+ );
795
+
796
+ await step(
797
+ 'interaction: the on and off states are exposed beyond the track colour (WCAG 1.4.1)',
798
+ async () => {
799
+ // `data-state` is what the card highlight keys off, so the state is in
800
+ // the DOM and not only in the rendered colour.
801
+ for (const { group } of sections) {
802
+ const rows = within(group);
803
+ await expect(
804
+ rows.getByRole('switch', { name: 'Push notifications' }),
805
+ ).toHaveAttribute('data-state', 'checked');
806
+ await expect(
807
+ rows.getByRole('switch', { name: 'Product updates' }),
808
+ ).toHaveAttribute('data-state', 'unchecked');
809
+ }
810
+ },
811
+ );
812
+
813
+ await step(
814
+ 'interaction: each section renders its size (36×20 and 28×16)',
815
+ async () => {
816
+ for (const { group, height } of sections) {
817
+ for (const toggle of within(group).getAllByRole('switch')) {
818
+ await expect(toggle.getBoundingClientRect().height).toBe(height);
819
+ }
820
+ }
821
+ },
822
+ );
823
+
824
+ await step(
825
+ 'interaction: order-last trails the switch, the default leads it',
826
+ async () => {
827
+ for (const { group, trailing } of sections) {
828
+ for (const toggle of within(group).getAllByRole('switch')) {
829
+ const content = toggle.parentElement?.querySelector(
830
+ '[data-slot="field-content"]',
831
+ ) as HTMLElement;
832
+ const toggleLeft = toggle.getBoundingClientRect().left;
833
+ const contentLeft = content.getBoundingClientRect().left;
834
+
835
+ if (trailing) await expect(toggleLeft).toBeGreaterThan(contentLeft);
836
+ else await expect(toggleLeft).toBeLessThan(contentLeft);
837
+ }
838
+ }
839
+ },
840
+ );
841
+
842
+ await step(
843
+ 'interaction: every row offers a target of at least 24px (WCAG 2.5.8)',
844
+ async () => {
845
+ // The label is the target the track is too short to be: the card label
846
+ // for a card row, the `min-h-6` text label for a plain one.
847
+ for (const toggle of canvas.getAllByRole('switch')) {
848
+ const label = canvasElement.querySelector(
849
+ `label[for="${toggle.id}"]`,
850
+ ) as HTMLElement;
851
+ await expect(
852
+ label.getBoundingClientRect().height,
853
+ ).toBeGreaterThanOrEqual(24);
854
+ }
855
+ },
856
+ );
857
+ },
858
+ };
279
859
  /**
280
860
  * Accessibility guidance for switches: a visible label that also enlarges the
281
861
  * tap target, state that survives without colour, and an explicit name when the
@@ -429,9 +1009,10 @@ export const Accessibility: Story = {
429
1009
  // literal properties so Storybook's static CSF indexer can read them.
430
1010
 
431
1011
  // Shared render for the test scenarios: an enabled switch that starts off, a
432
- // second one that starts on, and a disabled one — a single fixture covering
433
- // naming, both resting states, activation, and the disabled guard. Each row is
434
- // a `min-h-6` label so the whole row is a WCAG 2.5.8 sized target.
1012
+ // second one that starts on, a disabled one, and a small one — a single fixture
1013
+ // covering naming, both resting states, activation, the disabled guard, and
1014
+ // both sizes. Each row is a `min-h-6` label so the whole row is a WCAG 2.5.8
1015
+ // sized target, which is what makes the sub-24px tracks usable.
435
1016
  function renderTestSwitches() {
436
1017
  return {
437
1018
  components: { UiSwitch },
@@ -439,7 +1020,8 @@ function renderTestSwitches() {
439
1020
  const airplane = ref(false);
440
1021
  const wifi = ref(true);
441
1022
  const bluetooth = ref(false);
442
- return { airplane, wifi, bluetooth, onUpdate };
1023
+ const location = ref(false);
1024
+ return { airplane, wifi, bluetooth, location, onUpdate };
443
1025
  },
444
1026
  template: `
445
1027
  <div class="flex flex-col gap-y-2">
@@ -471,6 +1053,20 @@ function renderTestSwitches() {
471
1053
  <UiSwitch id="t-bluetooth" v-model="bluetooth" name="bluetooth" disabled />
472
1054
  <span>Bluetooth</span>
473
1055
  </label>
1056
+
1057
+ <label
1058
+ for="t-location"
1059
+ class="flex min-h-6 w-fit cursor-pointer items-center gap-x-3 py-1 label-14-medium"
1060
+ >
1061
+ <UiSwitch
1062
+ id="t-location"
1063
+ v-model="location"
1064
+ name="location"
1065
+ size="sm"
1066
+ @update:model-value="onUpdate"
1067
+ />
1068
+ <span>Location Services</span>
1069
+ </label>
474
1070
  </div>
475
1071
  `,
476
1072
  };
@@ -584,6 +1180,7 @@ export const KeyboardNavigation: Story = {
584
1180
  const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
585
1181
  const wifi = canvas.getByRole('switch', { name: 'Wi-Fi' });
586
1182
  const bluetooth = canvas.getByRole('switch', { name: 'Bluetooth' });
1183
+ const location = canvas.getByRole('switch', { name: 'Location Services' });
587
1184
 
588
1185
  await step(
589
1186
  'keyboard: the switch is reachable with Tab (WCAG 2.1.1 Keyboard)',
@@ -622,6 +1219,26 @@ export const KeyboardNavigation: Story = {
622
1219
  await expect(wifi).toHaveFocus();
623
1220
  await userEvent.tab();
624
1221
  await expect(bluetooth).not.toHaveFocus();
1222
+ // Tab lands on the row after the disabled one, which is the small
1223
+ // switch: size does not affect the tab order.
1224
+ await expect(location).toHaveFocus();
1225
+ },
1226
+ );
1227
+
1228
+ await step(
1229
+ 'keyboard: the small switch is operable with Space (WCAG 2.1.1)',
1230
+ async () => {
1231
+ await userEvent.keyboard(' ');
1232
+ await expect(location).toHaveAttribute('aria-checked', 'true');
1233
+ },
1234
+ );
1235
+
1236
+ await step(
1237
+ 'keyboard: the small switch shows the same visible focus indicator (WCAG 2.4.7)',
1238
+ async () => {
1239
+ const { boxShadow, outlineStyle } = getComputedStyle(location);
1240
+ await expect(outlineStyle).toBe('none');
1241
+ await expect(boxShadow).not.toBe('none');
625
1242
  },
626
1243
  );
627
1244
  },
@@ -653,11 +1270,12 @@ export const ScreenReader: Story = {
653
1270
  const airplane = canvas.getByRole('switch', { name: 'Airplane Mode' });
654
1271
  const wifi = canvas.getByRole('switch', { name: 'Wi-Fi' });
655
1272
  const bluetooth = canvas.getByRole('switch', { name: 'Bluetooth' });
1273
+ const location = canvas.getByRole('switch', { name: 'Location Services' });
656
1274
 
657
1275
  await step(
658
1276
  'screen-reader: every control exposes a switch role (WCAG 4.1.2)',
659
1277
  async () => {
660
- await expect(canvas.getAllByRole('switch')).toHaveLength(3);
1278
+ await expect(canvas.getAllByRole('switch')).toHaveLength(4);
661
1279
  await expect(airplane).toHaveAttribute('role', 'switch');
662
1280
  },
663
1281
  );
@@ -668,6 +1286,18 @@ export const ScreenReader: Story = {
668
1286
  await expect(airplane).toHaveAccessibleName('Airplane Mode');
669
1287
  await expect(wifi).toHaveAccessibleName('Wi-Fi');
670
1288
  await expect(bluetooth).toHaveAccessibleName('Bluetooth');
1289
+ await expect(location).toHaveAccessibleName('Location Services');
1290
+ },
1291
+ );
1292
+
1293
+ await step(
1294
+ 'screen-reader: the small size is announced identically to the default (WCAG 4.1.2)',
1295
+ async () => {
1296
+ // Size is visual only: the smaller track exposes the same role, name,
1297
+ // and state, so nothing about the announcement changes.
1298
+ await expect(location).toHaveAttribute('role', 'switch');
1299
+ await expect(location).toHaveAttribute('aria-checked', 'false');
1300
+ await expect(location).toBeEnabled();
671
1301
  },
672
1302
  );
673
1303
 
@@ -817,5 +1447,26 @@ export const MobileBreakpoint: Story = {
817
1447
  await expect(onUpdate).toHaveBeenLastCalledWith(false);
818
1448
  },
819
1449
  );
1450
+
1451
+ await step(
1452
+ 'mobile: the small switch relies on its row for the touch target (WCAG 2.5.8)',
1453
+ async () => {
1454
+ // The small track is 28×16 — further under the 24px minimum than the
1455
+ // default — so on touch screens the row has to carry the target.
1456
+ const location = canvas.getByRole('switch', {
1457
+ name: 'Location Services',
1458
+ });
1459
+ await expect(location.getBoundingClientRect().height).toBeLessThan(24);
1460
+
1461
+ const row = location.closest('label') as HTMLElement;
1462
+ const { width, height } = row.getBoundingClientRect();
1463
+ await expect(width).toBeGreaterThanOrEqual(24);
1464
+ await expect(height).toBeGreaterThanOrEqual(24);
1465
+
1466
+ await userEvent.click(canvas.getByText('Location Services'));
1467
+ await expect(location).toHaveAttribute('aria-checked', 'true');
1468
+ await expect(onUpdate).toHaveBeenLastCalledWith(true);
1469
+ },
1470
+ );
820
1471
  },
821
1472
  };