@aleph-alpha/ui-library 7.0.1 → 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.
Files changed (124) hide show
  1. package/dist/system/components/UiSlider/step-marks.d.ts +45 -0
  2. package/dist/system/components/UiSlider/types.d.ts +28 -0
  3. package/dist/system/components/core/slider/Slider.vue.d.ts +21 -1
  4. package/dist/system/icons/story-sidebar-icons.json +74 -17
  5. package/dist/system/lib.js +2026 -1912
  6. package/dist/system/patterns/UiPlaceholder/types.d.ts +1 -0
  7. package/dist/system/story-sidebar-icons.json +74 -17
  8. package/package.json +15 -10
  9. package/src/components/UiAccordion/UiAccordion.guidance.md +66 -0
  10. package/src/components/UiAccordion/UiAccordion.stories.ts +131 -139
  11. package/src/components/UiAlert/UiAlert.guidance.md +66 -0
  12. package/src/components/UiAlert/UiAlert.stories.ts +167 -143
  13. package/src/components/UiAlertDialog/UiAlertDialog.guidance.md +66 -0
  14. package/src/components/UiAlertDialog/UiAlertDialog.stories.ts +136 -112
  15. package/src/components/UiAutocomplete/UiAutocomplete.guidance.md +66 -0
  16. package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +123 -99
  17. package/src/components/UiAvatar/UiAvatar.guidance.md +63 -0
  18. package/src/components/UiAvatar/UiAvatar.stories.ts +340 -0
  19. package/src/components/UiAvatar/UiAvatarImage.vue +5 -1
  20. package/src/components/UiAvatar/__tests__/UiAvatar.test.ts +14 -0
  21. package/src/components/UiBadge/UiBadge.guidance.md +65 -0
  22. package/src/components/UiBadge/UiBadge.stories.ts +325 -0
  23. package/src/components/UiBreadcrumb/UiBreadcrumb.guidance.md +67 -0
  24. package/src/components/UiBreadcrumb/UiBreadcrumb.stories.ts +165 -141
  25. package/src/components/UiButton/UiButton.stories.ts +5 -2
  26. package/src/components/UiButtonGroup/UiButtonGroup.guidance.md +63 -0
  27. package/src/components/UiButtonGroup/UiButtonGroup.stories.ts +91 -67
  28. package/src/components/UiCalendar/UiCalendar.guidance.md +65 -0
  29. package/src/components/UiCalendar/UiCalendar.stories.ts +103 -110
  30. package/src/components/UiCard/UiCard.guidance.md +65 -0
  31. package/src/components/UiCard/UiCard.stories.ts +393 -0
  32. package/src/components/UiCheckbox/UiCheckbox.guidance.md +62 -0
  33. package/src/components/UiCheckbox/UiCheckbox.stories.ts +18 -56
  34. package/src/components/UiChip/UiChip.guidance.md +64 -0
  35. package/src/components/UiChip/UiChip.stories.ts +162 -138
  36. package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +291 -0
  37. package/src/components/UiCodeBlock/UiCodeBlock.vue +2 -1
  38. package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +18 -0
  39. package/src/components/UiDialog/UiDialog.guidance.md +66 -0
  40. package/src/components/UiDialog/UiDialog.stories.ts +90 -66
  41. package/src/components/UiDrawer/UiDrawer.guidance.md +66 -0
  42. package/src/components/UiDrawer/UiDrawer.stories.ts +186 -162
  43. package/src/components/UiDropdownMenu/UiDropdownMenu.guidance.md +68 -0
  44. package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +127 -86
  45. package/src/components/UiEmpty/UiEmpty.guidance.md +66 -0
  46. package/src/components/UiEmpty/UiEmpty.stories.ts +370 -15
  47. package/src/components/UiFadeMask/UiFadeMask.stories.ts +66 -0
  48. package/src/components/UiField/UiField.guidance.md +63 -0
  49. package/src/components/UiField/UiField.stories.ts +90 -113
  50. package/src/components/UiFileType/UiFileType.stories.ts +254 -0
  51. package/src/components/UiFileUploadBox/UiFileUploadBox.stories.ts +74 -72
  52. package/src/components/UiIcon/UiIcon.stories.ts +256 -1
  53. package/src/components/UiInput/UiInput.guidance.md +64 -0
  54. package/src/components/UiInput/UiInput.stories.ts +132 -80
  55. package/src/components/UiInputGroup/UiInputGroup.guidance.md +66 -0
  56. package/src/components/UiInputGroup/UiInputGroup.stories.ts +127 -103
  57. package/src/components/UiItem/UiItem.guidance.md +66 -0
  58. package/src/components/UiItem/UiItem.stories.ts +81 -28
  59. package/src/components/UiKbd/UiKbd.guidance.md +65 -0
  60. package/src/components/UiKbd/UiKbd.stories.ts +373 -3
  61. package/src/components/UiLabel/UiLabel.guidance.md +65 -0
  62. package/src/components/UiLabel/UiLabel.stories.ts +163 -139
  63. package/src/components/UiListbox/UiListbox.stories.ts +197 -195
  64. package/src/components/UiNavigationMenu/UiNavigationMenu.guidance.md +66 -0
  65. package/src/components/UiNavigationMenu/UiNavigationMenu.stories.ts +169 -169
  66. package/src/components/UiPopover/UiPopover.guidance.md +65 -0
  67. package/src/components/UiPopover/UiPopover.stories.ts +115 -91
  68. package/src/components/UiProgress/UiProgress.guidance.md +65 -0
  69. package/src/components/UiProgress/UiProgress.stories.ts +300 -0
  70. package/src/components/UiRadioGroup/UiRadioGroup.guidance.md +66 -0
  71. package/src/components/UiRadioGroup/UiRadioGroup.stories.ts +163 -139
  72. package/src/components/UiRangeCalendar/UiRangeCalendar.stories.ts +128 -127
  73. package/src/components/UiResizable/UiResizable.guidance.md +65 -0
  74. package/src/components/UiResizable/UiResizable.stories.ts +176 -152
  75. package/src/components/UiSelect/UiSelect.guidance.md +66 -0
  76. package/src/components/UiSelect/UiSelect.stories.ts +150 -126
  77. package/src/components/UiSeparator/UiSeparator.guidance.md +64 -0
  78. package/src/components/UiSeparator/UiSeparator.stories.ts +350 -0
  79. package/src/components/UiSheet/UiSheet.guidance.md +66 -0
  80. package/src/components/UiSheet/UiSheet.stories.ts +194 -170
  81. package/src/components/UiSkeleton/UiSkeleton.guidance.md +65 -0
  82. package/src/components/UiSkeleton/UiSkeleton.stories.ts +253 -0
  83. package/src/components/UiSlider/UiSlider.guidance.md +66 -0
  84. package/src/components/UiSlider/UiSlider.stories.ts +885 -153
  85. package/src/components/UiSlider/UiSlider.vue +109 -1
  86. package/src/components/UiSlider/__tests__/UiSliderValueAndSteps.test.ts +210 -0
  87. package/src/components/UiSlider/__tests__/step-marks.test.ts +94 -0
  88. package/src/components/UiSlider/step-marks.ts +96 -0
  89. package/src/components/UiSlider/types.ts +32 -0
  90. package/src/components/UiSnackbar/UiSnackbar.guidance.md +66 -0
  91. package/src/components/UiSnackbar/UiSnackbar.stories.ts +106 -96
  92. package/src/components/UiSpinner/UiSpinner.guidance.md +65 -0
  93. package/src/components/UiSpinner/UiSpinner.stories.ts +249 -0
  94. package/src/components/UiStepper/UiStepper.guidance.md +66 -0
  95. package/src/components/UiStepper/UiStepper.stories.ts +142 -124
  96. package/src/components/UiSwitch/UiSwitch.guidance.md +66 -0
  97. package/src/components/UiSwitch/UiSwitch.stories.ts +170 -146
  98. package/src/components/UiTable/UiTable.guidance.md +66 -0
  99. package/src/components/UiTable/UiTable.stories.ts +530 -12
  100. package/src/components/UiTabs/UiTabs.guidance.md +66 -0
  101. package/src/components/UiTabs/UiTabs.stories.ts +174 -150
  102. package/src/components/UiTagsInput/UiTagsInput.stories.ts +154 -152
  103. package/src/components/UiTextarea/UiTextarea.guidance.md +66 -0
  104. package/src/components/UiTextarea/UiTextarea.stories.ts +86 -62
  105. package/src/components/UiToggle/UiToggle.guidance.md +66 -0
  106. package/src/components/UiToggle/UiToggle.stories.ts +183 -159
  107. package/src/components/UiToggleGroup/UiToggleGroup.guidance.md +64 -0
  108. package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +222 -200
  109. package/src/components/UiTooltip/UiTooltip.guidance.md +66 -0
  110. package/src/components/UiTooltip/UiTooltip.stories.ts +90 -66
  111. package/src/components/core/slider/Slider.vue +16 -6
  112. package/src/patterns/UiDataTable/UiDataTable.guidance.md +66 -0
  113. package/src/patterns/UiDataTable/UiDataTable.stories.ts +177 -155
  114. package/src/patterns/UiDatePicker/UiDatePicker.guidance.md +66 -0
  115. package/src/patterns/UiDatePicker/UiDatePicker.stories.ts +136 -112
  116. package/src/patterns/UiFileUpload/UiFileItem.stories.ts +77 -75
  117. package/src/patterns/UiFileUpload/UiFileUpload.guidance.md +68 -0
  118. package/src/patterns/UiFileUpload/UiFileUpload.stories.ts +169 -145
  119. package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +103 -101
  120. package/src/patterns/UiLayout/UiLayoutContent.stories.ts +14 -13
  121. package/src/patterns/UiLayout/UiLayoutMobile.stories.ts +65 -65
  122. package/src/patterns/UiPlaceholder/types.ts +1 -0
  123. package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +86 -84
  124. package/src/patterns/UiTopBar/UiTopBar.stories.ts +84 -82
@@ -1,6 +1,15 @@
1
1
  <script setup lang="ts">
2
2
  import type { SliderRootEmits } from 'reka-ui';
3
+ import { useDirection } from 'reka-ui';
4
+ import { computed, toRef } from 'vue';
3
5
  import { Slider as ShadcnSlider } from '@/components/core/slider';
6
+ import { cn } from '@/lib/utils';
7
+ import {
8
+ stepMarkPercents,
9
+ stepsIntervalOrDefault,
10
+ trackStartEdge,
11
+ type TrackStartEdge,
12
+ } from './step-marks';
4
13
  import type { UiSliderProps, UiSliderEmits } from './types';
5
14
 
6
15
  defineOptions({
@@ -21,6 +30,81 @@ function handleModelValueUpdate(
21
30
  // reka-ui can emit undefined in some cases; keep our public API stable.
22
31
  emit('update:modelValue', []);
23
32
  }
33
+
34
+ const isVertical = computed(() => props.orientation === 'vertical');
35
+
36
+ // Room above the track for the shown value. The handle overhangs the thin
37
+ // track, so this is taller than the value's own line.
38
+ const rootClass = computed(() =>
39
+ cn(props.showValue && !isVertical.value && 'pt-6'),
40
+ );
41
+
42
+ function valueText(value: number): string {
43
+ return props.formatValue ? props.formatValue(value) : String(value);
44
+ }
45
+
46
+ // The same direction the vendor slider uses: the prop, else the app's
47
+ // ConfigProvider, else left to right.
48
+ const direction = useDirection(toRef(props, 'dir'));
49
+
50
+ const startEdge = computed(() =>
51
+ trackStartEdge(
52
+ props.orientation ?? 'horizontal',
53
+ direction.value,
54
+ props.inverted,
55
+ ),
56
+ );
57
+
58
+ // Defaults match the vendor slider's, so the marks line up with what it draws.
59
+ const markPercents = computed(() => {
60
+ const min = props.min ?? 0;
61
+ const max = props.max ?? 100;
62
+ const interval = stepsIntervalOrDefault(
63
+ min,
64
+ max,
65
+ props.step ?? 1,
66
+ props.stepsInterval,
67
+ );
68
+ return stepMarkPercents(min, max, interval);
69
+ });
70
+
71
+ // Lies over the track, centred on it the way the vendor centres the handle.
72
+ // In 'contain' mode the handle's centre stops half a handle short of each end,
73
+ // so the marks are inset by the same amount.
74
+ const marksLayerClass = computed(() =>
75
+ cn(
76
+ 'pointer-events-none absolute',
77
+ isVertical.value ? 'inset-y-0 w-1.5' : 'inset-x-0 h-1.5',
78
+ props.thumbAlignment !== 'overflow' &&
79
+ (isVertical.value
80
+ ? 'my-[calc(var(--slider-thumb-size)/2)]'
81
+ : 'mx-[calc(var(--slider-thumb-size)/2)]'),
82
+ ),
83
+ );
84
+
85
+ // Centres each mark on its position, the way the vendor centres the handle.
86
+ const markCentring: Record<TrackStartEdge, string> = {
87
+ left: 'top-1/2 -translate-x-1/2 -translate-y-1/2',
88
+ right: 'top-1/2 translate-x-1/2 -translate-y-1/2',
89
+ bottom: 'left-1/2 -translate-x-1/2 translate-y-1/2',
90
+ top: 'left-1/2 -translate-x-1/2 -translate-y-1/2',
91
+ };
92
+
93
+ const markClass = computed(() =>
94
+ cn(
95
+ 'bg-background-accent-soft absolute size-1 rounded-full',
96
+ markCentring[startEdge.value],
97
+ ),
98
+ );
99
+
100
+ const valueLabelClass = computed(() =>
101
+ cn(
102
+ 'label-xs-regular text-content-on-surface-primary pointer-events-none absolute whitespace-nowrap',
103
+ isVertical.value
104
+ ? 'top-1/2 left-full ml-2 -translate-y-1/2'
105
+ : 'bottom-full left-1/2 mb-1 -translate-x-1/2',
106
+ ),
107
+ );
24
108
  </script>
25
109
 
26
110
  <template>
@@ -38,7 +122,31 @@ function handleModelValueUpdate(
38
122
  :thumb-alignment="props.thumbAlignment"
39
123
  :name="props.name"
40
124
  :required="props.required"
125
+ :class="rootClass"
126
+ :thumb-value-text="props.formatValue"
41
127
  @update:model-value="handleModelValueUpdate"
42
128
  @value-commit="emit('valueCommit', $event)"
43
- />
129
+ >
130
+ <template v-if="props.showSteps" #track-overlay>
131
+ <div aria-hidden="true" :class="marksLayerClass">
132
+ <span
133
+ v-for="percent in markPercents"
134
+ :key="percent"
135
+ data-slot="slider-step-mark"
136
+ :class="markClass"
137
+ :style="{ [startEdge]: `${percent}%` }"
138
+ />
139
+ </div>
140
+ </template>
141
+
142
+ <template v-if="props.showValue" #thumb="{ value }">
143
+ <span
144
+ aria-hidden="true"
145
+ data-slot="slider-value"
146
+ :class="valueLabelClass"
147
+ >
148
+ {{ valueText(value) }}
149
+ </span>
150
+ </template>
151
+ </ShadcnSlider>
44
152
  </template>
@@ -0,0 +1,210 @@
1
+ import { render } from '@testing-library/vue';
2
+ import { ConfigProvider } from 'reka-ui';
3
+ import { describe, expect, test } from 'vitest';
4
+ import { h } from 'vue';
5
+ import { Slider as ShadcnSlider } from '@/components/core/slider';
6
+ import { UiSlider } from '../index';
7
+
8
+ // Mounted against the real vendor slider, unlike `UiSlider.test.ts`, which
9
+ // stubs it to exercise the payload guard.
10
+
11
+ const valueLabels = (container: Element) =>
12
+ [...container.querySelectorAll('[data-slot="slider-value"]')].map(
13
+ (label) => label.textContent,
14
+ );
15
+
16
+ const stepMarks = (container: Element) => [
17
+ ...container.querySelectorAll<HTMLElement>('[data-slot="slider-step-mark"]'),
18
+ ];
19
+
20
+ describe('UiSlider value and step marks', () => {
21
+ test('renders exactly what the base slider renders unless asked for more', () => {
22
+ const props = { modelValue: [25, 75], min: 0, max: 100, step: 10 };
23
+
24
+ const wrapped = render(UiSlider, { props }).container.innerHTML;
25
+ const base = render(ShadcnSlider, { props }).container.innerHTML;
26
+
27
+ expect(wrapped).toBe(base);
28
+ });
29
+
30
+ test('draws neither a value nor step marks unless asked to', () => {
31
+ const { container } = render(UiSlider, { props: { modelValue: [50] } });
32
+
33
+ expect(valueLabels(container)).toEqual([]);
34
+ expect(stepMarks(container)).toHaveLength(0);
35
+ });
36
+
37
+ test('shows the value above each handle', () => {
38
+ const { container } = render(UiSlider, {
39
+ props: { modelValue: [25, 75], showValue: true },
40
+ });
41
+
42
+ expect(valueLabels(container)).toEqual(['25', '75']);
43
+ });
44
+
45
+ test('writes the value the way formatValue says', () => {
46
+ const { container } = render(UiSlider, {
47
+ props: {
48
+ modelValue: [70],
49
+ showValue: true,
50
+ formatValue: (value: number) => `${value}%`,
51
+ },
52
+ });
53
+
54
+ expect(valueLabels(container)).toEqual(['70%']);
55
+ });
56
+
57
+ // That the handle still announces the value is checked by the stories: under
58
+ // jsdom the vendor renders the handle without its slider role and values.
59
+ test('keeps the shown value hidden from screen readers', () => {
60
+ const { container } = render(UiSlider, {
61
+ props: { modelValue: [70], showValue: true },
62
+ });
63
+
64
+ const label = container.querySelector('[data-slot="slider-value"]');
65
+ expect(label).toHaveAttribute('aria-hidden', 'true');
66
+ });
67
+
68
+ test('updates the shown value when the model changes', async () => {
69
+ const { container, rerender } = render(UiSlider, {
70
+ props: { modelValue: [10], showValue: true },
71
+ });
72
+
73
+ await rerender({ modelValue: [40], showValue: true });
74
+
75
+ expect(valueLabels(container)).toEqual(['40']);
76
+ });
77
+
78
+ test('draws one mark per reachable step, hidden from screen readers', () => {
79
+ const { container } = render(UiSlider, {
80
+ props: { modelValue: [50], min: 0, max: 100, step: 10, showSteps: true },
81
+ });
82
+
83
+ const marks = stepMarks(container);
84
+ expect(marks).toHaveLength(11);
85
+ expect(marks[0].closest('[aria-hidden="true"]')).not.toBeNull();
86
+ });
87
+
88
+ test('places the marks from the edge the track starts at', () => {
89
+ const { container } = render(UiSlider, {
90
+ props: {
91
+ modelValue: [50],
92
+ step: 50,
93
+ dir: 'rtl',
94
+ showSteps: true,
95
+ },
96
+ });
97
+
98
+ const marks = stepMarks(container);
99
+ expect(marks.map((mark) => mark.style.right)).toEqual([
100
+ '0%',
101
+ '50%',
102
+ '100%',
103
+ ]);
104
+ expect(marks.every((mark) => mark.style.left === '')).toBe(true);
105
+ });
106
+
107
+ test('mirrors the marks for an inverted slider when no direction is given', () => {
108
+ const { container } = render(UiSlider, {
109
+ props: { modelValue: [50], step: 50, inverted: true, showSteps: true },
110
+ });
111
+
112
+ const marks = stepMarks(container);
113
+ expect(marks.map((mark) => mark.style.right)).toEqual([
114
+ '0%',
115
+ '50%',
116
+ '100%',
117
+ ]);
118
+ expect(marks.every((mark) => mark.style.left === '')).toBe(true);
119
+ });
120
+
121
+ test('places the marks from the right when the app reads right to left', () => {
122
+ const { container } = render({
123
+ render: () =>
124
+ h(ConfigProvider, { dir: 'rtl' }, () =>
125
+ h(UiSlider, { modelValue: [50], step: 50, showSteps: true }),
126
+ ),
127
+ });
128
+
129
+ expect(stepMarks(container).map((mark) => mark.style.right)).toEqual([
130
+ '0%',
131
+ '50%',
132
+ '100%',
133
+ ]);
134
+ });
135
+
136
+ test('places the marks from the bottom of a vertical slider', () => {
137
+ const { container } = render(UiSlider, {
138
+ props: {
139
+ modelValue: [50],
140
+ step: 50,
141
+ orientation: 'vertical',
142
+ showSteps: true,
143
+ },
144
+ });
145
+
146
+ expect(stepMarks(container).map((mark) => mark.style.bottom)).toEqual([
147
+ '0%',
148
+ '50%',
149
+ '100%',
150
+ ]);
151
+ });
152
+
153
+ // The track clips what it holds, which would cut the end marks in half
154
+ // where the handle runs to the very ends.
155
+ test('draws the marks outside the track, so the end marks are not cut off', () => {
156
+ const { container } = render(UiSlider, {
157
+ props: { modelValue: [50], showSteps: true },
158
+ });
159
+
160
+ const track = container.querySelector('[data-slot="slider-track"]');
161
+ expect(stepMarks(container).length).toBeGreaterThan(0);
162
+ expect(stepMarks(container).some((mark) => track?.contains(mark))).toBe(
163
+ false,
164
+ );
165
+ });
166
+
167
+ test('draws the marks at their own interval when it differs from the step', () => {
168
+ const { container } = render(UiSlider, {
169
+ props: {
170
+ modelValue: [50],
171
+ step: 1,
172
+ stepsInterval: 10,
173
+ showSteps: true,
174
+ },
175
+ });
176
+
177
+ expect(stepMarks(container).map((mark) => mark.style.left)).toEqual(
178
+ Array.from({ length: 11 }, (_, index) => `${index * 10}%`),
179
+ );
180
+ });
181
+
182
+ test('spaces the marks of a fine slider out to a tenth of its range by default', () => {
183
+ const { container } = render(UiSlider, {
184
+ props: { modelValue: [50], step: 1, showSteps: true },
185
+ });
186
+
187
+ expect(stepMarks(container)).toHaveLength(11);
188
+ });
189
+
190
+ test('keeps an interval as fine as a hundredth of the range', () => {
191
+ const { container } = render(UiSlider, {
192
+ props: { modelValue: [50], step: 1, stepsInterval: 1, showSteps: true },
193
+ });
194
+
195
+ expect(stepMarks(container)).toHaveLength(101);
196
+ });
197
+
198
+ test('falls back to the default spacing for an interval finer than a hundredth of the range', () => {
199
+ const { container } = render(UiSlider, {
200
+ props: {
201
+ modelValue: [50],
202
+ step: 0.5,
203
+ stepsInterval: 0.5,
204
+ showSteps: true,
205
+ },
206
+ });
207
+
208
+ expect(stepMarks(container)).toHaveLength(11);
209
+ });
210
+ });
@@ -0,0 +1,94 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import {
3
+ defaultStepsInterval,
4
+ stepMarkPercents,
5
+ stepsIntervalOrDefault,
6
+ trackStartEdge,
7
+ } from '../step-marks';
8
+
9
+ describe('stepMarkPercents', () => {
10
+ test('places one mark per step, from the start to the end of the track', () => {
11
+ expect(stepMarkPercents(0, 100, 10)).toEqual([
12
+ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
13
+ ]);
14
+ });
15
+
16
+ test('ends with a mark at the maximum when the interval does not divide the range', () => {
17
+ // The handle reaches the maximum too: a step past 90 is clamped to it.
18
+ expect(stepMarkPercents(0, 100, 30)).toEqual([0, 30, 60, 90, 100]);
19
+ });
20
+
21
+ test('keeps the last mark at the end of the track for fractional steps', () => {
22
+ const percents = stepMarkPercents(0, 1, 0.1);
23
+
24
+ expect(percents).toHaveLength(11);
25
+ expect(percents.at(-1)).toBe(100);
26
+ });
27
+
28
+ test('measures from min when the range does not start at zero', () => {
29
+ expect(stepMarkPercents(20, 60, 10)).toEqual([0, 25, 50, 75, 100]);
30
+ });
31
+
32
+ test('returns no marks for an interval or range that cannot be drawn', () => {
33
+ expect(stepMarkPercents(0, 100, 0)).toEqual([]);
34
+ expect(stepMarkPercents(0, 100, -5)).toEqual([]);
35
+ expect(stepMarkPercents(50, 50, 1)).toEqual([]);
36
+ expect(stepMarkPercents(100, 0, 1)).toEqual([]);
37
+ });
38
+ });
39
+
40
+ describe('defaultStepsInterval', () => {
41
+ test('keeps the step when it already gives at most a dot every tenth of the range', () => {
42
+ expect(defaultStepsInterval(0, 100, 10)).toBe(10);
43
+ expect(defaultStepsInterval(0, 100, 30)).toBe(30);
44
+ });
45
+
46
+ test('spaces a fine slider out to a tenth of its range', () => {
47
+ expect(defaultStepsInterval(0, 100, 1)).toBe(10);
48
+ expect(defaultStepsInterval(0, 1, 0.01)).toBe(0.1);
49
+ });
50
+
51
+ test('stays on values the handle can reach when a tenth is not a whole number of steps', () => {
52
+ expect(defaultStepsInterval(0, 100, 3)).toBe(12);
53
+ });
54
+ });
55
+
56
+ describe('stepsIntervalOrDefault', () => {
57
+ test('uses the requested gap down to a hundredth of the range', () => {
58
+ expect(stepsIntervalOrDefault(0, 100, 1, 5)).toBe(5);
59
+ expect(stepsIntervalOrDefault(0, 100, 1, 1)).toBe(1);
60
+ expect(stepsIntervalOrDefault(0, 1, 0.01, 0.01)).toBe(0.01);
61
+ });
62
+
63
+ test('uses the default gap when none is requested or it is finer than a hundredth', () => {
64
+ expect(stepsIntervalOrDefault(0, 100, 1)).toBe(10);
65
+ expect(stepsIntervalOrDefault(0, 100, 1, 0.001)).toBe(10);
66
+ expect(stepsIntervalOrDefault(0, 100, 1, 0)).toBe(10);
67
+ });
68
+
69
+ test('keeps a gap of exactly a hundredth when floating-point rounding makes the range a hair larger', () => {
70
+ // 0.4 - 0.1 is 0.30000000000000004 in floating point.
71
+ expect(stepsIntervalOrDefault(0.1, 0.4, 0.003, 0.003)).toBe(0.003);
72
+ });
73
+
74
+ test('holds the hundredth limit on a very small range too', () => {
75
+ expect(stepsIntervalOrDefault(0, 1e-8, 1e-9, 1e-15)).toBe(1e-9);
76
+ });
77
+ });
78
+
79
+ describe('trackStartEdge', () => {
80
+ test('a horizontal track starts on the left, or the right when it reads right to left', () => {
81
+ expect(trackStartEdge('horizontal', 'ltr', false)).toBe('left');
82
+ expect(trackStartEdge('horizontal', 'rtl', false)).toBe('right');
83
+ });
84
+
85
+ test('inverting a horizontal track swaps its start edge', () => {
86
+ expect(trackStartEdge('horizontal', 'ltr', true)).toBe('right');
87
+ expect(trackStartEdge('horizontal', 'rtl', true)).toBe('left');
88
+ });
89
+
90
+ test('a vertical track starts at the bottom, or the top when inverted', () => {
91
+ expect(trackStartEdge('vertical', 'ltr', false)).toBe('bottom');
92
+ expect(trackStartEdge('vertical', 'ltr', true)).toBe('top');
93
+ });
94
+ });
@@ -0,0 +1,96 @@
1
+ import type { UiSliderProps } from './types';
2
+
3
+ /** The track edge the lowest value sits at. */
4
+ export type TrackStartEdge = 'left' | 'right' | 'top' | 'bottom';
5
+
6
+ /**
7
+ * Where each mark sits along the track, as a percentage from the start edge:
8
+ * one every `interval` from `min`, and one at `max`.
9
+ * @param min Lowest value of the range.
10
+ * @param max Highest value of the range.
11
+ * @param interval Gap between two marks, in the slider's own units.
12
+ * @returns One percentage per mark, or none when the range or interval cannot
13
+ * be drawn.
14
+ */
15
+ export function stepMarkPercents(
16
+ min: number,
17
+ max: number,
18
+ interval: number,
19
+ ): number[] {
20
+ const range = max - min;
21
+ if (interval <= 0 || range <= 0) return [];
22
+ // The small tolerance keeps a fractional interval such as 0.1 from losing a
23
+ // mark to floating-point rounding.
24
+ const intervalCount = Math.floor(range / interval + 1e-9);
25
+ const percents = Array.from({ length: intervalCount + 1 }, (_, index) =>
26
+ // Rounding drops floating-point noise such as 30.000000000000004.
27
+ Number(((index * interval * 100) / range).toFixed(6)),
28
+ );
29
+ // The handle can always reach max, even when the interval does not divide
30
+ // the range: a step past the last mark is clamped to it.
31
+ return percents.at(-1) === 100 ? percents : [...percents, 100];
32
+ }
33
+
34
+ /**
35
+ * The gap between dots: the one the caller asked for, unless it would put the
36
+ * dots closer than a hundredth of the range, where they would run together and
37
+ * could number in the millions. Then the default gap is used instead.
38
+ * @param min Lowest value of the range.
39
+ * @param max Highest value of the range.
40
+ * @param step The slider's stepping interval.
41
+ * @param requested The gap the caller set, if any.
42
+ * @returns The gap between two dots, in the slider's own units.
43
+ */
44
+ export function stepsIntervalOrDefault(
45
+ min: number,
46
+ max: number,
47
+ step: number,
48
+ requested?: number,
49
+ ): number {
50
+ const finestInterval = (max - min) / 100;
51
+ // Relative, so the tolerance never outweighs the limit on a tiny range.
52
+ return requested !== undefined && requested >= finestInterval * (1 - 1e-9)
53
+ ? requested
54
+ : defaultStepsInterval(min, max, step);
55
+ }
56
+
57
+ /**
58
+ * The gap between dots when the caller sets none: every step, but no closer
59
+ * together than a tenth of the range, so a fine slider gets about 11 dots.
60
+ * The gap stays a whole number of steps, so every dot is a reachable value.
61
+ * @param min Lowest value of the range.
62
+ * @param max Highest value of the range.
63
+ * @param step The slider's stepping interval.
64
+ * @returns The gap between two dots, in the slider's own units.
65
+ */
66
+ export function defaultStepsInterval(
67
+ min: number,
68
+ max: number,
69
+ step: number,
70
+ ): number {
71
+ const tenth = (max - min) / 10;
72
+ if (step <= 0 || step >= tenth) return step;
73
+ // The tolerance keeps 0.1 / 0.01 from rounding up to 11 steps.
74
+ const stepsPerDot = Math.ceil(tenth / step - 1e-9);
75
+ return Number((stepsPerDot * step).toFixed(10));
76
+ }
77
+
78
+ /**
79
+ * The edge the track starts from. reka-ui works this out for the handle but
80
+ * does not export it, so this follows the same rule as its `SliderHorizontal`
81
+ * and `SliderVertical`.
82
+ * @param orientation Slider orientation.
83
+ * @param dir Reading direction, already resolved as reka-ui's `useDirection`
84
+ * resolves it.
85
+ * @param inverted Whether the slider is visually inverted.
86
+ * @returns The edge the lowest value sits at.
87
+ */
88
+ export function trackStartEdge(
89
+ orientation: NonNullable<UiSliderProps['orientation']>,
90
+ dir: NonNullable<UiSliderProps['dir']>,
91
+ inverted: boolean,
92
+ ): TrackStartEdge {
93
+ if (orientation === 'vertical') return inverted ? 'top' : 'bottom';
94
+ const startsLeft = (dir === 'ltr') !== inverted;
95
+ return startsLeft ? 'left' : 'right';
96
+ }
@@ -85,6 +85,38 @@ export interface UiSliderProps {
85
85
  * @default false
86
86
  */
87
87
  required?: boolean;
88
+
89
+ /**
90
+ * Shows each handle's current value in text, above the handle (beside it
91
+ * when vertical), moving with it while dragging. The text is hidden from
92
+ * screen readers, which read the value from the handle itself. A vertical
93
+ * slider reserves no room beside itself, so leave space for the text there.
94
+ * @default false
95
+ */
96
+ showValue?: boolean;
97
+
98
+ /**
99
+ * Turns a value into text, e.g. `(value) => \`${value}%\``. Used for the text
100
+ * `showValue` shows, and announced by screen readers as the handle's value
101
+ * (`aria-valuetext`). Without it the plain number is used.
102
+ */
103
+ formatValue?: (value: number) => string;
104
+
105
+ /**
106
+ * Shows a dot on the track for every value the handle can reach, but no
107
+ * closer together than a tenth of the range, plus one at `max`. Set
108
+ * `stepsInterval` to choose the gap yourself.
109
+ * @default false
110
+ */
111
+ showSteps?: boolean;
112
+
113
+ /**
114
+ * The gap between two dots drawn by `showSteps`, in the slider's own units.
115
+ * A gap smaller than a hundredth of the range falls back to the default.
116
+ * @default every `step`, but no closer than a tenth of the range: a 0 to 100
117
+ * slider in steps of 1 gets a dot every 10
118
+ */
119
+ stepsInterval?: number;
88
120
  }
89
121
 
90
122
  export interface UiSliderEmits {
@@ -0,0 +1,66 @@
1
+ ---
2
+ figma: '29207:71529'
3
+ ---
4
+
5
+ <!-- Synced from Figma by scripts/sync-figma-docs.mjs. Edit the copy in Figma: changes made here are overwritten. -->
6
+
7
+ ## Usage
8
+
9
+ The Snackbar component reports what just happened. It appears after the fact, near the edge of the screen, and leaves on its own — so it can confirm without interrupting.
10
+
11
+ Use it for the outcome of a completed action, and for offering an undo. Alert is for status that belongs to the page itself, and a dialog is for anything the user has to answer.
12
+
13
+ ### After the fact, briefly
14
+
15
+ A snackbar has to work for a user who only half-notices it:
16
+
17
+ - The title states the outcome in a few words
18
+ - The icon matches the outcome: success, error, warning, info or loading
19
+ - It leaves on its own, unless it carries an action
20
+ - Nothing in it is the only record of what happened
21
+
22
+ ### Offer the undo, not the explanation
23
+
24
+ When a snackbar carries an action, keep it to one:
25
+
26
+ 1. Title states what happened
27
+ 2. Description adds a consequence, only where one matters
28
+ 3. A single action, usually undo
29
+ 4. Persisting while that action is available, so it can be taken
30
+
31
+ ### Consistency
32
+
33
+ Every snackbar is built from the same parts:
34
+
35
+ - Snackbar surface, default or focused
36
+ - Icon: success, error, warning, info or loading
37
+ - Title, and an optional description
38
+ - One action, default or secondary
39
+ - One screen position across a product
40
+
41
+ ## Do
42
+
43
+ - State the outcome in the title, in a few words
44
+ - Match the icon to the kind of outcome
45
+ - Offer undo where the action can be reversed
46
+ - Keep the snackbar on screen while its action is available
47
+ - Appear in the same position everywhere in a product
48
+ - Keep an important outcome recorded somewhere permanent as well
49
+
50
+ ## Don't
51
+
52
+ - Put anything the user must read in a snackbar
53
+ - Offer more than one action
54
+ - Stack several snackbars over each other
55
+ - Dismiss a snackbar before its action can be taken
56
+ - Use a snackbar to report page status — use Alert
57
+ - Rely on colour alone to distinguish success from error
58
+
59
+ ## Accessibility
60
+
61
+ - The outcome is carried by the icon and the words, so colour is never the only signal
62
+ - Success, error and warning are distinguishable by icon shape, not only by hue
63
+ - Text holds contrast against the snackbar's own surface, which sits over arbitrary content
64
+ - The action is a comfortable target and keeps a visible focus state
65
+ - The snackbar stays long enough to be read unhurriedly, and longer when it carries an action
66
+ - Anything important is also available in the interface, since a snackbar is easy to miss