@aleph-alpha/ui-library 7.0.2 → 7.1.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.
@@ -9,6 +9,7 @@ import {
9
9
  } from 'storybook/test';
10
10
  import { ref, watch } from 'vue';
11
11
  import { UiSlider } from '.';
12
+ import type { UiSliderProps } from './types';
12
13
  import {
13
14
  accessibilityRequirementsTemplate,
14
15
  designNotesStory,
@@ -60,6 +61,21 @@ const meta: Meta<typeof UiSlider> = {
60
61
  description:
61
62
  'Whether the slider direction is visually inverted (low/high swapped).',
62
63
  },
64
+ showValue: {
65
+ control: 'boolean',
66
+ description:
67
+ "Shows each handle's value above it (beside it when vertical), moving with the handle.",
68
+ },
69
+ showSteps: {
70
+ control: 'boolean',
71
+ description:
72
+ 'Shows a dot on the track for every value the handle can reach.',
73
+ },
74
+ stepsInterval: {
75
+ control: { type: 'number' },
76
+ description:
77
+ "Gap between two dots, in the slider's own units. Defaults to every step, but no closer than a tenth of the range. A gap finer than a hundredth of the range falls back to that default.",
78
+ },
63
79
  },
64
80
  args: {
65
81
  modelValue: [50],
@@ -69,6 +85,8 @@ const meta: Meta<typeof UiSlider> = {
69
85
  step: 1,
70
86
  orientation: 'horizontal',
71
87
  inverted: false,
88
+ showValue: false,
89
+ showSteps: false,
72
90
  },
73
91
  parameters: {
74
92
  docs: { links: guidanceLinks(guidance) },
@@ -127,6 +145,9 @@ export const Default: Story = {
127
145
  :step="args.step"
128
146
  :orientation="args.orientation"
129
147
  :inverted="args.inverted"
148
+ :show-value="args.showValue"
149
+ :show-steps="args.showSteps"
150
+ :steps-interval="args.stepsInterval"
130
151
  />
131
152
  <div class="body-s-regular text-content-on-surface-muted my-4">Value: {{ value }}</div>
132
153
  </div>
@@ -222,6 +243,13 @@ export const Accessibility: Story = {
222
243
  translated — the same number assistive technology gets from
223
244
  <code>aria-valuenow</code>.
224
245
  </p>
246
+ <p class="body-m-regular text-content-on-surface-primary">
247
+ <code>showValue</code> does this above the handle. The text is
248
+ hidden from assistive technology, which reads the value from the
249
+ handle instead; with <code>formatValue</code> the handle announces
250
+ the same text, such as <code>70%</code>, as its
251
+ <code>aria-valuetext</code>.
252
+ </p>
225
253
  </div>
226
254
 
227
255
  <div class="flex flex-col gap-2">
@@ -497,6 +525,501 @@ export const Disabled: Story = {
497
525
  },
498
526
  };
499
527
 
528
+ const STEP_MARK = '[data-slot="slider-step-mark"]';
529
+ const SHOWN_VALUE = '[data-slot="slider-value"]';
530
+ // The vendor positions the handle with calc(); sub-pixel rounding stays
531
+ // within this.
532
+ const ALIGNMENT_TOLERANCE_PX = 1;
533
+
534
+ /**
535
+ * The step dots a story rendered, in track order.
536
+ * @param root The story's root element.
537
+ * @returns Every step dot inside it.
538
+ */
539
+ function stepMarksIn(root: HTMLElement): HTMLElement[] {
540
+ return [...root.querySelectorAll<HTMLElement>(STEP_MARK)];
541
+ }
542
+
543
+ /**
544
+ * The value text shown on the first handle.
545
+ * @param root The story's root element.
546
+ * @returns The shown value element.
547
+ */
548
+ function shownValueIn(root: HTMLElement): HTMLElement {
549
+ const shownValue = root.querySelector<HTMLElement>(SHOWN_VALUE);
550
+ if (!shownValue) throw new Error('No shown value: is showValue on?');
551
+ return shownValue;
552
+ }
553
+
554
+ /**
555
+ * Centre of an element on screen.
556
+ * @param element Any rendered element.
557
+ * @returns Its centre in CSS pixels.
558
+ */
559
+ function centreOf(element: Element): { x: number; y: number } {
560
+ const { left, top, width, height } = element.getBoundingClientRect();
561
+ return { x: left + width / 2, y: top + height / 2 };
562
+ }
563
+
564
+ /**
565
+ * Waits until `element` is centred on the handle along one axis. Awaited
566
+ * because reka-ui pulls the handle in from the ends only once it has measured
567
+ * it, a moment after the first render.
568
+ * @param handle A `role="slider"` handle.
569
+ * @param element The dot or shown value that should line up with it.
570
+ * @param axis `x` along a horizontal slider, `y` along a vertical one.
571
+ */
572
+ async function expectCentredOnHandle(
573
+ handle: HTMLElement,
574
+ element: Element,
575
+ axis: 'x' | 'y' = 'x',
576
+ ) {
577
+ await waitFor(async () => {
578
+ await expect(
579
+ Math.abs(centreOf(element)[axis] - centreOf(handle)[axis]),
580
+ ).toBeLessThanOrEqual(ALIGNMENT_TOLERANCE_PX);
581
+ });
582
+ }
583
+
584
+ const withValueTemplateSource = `<script setup lang="ts">
585
+ import { ref } from 'vue'
586
+ import { UiSlider } from '@aleph-alpha/ui-library'
587
+
588
+ const value = ref([70])
589
+ </script>
590
+
591
+ <template>
592
+ <div class="w-72">
593
+ <UiSlider v-model="value" show-value :format-value="(value) => \`\${value}%\`" />
594
+ </div>
595
+ </template>`;
596
+
597
+ /**
598
+ * Shows the current value above the handle, moving with it while dragging.
599
+ * `formatValue` decides how it is written, and screen readers hear the same
600
+ * text; without it the plain number shows.
601
+ */
602
+ export const WithValue: Story = {
603
+ render: () => ({
604
+ components: { UiSlider },
605
+ setup() {
606
+ const value = ref([70]);
607
+ const formatValue = (value: number) => `${value}%`;
608
+ return { value, formatValue };
609
+ },
610
+ template: `
611
+ <div class="w-72">
612
+ <UiSlider v-model="value" show-value :format-value="formatValue" />
613
+ </div>
614
+ `,
615
+ }),
616
+ parameters: {
617
+ controls: { disable: true },
618
+ docs: { source: { code: withValueTemplateSource } },
619
+ },
620
+ play: async ({ canvasElement, step }) => {
621
+ // Scenario (UiSlider): "70%" reads right above the handle, and screen
622
+ // readers hear "70%" from the handle. Non-mutating.
623
+ const canvas = within(canvasElement);
624
+ const handle = canvas.getByRole('slider', { name: 'Value' });
625
+ const shownValue = shownValueIn(canvasElement);
626
+
627
+ await step(
628
+ 'interaction: the value sits above the handle, centred on it',
629
+ async () => {
630
+ await expect(shownValue).toHaveTextContent('70%');
631
+ await expectCentredOnHandle(handle, shownValue);
632
+ await expect(
633
+ shownValue.getBoundingClientRect().bottom,
634
+ ).toBeLessThanOrEqual(handle.getBoundingClientRect().top);
635
+ },
636
+ );
637
+
638
+ await step(
639
+ 'screen-reader: the handle announces the formatted value, the shown text is hidden (WCAG 4.1.2)',
640
+ async () => {
641
+ await expect(shownValue).toHaveAttribute('aria-hidden', 'true');
642
+ await expect(handle).toHaveAccessibleName('Value');
643
+ await expect(handle).toHaveAttribute('aria-valuenow', '70');
644
+ await expect(handle).toHaveAttribute('aria-valuetext', '70%');
645
+ },
646
+ );
647
+ },
648
+ };
649
+
650
+ const withStepsTemplateSource = `<script setup lang="ts">
651
+ import { ref } from 'vue'
652
+ import { UiSlider } from '@aleph-alpha/ui-library'
653
+
654
+ const value = ref([50])
655
+ </script>
656
+
657
+ <template>
658
+ <div class="w-72">
659
+ <UiSlider v-model="value" :step="10" show-steps />
660
+ </div>
661
+ </template>`;
662
+
663
+ /**
664
+ * Shows a dot for every value the handle can reach. A slider with many steps
665
+ * gets a dot every tenth of its range unless `stepsInterval` says otherwise.
666
+ */
667
+ export const WithSteps: Story = {
668
+ render: () => ({
669
+ components: { UiSlider },
670
+ setup() {
671
+ const value = ref([50]);
672
+ return { value };
673
+ },
674
+ template: `
675
+ <div class="w-72">
676
+ <UiSlider v-model="value" :step="10" show-steps />
677
+ </div>
678
+ `,
679
+ }),
680
+ parameters: {
681
+ controls: { disable: true },
682
+ docs: { source: { code: withStepsTemplateSource } },
683
+ },
684
+ play: async ({ canvasElement, step }) => {
685
+ // Scenario (UiSlider): one dot per reachable value, decorative only, and the
686
+ // handle sits on the dot of its value. Non-mutating.
687
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
688
+ const marks = stepMarksIn(canvasElement);
689
+
690
+ await step(
691
+ 'interaction: draws one dot per reachable value, with the handle on the dot of its value',
692
+ async () => {
693
+ await expect(marks).toHaveLength(11);
694
+ await expectCentredOnHandle(handle, marks[5]);
695
+ },
696
+ );
697
+
698
+ await step(
699
+ 'screen-reader: the dots are hidden from assistive technology',
700
+ async () => {
701
+ await expect(marks[0].closest('[aria-hidden="true"]')).not.toBeNull();
702
+ },
703
+ );
704
+ },
705
+ };
706
+
707
+ const stepsIntervalTemplateSource = `<script setup lang="ts">
708
+ import { ref } from 'vue'
709
+ import { UiSlider } from '@aleph-alpha/ui-library'
710
+
711
+ const value = ref([50])
712
+ </script>
713
+
714
+ <template>
715
+ <div class="w-72">
716
+ <UiSlider v-model="value" :step="1" show-steps :steps-interval="10" />
717
+ </div>
718
+ </template>`;
719
+
720
+ /**
721
+ * A slider that moves in steps of 1 but shows a dot only every 10, so a fine
722
+ * range still reads at a glance.
723
+ */
724
+ export const StepsInterval: Story = {
725
+ render: () => ({
726
+ components: { UiSlider },
727
+ setup() {
728
+ const value = ref([50]);
729
+ return { value };
730
+ },
731
+ template: `
732
+ <div class="w-72">
733
+ <UiSlider v-model="value" :step="1" show-steps :steps-interval="10" />
734
+ </div>
735
+ `,
736
+ }),
737
+ parameters: {
738
+ controls: { disable: true },
739
+ docs: { source: { code: stepsIntervalTemplateSource } },
740
+ },
741
+ play: async ({ canvasElement, step }) => {
742
+ // Scenario (UiSlider): a 0 to 100 slider in steps of 1 draws 11 dots, and
743
+ // the handle at 50 sits on the middle one. Non-mutating.
744
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
745
+ const marks = stepMarksIn(canvasElement);
746
+
747
+ await step(
748
+ 'interaction: draws a dot every 10, with the handle at 50 on the middle one',
749
+ async () => {
750
+ await expect(marks).toHaveLength(11);
751
+ await expectCentredOnHandle(handle, marks[5]);
752
+ },
753
+ );
754
+ },
755
+ };
756
+
757
+ const stepsRightToLeftTemplateSource = `<script setup lang="ts">
758
+ import { ref } from 'vue'
759
+ import { UiSlider } from '@aleph-alpha/ui-library'
760
+
761
+ const value = ref([60])
762
+ </script>
763
+
764
+ <template>
765
+ <div class="w-72">
766
+ <UiSlider v-model="value" :step="30" show-steps dir="rtl" />
767
+ </div>
768
+ </template>`;
769
+
770
+ const stepsInvertedTemplateSource = `<script setup lang="ts">
771
+ import { ref } from 'vue'
772
+ import { UiSlider } from '@aleph-alpha/ui-library'
773
+
774
+ const value = ref([60])
775
+ </script>
776
+
777
+ <template>
778
+ <div class="w-72">
779
+ <UiSlider v-model="value" :step="30" show-steps inverted />
780
+ </div>
781
+ </template>`;
782
+
783
+ const stepsOverflowAlignmentTemplateSource = `<script setup lang="ts">
784
+ import { ref } from 'vue'
785
+ import { UiSlider } from '@aleph-alpha/ui-library'
786
+
787
+ const value = ref([60])
788
+ </script>
789
+
790
+ <template>
791
+ <div class="w-72">
792
+ <UiSlider v-model="value" :step="30" show-steps thumb-alignment="overflow" />
793
+ </div>
794
+ </template>`;
795
+
796
+ /**
797
+ * Renders a slider with step dots at 60 on a 0 to 100 track in steps of 30, so
798
+ * the dots sit at 0, 30, 60, 90 and 100. The step does not divide the range,
799
+ * so dots drawn from the wrong edge cannot line up with the handle by chance.
800
+ * @param attributes Extra template attributes for `UiSlider`.
801
+ * @returns Vue component options for a story `render` function.
802
+ */
803
+ function renderStepsSlider(attributes: string) {
804
+ return {
805
+ components: { UiSlider },
806
+ setup() {
807
+ const value = ref([60]);
808
+ return { value };
809
+ },
810
+ template: `
811
+ <div class="w-72">
812
+ <UiSlider v-model="value" :step="30" show-steps ${attributes} />
813
+ </div>
814
+ `,
815
+ };
816
+ }
817
+
818
+ /**
819
+ * The shared check for the direction stories: the handle at 60 sits on the
820
+ * third dot.
821
+ * @param context Storybook play context.
822
+ * @param context.canvasElement The story's root element.
823
+ * @param context.step Storybook's step reporter.
824
+ */
825
+ const expectHandleOnItsDot: Story['play'] = async ({ canvasElement, step }) => {
826
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
827
+ const marks = stepMarksIn(canvasElement);
828
+
829
+ await step(
830
+ 'interaction: the handle at 60 sits on the dot for 60, the third from the start',
831
+ async () => {
832
+ await expect(marks).toHaveLength(5);
833
+ await expectCentredOnHandle(handle, marks[2]);
834
+ },
835
+ );
836
+ };
837
+
838
+ /**
839
+ * Step dots on a right-to-left slider start from the right edge, like the
840
+ * track itself.
841
+ */
842
+ export const StepsRightToLeft: Story = {
843
+ render: () => renderStepsSlider('dir="rtl"'),
844
+ parameters: {
845
+ controls: { disable: true },
846
+ docs: { source: { code: stepsRightToLeftTemplateSource } },
847
+ },
848
+ play: expectHandleOnItsDot,
849
+ };
850
+
851
+ /**
852
+ * Step dots on an inverted slider start from the right edge, like the track.
853
+ */
854
+ export const StepsInverted: Story = {
855
+ render: () => renderStepsSlider('inverted'),
856
+ parameters: {
857
+ controls: { disable: true },
858
+ docs: { source: { code: stepsInvertedTemplateSource } },
859
+ },
860
+ play: expectHandleOnItsDot,
861
+ };
862
+
863
+ /**
864
+ * With `thumbAlignment="overflow"` the handle reaches the very ends of the
865
+ * track, and the dots run to the ends with it.
866
+ */
867
+ export const StepsOverflowAlignment: Story = {
868
+ render: () => renderStepsSlider('thumb-alignment="overflow"'),
869
+ parameters: {
870
+ controls: { disable: true },
871
+ docs: {
872
+ source: { code: stepsOverflowAlignmentTemplateSource },
873
+ },
874
+ },
875
+ play: expectHandleOnItsDot,
876
+ };
877
+
878
+ const verticalWithValueAndStepsTemplateSource = `<script setup lang="ts">
879
+ import { ref } from 'vue'
880
+ import { UiSlider } from '@aleph-alpha/ui-library'
881
+
882
+ const value = ref([50])
883
+ </script>
884
+
885
+ <template>
886
+ <!-- A vertical slider reserves no room beside it for the value. -->
887
+ <div class="h-44 pr-12">
888
+ <UiSlider
889
+ v-model="value"
890
+ orientation="vertical"
891
+ :step="10"
892
+ show-value
893
+ show-steps
894
+ :format-value="(value) => \`\${value}%\`"
895
+ />
896
+ </div>
897
+ </template>`;
898
+
899
+ /**
900
+ * On a vertical slider the value sits beside the handle and the dots run from
901
+ * the bottom up. Leave room beside the slider for the value.
902
+ */
903
+ export const VerticalWithValueAndSteps: Story = {
904
+ render: () => ({
905
+ components: { UiSlider },
906
+ setup() {
907
+ const value = ref([50]);
908
+ const formatValue = (value: number) => `${value}%`;
909
+ return { value, formatValue };
910
+ },
911
+ template: `
912
+ <div class="h-44 pr-12">
913
+ <UiSlider
914
+ v-model="value"
915
+ orientation="vertical"
916
+ :step="10"
917
+ show-value
918
+ show-steps
919
+ :format-value="formatValue"
920
+ />
921
+ </div>
922
+ `,
923
+ }),
924
+ parameters: {
925
+ controls: { disable: true },
926
+ docs: { source: { code: verticalWithValueAndStepsTemplateSource } },
927
+ },
928
+ play: async ({ canvasElement, step }) => {
929
+ // Scenario (UiSlider): the handle at 50 sits on the middle dot, and "50%"
930
+ // reads beside it, level with it. Non-mutating.
931
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
932
+ const marks = stepMarksIn(canvasElement);
933
+ const shownValue = shownValueIn(canvasElement);
934
+
935
+ await step(
936
+ 'interaction: the handle sits on the dot of its value, counted from the bottom',
937
+ async () => {
938
+ await expect(marks).toHaveLength(11);
939
+ await expectCentredOnHandle(handle, marks[5], 'y');
940
+ await expect(centreOf(marks[0]).y).toBeGreaterThan(
941
+ centreOf(marks[10]).y,
942
+ );
943
+ },
944
+ );
945
+
946
+ await step(
947
+ 'interaction: the value sits beside the handle, level with it',
948
+ async () => {
949
+ await expect(shownValue).toHaveTextContent('50%');
950
+ await expectCentredOnHandle(handle, shownValue, 'y');
951
+ await expect(shownValue.getBoundingClientRect().left).toBeGreaterThan(
952
+ handle.getBoundingClientRect().right,
953
+ );
954
+ },
955
+ );
956
+ },
957
+ };
958
+
959
+ const rangeWithValueTemplateSource = `<script setup lang="ts">
960
+ import { ref } from 'vue'
961
+ import { UiSlider } from '@aleph-alpha/ui-library'
962
+
963
+ const range = ref([20, 80])
964
+ </script>
965
+
966
+ <template>
967
+ <div class="w-72">
968
+ <UiSlider v-model="range" show-value :format-value="(value) => \`\${value}%\`" />
969
+ </div>
970
+ </template>`;
971
+
972
+ /**
973
+ * A range shows one value above each handle.
974
+ */
975
+ export const RangeWithValue: Story = {
976
+ render: () => ({
977
+ components: { UiSlider },
978
+ setup() {
979
+ const range = ref([20, 80]);
980
+ const formatValue = (value: number) => `${value}%`;
981
+ return { range, formatValue };
982
+ },
983
+ template: `
984
+ <div class="w-72">
985
+ <UiSlider v-model="range" show-value :format-value="formatValue" />
986
+ </div>
987
+ `,
988
+ }),
989
+ parameters: {
990
+ controls: { disable: true },
991
+ docs: { source: { code: rangeWithValueTemplateSource } },
992
+ },
993
+ play: async ({ canvasElement, step }) => {
994
+ // Scenario (UiSlider): each handle carries its own value, centred above it.
995
+ // Non-mutating.
996
+ const canvas = within(canvasElement);
997
+ const handles = canvas.getAllByRole('slider');
998
+ const shownValues = [
999
+ ...canvasElement.querySelectorAll<HTMLElement>(SHOWN_VALUE),
1000
+ ];
1001
+
1002
+ await step(
1003
+ 'interaction: each handle shows its own value, centred above it',
1004
+ async () => {
1005
+ await expect(
1006
+ shownValues.map((value) => value.textContent?.trim()),
1007
+ ).toEqual(['20%', '80%']);
1008
+ await expectCentredOnHandle(handles[0], shownValues[0]);
1009
+ await expectCentredOnHandle(handles[1], shownValues[1]);
1010
+ },
1011
+ );
1012
+
1013
+ await step(
1014
+ 'screen-reader: each handle announces its own formatted value',
1015
+ async () => {
1016
+ await expect(handles[0]).toHaveAttribute('aria-valuetext', '20%');
1017
+ await expect(handles[1]).toHaveAttribute('aria-valuetext', '80%');
1018
+ },
1019
+ );
1020
+ },
1021
+ };
1022
+
500
1023
  /*
501
1024
  * ─────────────────────────────────────────────────────────────────────────────
502
1025
  * Interaction & accessibility tests (play functions)
@@ -526,9 +1049,13 @@ export const Disabled: Story = {
526
1049
  * feeds it straight back in, so every play also exercises the wrapper's
527
1050
  * controlled round-trip and its `valueCommit` re-emit.
528
1051
  * @param step Stepping interval for the slider under test.
1052
+ * @param extraProps Further `UiSlider` props, such as `showValue`.
529
1053
  * @returns Vue component options for a story `render` function.
530
1054
  */
531
- function renderControlledSlider(step = 5) {
1055
+ function renderControlledSlider(
1056
+ step = 5,
1057
+ extraProps: Partial<UiSliderProps> = {},
1058
+ ) {
532
1059
  return {
533
1060
  components: { UiSlider },
534
1061
  setup() {
@@ -537,7 +1064,7 @@ function renderControlledSlider(step = 5) {
537
1064
  value.value = next;
538
1065
  onUpdateModelValue(next);
539
1066
  };
540
- return { value, update, step, onValueCommit };
1067
+ return { value, update, step, extraProps, onValueCommit };
541
1068
  },
542
1069
  template: `
543
1070
  <div class="w-full max-w-72">
@@ -548,6 +1075,7 @@ function renderControlledSlider(step = 5) {
548
1075
  :min="0"
549
1076
  :max="100"
550
1077
  :step="step"
1078
+ v-bind="extraProps"
551
1079
  @update:model-value="update"
552
1080
  @value-commit="onValueCommit"
553
1081
  />
@@ -967,3 +1495,183 @@ export const MobileBreakpoint: Story = {
967
1495
  );
968
1496
  },
969
1497
  };
1498
+
1499
+ // The value-and-steps slider the test-only stories below drive.
1500
+ const valueAndStepsProps: Partial<UiSliderProps> = {
1501
+ showValue: true,
1502
+ showSteps: true,
1503
+ formatValue: (value: number) => `${value}%`,
1504
+ };
1505
+
1506
+ /**
1507
+ * The shown value follows the handle as it moves, and the handle lands on a
1508
+ * dot at every step, including both ends. Hidden test-only story.
1509
+ */
1510
+ export const ValueAndStepsKeyboard: Story = {
1511
+ name: 'test - Value And Steps Keyboard',
1512
+ tags: ['!dev', '!autodocs'],
1513
+ parameters: {
1514
+ // Focus moves and the value changes during the play function.
1515
+ chromatic: { disableSnapshot: true },
1516
+ },
1517
+ render: () => renderControlledSlider(10, valueAndStepsProps),
1518
+ play: async ({ canvasElement, step }) => {
1519
+ // Scenario (UiSlider): a keyboard user moves the handle step by step and
1520
+ // to both ends; the shown value, its dot and what is announced follow.
1521
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
1522
+ const marks = stepMarksIn(canvasElement);
1523
+
1524
+ await step(
1525
+ 'keyboard: the handle is reachable via Tab (WCAG 2.1.1)',
1526
+ async () => {
1527
+ await userEvent.tab();
1528
+ await expect(handle).toHaveFocus();
1529
+ },
1530
+ );
1531
+
1532
+ await step(
1533
+ 'keyboard: an arrow key moves one step and the shown value follows',
1534
+ async () => {
1535
+ await userEvent.keyboard('{ArrowRight}');
1536
+ await waitFor(async () => {
1537
+ await expect(shownValueIn(canvasElement)).toHaveTextContent('60%');
1538
+ });
1539
+ await expectCentredOnHandle(handle, shownValueIn(canvasElement));
1540
+ },
1541
+ );
1542
+
1543
+ await step(
1544
+ 'interaction: at both ends the handle sits on the first and last dot',
1545
+ async () => {
1546
+ await userEvent.keyboard('{End}');
1547
+ await waitFor(async () => {
1548
+ await expect(shownValueIn(canvasElement)).toHaveTextContent('100%');
1549
+ });
1550
+ await expectCentredOnHandle(handle, marks[marks.length - 1]);
1551
+
1552
+ await userEvent.keyboard('{Home}');
1553
+ await waitFor(async () => {
1554
+ await expect(shownValueIn(canvasElement)).toHaveTextContent('0%');
1555
+ });
1556
+ await expectCentredOnHandle(handle, marks[0]);
1557
+ },
1558
+ );
1559
+
1560
+ await step(
1561
+ 'screen-reader: the handle announces each new value in the shown format',
1562
+ async () => {
1563
+ await expect(handle).toHaveAttribute('aria-valuenow', '0');
1564
+ await expect(handle).toHaveAttribute('aria-valuetext', '0%');
1565
+ await expect(shownValueIn(canvasElement)).toHaveAttribute(
1566
+ 'aria-hidden',
1567
+ 'true',
1568
+ );
1569
+ },
1570
+ );
1571
+ },
1572
+ };
1573
+
1574
+ /**
1575
+ * The shown value stays readable, on screen and with its handle at 200% page
1576
+ * zoom (WCAG 1.4.4). Hidden test-only story.
1577
+ */
1578
+ export const ValueZoomMagnification: Story = {
1579
+ name: 'test - Value Zoom Magnification',
1580
+ tags: ['!dev', '!autodocs'],
1581
+ globals: { zoom: '200' },
1582
+ parameters: {
1583
+ // Zoom changes the layout scale; the accessible-reflow Chromatic mode
1584
+ // already covers magnified snapshots.
1585
+ chromatic: { disableSnapshot: true },
1586
+ },
1587
+ render: () => renderControlledSlider(10, valueAndStepsProps),
1588
+ play: async ({ canvasElement, step }) => {
1589
+ // Scenario (UiSlider): magnified, the value above the handle is still
1590
+ // visible inside the viewport and moves with the handle.
1591
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
1592
+
1593
+ await step(
1594
+ 'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
1595
+ async () => {
1596
+ await expect(document.documentElement.style.zoom).toBe('200%');
1597
+ },
1598
+ );
1599
+
1600
+ await step('zoom: the shown value stays visible on screen', async () => {
1601
+ const shownValue = shownValueIn(canvasElement);
1602
+ await expect(shownValue).toBeVisible();
1603
+ const { left, right, top } = shownValue.getBoundingClientRect();
1604
+ await expect(left).toBeGreaterThanOrEqual(0);
1605
+ await expect(right).toBeLessThanOrEqual(window.innerWidth);
1606
+ await expect(top).toBeGreaterThanOrEqual(0);
1607
+ });
1608
+
1609
+ await step(
1610
+ 'zoom: the shown value moves with the handle when magnified',
1611
+ async () => {
1612
+ await userEvent.tab();
1613
+ await expect(handle).toHaveFocus();
1614
+ await userEvent.keyboard('{ArrowRight}');
1615
+ await waitFor(async () => {
1616
+ await expect(shownValueIn(canvasElement)).toHaveTextContent('60%');
1617
+ });
1618
+ await expectCentredOnHandle(handle, shownValueIn(canvasElement));
1619
+ },
1620
+ );
1621
+ },
1622
+ };
1623
+
1624
+ /**
1625
+ * At the 320px breakpoint the shown value stays inside the viewport at both
1626
+ * ends of the range, never rises above the slider, and never blocks a tap on
1627
+ * the handle. Hidden test-only story.
1628
+ */
1629
+ export const ValueMobileBreakpoint: Story = {
1630
+ name: 'test - Value Mobile Breakpoint',
1631
+ tags: ['!dev', '!autodocs'],
1632
+ globals: { mediaBreakpoint: 'reflow' },
1633
+ parameters: {
1634
+ // Reflow snapshots are already covered by the accessible-reflow mode.
1635
+ chromatic: { disableSnapshot: true },
1636
+ },
1637
+ render: () => renderControlledSlider(10, valueAndStepsProps),
1638
+ play: async ({ canvasElement, step }) => {
1639
+ // Scenario (UiSlider): on a phone the value never pokes out of the screen
1640
+ // or over the content above the slider, whichever end the handle is at.
1641
+ const handle = within(canvasElement).getByRole('slider', { name: 'Value' });
1642
+ const slider = canvasElement.querySelector('[data-slot="slider"]');
1643
+ if (!slider) throw new Error('No slider in the story');
1644
+
1645
+ await step(
1646
+ 'mobile: the shown value stays inside the 320px viewport and below the top of the slider at both ends (WCAG 1.4.10 Reflow)',
1647
+ async () => {
1648
+ await userEvent.tab();
1649
+ await expect(handle).toHaveFocus();
1650
+
1651
+ for (const [key, text] of [
1652
+ ['{End}', '100%'],
1653
+ ['{Home}', '0%'],
1654
+ ]) {
1655
+ await userEvent.keyboard(key);
1656
+ await waitFor(async () => {
1657
+ await expect(shownValueIn(canvasElement)).toHaveTextContent(text);
1658
+ });
1659
+ const labelBox = shownValueIn(canvasElement).getBoundingClientRect();
1660
+ await expect(labelBox.left).toBeGreaterThanOrEqual(0);
1661
+ await expect(labelBox.right).toBeLessThanOrEqual(window.innerWidth);
1662
+ await expect(labelBox.top).toBeGreaterThanOrEqual(
1663
+ slider.getBoundingClientRect().top,
1664
+ );
1665
+ }
1666
+ },
1667
+ );
1668
+
1669
+ await step(
1670
+ 'mobile: nothing covers the handle, so a tap on it still reaches it (WCAG 2.5.8)',
1671
+ async () => {
1672
+ const { x, y } = centreOf(handle);
1673
+ await expect(document.elementFromPoint(x, y)).toBe(handle);
1674
+ },
1675
+ );
1676
+ },
1677
+ };