@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
@@ -0,0 +1,185 @@
1
+ import '@testing-library/jest-dom/vitest';
2
+ import { render, waitFor } from '@testing-library/vue';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { afterAll, beforeAll, describe, expect, test } from 'vitest';
5
+ import { stubScrollLayout } from '@/__tests__/test-utils';
6
+ import {
7
+ UiSelect,
8
+ UiSelectContent,
9
+ UiSelectItem,
10
+ UiSelectTrigger,
11
+ UiSelectValue,
12
+ UiSelectVirtualizer,
13
+ } from '../index';
14
+
15
+ const OPTION_COUNT = 500;
16
+ const VIEWPORT_HEIGHT = 300;
17
+
18
+ // One option is named differently, so that a single keystroke can only match
19
+ // that one, far outside the options mounted at first.
20
+ const DISTINCT_INDEX = 299;
21
+
22
+ const options = Array.from({ length: OPTION_COUNT }, (_, index) => ({
23
+ value: `option-${index + 1}`,
24
+ label: index === DISTINCT_INDEX ? 'Zebra' : `Option ${index + 1}`,
25
+ }));
26
+
27
+ let restoreScrollLayout: () => void;
28
+
29
+ // jsdom neither lays elements out nor scrolls them, so without a stand-in the
30
+ // virtualizer would measure a zero-height container and never see the scrolling
31
+ // that brings the options further down the list into range.
32
+ beforeAll(() => {
33
+ restoreScrollLayout = stubScrollLayout(VIEWPORT_HEIGHT);
34
+ });
35
+
36
+ afterAll(() => {
37
+ restoreScrollLayout();
38
+ });
39
+
40
+ /**
41
+ * Renders an open select over a long, virtualized option list.
42
+ * @param modelValue The value the select starts on.
43
+ * @returns The testing-library render result.
44
+ */
45
+ async function renderSelect(modelValue = '') {
46
+ const result = render({
47
+ components: {
48
+ UiSelect,
49
+ UiSelectContent,
50
+ UiSelectItem,
51
+ UiSelectTrigger,
52
+ UiSelectValue,
53
+ UiSelectVirtualizer,
54
+ },
55
+ setup: () => ({ options, modelValue }),
56
+ template: `
57
+ <UiSelect :model-value="modelValue">
58
+ <UiSelectTrigger>
59
+ <UiSelectValue placeholder="Select" />
60
+ </UiSelectTrigger>
61
+ <UiSelectContent>
62
+ <UiSelectVirtualizer
63
+ v-slot="{ option }"
64
+ :options="options"
65
+ :option-value="(option) => option.value"
66
+ :text-content="(option) => option.label"
67
+ >
68
+ <UiSelectItem :value="option.value">{{ option.label }}</UiSelectItem>
69
+ </UiSelectVirtualizer>
70
+ </UiSelectContent>
71
+ </UiSelect>
72
+ `,
73
+ });
74
+
75
+ await userEvent.setup().click(result.getByRole('combobox'));
76
+ await waitFor(() => {
77
+ expect(result.getAllByRole('option').length).toBeGreaterThan(0);
78
+ });
79
+
80
+ return result;
81
+ }
82
+
83
+ describe('UiSelectVirtualizer', () => {
84
+ test('mounts only the options in view instead of the whole list', async () => {
85
+ const { getAllByRole } = await renderSelect();
86
+
87
+ expect(getAllByRole('option').length).toBeLessThan(OPTION_COUNT);
88
+ });
89
+
90
+ // Only a fraction of the options is in the DOM, so each one has to carry its
91
+ // place in the full list for screen readers to announce it correctly.
92
+ test('announces the position of an option within the full list', async () => {
93
+ const { getAllByRole } = await renderSelect();
94
+
95
+ const [first] = getAllByRole('option');
96
+ expect(first).toHaveAttribute('aria-setsize', String(OPTION_COUNT));
97
+ expect(first).toHaveAttribute('aria-posinset', '1');
98
+ expect(first).toHaveAttribute('data-index', '0');
99
+ });
100
+
101
+ // reka-ui jumps to the first or last option it has collected, which under
102
+ // virtualization is only the mounted slice.
103
+ test('End moves to the last option of the full list', async () => {
104
+ const user = userEvent.setup();
105
+ const { getAllByRole, getByRole } = await renderSelect();
106
+
107
+ getAllByRole('option')[0]?.focus();
108
+ await user.keyboard('{End}');
109
+
110
+ await waitFor(() => {
111
+ expect(getByRole('option', { name: 'Option 500' })).toHaveFocus();
112
+ });
113
+ });
114
+
115
+ // reka-ui matches type-ahead against the mounted options only.
116
+ test('type-ahead reaches an option outside the mounted range', async () => {
117
+ const user = userEvent.setup();
118
+ const { getAllByRole, getByRole } = await renderSelect();
119
+
120
+ getAllByRole('option')[0]?.focus();
121
+ await user.keyboard('z');
122
+
123
+ await waitFor(() => {
124
+ expect(getByRole('option', { name: 'Zebra' })).toHaveFocus();
125
+ });
126
+ });
127
+
128
+ // Scrolling unmounts the option holding focus, and the browser drops that
129
+ // focus on the document, so the keys pressed next arrive there rather than at
130
+ // the listbox.
131
+ test('moves through the list when a key lands on the document', async () => {
132
+ const { getByRole } = await renderSelect();
133
+
134
+ document.body.dispatchEvent(
135
+ new KeyboardEvent('keydown', {
136
+ key: 'End',
137
+ bubbles: true,
138
+ cancelable: true,
139
+ }),
140
+ );
141
+
142
+ await waitFor(() => {
143
+ expect(getByRole('option', { name: 'Option 500' })).toHaveFocus();
144
+ });
145
+ });
146
+
147
+ test('carries on from the option reached before focus was dropped', async () => {
148
+ const user = userEvent.setup();
149
+ const { getAllByRole, getByRole } = await renderSelect();
150
+
151
+ const third = getAllByRole('option')[2];
152
+ third?.focus();
153
+ third?.blur();
154
+
155
+ await user.keyboard('{ArrowDown}');
156
+
157
+ await waitFor(() => {
158
+ expect(getByRole('option', { name: 'Option 4' })).toHaveFocus();
159
+ });
160
+ });
161
+
162
+ // Focus is handed back to the listbox, where reka-ui parks it between options,
163
+ // so that the select keeps hold of it.
164
+ test('takes focus back from the document when an option is unmounted', async () => {
165
+ const { getAllByRole, getByRole } = await renderSelect();
166
+
167
+ const [first] = getAllByRole('option');
168
+ first?.focus();
169
+ first?.dispatchEvent(
170
+ new FocusEvent('focusout', { bubbles: true, relatedTarget: null }),
171
+ );
172
+
173
+ expect(getByRole('listbox')).toHaveFocus();
174
+ });
175
+
176
+ // reka-ui opens on the selected option, which it can only do for an option it
177
+ // has collected.
178
+ test('opens on a selection that sits outside the first mounted range', async () => {
179
+ const { getByRole } = await renderSelect('option-400');
180
+
181
+ await waitFor(() => {
182
+ expect(getByRole('option', { name: 'Option 400' })).toBeInTheDocument();
183
+ });
184
+ });
185
+ });
@@ -2,6 +2,7 @@ export { default as UiSelect } from './UiSelect.vue';
2
2
  export { default as UiSelectTrigger } from './UiSelectTrigger.vue';
3
3
  export { default as UiSelectContent } from './UiSelectContent.vue';
4
4
  export { default as UiSelectItem } from './UiSelectItem.vue';
5
+ export { default as UiSelectVirtualizer } from './UiSelectVirtualizer.vue';
5
6
  export { default as UiSelectValue } from './UiSelectValue.vue';
6
7
  export { default as UiSelectLabel } from './UiSelectLabel.vue';
7
8
  export { default as UiSelectGroup } from './UiSelectGroup.vue';
@@ -130,6 +130,44 @@ export type UiSelectItemProps = {
130
130
  textValue?: string;
131
131
  };
132
132
 
133
+ /**
134
+ * Props for the UiSelectVirtualizer component.
135
+ * Renders only the options in view, for lists too long to mount at once.
136
+ */
137
+ export type UiSelectVirtualizerProps<T> = {
138
+ /** The full list of options to virtualize. */
139
+ options: T[];
140
+ /**
141
+ * The value of an option, matched against the select's own value to open on
142
+ * the current selection. Defaults to the option itself as a string.
143
+ */
144
+ optionValue?: (option: T) => string;
145
+ /**
146
+ * The text of an option, used for type-ahead. Required for object options,
147
+ * which have no meaningful string form.
148
+ */
149
+ textContent?: (option: T) => string;
150
+ /**
151
+ * The expected height of an option in pixels, used to size the scrollbar
152
+ * before the options are measured.
153
+ */
154
+ estimateSize?: number;
155
+ /** How many options to render outside the visible area, as a scroll buffer. */
156
+ overscan?: number;
157
+ /** Additional classes for the scroll container, such as its height. */
158
+ class?: HTMLAttributes['class'];
159
+ };
160
+
161
+ /**
162
+ * Slot props for the UiSelectVirtualizer default slot.
163
+ */
164
+ export type UiSelectVirtualizerSlotProps<T> = {
165
+ /** The option to render. */
166
+ option: T;
167
+ /** The position of the option in the full list, starting at 0. */
168
+ index: number;
169
+ };
170
+
133
171
  /**
134
172
  * Props for the UiSelectValue component.
135
173
  */
@@ -1,4 +1,5 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
+ import { fn } from 'storybook/test';
2
3
  import { ref } from 'vue';
3
4
  import { showSnackbar, showSnackbarPromise, UiSnackbar } from './index';
4
5
  import { UiButton } from '../UiButton';
@@ -89,7 +90,7 @@ export const Default: Story = {
89
90
  actionLabel: SNACKBAR_ACTION_LABEL,
90
91
  actionMode: 'default',
91
92
  iconMode: 'info',
92
- onAction: () => {},
93
+ onAction: fn(),
93
94
  toasterId,
94
95
  });
95
96
  return { args, show, toasterId };
@@ -192,7 +193,6 @@ const SNACKBAR_ICON_MODES = [
192
193
  * Built-in status icons for success, info, warning, error, and loading.
193
194
  */
194
195
  export const IconModes: Story = {
195
- name: 'Icon Modes',
196
196
  render: (args) => ({
197
197
  components: { UiSnackbar, UiButton },
198
198
  setup() {
@@ -263,7 +263,6 @@ const SNACKBAR_ACTION_MODES = ['default', 'secondary'] as const;
263
263
  * Built-in action button modes: default (primary) and secondary.
264
264
  */
265
265
  export const ActionModes: Story = {
266
- name: 'Action Modes',
267
266
  render: (args) => ({
268
267
  components: { UiSnackbar, UiButton },
269
268
  setup() {
@@ -273,7 +272,7 @@ export const ActionModes: Story = {
273
272
  title: SNACKBAR_ACTION_TITLE,
274
273
  actionLabel: SNACKBAR_ACTION_LABEL,
275
274
  actionMode,
276
- onAction: () => {},
275
+ onAction: fn(),
277
276
  toasterId,
278
277
  });
279
278
  return { args, show, toasterId, modes: SNACKBAR_ACTION_MODES };
@@ -332,7 +331,7 @@ export const WithAction: Story = {
332
331
  title: SNACKBAR_ACTION_TITLE,
333
332
  actionLabel: SNACKBAR_ACTION_LABEL,
334
333
  actionMode: 'default',
335
- onAction: () => {},
334
+ onAction: fn(),
336
335
  toasterId,
337
336
  });
338
337
  return { args, show, toasterId };