@aleph-alpha/ui-library 5.7.0 → 5.9.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.
- package/README.md +12 -7
- package/config.d.ts +5 -1
- package/config.js +18 -1
- package/dist/system/__tests__/test-utils.d.ts +24 -0
- package/dist/system/components/UiAutocomplete/UiAutocomplete.vue.d.ts +8 -4
- package/dist/system/components/UiAutocomplete/types.d.ts +11 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumb.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbItem.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbLink.vue.d.ts +18 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbList.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbPage.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/UiBreadcrumbSeparator.vue.d.ts +17 -0
- package/dist/system/components/UiBreadcrumb/index.d.ts +8 -0
- package/dist/system/components/UiBreadcrumb/types.d.ts +22 -0
- package/dist/system/components/UiFadeMask/UiFadeMask.vue.d.ts +21 -0
- package/dist/system/components/UiFadeMask/index.d.ts +2 -0
- package/dist/system/components/UiFadeMask/types.d.ts +33 -0
- package/dist/system/components/UiListbox/UiListboxVirtualizer.vue.d.ts +21 -0
- package/dist/system/components/UiListbox/index.d.ts +1 -0
- package/dist/system/components/UiListbox/types.d.ts +36 -0
- package/dist/system/components/UiSelect/UiSelectVirtualizer.vue.d.ts +21 -0
- package/dist/system/components/UiSelect/index.d.ts +1 -0
- package/dist/system/components/UiSelect/types.d.ts +36 -0
- package/dist/system/components/UiToggleGroup/types.d.ts +0 -5
- package/dist/system/components/core/breadcrumb/Breadcrumb.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbEllipsis.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbItem.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbLink.vue.d.ts +24 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbList.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbPage.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/BreadcrumbSeparator.vue.d.ts +21 -0
- package/dist/system/components/core/breadcrumb/index.d.ts +7 -0
- package/dist/system/components/core/listbox/Listbox.vue.d.ts +28 -2
- package/dist/system/components/core/listbox/context.d.ts +21 -0
- package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
- package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
- package/dist/system/components/index.d.ts +2 -0
- package/dist/system/lib.js +22570 -21061
- package/dist/system/mobile-device-frame.png +0 -0
- package/dist/system/patterns/UiDatePicker/UiDatePicker.vue.d.ts +1 -1
- package/dist/system/patterns/UiFloatActions/types.d.ts +2 -1
- package/dist/system/patterns/UiProfileCard/index.d.ts +7 -0
- package/dist/system/patterns/UiTopBar/UiTopBar.vue.d.ts +23 -0
- package/dist/system/patterns/UiTopBar/index.d.ts +2 -0
- package/dist/system/patterns/UiTopBar/types.d.ts +28 -0
- package/dist/system/patterns/index.d.ts +2 -0
- package/dist/system/story-sidebar-icons.json +10 -1
- package/package.json +3 -2
- package/src/__tests__/test-utils.ts +74 -0
- package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +94 -0
- package/src/components/UiAutocomplete/UiAutocomplete.vue +164 -44
- package/src/components/UiAutocomplete/__tests__/UiAutocompleteVirtualized.test.ts +107 -0
- package/src/components/UiAutocomplete/types.ts +11 -0
- package/src/components/UiBreadcrumb/UiBreadcrumb.stories.ts +1419 -0
- package/src/components/UiBreadcrumb/UiBreadcrumb.vue +19 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbEllipsis.vue +22 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbItem.vue +19 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbLink.vue +26 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbList.vue +19 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbPage.vue +19 -0
- package/src/components/UiBreadcrumb/UiBreadcrumbSeparator.vue +22 -0
- package/src/components/UiBreadcrumb/index.ts +8 -0
- package/src/components/UiBreadcrumb/types.ts +28 -0
- package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +760 -0
- package/src/components/UiDropdownMenu/UiDropdownMenuTrigger.vue +2 -0
- package/src/components/UiFadeMask/UiFadeMask.stories.ts +563 -0
- package/src/components/UiFadeMask/UiFadeMask.vue +86 -0
- package/src/components/UiFadeMask/__tests__/UiFadeMask.test.ts +52 -0
- package/src/components/UiFadeMask/index.ts +6 -0
- package/src/components/UiFadeMask/types.ts +35 -0
- package/src/components/UiListbox/UiListbox.stories.ts +1404 -30
- package/src/components/UiListbox/UiListboxVirtualizer.vue +42 -0
- package/src/components/UiListbox/__tests__/UiListbox.test.ts +70 -35
- package/src/components/UiListbox/__tests__/UiListboxVirtualizer.test.ts +96 -0
- package/src/components/UiListbox/index.ts +1 -0
- package/src/components/UiListbox/types.ts +42 -0
- package/src/components/UiSelect/UiSelect.stories.ts +109 -1
- package/src/components/UiSelect/UiSelectVirtualizer.vue +373 -0
- package/src/components/UiSelect/__tests__/UiSelectVirtualizer.test.ts +185 -0
- package/src/components/UiSelect/index.ts +1 -0
- package/src/components/UiSelect/types.ts +38 -0
- package/src/components/UiSwitch/UiSwitch.stories.ts +706 -9
- package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +2 -6
- package/src/components/UiToggle/UiToggle.stories.ts +961 -130
- package/src/components/UiToggle/UiToggle.vue +8 -1
- package/src/components/UiToggle/__tests__/UiToggle.test.ts +55 -65
- package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +1060 -173
- package/src/components/UiToggleGroup/UiToggleGroup.vue +4 -2
- package/src/components/UiToggleGroup/__tests__/UiToggleGroup.test.ts +117 -325
- package/src/components/UiToggleGroup/types.ts +0 -5
- package/src/components/core/breadcrumb/Breadcrumb.vue +13 -0
- package/src/components/core/breadcrumb/BreadcrumbEllipsis.vue +23 -0
- package/src/components/core/breadcrumb/BreadcrumbItem.vue +17 -0
- package/src/components/core/breadcrumb/BreadcrumbLink.vue +29 -0
- package/src/components/core/breadcrumb/BreadcrumbList.vue +22 -0
- package/src/components/core/breadcrumb/BreadcrumbPage.vue +20 -0
- package/src/components/core/breadcrumb/BreadcrumbSeparator.vue +22 -0
- package/src/components/core/breadcrumb/index.ts +7 -0
- package/src/components/core/dropdown-menu/DropdownMenuCheckboxItem.vue +8 -2
- package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
- package/src/components/core/dropdown-menu/DropdownMenuRadioItem.vue +8 -2
- package/src/components/core/dropdown-menu/DropdownMenuSubTrigger.vue +1 -1
- package/src/components/core/listbox/Listbox.vue +14 -2
- package/src/components/core/listbox/ListboxContent.vue +61 -3
- package/src/components/core/listbox/ListboxItem.vue +6 -1
- package/src/components/core/listbox/context.ts +34 -0
- package/src/components/core/toggle/Toggle.vue +15 -1
- package/src/components/core/toggle/index.ts +2 -2
- package/src/components/core/toggle-group/ToggleGroup.vue +1 -1
- package/src/components/core/toggle-group/index.ts +5 -2
- package/src/components/index.ts +2 -0
- package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +4 -5
- package/src/patterns/UiFloatActions/UiFloatActions.vue +23 -11
- package/src/patterns/UiFloatActions/__tests__/UiFloatActions.test.ts +23 -0
- package/src/patterns/UiFloatActions/types.ts +2 -1
- package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +1064 -0
- package/src/patterns/UiProfileCard/index.ts +7 -0
- package/src/patterns/UiTopBar/UiTopBar.stories.ts +1121 -0
- package/src/patterns/UiTopBar/UiTopBar.vue +166 -0
- package/src/patterns/UiTopBar/__tests__/UiTopBar.test.ts +245 -0
- package/src/patterns/UiTopBar/index.ts +2 -0
- package/src/patterns/UiTopBar/types.ts +29 -0
- package/src/patterns/index.ts +2 -0
|
@@ -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>
|
|
@@ -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
|
*/
|