-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Virtualizer.tsx
320 lines (301 loc) · 8.77 KB
/
Virtualizer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** @jsxImportSource vue */
import {
ref,
onMounted,
defineComponent,
onUnmounted,
VNode,
watch,
ComponentOptionsMixin,
SlotsType,
ComponentOptionsWithObjectProps,
ComponentObjectPropsOptions,
PropType,
NativeElements,
} from "vue";
import {
SCROLL_IDLE,
UPDATE_SCROLL_EVENT,
UPDATE_SCROLL_END_EVENT,
UPDATE_VIRTUAL_STATE,
getOverscanedRange,
createVirtualStore,
ACTION_ITEMS_LENGTH_CHANGE,
getScrollSize,
ACTION_START_OFFSET_CHANGE,
} from "../core/store";
import { createResizer } from "../core/resizer";
import { createScroller } from "../core/scroller";
import { ScrollToIndexOpts } from "../core/types";
import { ListItem } from "./ListItem";
import { getKey } from "./utils";
import { microtask } from "../core/utils";
export interface VirtualizerHandle {
/**
* Get current scrollTop or scrollLeft.
*/
readonly scrollOffset: number;
/**
* Get current scrollHeight or scrollWidth.
*/
readonly scrollSize: number;
/**
* Get current offsetHeight or offsetWidth.
*/
readonly viewportSize: number;
/**
* Get item offset from start.
* @param index index of item
*/
getItemOffset(index: number): number;
/**
* Scroll to the item specified by index.
* @param index index of item
* @param opts options
*/
scrollToIndex(index: number, opts?: ScrollToIndexOpts): void;
/**
* Scroll to the given offset.
* @param offset offset from start
*/
scrollTo(offset: number): void;
/**
* Scroll by the given offset.
* @param offset offset from current position
*/
scrollBy(offset: number): void;
}
const props = {
/**
* The data items rendered by this component.
*/
data: { type: Array, required: true },
/**
* Number of items to render above/below the visible bounds of the list. You can increase to avoid showing blank items in fast scrolling.
* @defaultValue 4
*/
overscan: { type: Number, default: 4 },
/**
* Item size hint for unmeasured items. It will help to reduce scroll jump when items are measured if used properly.
*
* - If not set, initial item sizes will be automatically estimated from measured sizes. This is recommended for most cases.
* - If set, you can opt out estimation and use the value as initial item size.
*/
itemSize: Number,
/**
* While true is set, scroll position will be maintained from the end not usual start when items are added to/removed from start. It's recommended to set false if you add to/remove from mid/end of the list because it can cause unexpected behavior. This prop is useful for reverse infinite scrolling.
*/
shift: Boolean,
/**
* If true, rendered as a horizontally scrollable list. Otherwise rendered as a vertically scrollable list.
*/
horizontal: Boolean,
/**
* If you put an element before virtualizer, you have to define its height with this prop.
*/
startMargin: { type: Number, default: 0 },
/**
* A prop for SSR. If set, the specified amount of items will be mounted in the initial rendering regardless of the container size until hydrated.
*/
ssrCount: Number,
/**
* Reference to the scrollable element. The default will get the parent element of virtualizer.
*/
scrollRef: Object as PropType<HTMLElement>,
/**
* Component or element type for container element.
* @defaultValue "div"
*/
as: { type: String as PropType<keyof NativeElements>, default: "div" },
/**
* Component or element type for item element.
* @defaultValue "div"
*/
item: { type: String as PropType<keyof NativeElements>, default: "div" },
} satisfies ComponentObjectPropsOptions;
export const Virtualizer = /*#__PURE__*/ defineComponent({
props: props,
emits: ["scroll", "scrollEnd", "rangeChange"],
setup(props, { emit, expose, slots }) {
let isSSR = !!props.ssrCount;
const isHorizontal = props.horizontal;
const containerRef = ref<HTMLDivElement>();
const store = createVirtualStore(
props.data.length,
props.itemSize ?? 40,
props.ssrCount,
undefined,
!props.itemSize
);
const resizer = createResizer(store, isHorizontal);
const scroller = createScroller(store, isHorizontal);
const rerender = ref(store._getStateVersion());
const unsubscribeStore = store._subscribe(UPDATE_VIRTUAL_STATE, () => {
rerender.value = store._getStateVersion();
});
const unsubscribeOnScroll = store._subscribe(UPDATE_SCROLL_EVENT, () => {
emit("scroll", store._getScrollOffset());
});
const unsubscribeOnScrollEnd = store._subscribe(
UPDATE_SCROLL_END_EVENT,
() => {
emit("scrollEnd");
}
);
onMounted(() => {
isSSR = false;
microtask(() => {
const assignScrollableElement = (e: HTMLElement) => {
resizer._observeRoot(e);
scroller._observe(e);
};
if (props.scrollRef) {
// parent's ref doesn't exist when onMounted is called
assignScrollableElement(props.scrollRef!);
} else {
assignScrollableElement(containerRef.value!.parentElement!);
}
});
});
onUnmounted(() => {
unsubscribeStore();
unsubscribeOnScroll();
unsubscribeOnScrollEnd();
resizer._dispose();
scroller._dispose();
});
watch(
() => props.data.length,
(count) => {
store._update(ACTION_ITEMS_LENGTH_CHANGE, [count, props.shift]);
}
);
watch(
() => props.startMargin,
(value) => {
store._update(ACTION_START_OFFSET_CHANGE, value);
},
{ immediate: true }
);
watch(
[rerender, store._getJumpCount],
([, count], [, prevCount]) => {
if (count === prevCount) return;
scroller._fixScrollJump();
},
{ flush: "post" }
);
watch(
[rerender, store._getRange],
([, [start, end]], [, [prevStart, prevEnd]]) => {
if (prevStart === start && prevEnd === end) return;
emit("rangeChange", start, end);
},
{ flush: "post" }
);
expose({
get scrollOffset() {
return store._getScrollOffset();
},
get scrollSize() {
return getScrollSize(store);
},
get viewportSize() {
return store._getViewportSize();
},
getItemOffset: store._getItemOffset,
scrollToIndex: scroller._scrollToIndex,
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
} satisfies VirtualizerHandle);
return () => {
rerender.value;
const Element = props.as;
const ItemElement = props.item;
const count = props.data.length;
const [startIndex, endIndex] = store._getRange();
const scrollDirection = store._getScrollDirection();
const totalSize = store._getTotalSize();
const items: VNode[] = [];
for (
let [i, j] = getOverscanedRange(
startIndex,
endIndex,
props.overscan,
scrollDirection,
count
);
i <= j;
i++
) {
const e = slots.default(props.data![i]!)[0]! as VNode;
items.push(
<ListItem
key={getKey(e, i)}
_resizer={resizer._observeItem}
_index={i}
_offset={store._getItemOffset(i)}
_hide={store._isUnmeasuredItem(i)}
_children={e}
_isHorizontal={isHorizontal}
_isSSR={isSSR}
_as={ItemElement}
/>
);
}
return (
<Element
ref={containerRef}
style={{
// contain: "content",
overflowAnchor: "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
flex: "none", // flex style can break layout
position: "relative",
visibility: "hidden", // TODO replace with other optimization methods
width: isHorizontal ? totalSize + "px" : "100%",
height: isHorizontal ? "100%" : totalSize + "px",
pointerEvents: scrollDirection !== SCROLL_IDLE ? "none" : "auto",
}}
>
{items}
</Element>
);
};
},
} as ComponentOptionsWithObjectProps<
typeof props,
VirtualizerHandle,
{},
{},
{},
ComponentOptionsMixin,
ComponentOptionsMixin,
{
/**
* Callback invoked whenever scroll offset changes.
* @param offset Current scrollTop or scrollLeft.
*/
scroll: (offset: number) => void;
/**
* Callback invoked when scrolling stops.
*/
scrollEnd: () => void;
/**
* Callback invoked when visible items range changes.
*/
rangeChange: (
/**
* The start index of viewable items.
*/
startIndex: number,
/**
* The end index of viewable items.
*/
endIndex: number
) => void;
},
string,
{},
string,
SlotsType<{ default: any }>
>);