virtua 0.50.2 → 0.50.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lib/angular/VList.d.ts +7 -7
  2. package/lib/angular/Virtualizer.d.ts +11 -11
  3. package/lib/angular/WindowVirtualizer.d.ts +13 -9
  4. package/lib/angular/index.js +440 -434
  5. package/lib/angular/index.js.map +1 -1
  6. package/lib/core/index.cjs +342 -335
  7. package/lib/core/index.cjs.map +1 -1
  8. package/lib/core/index.d.ts +5 -2
  9. package/lib/core/index.js +326 -318
  10. package/lib/core/index.js.map +1 -1
  11. package/lib/core/layouts/types.d.ts +1 -0
  12. package/lib/core/observer.d.ts +1 -0
  13. package/lib/core/scroll-to.d.ts +1 -0
  14. package/lib/core/types.d.ts +1 -5
  15. package/lib/index.cjs +534 -532
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.js +496 -494
  18. package/lib/index.js.map +1 -1
  19. package/lib/solid/VList.d.ts +1 -1
  20. package/lib/solid/Virtualizer.d.ts +4 -0
  21. package/lib/solid/WindowVirtualizer.d.ts +4 -0
  22. package/lib/solid/index.cjs +366 -378
  23. package/lib/solid/index.cjs.map +1 -1
  24. package/lib/solid/index.js +373 -386
  25. package/lib/solid/index.js.map +1 -1
  26. package/lib/solid/index.jsx +383 -368
  27. package/lib/solid/index.jsx.map +1 -1
  28. package/lib/svelte/ListItem.svelte +2 -2
  29. package/lib/svelte/Virtualizer.svelte +22 -32
  30. package/lib/svelte/WindowVirtualizer.svelte +22 -21
  31. package/lib/svelte/WindowVirtualizer.type.d.ts +4 -0
  32. package/lib/svelte/utils.js.map +1 -1
  33. package/lib/vue/WindowVirtualizer.d.ts +4 -0
  34. package/lib/vue/index.cjs +366 -385
  35. package/lib/vue/index.cjs.map +1 -1
  36. package/lib/vue/index.js +342 -361
  37. package/lib/vue/index.js.map +1 -1
  38. package/package.json +30 -29
  39. /package/lib/core/{resizer.d.ts → driver.d.ts} +0 -0
  40. /package/lib/core/{scroller.d.ts → layouts/list.d.ts} +0 -0
@@ -7,9 +7,12 @@
7
7
  UPDATE_SCROLL_END_EVENT,
8
8
  UPDATE_SCROLL_EVENT,
9
9
  UPDATE_VIRTUAL_STATE,
10
- createResizer,
11
- createScroller,
10
+ createContainerDriver,
11
+ scrollTo as _scrollTo,
12
+ scrollBy as _scrollBy,
13
+ scrollToIndex as _scrollToIndex,
12
14
  createVirtualStore,
15
+ createListLayout,
13
16
  getScrollSize as _getScrollSize,
14
17
  sort,
15
18
  } from "../core/index.js";
@@ -42,15 +45,9 @@
42
45
  onscrollend,
43
46
  }: Props = $props();
44
47
 
45
- const store = createVirtualStore(
46
- data.length,
47
- itemSize,
48
- ssrCount,
49
- cache,
50
- !itemSize,
51
- );
52
- const resizer = createResizer(store, horizontal);
53
- const scroller = createScroller(store, horizontal);
48
+ const layout = createListLayout(data.length, itemSize, cache);
49
+ const store = createVirtualStore(layout, ssrCount);
50
+ const driver = createContainerDriver(store, horizontal);
54
51
  store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
55
52
  stateVersion = store.$getStateVersion();
56
53
  });
@@ -70,7 +67,7 @@
70
67
  let range = $derived(stateVersion && store.$getRange(bufferSize));
71
68
  let isScrolling = $derived(stateVersion && store.$isScrolling());
72
69
  let totalSize = $derived(stateVersion && store.$getTotalSize());
73
- let negative = $derived(stateVersion && scroller.$isNegative());
70
+ let negative = $derived(stateVersion && driver.$isNegative());
74
71
 
75
72
  let indexes = $derived.by(() => {
76
73
  // https://github.com/inokawa/virtua/pull/847
@@ -107,15 +104,7 @@
107
104
  tick().then(() => {
108
105
  // https://github.com/inokawa/virtua/pull/914
109
106
  if (unmounted) return;
110
- const assignRef = (scrollable: HTMLElement) => {
111
- resizer.$observeRoot(scrollable);
112
- scroller.$observe(container, scrollable);
113
- };
114
- if (scrollRef) {
115
- assignRef(scrollRef);
116
- } else {
117
- assignRef(container.parentElement!);
118
- }
107
+ driver.$observe(container, scrollRef);
119
108
  });
120
109
  return () => {
121
110
  unmounted = true;
@@ -123,8 +112,7 @@
123
112
  });
124
113
  onDestroy(() => {
125
114
  store.$dispose();
126
- resizer.$dispose();
127
- scroller.$dispose();
115
+ driver.$dispose();
128
116
  });
129
117
 
130
118
  $effect.pre(() => {
@@ -143,11 +131,11 @@
143
131
  $effect(() => {
144
132
  if (prevStateVersion === stateVersion) return;
145
133
  prevStateVersion = stateVersion;
146
- scroller.$fixScrollJump();
134
+ driver.$fixScrollJump();
147
135
  });
148
136
 
149
137
  export const getCache =
150
- store.$getCacheSnapshot satisfies VirtualizerHandle["getCache"] as VirtualizerHandle["getCache"];
138
+ layout.$snapshot satisfies VirtualizerHandle["getCache"] as VirtualizerHandle["getCache"];
151
139
  export const getScrollOffset =
152
140
  store.$getScrollOffset satisfies VirtualizerHandle["getScrollOffset"] as VirtualizerHandle["getScrollOffset"];
153
141
  export const getScrollSize = (() =>
@@ -162,12 +150,14 @@
162
150
  store.$getItemOffset satisfies VirtualizerHandle["getItemOffset"] as VirtualizerHandle["getItemOffset"];
163
151
  export const getItemSize =
164
152
  store.$getItemSize satisfies VirtualizerHandle["getItemSize"] as VirtualizerHandle["getItemSize"];
165
- export const scrollToIndex =
166
- scroller.$scrollToIndex satisfies VirtualizerHandle["scrollToIndex"] as VirtualizerHandle["scrollToIndex"];
167
- export const scrollTo =
168
- scroller.$scrollTo satisfies VirtualizerHandle["scrollTo"] as VirtualizerHandle["scrollTo"];
169
- export const scrollBy =
170
- scroller.$scrollBy satisfies VirtualizerHandle["scrollBy"] as VirtualizerHandle["scrollBy"];
153
+ export const scrollToIndex: VirtualizerHandle["scrollToIndex"] = (
154
+ index,
155
+ opts,
156
+ ) => _scrollToIndex(driver, store, index, opts);
157
+ export const scrollTo: VirtualizerHandle["scrollTo"] = (offset) =>
158
+ _scrollTo(driver, offset);
159
+ export const scrollBy: VirtualizerHandle["scrollBy"] = (offset) =>
160
+ _scrollBy(driver, store, offset);
171
161
 
172
162
  let containerStyle = $derived(
173
163
  styleToString({
@@ -198,7 +188,7 @@
198
188
  hide={stateVersion && store.$isUnmeasuredItem(index)}
199
189
  {horizontal}
200
190
  {isSSR}
201
- resizer={resizer.$observeItem}
191
+ resizer={driver.$observeItem}
202
192
  itemProps={itemProps?.({ item, index })}
203
193
  />
204
194
  {/each}
@@ -7,9 +7,10 @@
7
7
  UPDATE_SCROLL_EVENT,
8
8
  UPDATE_VIRTUAL_STATE,
9
9
  createVirtualStore,
10
+ createListLayout,
10
11
  getScrollSize as _getScrollSize,
11
- createWindowResizer,
12
- createWindowScroller,
12
+ createWindowDriver,
13
+ scrollToIndex as _scrollToIndex,
13
14
  } from "../core/index.js";
14
15
  import { defaultGetKey, styleToString } from "./utils.js";
15
16
  import ListItem from "./ListItem.svelte";
@@ -25,6 +26,7 @@
25
26
  getKey = defaultGetKey,
26
27
  bufferSize,
27
28
  itemSize,
29
+ ssrCount,
28
30
  shift = false,
29
31
  horizontal = false,
30
32
  cache,
@@ -33,15 +35,9 @@
33
35
  onscrollend,
34
36
  }: Props = $props();
35
37
 
36
- const store = createVirtualStore(
37
- data.length,
38
- itemSize,
39
- undefined,
40
- cache,
41
- !itemSize,
42
- );
43
- const resizer = createWindowResizer(store, horizontal);
44
- const scroller = createWindowScroller(store, horizontal);
38
+ const layout = createListLayout(data.length, itemSize, cache);
39
+ const store = createVirtualStore(layout, ssrCount);
40
+ const driver = createWindowDriver(store, horizontal);
45
41
  store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
46
42
  stateVersion = store.$getStateVersion();
47
43
  });
@@ -53,6 +49,8 @@
53
49
  onscrollend && onscrollend();
54
50
  });
55
51
 
52
+ let isSSR = $state(!!ssrCount);
53
+
56
54
  let containerRef: HTMLDivElement | undefined = $state();
57
55
 
58
56
  let stateVersion: StateVersion = $state(store.$getStateVersion());
@@ -60,7 +58,7 @@
60
58
  let range = $derived(stateVersion && store.$getRange(bufferSize));
61
59
  let isScrolling = $derived(stateVersion && store.$isScrolling());
62
60
  let totalSize = $derived(stateVersion && store.$getTotalSize());
63
- let negative = $derived(stateVersion && scroller.$isNegative());
61
+ let negative = $derived(stateVersion && driver.$isNegative());
64
62
 
65
63
  let indexes = $derived.by(() => {
66
64
  // https://github.com/inokawa/virtua/pull/847
@@ -77,13 +75,13 @@
77
75
  });
78
76
 
79
77
  onMount(() => {
80
- resizer.$observeRoot(containerRef!);
81
- scroller.$observe(containerRef!);
78
+ isSSR = false;
79
+
80
+ driver.$observe(containerRef!);
82
81
  });
83
82
  onDestroy(() => {
84
83
  store.$dispose();
85
- resizer.$dispose();
86
- scroller.$dispose();
84
+ driver.$dispose();
87
85
  });
88
86
 
89
87
  $effect.pre(() => {
@@ -96,11 +94,11 @@
96
94
  $effect(() => {
97
95
  if (prevStateVersion === stateVersion) return;
98
96
  prevStateVersion = stateVersion;
99
- scroller.$fixScrollJump();
97
+ driver.$fixScrollJump();
100
98
  });
101
99
 
102
100
  export const getCache =
103
- store.$getCacheSnapshot satisfies WindowVirtualizerHandle["getCache"] as WindowVirtualizerHandle["getCache"];
101
+ layout.$snapshot satisfies WindowVirtualizerHandle["getCache"] as WindowVirtualizerHandle["getCache"];
104
102
  export const getScrollOffset =
105
103
  store.$getScrollOffset satisfies WindowVirtualizerHandle["getScrollOffset"] as WindowVirtualizerHandle["getScrollOffset"];
106
104
  export const getViewportSize =
@@ -111,8 +109,10 @@
111
109
  store.$getItemOffset satisfies WindowVirtualizerHandle["getItemOffset"] as WindowVirtualizerHandle["getItemOffset"];
112
110
  export const getItemSize =
113
111
  store.$getItemSize satisfies WindowVirtualizerHandle["getItemSize"] as WindowVirtualizerHandle["getItemSize"];
114
- export const scrollToIndex =
115
- scroller.$scrollToIndex satisfies WindowVirtualizerHandle["scrollToIndex"] as WindowVirtualizerHandle["scrollToIndex"];
112
+ export const scrollToIndex: WindowVirtualizerHandle["scrollToIndex"] = (
113
+ index,
114
+ opts,
115
+ ) => _scrollToIndex(driver, store, index, opts);
116
116
 
117
117
  let containerStyle = $derived(
118
118
  styleToString({
@@ -142,7 +142,8 @@
142
142
  offset={stateVersion && store.$getItemOffset(index, negative)}
143
143
  hide={stateVersion && store.$isUnmeasuredItem(index)}
144
144
  {horizontal}
145
- resizer={resizer.$observeItem}
145
+ {isSSR}
146
+ resizer={driver.$observeItem}
146
147
  />
147
148
  {/each}
148
149
  </div>
@@ -29,6 +29,10 @@ export interface WindowVirtualizerProps<T> {
29
29
  * - If set, you can opt out estimation and use the value as initial item size.
30
30
  */
31
31
  itemSize?: number;
32
+ /**
33
+ * 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. The minimum value is 0.
34
+ */
35
+ ssrCount?: number;
32
36
  /**
33
37
  * 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.
34
38
  */
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":[],"sources":["../../src/svelte/utils.ts"],"sourcesContent":["export const styleToString = (\n obj: Record<string, string | undefined>,\n): string => {\n return Object.keys(obj).reduce((acc, k) => {\n const value = obj[k];\n if (value == null) {\n return acc;\n }\n return acc + `${k}:${value};`;\n }, \"\");\n};\n\nexport const defaultGetKey = (_data: unknown, i: number) => \"_\" + i;\n\n/**\n * @internal\n */\nexport type ItemAttrs = {\n [key: string]: any;\n style?: Record<string, string | undefined>;\n class?: string;\n};\n\n/**\n * A function that provides properties/attributes for item element\n */\nexport type ItemProps<T = unknown> = (payload: {\n item: T;\n index: number;\n}) => ItemAttrs | undefined;\n"],"mappings":";AAAA,IAAa,iBACX,QACW;CACX,OAAO,OAAO,KAAK,GAAG,EAAE,QAAQ,KAAK,MAAM;EACzC,MAAM,QAAQ,IAAI;EAClB,IAAI,SAAS,MACX,OAAO;EAET,OAAO,MAAM,GAAG,EAAE,GAAG,MAAM;CAC7B,GAAG,EAAE;AACP;AAEA,IAAa,iBAAiB,OAAgB,MAAc,MAAM"}
1
+ {"version":3,"file":"utils.js","names":[],"sources":["../../src/svelte/utils.ts"],"sourcesContent":["export const styleToString = (\n obj: Record<string, string | undefined>,\n): string => {\n return Object.keys(obj).reduce((acc, k) => {\n const value = obj[k];\n if (value == null) {\n return acc;\n }\n return acc + `${k}:${value};`;\n }, \"\");\n};\n\nexport const defaultGetKey = (_data: unknown, i: number) => \"_\" + i;\n\n/**\n * @internal\n */\nexport type ItemAttrs = {\n [key: string]: any;\n style?: Record<string, string | undefined>;\n class?: string;\n};\n\n/**\n * A function that provides properties/attributes for item element\n */\nexport type ItemProps<T = unknown> = (payload: {\n item: T;\n index: number;\n}) => ItemAttrs | undefined;\n"],"mappings":";AAAA,IAAa,iBACX,QACW;CACX,OAAO,OAAO,KAAK,GAAG,CAAC,CAAC,QAAQ,KAAK,MAAM;EACzC,MAAM,QAAQ,IAAI;EAClB,IAAI,SAAS,MACX,OAAO;EAET,OAAO,MAAM,GAAG,EAAE,GAAG,MAAM;CAC7B,GAAG,EAAE;AACP;AAEA,IAAa,iBAAiB,OAAgB,MAAc,MAAM"}
@@ -20,6 +20,10 @@ export interface WindowVirtualizerProps<T = unknown> extends PublicProps {
20
20
  * - If set, you can opt out estimation and use the value as initial item size.
21
21
  */
22
22
  itemSize?: number;
23
+ /**
24
+ * 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. The minimum value is 0.
25
+ */
26
+ ssrCount?: number;
23
27
  /**
24
28
  * 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.
25
29
  */