@grucloud/bau-ui 0.302.0 → 0.304.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/package.json +4 -4
- package/virtualList/index.d.ts +87 -0
- package/virtualList/index.js +1 -0
- package/virtualList/virtualList.js +498 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grucloud/bau-ui",
|
|
3
3
|
"description": "Components library built with Bau",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.304.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "BSD",
|
|
7
7
|
"keywords": [
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"directory": "bau-ui"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@grucloud/bau": "^0.
|
|
26
|
-
"@grucloud/bau-css": "^0.
|
|
25
|
+
"@grucloud/bau": "^0.304.0",
|
|
26
|
+
"@grucloud/bau-css": "^0.304.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@vitest/browser-playwright": "^5.1.2",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"vite": "^8.4.1",
|
|
35
35
|
"vitest": "^5.1.2"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "5d31e349e55a33dfa067fe7daba4c90d6f9a2c65"
|
|
38
38
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
declare module "@grucloud/bau-ui/virtualList" {
|
|
2
|
+
type DefaultDesignProps = import("../constants").DefaultDesignProps;
|
|
3
|
+
type ComponentOption = import("../bau-ui").ComponentOption;
|
|
4
|
+
type ReadonlyState<T> = import("@grucloud/bau").ReadonlyState<T>;
|
|
5
|
+
|
|
6
|
+
export type VirtualListProps<T = any> = {
|
|
7
|
+
// The items, a state of an array
|
|
8
|
+
items: ReadonlyState<T[]>;
|
|
9
|
+
// The element of an item, given the item itself: a changed item is
|
|
10
|
+
// replaced, items.val[i] = { ...item, done: true }, its row rendered again
|
|
11
|
+
Item: (item: T, index: number) => HTMLElement;
|
|
12
|
+
// The key of an item: its row is kept, with its state and focus, while it
|
|
13
|
+
// stays in range; the index by default
|
|
14
|
+
key?: (item: T, index: number) => unknown;
|
|
15
|
+
// The height of a row in px, the same for all: the fastest. Without it,
|
|
16
|
+
// the rows are measured once shown.
|
|
17
|
+
itemHeight?: number;
|
|
18
|
+
// The height of a row not measured yet; 40 by default
|
|
19
|
+
estimatedItemHeight?: number;
|
|
20
|
+
// Keeps the content in view still when the rows above it change height
|
|
21
|
+
// (size), or when items are added or removed above it (data); a chat
|
|
22
|
+
// stays at its end (end)
|
|
23
|
+
anchor?: Anchor;
|
|
24
|
+
// The rows rendered before and after those in view; 5 by default
|
|
25
|
+
overscan?: number;
|
|
26
|
+
// Called when the scroll comes near the end, to load the next page:
|
|
27
|
+
// once, then again after it went away
|
|
28
|
+
onEndReached?: () => void;
|
|
29
|
+
// Called when the scroll comes near the start, to load older items
|
|
30
|
+
onStartReached?: () => void;
|
|
31
|
+
// How near, a fraction of the height of the view; 0.5 by default
|
|
32
|
+
reachedThreshold?: number;
|
|
33
|
+
} & DefaultDesignProps &
|
|
34
|
+
import("@grucloud/bau").PropsAll<HTMLDivElement>;
|
|
35
|
+
|
|
36
|
+
export type Anchor = {
|
|
37
|
+
// The rows above the view change height; true by default
|
|
38
|
+
size?: boolean;
|
|
39
|
+
// Items added or removed above the view; false by default
|
|
40
|
+
data?: boolean;
|
|
41
|
+
// Opens at the end, and stays there while scrolled to it; "smooth":
|
|
42
|
+
// follows the items added and the last row growing smoothly; false by
|
|
43
|
+
// default
|
|
44
|
+
end?: boolean | "smooth";
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type ScrollToIndexInput = {
|
|
48
|
+
index: number;
|
|
49
|
+
// Animated; false by default
|
|
50
|
+
smooth?: boolean;
|
|
51
|
+
// Where the item lands: 0 at the top, 0.5 in the middle, 1 at the bottom
|
|
52
|
+
align?: number;
|
|
53
|
+
// Moved up by these px
|
|
54
|
+
offset?: number;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type VirtualListControl = {
|
|
58
|
+
// The component; the control drives the last list it created
|
|
59
|
+
VirtualList: <T>(props: VirtualListProps<T>) => HTMLDivElement;
|
|
60
|
+
// Resolved once the list is there, or the scroll stopped
|
|
61
|
+
scrollToIndex: (input: ScrollToIndexInput) => Promise<void>;
|
|
62
|
+
// Scrolls to offset px from the top: a position kept; resolved once there
|
|
63
|
+
scrollToOffset: (input: {
|
|
64
|
+
offset: number;
|
|
65
|
+
smooth?: boolean;
|
|
66
|
+
}) => Promise<void>;
|
|
67
|
+
// Stops a smooth scroll where it is
|
|
68
|
+
cancelScroll: () => void;
|
|
69
|
+
// Where the item at index is, measured or estimated, in px
|
|
70
|
+
itemLayout: (index: number) => { top: number; height: number };
|
|
71
|
+
isAtStart: ReadonlyState<boolean>;
|
|
72
|
+
isAtEnd: ReadonlyState<boolean>;
|
|
73
|
+
// The scroll position in px
|
|
74
|
+
scrollOffset: ReadonlyState<number>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
type Option = {
|
|
78
|
+
key?: (item: any, index: number) => unknown;
|
|
79
|
+
itemHeight?: number;
|
|
80
|
+
estimatedItemHeight?: number;
|
|
81
|
+
anchor?: Anchor;
|
|
82
|
+
overscan?: number;
|
|
83
|
+
reachedThreshold?: number;
|
|
84
|
+
} & ComponentOption;
|
|
85
|
+
|
|
86
|
+
export default function (context: any, option?: Option): VirtualListControl;
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./virtualList";
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
export default function (context, options = {}) {
|
|
2
|
+
const { bau, css, window } = context;
|
|
3
|
+
const { div, ul, li } = bau.tags;
|
|
4
|
+
|
|
5
|
+
const className = css`
|
|
6
|
+
display: block;
|
|
7
|
+
height: 20rem;
|
|
8
|
+
overflow-y: auto;
|
|
9
|
+
position: relative;
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
border-radius: var(--global-radius);
|
|
12
|
+
&.outline {
|
|
13
|
+
border: 1px solid var(--color-border-subtle);
|
|
14
|
+
}
|
|
15
|
+
&.solid {
|
|
16
|
+
background-color: var(--accent-lightest);
|
|
17
|
+
}
|
|
18
|
+
& > ul {
|
|
19
|
+
position: relative;
|
|
20
|
+
list-style: none;
|
|
21
|
+
margin: 0;
|
|
22
|
+
padding: 0;
|
|
23
|
+
/* A row taller than its estimate, not measured yet, does not move the
|
|
24
|
+
end of the scroll */
|
|
25
|
+
overflow: clip;
|
|
26
|
+
}
|
|
27
|
+
& > ul > li {
|
|
28
|
+
position: absolute;
|
|
29
|
+
left: 0;
|
|
30
|
+
right: 0;
|
|
31
|
+
box-sizing: border-box;
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
overflow: hidden;
|
|
35
|
+
/* A row is laid out and painted on its own */
|
|
36
|
+
contain: layout paint style;
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
// The key of an item: its index by default
|
|
41
|
+
const byIndex = (item, index) => index;
|
|
42
|
+
// An object read from a state is a proxy: the object itself, to compare
|
|
43
|
+
const toRaw = (item) => item?._bauRaw ?? item;
|
|
44
|
+
|
|
45
|
+
// The control of the list, returned with the component: one list per
|
|
46
|
+
// factory, the last one created. Where it is scrolled: states the page
|
|
47
|
+
// can show, a fade at the end for instance.
|
|
48
|
+
const isAtStart = bau.state(true);
|
|
49
|
+
const isAtEnd = bau.state(true);
|
|
50
|
+
// Not scrollTop: the scroll position in px
|
|
51
|
+
const scrollOffset = bau.state(0);
|
|
52
|
+
// The list driven: { element, topOf, heightAt, render, stopFollowing }
|
|
53
|
+
let list;
|
|
54
|
+
// The smooth scroll under way: { top, resolve }
|
|
55
|
+
let scrolled;
|
|
56
|
+
const settle = () => {
|
|
57
|
+
scrolled?.resolve();
|
|
58
|
+
scrolled = undefined;
|
|
59
|
+
};
|
|
60
|
+
// The item scrolled to, { index, align, offset }: scrolled to again when
|
|
61
|
+
// the rows measured move it, until the user scrolls or the items change
|
|
62
|
+
let target;
|
|
63
|
+
const release = () => {
|
|
64
|
+
target = undefined;
|
|
65
|
+
settle();
|
|
66
|
+
};
|
|
67
|
+
// Scrolls the list to top, smoothly or at once
|
|
68
|
+
const scrollTop = ({ top, smooth, resolve }) => {
|
|
69
|
+
const { element, render } = list;
|
|
70
|
+
if (smooth && Math.abs(top - element.scrollTop) >= 1) {
|
|
71
|
+
scrolled = { top, resolve };
|
|
72
|
+
element.scrollTo({ top, behavior: "smooth" });
|
|
73
|
+
} else {
|
|
74
|
+
element.scrollTo({ top });
|
|
75
|
+
render(false);
|
|
76
|
+
resolve();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// Where the list is scrolled to show the target
|
|
81
|
+
const topOfTarget = () => {
|
|
82
|
+
const { element, topOf, heightAt } = list;
|
|
83
|
+
const { index, align, offset } = target;
|
|
84
|
+
const { clientHeight, scrollHeight } = element;
|
|
85
|
+
return Math.max(
|
|
86
|
+
0,
|
|
87
|
+
Math.min(
|
|
88
|
+
scrollHeight - clientHeight,
|
|
89
|
+
topOf(index) - align * (clientHeight - heightAt(index)) - offset
|
|
90
|
+
)
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
function VirtualList({
|
|
95
|
+
size = options.size ?? "md",
|
|
96
|
+
variant = options.variant ?? "outline",
|
|
97
|
+
color = options.color ?? "neutral",
|
|
98
|
+
// The items, a state of an array
|
|
99
|
+
items,
|
|
100
|
+
// The element of an item: Item(item, index)
|
|
101
|
+
Item,
|
|
102
|
+
// The key of an item, key(item, index): its row is kept, with its state
|
|
103
|
+
// and focus, while it stays in range. Rendered again when the item is
|
|
104
|
+
// replaced by another object. Its measured height is kept by key.
|
|
105
|
+
key = options.key ?? byIndex,
|
|
106
|
+
// The height of a row in px, the same for all: the fastest. Without it,
|
|
107
|
+
// the rows are measured once shown.
|
|
108
|
+
itemHeight = options.itemHeight,
|
|
109
|
+
// The height of a row not measured yet
|
|
110
|
+
estimatedItemHeight = options.estimatedItemHeight ?? 40,
|
|
111
|
+
// Keeps the content in view still when the rows above it change height
|
|
112
|
+
// (size, by default), or when items are added or removed above it
|
|
113
|
+
// (data). With end, a list opens at its end, and stays there while it
|
|
114
|
+
// is scrolled to it: items added, a row growing, like a chat; with end
|
|
115
|
+
// "smooth", it follows them smoothly.
|
|
116
|
+
anchor = options.anchor ?? {},
|
|
117
|
+
// The rows rendered before and after those in view
|
|
118
|
+
overscan = options.overscan ?? 5,
|
|
119
|
+
// Called when the scroll comes near the end, to load the next page, or
|
|
120
|
+
// near the start, to load older items: once, then again after it went
|
|
121
|
+
// away, when the items were added or the user scrolled back
|
|
122
|
+
onEndReached,
|
|
123
|
+
onStartReached,
|
|
124
|
+
// How near: a fraction of the height of the view
|
|
125
|
+
reachedThreshold = options.reachedThreshold ?? 0.5,
|
|
126
|
+
...props
|
|
127
|
+
}) {
|
|
128
|
+
const fixed = itemHeight != null;
|
|
129
|
+
const {
|
|
130
|
+
size: anchorSize = true,
|
|
131
|
+
data: anchorData = false,
|
|
132
|
+
end: anchorEnd = false,
|
|
133
|
+
} = anchor;
|
|
134
|
+
// The rows in the page, by key: { element, item, index, top, size }
|
|
135
|
+
let rows = new Map();
|
|
136
|
+
// The rows rendered, from start to end, excluded
|
|
137
|
+
let start = 0;
|
|
138
|
+
let end = 0;
|
|
139
|
+
let observer;
|
|
140
|
+
let rowObserver;
|
|
141
|
+
|
|
142
|
+
// The layout: the number of items, their keys, and with measured rows,
|
|
143
|
+
// the top of each, offsets[count] the height of the list
|
|
144
|
+
let count = 0;
|
|
145
|
+
let keys = [];
|
|
146
|
+
let indexByKey = new Map();
|
|
147
|
+
let offsets = new Float64Array(1);
|
|
148
|
+
// The measured heights, by key
|
|
149
|
+
const heights = new Map();
|
|
150
|
+
// The key of a row element, to read its height
|
|
151
|
+
const keyOfElement = new WeakMap();
|
|
152
|
+
const heightOf = (k) => heights.get(k) ?? estimatedItemHeight;
|
|
153
|
+
// Past the last item: the end of the list
|
|
154
|
+
const topOf = (index) =>
|
|
155
|
+
fixed ? index * itemHeight : offsets[Math.min(index, count)];
|
|
156
|
+
const heightAt = (index) => (fixed ? itemHeight : heightOf(keys[index]));
|
|
157
|
+
const totalHeight = () => topOf(count);
|
|
158
|
+
// The index of the row at y, by bisection on the tops
|
|
159
|
+
const indexAt = (y) => {
|
|
160
|
+
if (fixed) return Math.floor(y / itemHeight);
|
|
161
|
+
let low = 0;
|
|
162
|
+
let high = count;
|
|
163
|
+
while (low < high) {
|
|
164
|
+
const middle = (low + high) >> 1;
|
|
165
|
+
if (offsets[middle + 1] <= y) low = middle + 1;
|
|
166
|
+
else high = middle;
|
|
167
|
+
}
|
|
168
|
+
return low;
|
|
169
|
+
};
|
|
170
|
+
// The tops of the rows from the one at index: the rows above did not
|
|
171
|
+
// change. The array is kept, grown when too small: a list of 100,000
|
|
172
|
+
// rows would allocate 800KB on each change.
|
|
173
|
+
const placeRows = (from = 0) => {
|
|
174
|
+
if (fixed) return;
|
|
175
|
+
if (offsets.length < count + 1) {
|
|
176
|
+
const grown = new Float64Array(Math.ceil((count + 1) * 1.5));
|
|
177
|
+
grown.set(offsets.subarray(0, from + 1));
|
|
178
|
+
offsets = grown;
|
|
179
|
+
}
|
|
180
|
+
for (let index = from; index < count; index++)
|
|
181
|
+
offsets[index + 1] = offsets[index] + heightOf(keys[index]);
|
|
182
|
+
};
|
|
183
|
+
// From the items: their keys, needed to measure or to anchor by data
|
|
184
|
+
const layout = () => {
|
|
185
|
+
// The array itself: reading its items through the proxy of the state
|
|
186
|
+
// would make a proxy of each
|
|
187
|
+
const all = toRaw(items.val);
|
|
188
|
+
count = all.length;
|
|
189
|
+
if (fixed && !anchorData) return;
|
|
190
|
+
// Kept from a layout to the next: no array, no map allocated
|
|
191
|
+
keys.length = count;
|
|
192
|
+
indexByKey.clear();
|
|
193
|
+
for (let index = 0; index < count; index++) {
|
|
194
|
+
const k = key(all[index], index);
|
|
195
|
+
keys[index] = k;
|
|
196
|
+
indexByKey.set(k, index);
|
|
197
|
+
}
|
|
198
|
+
// The heights of the items gone: dropped once they are as many as the
|
|
199
|
+
// items, a log removing its old lines would keep them all
|
|
200
|
+
if (heights.size > count + 1000)
|
|
201
|
+
for (const k of heights.keys()) indexByKey.has(k) || heights.delete(k);
|
|
202
|
+
placeRows();
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// The first row in view and how far its top is from the scroll: with
|
|
206
|
+
// anchor.data, it stays there when the items change
|
|
207
|
+
let anchorKey;
|
|
208
|
+
let anchorDistance = 0;
|
|
209
|
+
let anchorPending = false;
|
|
210
|
+
// With anchor.end, following the end: scrolled to it at the last
|
|
211
|
+
// render, or on the way to it, smoothly (chasing). Scrolled to the end
|
|
212
|
+
// again once in the document, at once.
|
|
213
|
+
const smoothEnd = anchorEnd === "smooth";
|
|
214
|
+
let following = !!anchorEnd;
|
|
215
|
+
let chasing = false;
|
|
216
|
+
let endPending = !!anchorEnd;
|
|
217
|
+
let opened = false;
|
|
218
|
+
const toEnd = (smooth) => {
|
|
219
|
+
const top = element.scrollHeight - element.clientHeight;
|
|
220
|
+
if (!smooth) element.scrollTop = top;
|
|
221
|
+
else if (Math.abs(top - element.scrollTop) >= 1) {
|
|
222
|
+
chasing = true;
|
|
223
|
+
element.scrollTo({ top, behavior: "smooth" });
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
// The user scrolls, or the page: no longer following
|
|
227
|
+
const stopFollowing = () => {
|
|
228
|
+
chasing = false;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const setRow = (row, index) => {
|
|
232
|
+
const top = topOf(index);
|
|
233
|
+
if (row.top !== top) {
|
|
234
|
+
row.top = top;
|
|
235
|
+
row.element.style.top = `${top}px`;
|
|
236
|
+
}
|
|
237
|
+
if (row.index !== index) {
|
|
238
|
+
row.index = index;
|
|
239
|
+
row.element.setAttribute("aria-posinset", index + 1);
|
|
240
|
+
}
|
|
241
|
+
if (row.size !== count) {
|
|
242
|
+
row.size = count;
|
|
243
|
+
row.element.setAttribute("aria-setsize", count);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
let endReached = false;
|
|
248
|
+
let startReached = false;
|
|
249
|
+
const reached = () => {
|
|
250
|
+
const { scrollTop: top, clientHeight, scrollHeight } = element;
|
|
251
|
+
// Not laid out yet
|
|
252
|
+
if (!clientHeight) return;
|
|
253
|
+
const near = reachedThreshold * clientHeight;
|
|
254
|
+
const wasEnd = endReached;
|
|
255
|
+
const wasStart = startReached;
|
|
256
|
+
endReached = scrollHeight - top - clientHeight <= near;
|
|
257
|
+
startReached = top <= near;
|
|
258
|
+
if (endReached && !wasEnd) onEndReached?.();
|
|
259
|
+
if (startReached && !wasStart) onStartReached?.();
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const render = (force) => {
|
|
263
|
+
renderRows(force);
|
|
264
|
+
(onEndReached || onStartReached) && reached();
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// Shows the rows in view. Again only when they are not all rendered,
|
|
268
|
+
// or when the items or the heights changed (force).
|
|
269
|
+
const renderRows = (force) => {
|
|
270
|
+
if (anchorPending) {
|
|
271
|
+
anchorPending = false;
|
|
272
|
+
const index = indexByKey.get(anchorKey);
|
|
273
|
+
if (index !== undefined)
|
|
274
|
+
element.scrollTop = topOf(index) - anchorDistance;
|
|
275
|
+
}
|
|
276
|
+
if (endPending && element.isConnected) {
|
|
277
|
+
endPending = false;
|
|
278
|
+
toEnd(opened && smoothEnd);
|
|
279
|
+
opened = true;
|
|
280
|
+
}
|
|
281
|
+
const { scrollTop: top, clientHeight, scrollHeight } = element;
|
|
282
|
+
const atEnd = top + clientHeight >= scrollHeight - 1;
|
|
283
|
+
if (atEnd) {
|
|
284
|
+
following = true;
|
|
285
|
+
chasing = false;
|
|
286
|
+
} else if (!chasing) following = false;
|
|
287
|
+
scrollOffset.val = top;
|
|
288
|
+
isAtStart.val = top <= 0;
|
|
289
|
+
isAtEnd.val = atEnd;
|
|
290
|
+
const first = indexAt(top);
|
|
291
|
+
const last = indexAt(top + clientHeight) + 1;
|
|
292
|
+
if (anchorData && first < count) {
|
|
293
|
+
anchorKey = keys[first];
|
|
294
|
+
anchorDistance = topOf(first) - top;
|
|
295
|
+
}
|
|
296
|
+
if (!force && first >= start && last <= end) return;
|
|
297
|
+
start = Math.max(0, first - overscan);
|
|
298
|
+
end = last + overscan;
|
|
299
|
+
|
|
300
|
+
// The items themselves, like bau.loop: an item read through the proxy
|
|
301
|
+
// of the state is replaced in the array by a proxy, kept as long as
|
|
302
|
+
// the array, 80 bytes for each row ever shown. A changed item is
|
|
303
|
+
// replaced, not mutated: its row is rendered again.
|
|
304
|
+
const all = toRaw(items.val);
|
|
305
|
+
const stop = Math.min(end, count);
|
|
306
|
+
const kept = new Map();
|
|
307
|
+
const inRange = [];
|
|
308
|
+
for (let index = start; index < stop; index++) {
|
|
309
|
+
const item = all[index];
|
|
310
|
+
const k = key(item, index);
|
|
311
|
+
let row = rows.get(k);
|
|
312
|
+
if (!row || toRaw(row.item) !== toRaw(item)) {
|
|
313
|
+
// New, or its item replaced: a new element
|
|
314
|
+
row?.element.remove();
|
|
315
|
+
const rowEl = li(
|
|
316
|
+
fixed ? { style: `height: ${itemHeight}px` } : {},
|
|
317
|
+
Item(item, index)
|
|
318
|
+
);
|
|
319
|
+
if (!fixed) {
|
|
320
|
+
keyOfElement.set(rowEl, k);
|
|
321
|
+
rowObserver.observe(rowEl);
|
|
322
|
+
}
|
|
323
|
+
row = { element: rowEl, item };
|
|
324
|
+
}
|
|
325
|
+
setRow(row, index);
|
|
326
|
+
kept.set(k, row);
|
|
327
|
+
inRange.push(row);
|
|
328
|
+
}
|
|
329
|
+
// The rows out of range are removed, but the focused one: removing it
|
|
330
|
+
// would move the focus to the page
|
|
331
|
+
const focused = window.document.activeElement;
|
|
332
|
+
const before = [];
|
|
333
|
+
const after = [];
|
|
334
|
+
for (const [k, row] of rows) {
|
|
335
|
+
if (kept.has(k)) continue;
|
|
336
|
+
if (row.element.contains(focused)) {
|
|
337
|
+
kept.set(k, row);
|
|
338
|
+
(row.index < start ? before : after).push(row);
|
|
339
|
+
} else {
|
|
340
|
+
row.element.remove();
|
|
341
|
+
fixed || rowObserver.unobserve(row.element);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
rows = kept;
|
|
345
|
+
// In the order of the items, for the keyboard and the screen readers.
|
|
346
|
+
// A row already in place is not moved: it keeps its focus.
|
|
347
|
+
let next = listEl.firstChild;
|
|
348
|
+
for (const { element: rowEl } of [...before, ...inRange, ...after]) {
|
|
349
|
+
if (rowEl === next) next = next.nextSibling;
|
|
350
|
+
else listEl.insertBefore(rowEl, next);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// The heights of the rows shown: the rows below move, and with
|
|
355
|
+
// anchor.size, the scroll follows the rows above the view
|
|
356
|
+
const onRowsResized = (entries) => {
|
|
357
|
+
// The first row changed: the tops above it stay
|
|
358
|
+
let from = count;
|
|
359
|
+
let shift = 0;
|
|
360
|
+
const top = element.scrollTop;
|
|
361
|
+
for (const { target, borderBoxSize } of entries) {
|
|
362
|
+
const k = keyOfElement.get(target);
|
|
363
|
+
const height = borderBoxSize[0].blockSize;
|
|
364
|
+
const before = heightOf(k);
|
|
365
|
+
if (!target.isConnected || height == before) continue;
|
|
366
|
+
heights.set(k, height);
|
|
367
|
+
// Its item removed since: all the tops again
|
|
368
|
+
const index = indexByKey.get(k);
|
|
369
|
+
if (index === undefined) {
|
|
370
|
+
from = 0;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (index < from) from = index;
|
|
374
|
+
if (anchorSize && topOf(index) < top) shift += height - before;
|
|
375
|
+
}
|
|
376
|
+
if (from == count) return;
|
|
377
|
+
placeRows(from);
|
|
378
|
+
listEl.style.height = `${totalHeight()}px`;
|
|
379
|
+
// Scrolled to an item: kept in view where it was asked to be
|
|
380
|
+
if (target) scrolled || (element.scrollTop = topOfTarget());
|
|
381
|
+
else if (anchorEnd && following) toEnd(smoothEnd);
|
|
382
|
+
else if (shift) element.scrollTop = top + shift;
|
|
383
|
+
render(true);
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const listEl = ul({
|
|
387
|
+
// Follows the items: their number sets the height, and the rows in
|
|
388
|
+
// view are rendered again once the update is done, outside of it: the
|
|
389
|
+
// items read by Item do not subscribe this binding
|
|
390
|
+
style: () => {
|
|
391
|
+
layout();
|
|
392
|
+
target = undefined;
|
|
393
|
+
anchorPending = anchorData && anchorKey !== undefined;
|
|
394
|
+
endPending ||= anchorEnd && following;
|
|
395
|
+
queueMicrotask(() => render(true));
|
|
396
|
+
return `height: ${totalHeight()}px`;
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
if (!fixed) rowObserver = new window.ResizeObserver(onRowsResized);
|
|
401
|
+
|
|
402
|
+
const element = div(
|
|
403
|
+
{
|
|
404
|
+
// Scrolled by the keyboard: arrows, page up and down, home, end
|
|
405
|
+
tabindex: 0,
|
|
406
|
+
...props,
|
|
407
|
+
class: [
|
|
408
|
+
"virtualList",
|
|
409
|
+
className,
|
|
410
|
+
color,
|
|
411
|
+
variant,
|
|
412
|
+
size,
|
|
413
|
+
options.class,
|
|
414
|
+
props.class,
|
|
415
|
+
],
|
|
416
|
+
onscroll: () => render(false),
|
|
417
|
+
bauMounted: () => {
|
|
418
|
+
// The height of the list, known once in the document, and when it
|
|
419
|
+
// changes
|
|
420
|
+
observer = new window.ResizeObserver(() => render(false));
|
|
421
|
+
observer.observe(element);
|
|
422
|
+
},
|
|
423
|
+
bauUnmounted: () => {
|
|
424
|
+
observer.disconnect();
|
|
425
|
+
rowObserver?.disconnect();
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
listEl
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
// A smooth scroll is done once there: the end of an earlier scroll, an
|
|
432
|
+
// instant one, can come after it started. The rows measured on the way
|
|
433
|
+
// may have moved the target: scrolled to it again.
|
|
434
|
+
element.addEventListener("scrollend", () => {
|
|
435
|
+
if (!scrolled || Math.abs(element.scrollTop - scrolled.top) >= 1) return;
|
|
436
|
+
settle();
|
|
437
|
+
if (target) element.scrollTop = topOfTarget();
|
|
438
|
+
});
|
|
439
|
+
// The user scrolling stops a scroll to an item, and the following
|
|
440
|
+
const stop = () => {
|
|
441
|
+
stopFollowing();
|
|
442
|
+
release();
|
|
443
|
+
};
|
|
444
|
+
for (const type of ["wheel", "touchstart", "pointerdown", "keydown"])
|
|
445
|
+
element.addEventListener(type, stop, { passive: true });
|
|
446
|
+
|
|
447
|
+
list = { element, topOf, heightAt, render, stopFollowing };
|
|
448
|
+
return element;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Scrolls to the item at index: at the top of the list (align 0), its
|
|
452
|
+
// middle (0.5) or its bottom (1), moved up by offset px. Resolved once
|
|
453
|
+
// the list is there.
|
|
454
|
+
const scrollToIndex = ({ index, smooth = false, align = 0, offset = 0 }) =>
|
|
455
|
+
new Promise((resolve) => {
|
|
456
|
+
list.stopFollowing();
|
|
457
|
+
settle();
|
|
458
|
+
target = { index, align, offset };
|
|
459
|
+
scrollTop({ top: topOfTarget(), smooth, resolve });
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
// Scrolls to offset px from the top: a position kept, of a thread
|
|
463
|
+
// shown again for instance. Resolved once there.
|
|
464
|
+
const scrollToOffset = ({ offset, smooth = false }) =>
|
|
465
|
+
new Promise((resolve) => {
|
|
466
|
+
const { element, stopFollowing } = list;
|
|
467
|
+
stopFollowing();
|
|
468
|
+
release();
|
|
469
|
+
const max = element.scrollHeight - element.clientHeight;
|
|
470
|
+
scrollTop({ top: Math.max(0, Math.min(max, offset)), smooth, resolve });
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// Stops a smooth scroll where it is
|
|
474
|
+
const cancelScroll = () => {
|
|
475
|
+
const { element, stopFollowing } = list;
|
|
476
|
+
element.scrollTo({ top: element.scrollTop });
|
|
477
|
+
stopFollowing();
|
|
478
|
+
release();
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
// Where the item at index is, measured or estimated: { top, height } in
|
|
482
|
+
// px, for a minimap, or to find a text in it
|
|
483
|
+
const itemLayout = (index) => ({
|
|
484
|
+
top: list.topOf(index),
|
|
485
|
+
height: list.heightAt(index),
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
return {
|
|
489
|
+
VirtualList,
|
|
490
|
+
scrollToIndex,
|
|
491
|
+
scrollToOffset,
|
|
492
|
+
cancelScroll,
|
|
493
|
+
itemLayout,
|
|
494
|
+
isAtStart,
|
|
495
|
+
isAtEnd,
|
|
496
|
+
scrollOffset,
|
|
497
|
+
};
|
|
498
|
+
}
|