@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
@@ -90,6 +90,48 @@ export interface UiListboxItemProps {
90
90
  disabled?: boolean;
91
91
  }
92
92
 
93
+ /**
94
+ * Props for the UiListboxVirtualizer component.
95
+ * Renders only the options in view, for lists too long to mount at once.
96
+ */
97
+ export interface UiListboxVirtualizerProps<
98
+ T extends AcceptableValue = AcceptableValue,
99
+ > {
100
+ /**
101
+ * The full list of options to virtualize.
102
+ */
103
+ options: T[];
104
+ /**
105
+ * How many options to render outside the visible area, as a scroll buffer.
106
+ * @default 12
107
+ */
108
+ overscan?: number;
109
+ /**
110
+ * The expected height of an option in pixels, used to size the scrollbar
111
+ * before the options are measured.
112
+ * @default 28
113
+ */
114
+ estimateSize?: number | ((index: number) => number);
115
+ /**
116
+ * The text of an option, used for type-ahead. Required for object options,
117
+ * which have no meaningful string form.
118
+ */
119
+ textContent?: (option: T) => string;
120
+ }
121
+
122
+ /**
123
+ * Slot props for the UiListboxVirtualizer default slot.
124
+ * Describes the option to render.
125
+ */
126
+ export interface UiListboxVirtualizerSlotProps<
127
+ T extends AcceptableValue = AcceptableValue,
128
+ > {
129
+ /** The option to render. */
130
+ option: T;
131
+ /** The position of the option in the full list, starting at 0. */
132
+ index: number;
133
+ }
134
+
93
135
  /**
94
136
  * Props for the UiListboxItemIndicator component.
95
137
  * Displays a visual indicator when the item is selected.
@@ -1,6 +1,6 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3-vite';
2
2
  import { expect, userEvent, waitFor, within } from 'storybook/test';
3
- import { ref } from 'vue';
3
+ import { computed, ref } from 'vue';
4
4
  import {
5
5
  UiSelect,
6
6
  UiSelectTrigger,
@@ -10,6 +10,7 @@ import {
10
10
  UiSelectLabel,
11
11
  UiSelectGroup,
12
12
  UiSelectSeparator,
13
+ UiSelectVirtualizer,
13
14
  } from './index';
14
15
  import { UiIcon } from '@/components/UiIcon';
15
16
 
@@ -880,6 +881,113 @@ async function waitForDropdownClosed(body: ReturnType<typeof within>) {
880
881
  });
881
882
  }
882
883
 
884
+ /**
885
+ * Long option lists can be handed to `UiSelectVirtualizer`, which mounts only
886
+ * the options in view. Arrow keys, Home, End and type-ahead still move through
887
+ * the full list, and the select opens on the current selection wherever it sits.
888
+ *
889
+ * The trigger reads its label from the options it has mounted, and a virtualized
890
+ * list has mounted none of them while it is closed, so the label for the current
891
+ * value is passed to `UiSelectValue` from the same data the list is built from.
892
+ */
893
+ export const Virtualized: Story = {
894
+ render: () => ({
895
+ components: {
896
+ UiSelect,
897
+ UiSelectTrigger,
898
+ UiSelectContent,
899
+ UiSelectItem,
900
+ UiSelectValue,
901
+ UiSelectVirtualizer,
902
+ },
903
+ setup() {
904
+ const selected = ref('option-5000');
905
+ const options = Array.from({ length: 10000 }, (_, index) => ({
906
+ value: `option-${index + 1}`,
907
+ label: `Option ${index + 1}`,
908
+ }));
909
+ const selectedLabel = computed(
910
+ () =>
911
+ options.find((option) => option.value === selected.value)?.label ??
912
+ 'Select option',
913
+ );
914
+ return { selected, options, selectedLabel };
915
+ },
916
+ template: `
917
+ <UiSelect v-model="selected">
918
+ <UiSelectTrigger class="w-[220px]" ariaLabel="Select option">
919
+ <UiSelectValue placeholder="Select option">
920
+ {{ selectedLabel }}
921
+ </UiSelectValue>
922
+ </UiSelectTrigger>
923
+ <UiSelectContent>
924
+ <UiSelectVirtualizer
925
+ v-slot="{ option }"
926
+ :options="options"
927
+ :option-value="(option) => option.value"
928
+ :text-content="(option) => option.label"
929
+ >
930
+ <UiSelectItem :value="option.value">{{ option.label }}</UiSelectItem>
931
+ </UiSelectVirtualizer>
932
+ </UiSelectContent>
933
+ </UiSelect>
934
+ `,
935
+ }),
936
+ play: async ({ canvasElement, step }) => {
937
+ // Scenario (UiSelect): a 10,000 option select keeps only the visible
938
+ // options in the DOM, opens on the current selection, and still reaches the
939
+ // end of the list from the keyboard.
940
+ const canvas = within(canvasElement);
941
+ const body = within(document.body);
942
+ const trigger = canvas.getByRole('combobox');
943
+
944
+ await userEvent.click(trigger);
945
+ await findOpenListbox(body);
946
+
947
+ await step(
948
+ 'interaction: only the options in view are in the DOM',
949
+ async () => {
950
+ const rendered = body.getAllByRole('option');
951
+ await expect(rendered.length).toBeGreaterThan(0);
952
+ await expect(rendered.length).toBeLessThan(100);
953
+ },
954
+ );
955
+
956
+ await step('interaction: the list opens on the selection', async () => {
957
+ await waitFor(async () => {
958
+ await expect(
959
+ body.getByRole('option', { name: 'Option 5000' }),
960
+ ).toHaveFocus();
961
+ });
962
+ });
963
+
964
+ await step(
965
+ 'screen-reader: an option announces its place in the full list (WCAG 4.1.2)',
966
+ async () => {
967
+ const option = body.getByRole('option', { name: 'Option 5000' });
968
+ await expect(option).toHaveAttribute('aria-setsize', '10000');
969
+ await expect(option).toHaveAttribute('aria-posinset', '5000');
970
+ },
971
+ );
972
+
973
+ await step(
974
+ 'keyboard: End reaches the last option of the full list (WCAG 2.1.1)',
975
+ async () => {
976
+ await userEvent.keyboard('{End}');
977
+ await waitFor(async () => {
978
+ await expect(
979
+ body.getByRole('option', { name: 'Option 10000' }),
980
+ ).toHaveFocus();
981
+ });
982
+
983
+ // Close again so the end-of-play axe audit sees the resting state.
984
+ await userEvent.keyboard('{Escape}');
985
+ await waitForDropdownClosed(body);
986
+ },
987
+ );
988
+ },
989
+ };
990
+
883
991
  /**
884
992
  * Accessibility guidance for selects: an accessible trigger name, labelled
885
993
  * groups, and visible disabled options — shown side by side in the default
@@ -0,0 +1,373 @@
1
+ <script setup lang="ts" generic="T">
2
+ import { useVirtualizer } from '@tanstack/vue-virtual';
3
+ import { refAutoReset, useEventListener } from '@vueuse/core';
4
+ import { injectSelectRootContext } from 'reka-ui';
5
+ import {
6
+ Fragment,
7
+ cloneVNode,
8
+ computed,
9
+ isVNode,
10
+ onMounted,
11
+ ref,
12
+ type VNode,
13
+ } from 'vue';
14
+ import { cn } from '@/lib/utils';
15
+ import type {
16
+ UiSelectVirtualizerProps,
17
+ UiSelectVirtualizerSlotProps,
18
+ } from './types';
19
+
20
+ defineOptions({
21
+ name: 'UiSelectVirtualizer',
22
+ inheritAttrs: false,
23
+ });
24
+
25
+ const props = withDefaults(defineProps<UiSelectVirtualizerProps<T>>(), {
26
+ estimateSize: 32,
27
+ overscan: 12,
28
+ optionValue: undefined,
29
+ textContent: undefined,
30
+ class: undefined,
31
+ });
32
+
33
+ const slots = defineSlots<{
34
+ /** A single option, which has to render exactly one `UiSelectItem`. */
35
+ default(props: UiSelectVirtualizerSlotProps<T>): VNode[];
36
+ }>();
37
+
38
+ const TYPEAHEAD_RESET_MS = 1000;
39
+
40
+ const rootContext = injectSelectRootContext();
41
+
42
+ const scrollEl = ref<HTMLElement | null>(null);
43
+ const search = refAutoReset('', TYPEAHEAD_RESET_MS);
44
+ // The option the user is on, kept across the scrolls that unmount it.
45
+ const lastIndex = ref(-1);
46
+ // Counts the moves made, so that only the last one to be asked for lands.
47
+ let moves = 0;
48
+
49
+ const listboxEl = computed(
50
+ () => scrollEl.value?.closest<HTMLElement>('[role="listbox"]') ?? null,
51
+ );
52
+
53
+ const optionValue = computed(
54
+ () => props.optionValue ?? ((option: T) => String(option)),
55
+ );
56
+ const optionText = computed(() => props.textContent ?? optionValue.value);
57
+
58
+ const virtualizer = useVirtualizer(
59
+ computed(() => ({
60
+ count: props.options.length,
61
+ getScrollElement: () => scrollEl.value,
62
+ estimateSize: () => props.estimateSize,
63
+ overscan: props.overscan,
64
+ })),
65
+ );
66
+
67
+ /**
68
+ * The mounted options, each cloned with its offset and its place in the full
69
+ * list.
70
+ *
71
+ * The slot renders the consumer's `UiSelectItem` directly, rather than wrapping
72
+ * it, so that it stays the option reka-ui collects and labels. A slot always
73
+ * comes back wrapped in one fragment, which is unwrapped here.
74
+ */
75
+ const rows = computed(() =>
76
+ virtualizer.value.getVirtualItems().flatMap((item) => {
77
+ const option = props.options[item.index];
78
+ if (!option) {
79
+ return [];
80
+ }
81
+
82
+ const [node] = slots.default?.({ option, index: item.index }) ?? [];
83
+ if (!node) {
84
+ return [];
85
+ }
86
+
87
+ const target =
88
+ node.type === Fragment && Array.isArray(node.children)
89
+ ? node.children[0]
90
+ : node;
91
+ if (!isVNode(target)) {
92
+ return [];
93
+ }
94
+
95
+ return {
96
+ key: item.key,
97
+ node: cloneVNode(target, {
98
+ 'data-index': item.index,
99
+ 'aria-setsize': props.options.length,
100
+ 'aria-posinset': item.index + 1,
101
+ style: {
102
+ position: 'absolute',
103
+ top: 0,
104
+ left: 0,
105
+ width: '100%',
106
+ transform: `translateY(${item.start}px)`,
107
+ },
108
+ }),
109
+ };
110
+ }),
111
+ );
112
+
113
+ /**
114
+ * Waits for the options mounted by a scroll to reach the DOM.
115
+ * @returns A promise resolving after the next two animation frames.
116
+ */
117
+ function afterRender() {
118
+ return new Promise<void>((resolve) => {
119
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
120
+ });
121
+ }
122
+
123
+ /**
124
+ * The index of the option the user is on, or -1 before they reach one.
125
+ *
126
+ * Focus outlives the option it is on: scrolling unmounts that option, so the
127
+ * last one focused stands in until another is reached.
128
+ * @returns The index of the current option.
129
+ */
130
+ function focusedIndex() {
131
+ const active = document.activeElement;
132
+ const element =
133
+ active instanceof HTMLElement
134
+ ? active.closest<HTMLElement>('[data-index]')
135
+ : null;
136
+
137
+ return element && scrollEl.value?.contains(element)
138
+ ? Number(element.dataset.index)
139
+ : lastIndex.value;
140
+ }
141
+
142
+ /**
143
+ * The index of the selected option, or -1 when nothing is selected.
144
+ * @returns The index of the selected option.
145
+ */
146
+ function selectedIndex() {
147
+ const model = rootContext.modelValue.value;
148
+ const value = Array.isArray(model) ? model[0] : model;
149
+ if (value === undefined || value === null || value === '') {
150
+ return -1;
151
+ }
152
+
153
+ return props.options.findIndex(
154
+ (option) => optionValue.value(option) === String(value),
155
+ );
156
+ }
157
+
158
+ /**
159
+ * Scrolls an option into view and moves focus to it.
160
+ *
161
+ * Which options are disabled is up to the consumer's items, so a disabled
162
+ * option can only be recognised once it is mounted, and skipping it means
163
+ * carrying on in the direction the user was moving.
164
+ * @param index The option to focus.
165
+ * @param step The direction to keep searching in, or 0 to stay put.
166
+ * @param align Where to place the option in the viewport.
167
+ */
168
+ async function focusIndex(
169
+ index: number,
170
+ step: -1 | 0 | 1,
171
+ align: 'auto' | 'center' = 'auto',
172
+ ) {
173
+ const run = ++moves;
174
+ let target = index;
175
+
176
+ while (target >= 0 && target < props.options.length) {
177
+ virtualizer.value.scrollToIndex(target, { align });
178
+ await afterRender();
179
+
180
+ // Waiting for the scroll leaves room for a further key, which takes over.
181
+ if (run !== moves) {
182
+ return;
183
+ }
184
+
185
+ const element = scrollEl.value?.querySelector<HTMLElement>(
186
+ `[data-index="${target}"]`,
187
+ );
188
+ if (!element) {
189
+ return;
190
+ }
191
+
192
+ if (!element.hasAttribute('data-disabled')) {
193
+ element.focus({ preventScroll: true });
194
+ return;
195
+ }
196
+
197
+ if (step === 0) {
198
+ return;
199
+ }
200
+
201
+ target += step;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * The next option whose text starts with the typed query.
207
+ * @param query The characters typed so far.
208
+ * @param from The option to search on from.
209
+ * @returns The matching index, or -1 when nothing matches.
210
+ */
211
+ function matchingIndex(query: string, from: number) {
212
+ const normalized = query.toLowerCase();
213
+ // A query of several characters can still describe the current option, so
214
+ // that one is checked first and only repeated single keys move on.
215
+ const start = query.length > 1 ? from - 1 : from;
216
+
217
+ for (let offset = 1; offset <= props.options.length; offset++) {
218
+ const index =
219
+ (start + offset + props.options.length) % props.options.length;
220
+ const option = props.options[index];
221
+ if (
222
+ option &&
223
+ optionText.value(option).toLowerCase().startsWith(normalized)
224
+ ) {
225
+ return index;
226
+ }
227
+ }
228
+
229
+ return -1;
230
+ }
231
+
232
+ /**
233
+ * Moves through the full option list.
234
+ *
235
+ * reka-ui navigates and matches over the options it has collected, which under
236
+ * virtualization is only the mounted slice, so anything that can leave that
237
+ * slice is handled here instead.
238
+ * @param event The keyboard event, from wherever focus happens to sit.
239
+ */
240
+ function handleKeydown(event: KeyboardEvent) {
241
+ const count = props.options.length;
242
+ if (
243
+ count === 0 ||
244
+ !scrollEl.value?.isConnected ||
245
+ event.altKey ||
246
+ event.ctrlKey ||
247
+ event.metaKey
248
+ ) {
249
+ return;
250
+ }
251
+
252
+ // Whatever the keys are being typed into gets to keep them.
253
+ const typedInto = event.target;
254
+ if (
255
+ typedInto instanceof HTMLElement &&
256
+ (typedInto.isContentEditable ||
257
+ typedInto.tagName === 'INPUT' ||
258
+ typedInto.tagName === 'TEXTAREA')
259
+ ) {
260
+ return;
261
+ }
262
+
263
+ // Options next to the virtualized list, if a consumer mixes both, stay
264
+ // reka-ui's to navigate.
265
+ const active = document.activeElement;
266
+ if (
267
+ active?.getAttribute('role') === 'option' &&
268
+ !scrollEl.value.contains(active)
269
+ ) {
270
+ return;
271
+ }
272
+
273
+ const current = focusedIndex();
274
+ let target: number;
275
+ let step: -1 | 0 | 1 = 0;
276
+
277
+ switch (event.key) {
278
+ case 'ArrowDown':
279
+ target = current + 1;
280
+ step = 1;
281
+ break;
282
+ case 'ArrowUp':
283
+ target = current === -1 ? count - 1 : current - 1;
284
+ step = -1;
285
+ break;
286
+ case 'Home':
287
+ target = 0;
288
+ step = 1;
289
+ break;
290
+ case 'End':
291
+ target = count - 1;
292
+ step = -1;
293
+ break;
294
+ default:
295
+ // Space selects the focused option until a search is under way, where it
296
+ // can only be part of the query.
297
+ if (event.key.length !== 1 || (event.key === ' ' && !search.value)) {
298
+ return;
299
+ }
300
+ search.value += event.key;
301
+ target = matchingIndex(search.value, current);
302
+ }
303
+
304
+ if (target < 0 || target >= count) {
305
+ return;
306
+ }
307
+
308
+ event.preventDefault();
309
+ event.stopPropagation();
310
+ focusIndex(target, step);
311
+ }
312
+
313
+ useEventListener(scrollEl, 'focusin', (event: FocusEvent) => {
314
+ const index =
315
+ event.target instanceof HTMLElement
316
+ ? event.target.closest<HTMLElement>('[data-index]')?.dataset.index
317
+ : undefined;
318
+
319
+ if (index !== undefined) {
320
+ lastIndex.value = Number(index);
321
+ }
322
+ });
323
+
324
+ // An option that scrolls out of view is unmounted under the focus it holds,
325
+ // which the browser then drops on the document. Focus goes back to the listbox,
326
+ // where reka-ui parks it between options, so that the select keeps it.
327
+ useEventListener(scrollEl, 'focusout', (event: FocusEvent) => {
328
+ if (event.relatedTarget === null && rootContext.open.value) {
329
+ listboxEl.value?.focus({ preventScroll: true });
330
+ }
331
+ });
332
+
333
+ // Keys are taken from the document rather than from the options, so that none
334
+ // are missed in the moment between an option being unmounted and focus being
335
+ // handed back.
336
+ useEventListener(document, 'keydown', handleKeydown, { capture: true });
337
+
338
+ // reka-ui opens on the selected option, which it can only do for an option it
339
+ // has collected.
340
+ onMounted(async () => {
341
+ const index = selectedIndex();
342
+ if (index > 0) {
343
+ await afterRender();
344
+ focusIndex(index, 0, 'center');
345
+ }
346
+ });
347
+ </script>
348
+
349
+ <template>
350
+ <!--
351
+ The options scroll here rather than in the select's own viewport, so that
352
+ the height the virtualizer measures does not depend on how the floating
353
+ content happens to be sized.
354
+ -->
355
+ <div
356
+ ref="scrollEl"
357
+ v-bind="$attrs"
358
+ data-slot="select-virtualizer"
359
+ role="presentation"
360
+ :class="cn('max-h-[300px] overflow-y-auto', props.class)"
361
+ >
362
+ <div
363
+ role="presentation"
364
+ :style="{
365
+ position: 'relative',
366
+ width: '100%',
367
+ height: `${virtualizer.getTotalSize()}px`,
368
+ }"
369
+ >
370
+ <component :is="row.node" v-for="row in rows" :key="row.key" />
371
+ </div>
372
+ </div>
373
+ </template>