@human-kit/ui 1.0.0-beta.0 → 1.0.0-beta.2
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 +2 -0
- package/dist/FOCUS_STATE_CONTRACT.md +1 -0
- package/dist/accordion/root/accordion-root.svelte +15 -3
- package/dist/accordion/root/context.svelte.js +4 -4
- package/dist/accordion/types.d.ts +9 -2
- package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +6 -0
- package/dist/calendar/root/context.svelte.js +8 -2
- package/dist/checkbox/root/checkbox-root.svelte +28 -11
- package/dist/checkbox/root/checkbox-root.svelte.d.ts +13 -0
- package/dist/clock/root/create-time-selection-state.svelte.d.ts +5 -0
- package/dist/clock/root/create-time-selection-state.svelte.js +113 -10
- package/dist/clock/root/wheel-options.d.ts +7 -3
- package/dist/clock/root/wheel-options.js +7 -15
- package/dist/clock/wheel-column/clock-wheel-column.svelte +10 -2
- package/dist/clock/wheel-column/clock-wheel-column.svelte.d.ts +2 -1
- package/dist/collapsible/root/collapsible-root.svelte +9 -4
- package/dist/collapsible/types.d.ts +9 -0
- package/dist/combobox/input/combobox-input.svelte +12 -1
- package/dist/combobox/list/combobox-listbox.svelte +93 -1
- package/dist/combobox/list/combobox-listbox.svelte.d.ts +20 -0
- package/dist/combobox/root/combobox.svelte +118 -45
- package/dist/combobox/root/combobox.svelte.d.ts +11 -0
- package/dist/combobox/root/context.d.ts +12 -0
- package/dist/combobox/status/README.md +2 -2
- package/dist/combobox/tag-remove/combobox-tag-remove.svelte +14 -2
- package/dist/combobox/tag-remove/combobox-tag-remove.svelte.d.ts +7 -0
- package/dist/combobox/tags/combobox-tags.svelte +5 -2
- package/dist/combobox/tags/combobox-tags.svelte.d.ts +2 -0
- package/dist/combobox/trigger/combobox-trigger.svelte +4 -1
- package/dist/datepicker/README.md +3 -2
- package/dist/datepicker/root/date-picker-root.svelte +38 -2
- package/dist/datepicker/root/date-picker-root.svelte.d.ts +2 -0
- package/dist/datepicker/root/draft-evaluation.d.ts +7 -0
- package/dist/datepicker/root/draft-evaluation.js +2 -2
- package/dist/daterangepicker/root/date-range-picker-root.svelte +19 -1
- package/dist/daterangepicker/root/date-range-picker-root.svelte.d.ts +2 -0
- package/dist/dialog/README.md +9 -1
- package/dist/dialog/close/dialog-close.svelte +57 -0
- package/dist/dialog/close/dialog-close.svelte.d.ts +19 -0
- package/dist/dialog/content/dialog-content.svelte +2 -0
- package/dist/dialog/description/dialog-description.svelte +46 -0
- package/dist/dialog/description/dialog-description.svelte.d.ts +20 -0
- package/dist/dialog/index.d.ts +9 -0
- package/dist/dialog/index.js +3 -0
- package/dist/dialog/index.parts.d.ts +3 -0
- package/dist/dialog/index.parts.js +3 -0
- package/dist/dialog/root/context.d.ts +8 -0
- package/dist/dialog/root/dialog-root.svelte +58 -16
- package/dist/dialog/root/dialog-root.svelte.d.ts +10 -3
- package/dist/dialog/root/dialog-stack.d.ts +6 -22
- package/dist/dialog/root/dialog-stack.js +4 -33
- package/dist/dialog/title/dialog-title.svelte +43 -0
- package/dist/dialog/title/dialog-title.svelte.d.ts +21 -0
- package/dist/drawer/README.md +111 -0
- package/dist/drawer/body/drawer-body.svelte +47 -0
- package/dist/drawer/body/drawer-body.svelte.d.ts +32 -0
- package/dist/drawer/close/drawer-close.svelte +52 -0
- package/dist/drawer/close/drawer-close.svelte.d.ts +20 -0
- package/dist/drawer/content/drawer-content.svelte +597 -0
- package/dist/drawer/content/drawer-content.svelte.d.ts +28 -0
- package/dist/drawer/description/drawer-description.svelte +40 -0
- package/dist/drawer/description/drawer-description.svelte.d.ts +20 -0
- package/dist/drawer/indent/drawer-indent-background.svelte +36 -0
- package/dist/drawer/indent/drawer-indent-background.svelte.d.ts +17 -0
- package/dist/drawer/indent/drawer-indent.svelte +59 -0
- package/dist/drawer/indent/drawer-indent.svelte.d.ts +32 -0
- package/dist/drawer/index.d.ts +49 -0
- package/dist/drawer/index.js +25 -0
- package/dist/drawer/index.parts.d.ts +14 -0
- package/dist/drawer/index.parts.js +15 -0
- package/dist/drawer/overlay/drawer-overlay.svelte +67 -0
- package/dist/drawer/overlay/drawer-overlay.svelte.d.ts +12 -0
- package/dist/drawer/portal/drawer-portal.svelte +57 -0
- package/dist/drawer/portal/drawer-portal.svelte.d.ts +13 -0
- package/dist/drawer/root/context.d.ts +81 -0
- package/dist/drawer/root/context.js +20 -0
- package/dist/drawer/root/drawer-root.svelte +333 -0
- package/dist/drawer/root/drawer-root.svelte.d.ts +77 -0
- package/dist/drawer/root/drawer-stack.svelte.d.ts +73 -0
- package/dist/drawer/root/drawer-stack.svelte.js +113 -0
- package/dist/drawer/root/handle.svelte.d.ts +39 -0
- package/dist/drawer/root/handle.svelte.js +64 -0
- package/dist/drawer/root/keyboard-context.d.ts +13 -0
- package/dist/drawer/root/keyboard-context.js +8 -0
- package/dist/drawer/root/presence-context.d.ts +22 -0
- package/dist/drawer/root/presence-context.js +8 -0
- package/dist/drawer/root/snap-points.d.ts +68 -0
- package/dist/drawer/root/snap-points.js +120 -0
- package/dist/drawer/root/starting-style.svelte.d.ts +18 -0
- package/dist/drawer/root/starting-style.svelte.js +38 -0
- package/dist/drawer/root/types.d.ts +36 -0
- package/dist/drawer/root/types.js +1 -0
- package/dist/drawer/root/viewport-context.d.ts +2 -0
- package/dist/drawer/root/viewport-context.js +16 -0
- package/dist/drawer/swipe-area/drawer-swipe-area.svelte +143 -0
- package/dist/drawer/swipe-area/drawer-swipe-area.svelte.d.ts +22 -0
- package/dist/drawer/title/drawer-title.svelte +37 -0
- package/dist/drawer/title/drawer-title.svelte.d.ts +21 -0
- package/dist/drawer/trigger/drawer-trigger.svelte +90 -0
- package/dist/drawer/trigger/drawer-trigger.svelte.d.ts +41 -0
- package/dist/drawer/viewport/drawer-viewport.svelte +69 -0
- package/dist/drawer/viewport/drawer-viewport.svelte.d.ts +15 -0
- package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte +88 -0
- package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte.d.ts +29 -0
- package/dist/hooks/use-virtual-focus.svelte.d.ts +9 -0
- package/dist/hooks/use-virtual-focus.svelte.js +7 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/listbox/item/listbox-item.svelte +8 -1
- package/dist/listbox/root/context.d.ts +30 -0
- package/dist/listbox/root/context.js +10 -0
- package/dist/listbox/root/listbox.svelte +291 -8
- package/dist/listbox/root/listbox.svelte.d.ts +35 -2
- package/dist/menu/content/menu-content.svelte +3 -1
- package/dist/menu/overlay/menu-overlay.svelte +1 -1
- package/dist/numberfield/root/number-field-root.svelte +2 -1
- package/dist/popover/content/popover-content.svelte +1 -1
- package/dist/popover/overlay/popover-overlay.svelte +1 -1
- package/dist/popover/root/popover-root.svelte +23 -11
- package/dist/popover/root/popover-root.svelte.d.ts +10 -2
- package/dist/primitives/floating.d.ts +7 -0
- package/dist/primitives/floating.js +6 -2
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/keyboard-navigation.js +3 -1
- package/dist/primitives/layer-stack.d.ts +29 -0
- package/dist/primitives/layer-stack.js +35 -0
- package/dist/primitives/list-virtualizer.d.ts +75 -0
- package/dist/primitives/list-virtualizer.js +59 -0
- package/dist/primitives/scroll-lock.d.ts +23 -3
- package/dist/primitives/scroll-lock.js +122 -9
- package/dist/primitives/swipe-gesture.d.ts +126 -0
- package/dist/primitives/swipe-gesture.js +351 -0
- package/dist/primitives/text.d.ts +8 -0
- package/dist/primitives/text.js +13 -0
- package/dist/switch/root/switch-root.svelte +23 -9
- package/dist/switch/root/switch-root.svelte.d.ts +9 -2
- package/dist/table/body/table-body.svelte +84 -9
- package/dist/table/cell/table-cell.svelte +1 -3
- package/dist/table/column/table-column.svelte +5 -1
- package/dist/table/column-resizer/table-column-resizer.svelte +1 -5
- package/dist/table/index.d.ts +1 -1
- package/dist/table/index.js +1 -1
- package/dist/table/interactive-cell/table-interactive-cell.svelte +1 -3
- package/dist/table/root/context.svelte.js +99 -49
- package/dist/table/row/table-row.svelte +7 -7
- package/dist/table/sort-trigger/table-sort-trigger.svelte +5 -1
- package/dist/table/types.d.ts +6 -0
- package/dist/tabs/root/tabs-root.svelte +20 -6
- package/dist/tabs/types.d.ts +9 -0
- package/dist/timepicker/root/time-picker-root.svelte +11 -0
- package/dist/timepicker/root/time-picker-root.svelte.d.ts +2 -0
- package/dist/toggle-group/root/context.svelte.js +11 -4
- package/dist/toggle-group/root/toggle-group-root.svelte +19 -4
- package/dist/toggle-group/types.d.ts +9 -0
- package/dist/tree/root/context.svelte.js +6 -0
- package/dist/tree/root/tree-root.svelte +22 -8
- package/dist/tree/types.d.ts +13 -0
- package/package.json +227 -224
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
<script lang="ts" generics="T extends object = object">
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
|
-
import { onMount, untrack } from 'svelte';
|
|
3
|
+
import { onMount, tick, untrack } from 'svelte';
|
|
4
4
|
import { createListBoxContext, type ListBoxContext } from './context';
|
|
5
5
|
import { trackInteractionModality } from '../../primitives/input-modality';
|
|
6
|
+
import { computeListWindow, scrollTopForIndex } from '../../primitives/list-virtualizer';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Props for the ListBox component.
|
|
@@ -19,12 +20,28 @@
|
|
|
19
20
|
disabledKeys?: Iterable<string | number>;
|
|
20
21
|
/** Selection mode: 'single' allows one selection, 'multiple' allows many. */
|
|
21
22
|
selectionMode?: 'single' | 'multiple';
|
|
22
|
-
/**
|
|
23
|
+
/** Selected keys. Two-way by default — use `bind:value`. */
|
|
23
24
|
value?: Iterable<string | number>;
|
|
24
|
-
/** Initial selection for
|
|
25
|
+
/** Initial selection, for when `value` is not supplied. */
|
|
25
26
|
defaultValue?: Iterable<string | number>;
|
|
27
|
+
/**
|
|
28
|
+
* Opt into fully controlled state: the component stops writing back to `value` and
|
|
29
|
+
* only reports through `onChange`, so the parent can reject a change by not flowing
|
|
30
|
+
* the new selection back down. Off by default, because `bind:value` — the common
|
|
31
|
+
* case — needs the write-back to work at all.
|
|
32
|
+
*/
|
|
33
|
+
controlledValue?: boolean;
|
|
26
34
|
/** Content of the listbox. Can be static children or a snippet receiving items. */
|
|
27
35
|
children?: Snippet | Snippet<[T]>;
|
|
36
|
+
/**
|
|
37
|
+
* Rows rendered inside the listbox above the items — an action row such as
|
|
38
|
+
* "Create …". Kept out of `children` because in dynamic mode that snippet is called
|
|
39
|
+
* once per item.
|
|
40
|
+
*
|
|
41
|
+
* A virtualized list does not navigate into it: navigation there walks the item
|
|
42
|
+
* array, which this is not part of.
|
|
43
|
+
*/
|
|
44
|
+
header?: Snippet;
|
|
28
45
|
/** CSS class to apply to the listbox container. */
|
|
29
46
|
class?: string;
|
|
30
47
|
/** HTML id attribute for the listbox element. */
|
|
@@ -39,6 +56,20 @@
|
|
|
39
56
|
loop?: boolean;
|
|
40
57
|
/** Whether typing characters moves focus to the option whose text matches. */
|
|
41
58
|
typeahead?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Renders only the rows near the viewport instead of all of them.
|
|
61
|
+
*
|
|
62
|
+
* Only honoured together with `items`: the listbox has to own the loop to be able to
|
|
63
|
+
* slice it, so a list written as static children is never virtualized.
|
|
64
|
+
*
|
|
65
|
+
* Rows must all be the same height — that assumption is what lets the scrollbar be
|
|
66
|
+
* sized without measuring every row. The height is measured from the first rendered
|
|
67
|
+
* row unless `rowHeight` says otherwise, so a themed list needs no magic number.
|
|
68
|
+
*
|
|
69
|
+
* The listbox element itself is the scroller: give it a max height and
|
|
70
|
+
* `overflow-y: auto`, as an unvirtualized long list would need anyway.
|
|
71
|
+
*/
|
|
72
|
+
virtualizer?: { rowHeight?: number; overscan?: number };
|
|
42
73
|
};
|
|
43
74
|
|
|
44
75
|
let {
|
|
@@ -49,7 +80,9 @@
|
|
|
49
80
|
selectionMode = 'single',
|
|
50
81
|
value = $bindable(),
|
|
51
82
|
defaultValue,
|
|
83
|
+
controlledValue = false,
|
|
52
84
|
children,
|
|
85
|
+
header,
|
|
53
86
|
class: className = '',
|
|
54
87
|
id,
|
|
55
88
|
'aria-label': ariaLabel,
|
|
@@ -57,6 +90,7 @@
|
|
|
57
90
|
disableFocusHandling = false,
|
|
58
91
|
loop = false,
|
|
59
92
|
typeahead = false,
|
|
93
|
+
virtualizer,
|
|
60
94
|
context = $bindable(),
|
|
61
95
|
element = $bindable()
|
|
62
96
|
}: ListBoxProps & { context?: ListBoxContext; element?: HTMLElement } = $props();
|
|
@@ -73,10 +107,12 @@
|
|
|
73
107
|
return new Set(val);
|
|
74
108
|
}
|
|
75
109
|
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
|
|
110
|
+
// In controlled mode the parent owns the selection: `select` only emits `onChange`
|
|
111
|
+
// and the sync effect below applies whatever the parent passes down. Controlled-ness
|
|
112
|
+
// is NOT inferred from `value` being defined: `bind:value={keys}` and `value={keys}`
|
|
113
|
+
// are indistinguishable at runtime, so inferring it silently broke every `bind:value`.
|
|
114
|
+
// It is opt-in via `controlledValue` instead.
|
|
115
|
+
const isControlled = untrack(() => controlledValue);
|
|
80
116
|
|
|
81
117
|
const ctx = createListBoxContext({
|
|
82
118
|
get selectionMode() {
|
|
@@ -142,6 +178,228 @@
|
|
|
142
178
|
|
|
143
179
|
const itemsArray = $derived(items ? Array.from(items) : []);
|
|
144
180
|
const hasDynamicItems = $derived(items !== undefined);
|
|
181
|
+
|
|
182
|
+
// --- Virtualization -----------------------------------------------------
|
|
183
|
+
// Only the rows near the viewport are rendered; the rest is two numbers — a spacer that
|
|
184
|
+
// holds the scrollbar at the height of the full list, and an offset that puts the
|
|
185
|
+
// rendered block where those rows would have been.
|
|
186
|
+
const isVirtual = $derived(Boolean(virtualizer) && hasDynamicItems);
|
|
187
|
+
|
|
188
|
+
let scrollTop = $state(0);
|
|
189
|
+
let viewportHeight = $state(0);
|
|
190
|
+
/**
|
|
191
|
+
* The distance from one row's top edge to the next one's, measured off two rendered rows.
|
|
192
|
+
*
|
|
193
|
+
* Measured rather than declared because the row's height comes from the consumer's own
|
|
194
|
+
* styles — and measured as a *distance*, not as height + `row-gap`, because that sum was
|
|
195
|
+
* wrong by about a pixel and the error is cumulative: the offset of row N is N times it,
|
|
196
|
+
* so a thousand rows down the window was placed a thousand pixels away from where the
|
|
197
|
+
* scrollbar said it was and the list rendered blank.
|
|
198
|
+
*/
|
|
199
|
+
let measuredPitch = $state(0);
|
|
200
|
+
/** The row's own height, for deciding when a row is fully inside the viewport. */
|
|
201
|
+
let measuredRowHeight = $state(0);
|
|
202
|
+
/**
|
|
203
|
+
* The listbox's own `row-gap`, applied to the rendered window so the rows keep the
|
|
204
|
+
* spacing the consumer styled them with.
|
|
205
|
+
*
|
|
206
|
+
* Read from the style rather than derived from the pitch on purpose: deriving it would
|
|
207
|
+
* close a circle — the gap would come from a distance the gap itself produces — and the
|
|
208
|
+
* list would settle on whatever it measured first, gap and all.
|
|
209
|
+
*/
|
|
210
|
+
let measuredGap = $state(0);
|
|
211
|
+
/** The scroller's own top padding, which sits between `scrollTop` and the first row. */
|
|
212
|
+
let measuredPadding = $state(0);
|
|
213
|
+
|
|
214
|
+
const rowHeight = $derived(virtualizer?.rowHeight ?? measuredRowHeight);
|
|
215
|
+
const pitch = $derived(
|
|
216
|
+
virtualizer?.rowHeight !== undefined ? virtualizer.rowHeight + measuredGap : measuredPitch
|
|
217
|
+
);
|
|
218
|
+
// What the maths needs is the distance between rows; expressing it as height + gap keeps
|
|
219
|
+
// `computeListWindow`'s signature honest about what a row occupies.
|
|
220
|
+
const gap = $derived(Math.max(0, pitch - rowHeight));
|
|
221
|
+
/**
|
|
222
|
+
* Rows rendered on the very first pass, before anything has been measured.
|
|
223
|
+
*
|
|
224
|
+
* Without it the list would deadlock: the window needs a row height, the height comes
|
|
225
|
+
* from measuring rendered rows, and nothing is rendered until there's a window. A
|
|
226
|
+
* viewport's worth is enough to measure and to fill the first paint.
|
|
227
|
+
*/
|
|
228
|
+
const BOOTSTRAP_ROWS = 20;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The viewport to size the window against, never taller than the screen.
|
|
232
|
+
*
|
|
233
|
+
* `clientHeight` is the height of the scroller, which is only meaningful once something
|
|
234
|
+
* constrains it. A popover applies its max height *after* it opens, so on the first pass
|
|
235
|
+
* the list is as tall as its own content — and a "viewport" of 28.000px asks for a
|
|
236
|
+
* window of every row there is. That is how opening a 1122-row list mounted 1122 rows
|
|
237
|
+
* (~7s) before shrinking back to 19: virtualized in the end, but only after paying for
|
|
238
|
+
* the whole list.
|
|
239
|
+
*
|
|
240
|
+
* A list can legitimately be as tall as the screen, never taller, so the screen is the
|
|
241
|
+
* cap. The ResizeObserver corrects the number the moment the real constraint lands.
|
|
242
|
+
*/
|
|
243
|
+
const effectiveViewportHeight = $derived(
|
|
244
|
+
typeof window === 'undefined' ? viewportHeight : Math.min(viewportHeight, window.innerHeight)
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
const listWindow = $derived(
|
|
248
|
+
isVirtual && rowHeight > 0 && pitch > 0
|
|
249
|
+
? computeListWindow({
|
|
250
|
+
count: itemsArray.length,
|
|
251
|
+
itemHeight: rowHeight,
|
|
252
|
+
gap,
|
|
253
|
+
viewportHeight: effectiveViewportHeight,
|
|
254
|
+
scrollTop,
|
|
255
|
+
overscan: virtualizer?.overscan ?? 8,
|
|
256
|
+
padding: measuredPadding
|
|
257
|
+
})
|
|
258
|
+
: undefined
|
|
259
|
+
);
|
|
260
|
+
const visibleItems = $derived.by(() => {
|
|
261
|
+
if (listWindow) {
|
|
262
|
+
return itemsArray.slice(listWindow.from, listWindow.to + 1);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return isVirtual ? itemsArray.slice(0, BOOTSTRAP_ROWS) : itemsArray;
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
/** Below this a re-measurement is noise, not a correction worth re-rendering for. */
|
|
269
|
+
const MEASUREMENT_EPSILON = 0.5;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Reads the row geometry off what is actually on screen.
|
|
273
|
+
*
|
|
274
|
+
* Only ever *writes* the measurements — reading them here would close a loop with the
|
|
275
|
+
* window that renders the rows it measures (an effect that reads and writes the same
|
|
276
|
+
* state re-runs until Svelte gives up).
|
|
277
|
+
*/
|
|
278
|
+
function measureRows() {
|
|
279
|
+
if (!listboxElement || !isVirtual) return;
|
|
280
|
+
|
|
281
|
+
const rows = listboxElement.querySelectorAll<HTMLElement>('[data-listbox-window] > *');
|
|
282
|
+
const first = rows[0]?.getBoundingClientRect();
|
|
283
|
+
if (!first || first.height <= 0) return;
|
|
284
|
+
|
|
285
|
+
// Two rows give the pitch exactly, gap included. With only one, fall back to its
|
|
286
|
+
// height plus the declared gap — the same guess as before, but now it is the
|
|
287
|
+
// exception rather than the rule.
|
|
288
|
+
const second = rows[1]?.getBoundingClientRect();
|
|
289
|
+
const style = getComputedStyle(listboxElement);
|
|
290
|
+
const declaredGap = Number.parseFloat(style.rowGap);
|
|
291
|
+
const declaredPadding = Number.parseFloat(style.paddingTop);
|
|
292
|
+
const nextPitch = second
|
|
293
|
+
? second.top - first.top
|
|
294
|
+
: first.height + (Number.isFinite(declaredGap) ? declaredGap : 0);
|
|
295
|
+
|
|
296
|
+
const nextGap = Number.isFinite(declaredGap) ? declaredGap : 0;
|
|
297
|
+
const nextPadding = Number.isFinite(declaredPadding) ? declaredPadding : 0;
|
|
298
|
+
|
|
299
|
+
untrack(() => {
|
|
300
|
+
if (Math.abs(first.height - measuredRowHeight) > MEASUREMENT_EPSILON) {
|
|
301
|
+
measuredRowHeight = first.height;
|
|
302
|
+
}
|
|
303
|
+
if (nextPitch > 0 && Math.abs(nextPitch - measuredPitch) > MEASUREMENT_EPSILON) {
|
|
304
|
+
measuredPitch = nextPitch;
|
|
305
|
+
}
|
|
306
|
+
if (Math.abs(nextGap - measuredGap) > MEASUREMENT_EPSILON) {
|
|
307
|
+
measuredGap = nextGap;
|
|
308
|
+
}
|
|
309
|
+
if (Math.abs(nextPadding - measuredPadding) > MEASUREMENT_EPSILON) {
|
|
310
|
+
measuredPadding = nextPadding;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
$effect(() => {
|
|
316
|
+
if (!isVirtual || !listboxElement) return;
|
|
317
|
+
|
|
318
|
+
// Re-measure whenever the rendered set changes — a list that was filtered, a window
|
|
319
|
+
// that moved — so a first measurement taken mid-animation gets corrected instead of
|
|
320
|
+
// misplacing every row after it. Reads nothing it writes.
|
|
321
|
+
void itemsArray.length;
|
|
322
|
+
void listWindow?.from;
|
|
323
|
+
|
|
324
|
+
const element = listboxElement;
|
|
325
|
+
|
|
326
|
+
measureRows();
|
|
327
|
+
|
|
328
|
+
const observer = new ResizeObserver(() => {
|
|
329
|
+
untrack(() => {
|
|
330
|
+
if (element.clientHeight !== viewportHeight) {
|
|
331
|
+
viewportHeight = element.clientHeight;
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
measureRows();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
observer.observe(element);
|
|
338
|
+
viewportHeight = element.clientHeight;
|
|
339
|
+
|
|
340
|
+
return () => observer.disconnect();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
function handleScroll() {
|
|
344
|
+
if (isVirtual && listboxElement) {
|
|
345
|
+
scrollTop = listboxElement.scrollTop;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Brings a row into view by index, mounting it if the window didn't reach it.
|
|
351
|
+
*
|
|
352
|
+
* This is what makes keyboard navigation work over rows that aren't rendered: the caller
|
|
353
|
+
* moves its focus by index over the whole list and asks for the row, and by the time the
|
|
354
|
+
* promise resolves the element exists and can be pointed at with `aria-activedescendant`.
|
|
355
|
+
*/
|
|
356
|
+
async function scrollIndexIntoView(
|
|
357
|
+
index: number,
|
|
358
|
+
options?: { align?: 'nearest' | 'center' }
|
|
359
|
+
): Promise<boolean> {
|
|
360
|
+
// Nothing to compute before the first measurement: a zero row height or an unmeasured
|
|
361
|
+
// viewport would send the list to an offset with no rows in it, which is what a blank
|
|
362
|
+
// list is.
|
|
363
|
+
if (!isVirtual || !listboxElement || rowHeight <= 0 || effectiveViewportHeight <= 0) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const next = scrollTopForIndex({
|
|
368
|
+
index,
|
|
369
|
+
count: itemsArray.length,
|
|
370
|
+
itemHeight: rowHeight,
|
|
371
|
+
gap,
|
|
372
|
+
viewportHeight: effectiveViewportHeight,
|
|
373
|
+
scrollTop,
|
|
374
|
+
padding: measuredPadding,
|
|
375
|
+
align: options?.align
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
if (next === null) {
|
|
379
|
+
// Already where it should be.
|
|
380
|
+
await tick();
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
listboxElement.scrollTop = next;
|
|
385
|
+
// Read it back instead of trusting the assignment: the browser clamps to the real
|
|
386
|
+
// scroll range, and a state that disagrees with the element renders a window the
|
|
387
|
+
// viewport isn't looking at.
|
|
388
|
+
scrollTop = listboxElement.scrollTop;
|
|
389
|
+
|
|
390
|
+
// The window is derived from `scrollTop`, so the row exists only after the update.
|
|
391
|
+
await tick();
|
|
392
|
+
|
|
393
|
+
// Whether it actually landed. A scroll asked for before the list has its final
|
|
394
|
+
// height gets clamped to a range that doesn't exist yet, and the caller needs to
|
|
395
|
+
// know so it can ask again rather than assume the row is where it wanted it.
|
|
396
|
+
return Math.abs(scrollTop - next) < 1;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Published on the context so a composite widget (a ComboBox, whose keyboard focus lives
|
|
400
|
+
// on its input) can reach a row the window hasn't rendered yet.
|
|
401
|
+
ctx.setScrollIndexIntoView(scrollIndexIntoView);
|
|
402
|
+
ctx.setVirtualized(() => isVirtual);
|
|
145
403
|
let hasMounted = $state(false);
|
|
146
404
|
const registeredItemCount = $derived(hasMounted ? itemCount : ctx.getItemCount());
|
|
147
405
|
const shouldShowEmptyPlaceholder = $derived(
|
|
@@ -199,8 +457,33 @@
|
|
|
199
457
|
onfocusout={handleFocusOut}
|
|
200
458
|
onmousedown={handleMouseDown}
|
|
201
459
|
onkeydown={handleKeyDown}
|
|
460
|
+
onscroll={handleScroll}
|
|
202
461
|
>
|
|
203
|
-
{#if
|
|
462
|
+
{#if header}
|
|
463
|
+
{@render header()}
|
|
464
|
+
{/if}
|
|
465
|
+
|
|
466
|
+
{#if items && children && isVirtual}
|
|
467
|
+
<!-- The spacer carries the height of the whole list so the scrollbar matches it; the
|
|
468
|
+
window is placed at the offset the first rendered row would have had. It repeats the
|
|
469
|
+
listbox's own gap so rows sit exactly where the maths says they do. -->
|
|
470
|
+
<!-- `flex: 0 0 auto` because the listbox is usually a flex column: without it the
|
|
471
|
+
spacer is shrunk to the viewport height and there is nothing left to scroll. -->
|
|
472
|
+
<div
|
|
473
|
+
data-listbox-spacer
|
|
474
|
+
style="position: relative; flex: 0 0 auto; height: {listWindow?.contentHeight ?? 0}px"
|
|
475
|
+
>
|
|
476
|
+
<div
|
|
477
|
+
data-listbox-window
|
|
478
|
+
style="position: absolute; top: {listWindow?.offset ??
|
|
479
|
+
0}px; left: 0; right: 0; display: flex; flex-direction: column; gap: {measuredGap}px"
|
|
480
|
+
>
|
|
481
|
+
{#each visibleItems as item (item)}
|
|
482
|
+
{@render (children as Snippet<[T]>)(item)}
|
|
483
|
+
{/each}
|
|
484
|
+
</div>
|
|
485
|
+
</div>
|
|
486
|
+
{:else if items && children}
|
|
204
487
|
{#each itemsArray as item (item)}
|
|
205
488
|
{@render (children as Snippet<[T]>)(item)}
|
|
206
489
|
{/each}
|
|
@@ -12,12 +12,28 @@ declare function $$render<T extends object = object>(): {
|
|
|
12
12
|
disabledKeys?: Iterable<string | number>;
|
|
13
13
|
/** Selection mode: 'single' allows one selection, 'multiple' allows many. */
|
|
14
14
|
selectionMode?: "single" | "multiple";
|
|
15
|
-
/**
|
|
15
|
+
/** Selected keys. Two-way by default — use `bind:value`. */
|
|
16
16
|
value?: Iterable<string | number>;
|
|
17
|
-
/** Initial selection for
|
|
17
|
+
/** Initial selection, for when `value` is not supplied. */
|
|
18
18
|
defaultValue?: Iterable<string | number>;
|
|
19
|
+
/**
|
|
20
|
+
* Opt into fully controlled state: the component stops writing back to `value` and
|
|
21
|
+
* only reports through `onChange`, so the parent can reject a change by not flowing
|
|
22
|
+
* the new selection back down. Off by default, because `bind:value` — the common
|
|
23
|
+
* case — needs the write-back to work at all.
|
|
24
|
+
*/
|
|
25
|
+
controlledValue?: boolean;
|
|
19
26
|
/** Content of the listbox. Can be static children or a snippet receiving items. */
|
|
20
27
|
children?: Snippet | Snippet<[T]>;
|
|
28
|
+
/**
|
|
29
|
+
* Rows rendered inside the listbox above the items — an action row such as
|
|
30
|
+
* "Create …". Kept out of `children` because in dynamic mode that snippet is called
|
|
31
|
+
* once per item.
|
|
32
|
+
*
|
|
33
|
+
* A virtualized list does not navigate into it: navigation there walks the item
|
|
34
|
+
* array, which this is not part of.
|
|
35
|
+
*/
|
|
36
|
+
header?: Snippet;
|
|
21
37
|
/** CSS class to apply to the listbox container. */
|
|
22
38
|
class?: string;
|
|
23
39
|
/** HTML id attribute for the listbox element. */
|
|
@@ -32,6 +48,23 @@ declare function $$render<T extends object = object>(): {
|
|
|
32
48
|
loop?: boolean;
|
|
33
49
|
/** Whether typing characters moves focus to the option whose text matches. */
|
|
34
50
|
typeahead?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Renders only the rows near the viewport instead of all of them.
|
|
53
|
+
*
|
|
54
|
+
* Only honoured together with `items`: the listbox has to own the loop to be able to
|
|
55
|
+
* slice it, so a list written as static children is never virtualized.
|
|
56
|
+
*
|
|
57
|
+
* Rows must all be the same height — that assumption is what lets the scrollbar be
|
|
58
|
+
* sized without measuring every row. The height is measured from the first rendered
|
|
59
|
+
* row unless `rowHeight` says otherwise, so a themed list needs no magic number.
|
|
60
|
+
*
|
|
61
|
+
* The listbox element itself is the scroller: give it a max height and
|
|
62
|
+
* `overflow-y: auto`, as an unvirtualized long list would need anyway.
|
|
63
|
+
*/
|
|
64
|
+
virtualizer?: {
|
|
65
|
+
rowHeight?: number;
|
|
66
|
+
overscan?: number;
|
|
67
|
+
};
|
|
35
68
|
} & {
|
|
36
69
|
context?: ListBoxContext;
|
|
37
70
|
element?: HTMLElement;
|
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
import { browser } from '../../internal/environment';
|
|
6
6
|
import { floating, type ExtendedPlacement } from '../../primitives/floating';
|
|
7
7
|
import { clickOutside, isTargetInTopLayerAbove } from '../../primitives/click-outside';
|
|
8
|
+
import { scrollLock } from '../../primitives/scroll-lock';
|
|
8
9
|
import { releaseFocusedDescendant } from '../../primitives/release-focused-descendant';
|
|
9
10
|
import { createPresence } from '../../primitives/presence.svelte';
|
|
10
11
|
import { Portal } from '../../portal';
|
|
11
12
|
import { useMenuContext } from '../root/context';
|
|
12
13
|
import { pushMenuLayer, removeMenuLayer, isTopmostMenu } from '../root/menu-stack';
|
|
13
|
-
import { getFloatingLayerZIndex } from '../../
|
|
14
|
+
import { getFloatingLayerZIndex } from '../../primitives/layer-stack';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Menu.Content - The floating menu panel (role="menu").
|
|
@@ -296,6 +297,7 @@
|
|
|
296
297
|
enabled: isOpen && shouldCloseOnInteractOutside,
|
|
297
298
|
ignore: [triggerRef]
|
|
298
299
|
}}
|
|
300
|
+
use:scrollLock={presence.isMounted}
|
|
299
301
|
use:ctx.keyboardNav.action
|
|
300
302
|
onpointerenter={() => ctx.parent?.cancelPendingHighlight()}
|
|
301
303
|
style="position: fixed; z-index: {zIndex};"
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { Portal } from '../../portal';
|
|
4
4
|
import { createPresence } from '../../primitives/presence.svelte';
|
|
5
5
|
import { useMenuContext } from '../root/context';
|
|
6
|
-
import { getFloatingLayerOverlayZIndex } from '../../
|
|
6
|
+
import { getFloatingLayerOverlayZIndex } from '../../primitives/layer-stack';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Menu.Overlay - An optional backdrop rendered behind the menu.
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
type PopoverCloseReason
|
|
18
18
|
} from '../root/context';
|
|
19
19
|
import { pushPopoverLayer, removePopoverLayer, isTopmostPopover } from '../root/popover-stack';
|
|
20
|
-
import { getFloatingLayerZIndex } from '../../
|
|
20
|
+
import { getFloatingLayerZIndex } from '../../primitives/layer-stack';
|
|
21
21
|
import {
|
|
22
22
|
addTriggerBlurCleanup,
|
|
23
23
|
applyTriggerCloseFocusState,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { Portal } from '../../portal';
|
|
4
4
|
import { createPresence } from '../../primitives/presence.svelte';
|
|
5
5
|
import { getPopoverContext } from '../root/context';
|
|
6
|
-
import { getFloatingLayerOverlayZIndex } from '../../
|
|
6
|
+
import { getFloatingLayerOverlayZIndex } from '../../primitives/layer-stack';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Popover.Overlay - An optional backdrop rendered behind the popover.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
|
-
import { onDestroy
|
|
3
|
+
import { onDestroy } from 'svelte';
|
|
4
4
|
import {
|
|
5
5
|
setPopoverContext,
|
|
6
6
|
type PopoverCanonicalCloseReason,
|
|
@@ -21,10 +21,18 @@
|
|
|
21
21
|
* Provides context for Trigger and Content children.
|
|
22
22
|
*/
|
|
23
23
|
type PopoverRootProps = {
|
|
24
|
-
/**
|
|
24
|
+
/** Open state. Two-way by default — use `bind:open`. */
|
|
25
25
|
open?: boolean;
|
|
26
|
-
/** Initial open state for
|
|
26
|
+
/** Initial open state, for when `open` is not supplied. */
|
|
27
27
|
defaultOpen?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Opt into fully controlled state: the component stops writing back to `open` and
|
|
30
|
+
* only reports through `onOpenChange`, so the parent can reject a change by not
|
|
31
|
+
* flowing the new value back down. Off by default, because `bind:open` — the
|
|
32
|
+
* common case — needs the write-back to work at all. For one-off rejections
|
|
33
|
+
* prefer `details.cancel()`, which works in either mode.
|
|
34
|
+
*/
|
|
35
|
+
controlledOpen?: boolean;
|
|
28
36
|
/** Callback when open state changes. */
|
|
29
37
|
onOpenChange?: (open: boolean, details: PopoverOpenChangeDetails) => void;
|
|
30
38
|
/** Reference to the trigger element. Can be set manually or via Popover.Trigger. */
|
|
@@ -36,6 +44,7 @@
|
|
|
36
44
|
let {
|
|
37
45
|
open = $bindable(),
|
|
38
46
|
defaultOpen = false,
|
|
47
|
+
controlledOpen = false,
|
|
39
48
|
onOpenChange,
|
|
40
49
|
triggerRef = $bindable<HTMLElement | null>(null),
|
|
41
50
|
children
|
|
@@ -47,10 +56,13 @@
|
|
|
47
56
|
// Use function to capture initial value only (not reactive)
|
|
48
57
|
let isOpenInternal = $state((() => defaultOpen)());
|
|
49
58
|
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
// `open` wins whenever it is supplied — that covers both `bind:open` and a plain
|
|
60
|
+
// `open={...}` — and the internal state only carries the fully uncontrolled case.
|
|
61
|
+
// Controlled-ness is NOT inferred from `open` being defined: `bind:open={value}` and
|
|
62
|
+
// `open={value}` are indistinguishable at runtime, so inferring it silently broke
|
|
63
|
+
// every `bind:open` seeded with `false` — the popover would report the open and then
|
|
64
|
+
// drop it, never appearing at all. It is opt-in via `controlledOpen` instead.
|
|
65
|
+
const isOpen = $derived(controlledOpen ? Boolean(open) : (open ?? isOpenInternal));
|
|
54
66
|
|
|
55
67
|
function setOpenWithDetails(
|
|
56
68
|
value: boolean,
|
|
@@ -71,10 +83,10 @@
|
|
|
71
83
|
onOpenChange?.(value, details);
|
|
72
84
|
if (details.isCanceled) return;
|
|
73
85
|
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
if (
|
|
86
|
+
// Fully controlled: the parent owns the state and reacts in `onOpenChange`,
|
|
87
|
+
// flowing the value back down (or ignoring it to reject the change). Writing
|
|
88
|
+
// `open` here would locally override the parent's prop.
|
|
89
|
+
if (controlledOpen) return;
|
|
78
90
|
|
|
79
91
|
isOpenInternal = value;
|
|
80
92
|
open = value;
|
|
@@ -5,10 +5,18 @@ import { type PopoverOpenChangeDetails } from './context';
|
|
|
5
5
|
* Provides context for Trigger and Content children.
|
|
6
6
|
*/
|
|
7
7
|
type PopoverRootProps = {
|
|
8
|
-
/**
|
|
8
|
+
/** Open state. Two-way by default — use `bind:open`. */
|
|
9
9
|
open?: boolean;
|
|
10
|
-
/** Initial open state for
|
|
10
|
+
/** Initial open state, for when `open` is not supplied. */
|
|
11
11
|
defaultOpen?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Opt into fully controlled state: the component stops writing back to `open` and
|
|
14
|
+
* only reports through `onOpenChange`, so the parent can reject a change by not
|
|
15
|
+
* flowing the new value back down. Off by default, because `bind:open` — the
|
|
16
|
+
* common case — needs the write-back to work at all. For one-off rejections
|
|
17
|
+
* prefer `details.cancel()`, which works in either mode.
|
|
18
|
+
*/
|
|
19
|
+
controlledOpen?: boolean;
|
|
12
20
|
/** Callback when open state changes. */
|
|
13
21
|
onOpenChange?: (open: boolean, details: PopoverOpenChangeDetails) => void;
|
|
14
22
|
/** Reference to the trigger element. Can be set manually or via Popover.Trigger. */
|
|
@@ -20,6 +20,13 @@ export type FloatingOptions = {
|
|
|
20
20
|
shouldFlip?: boolean;
|
|
21
21
|
/** Boundary element for positioning constraints. */
|
|
22
22
|
boundaryElement?: Element | null;
|
|
23
|
+
/**
|
|
24
|
+
* Minimum gap kept between the floating element and the boundary edges, in
|
|
25
|
+
* pixels. Without it the panel sits flush against the viewport edge, which on
|
|
26
|
+
* a phone reads as clipped content; it also caps `--available-width/height`
|
|
27
|
+
* so a panel that shrinks to fit still leaves the gutter visible.
|
|
28
|
+
*/
|
|
29
|
+
collisionPadding?: number;
|
|
23
30
|
/** Callback when position is updated. */
|
|
24
31
|
onPositionUpdate?: (x: number, y: number, placement: FloatingPlacement) => void;
|
|
25
32
|
};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { computePosition, flip, shift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
|
|
2
|
+
/** Viewport gutter applied when no `collisionPadding` is given. */
|
|
3
|
+
const DEFAULT_COLLISION_PADDING = 8;
|
|
2
4
|
/**
|
|
3
5
|
* Converts extended placement syntax to Floating UI placement.
|
|
4
6
|
*/
|
|
@@ -102,11 +104,13 @@ export function floating(floatingElement, options) {
|
|
|
102
104
|
const offset = currentOptions.offset ?? 8;
|
|
103
105
|
const shouldFlip = currentOptions.shouldFlip ?? true;
|
|
104
106
|
const boundaryElement = currentOptions.boundaryElement || null;
|
|
107
|
+
const padding = currentOptions.collisionPadding ?? DEFAULT_COLLISION_PADDING;
|
|
105
108
|
return [
|
|
106
109
|
offsetMiddleware(offset),
|
|
107
|
-
...(shouldFlip ? [flip({ boundary: boundaryElement || undefined })] : []),
|
|
108
|
-
shift({ boundary: boundaryElement || undefined }),
|
|
110
|
+
...(shouldFlip ? [flip({ boundary: boundaryElement || undefined, padding })] : []),
|
|
111
|
+
shift({ boundary: boundaryElement || undefined, padding }),
|
|
109
112
|
size({
|
|
113
|
+
padding,
|
|
110
114
|
apply({ rects, availableWidth, availableHeight, elements, placement }) {
|
|
111
115
|
const floatingEl = elements.floating;
|
|
112
116
|
const clampedAvailableWidth = Math.max(0, availableWidth);
|
package/dist/primitives/index.js
CHANGED
|
@@ -216,7 +216,9 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
216
216
|
// the candidate list as the LAST entry, so a growing multi-character buffer can still
|
|
217
217
|
// confirm it. With nothing focused, this is a plain first-match-from-start search.
|
|
218
218
|
const currentIdx = getCurrentIndex();
|
|
219
|
-
const ordered = currentIdx === -1
|
|
219
|
+
const ordered = currentIdx === -1
|
|
220
|
+
? items
|
|
221
|
+
: [...items.slice(currentIdx + 1), ...items.slice(0, currentIdx + 1)];
|
|
220
222
|
const match = ordered.find((el) => {
|
|
221
223
|
// Prefer an explicit text value (e.g. Menu.Item's `textValue` exposed as
|
|
222
224
|
// `data-text-value`) over the rendered text content.
|