virtua 0.53.0 → 0.53.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.
Files changed (55) hide show
  1. package/lib/angular/VGrid.d.ts +5 -1
  2. package/lib/angular/VList.d.ts +5 -1
  3. package/lib/angular/VMasonry.d.ts +10 -1
  4. package/lib/angular/Virtualizer.d.ts +5 -1
  5. package/lib/angular/WindowVirtualizer.d.ts +5 -1
  6. package/lib/angular/index.js +410 -376
  7. package/lib/angular/index.js.map +1 -1
  8. package/lib/core/index.cjs +101 -108
  9. package/lib/core/index.cjs.map +1 -1
  10. package/lib/core/index.d.ts +1 -1
  11. package/lib/core/index.js +193 -200
  12. package/lib/core/index.js.map +1 -1
  13. package/lib/index.cjs +423 -407
  14. package/lib/index.cjs.map +1 -1
  15. package/lib/index.js +436 -421
  16. package/lib/index.js.map +1 -1
  17. package/lib/react/VGrid.d.ts +4 -0
  18. package/lib/react/VList.d.ts +1 -1
  19. package/lib/react/VMasonry.d.ts +8 -0
  20. package/lib/react/Virtualizer.d.ts +4 -0
  21. package/lib/react/WindowVirtualizer.d.ts +4 -0
  22. package/lib/react/types.d.ts +1 -1
  23. package/lib/solid/VGrid.d.ts +4 -0
  24. package/lib/solid/VList.d.ts +1 -1
  25. package/lib/solid/VMasonry.d.ts +8 -0
  26. package/lib/solid/Virtualizer.d.ts +4 -0
  27. package/lib/solid/WindowVirtualizer.d.ts +4 -0
  28. package/lib/solid/index.cjs +453 -437
  29. package/lib/solid/index.cjs.map +1 -1
  30. package/lib/solid/index.js +415 -399
  31. package/lib/solid/index.js.map +1 -1
  32. package/lib/solid/index.jsx +55 -25
  33. package/lib/solid/index.jsx.map +1 -1
  34. package/lib/solid/types.d.ts +1 -1
  35. package/lib/svelte/VGrid.svelte +26 -6
  36. package/lib/svelte/VGrid.type.d.ts +4 -0
  37. package/lib/svelte/VList.svelte +2 -0
  38. package/lib/svelte/VList.type.d.ts +1 -1
  39. package/lib/svelte/VMasonry.svelte +28 -7
  40. package/lib/svelte/VMasonry.type.d.ts +8 -0
  41. package/lib/svelte/Virtualizer.svelte +9 -2
  42. package/lib/svelte/Virtualizer.type.d.ts +4 -0
  43. package/lib/svelte/WindowVirtualizer.svelte +9 -2
  44. package/lib/svelte/WindowVirtualizer.type.d.ts +4 -0
  45. package/lib/svelte/types.d.ts +1 -1
  46. package/lib/vue/VGrid.d.ts +4 -0
  47. package/lib/vue/VList.d.ts +1 -1
  48. package/lib/vue/VMasonry.d.ts +8 -0
  49. package/lib/vue/Virtualizer.d.ts +4 -0
  50. package/lib/vue/WindowVirtualizer.d.ts +4 -0
  51. package/lib/vue/index.cjs +625 -608
  52. package/lib/vue/index.cjs.map +1 -1
  53. package/lib/vue/index.js +605 -588
  54. package/lib/vue/index.js.map +1 -1
  55. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import { JSX, ParentComponent } from 'solid-js';
2
- export type ViewportComponentAttributes = Omit<JSX.HTMLAttributes<HTMLDivElement>, "ref" | "children" | "style" | "onScroll" | "onScrollEnd"> & {
2
+ export type ViewportComponentAttributes = Omit<JSX.HTMLAttributes<HTMLDivElement>, "ref" | "children" | "style" | "onScroll" | "onScrollEnd" | "onResize"> & {
3
3
  style?: JSX.CSSProperties;
4
4
  };
5
5
  export interface CustomContainerComponentProps {
@@ -5,6 +5,8 @@
5
5
  ACTION_ITEMS_LENGTH_CHANGE,
6
6
  ACTION_RELAYOUT,
7
7
  UPDATE_SCROLL_END_EVENT,
8
+ UPDATE_SIZE_EVENT,
9
+ microtask,
8
10
  UPDATE_SCROLL_EVENT,
9
11
  UPDATE_VIRTUAL_STATE,
10
12
  createContainerGridDriver,
@@ -45,6 +47,7 @@
45
47
  onverticalscroll,
46
48
  onhorizontalscroll,
47
49
  onscrollend,
50
+ onresize,
48
51
  ...rest
49
52
  }: Props = $props();
50
53
 
@@ -86,6 +89,19 @@
86
89
  };
87
90
  rowStore.$subscribe(UPDATE_SCROLL_END_EVENT, notifyScrollEnd);
88
91
  colStore.$subscribe(UPDATE_SCROLL_END_EVENT, notifyScrollEnd);
92
+ // Notify once after both stores are updated with the sizes observed together
93
+ let resized: boolean | undefined;
94
+ const notifyResize = () => {
95
+ if (!resized) {
96
+ resized = true;
97
+ microtask(() => {
98
+ resized = false;
99
+ onresize && onresize();
100
+ });
101
+ }
102
+ };
103
+ rowStore.$subscribe(UPDATE_SIZE_EVENT, notifyResize);
104
+ colStore.$subscribe(UPDATE_SIZE_EVENT, notifyResize);
89
105
 
90
106
  $effect.pre(() => {
91
107
  rowLayout.$setPinned(headerRows, footerRows);
@@ -98,8 +114,14 @@
98
114
  if (colLength !== colStore.$getItemsLength()) {
99
115
  colStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [colLength]);
100
116
  }
101
- rowStore.$update(ACTION_RELAYOUT, rowSizes);
102
- colStore.$update(ACTION_RELAYOUT, colSizes);
117
+ rowStore.$update(
118
+ ACTION_RELAYOUT,
119
+ rowLayout.$relayout(rowSizes, rowStore.$getVisibleOffset()),
120
+ );
121
+ colStore.$update(
122
+ ACTION_RELAYOUT,
123
+ colLayout.$relayout(colSizes, colStore.$getVisibleOffset()),
124
+ );
103
125
  });
104
126
  let rowCount = $derived(stateVersion && rowStore.$getItemsLength());
105
127
  let colCount = $derived(stateVersion && colStore.$getItemsLength());
@@ -148,10 +170,8 @@
148
170
  rowStore.$getViewportSize;
149
171
  export const getViewportWidth: VGridHandle["getViewportWidth"] =
150
172
  colStore.$getViewportSize;
151
- export const findRowIndex: VGridHandle["findRowIndex"] =
152
- rowStore.$findItemIndex;
153
- export const findColIndex: VGridHandle["findColIndex"] =
154
- colStore.$findItemIndex;
173
+ export const findRowIndex: VGridHandle["findRowIndex"] = rowLayout.$findIndex;
174
+ export const findColIndex: VGridHandle["findColIndex"] = colLayout.$findIndex;
155
175
  export const getRowOffset: VGridHandle["getRowOffset"] =
156
176
  rowStore.$getItemOffset;
157
177
  export const getColOffset: VGridHandle["getColOffset"] =
@@ -102,6 +102,10 @@ export interface VGridProps<R = number, C = number> extends Omit<ViewportCompone
102
102
  * Callback invoked when scrolling stops.
103
103
  */
104
104
  onscrollend?: () => void;
105
+ /**
106
+ * Callback invoked when the size of the viewport or the items changes.
107
+ */
108
+ onresize?: () => void;
105
109
  }
106
110
  /**
107
111
  * Methods of {@link VGrid}.
@@ -19,6 +19,7 @@
19
19
  children,
20
20
  onscroll,
21
21
  onscrollend,
22
+ onresize,
22
23
  ...rest
23
24
  }: Props = $props();
24
25
 
@@ -86,5 +87,6 @@
86
87
  {itemProps}
87
88
  {onscroll}
88
89
  {onscrollend}
90
+ {onresize}
89
91
  />
90
92
  </div>
@@ -3,7 +3,7 @@ import { VirtualizerHandle, VirtualizerProps } from './Virtualizer.type.js';
3
3
  /**
4
4
  * Props of {@link VList}.
5
5
  */
6
- export interface VListProps<T> extends Pick<VirtualizerProps<T>, "data" | "getKey" | "bufferSize" | "itemSize" | "ssrCount" | "shift" | "horizontal" | "children" | "onscroll" | "onscrollend" | "keepMounted" | "cache" | "itemProps">, ViewportComponentAttributes {
6
+ export interface VListProps<T> extends Pick<VirtualizerProps<T>, "data" | "getKey" | "bufferSize" | "itemSize" | "ssrCount" | "shift" | "horizontal" | "children" | "onscroll" | "onscrollend" | "onresize" | "keepMounted" | "cache" | "itemProps">, ViewportComponentAttributes {
7
7
  }
8
8
  /**
9
9
  * Methods of {@link VList}.
@@ -4,6 +4,7 @@
4
4
  type ItemsRange,
5
5
  type StateVersion,
6
6
  UPDATE_SCROLL_END_EVENT,
7
+ UPDATE_SIZE_EVENT,
7
8
  UPDATE_SCROLL_EVENT,
8
9
  UPDATE_VIRTUAL_STATE,
9
10
  createContainerDriver,
@@ -15,6 +16,7 @@
15
16
  scrollTo as _scrollTo,
16
17
  scrollBy as _scrollBy,
17
18
  scrollToIndex as _scrollToIndex,
19
+ sort,
18
20
  } from "../core/index.js";
19
21
  import { defaultGetKey, isSameRange, styleToString } from "./utils.js";
20
22
  import MasonryItem from "./MasonryItem.svelte";
@@ -32,10 +34,12 @@
32
34
  gap: gapProp,
33
35
  itemSize,
34
36
  bufferSize,
37
+ keepMounted,
35
38
  cache,
36
39
  children,
37
40
  onscroll,
38
41
  onscrollend,
42
+ onresize,
39
43
  ...rest
40
44
  }: Props = $props();
41
45
 
@@ -57,6 +61,9 @@
57
61
  store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
58
62
  onscrollend && onscrollend();
59
63
  });
64
+ store.$subscribe(UPDATE_SIZE_EVENT, () => {
65
+ onresize && onresize();
66
+ });
60
67
 
61
68
  let containerRef: HTMLDivElement | undefined = $state();
62
69
 
@@ -82,11 +89,23 @@
82
89
  const len = data.length;
83
90
  const [start, end] = range;
84
91
  const arr: number[] = [];
85
- for (let i = start; i <= end; i++) {
86
- // Guard for experimental.async: true, which runs the each block before $effect.pre
87
- // https://github.com/inokawa/virtua/pull/847
88
- if (i < len) {
89
- arr.push(i);
92
+ if (keepMounted) {
93
+ const mounted = new Set(keepMounted);
94
+ for (let i = start; i <= end; i++) {
95
+ mounted.add(i);
96
+ }
97
+ for (const index of sort([...mounted])) {
98
+ if (index < len) {
99
+ arr.push(index);
100
+ }
101
+ }
102
+ } else {
103
+ for (let i = start; i <= end; i++) {
104
+ // Guard for experimental.async: true, which runs the each block before $effect.pre
105
+ // https://github.com/inokawa/virtua/pull/847
106
+ if (i < len) {
107
+ arr.push(i);
108
+ }
90
109
  }
91
110
  }
92
111
  return arr;
@@ -106,8 +125,10 @@
106
125
  }
107
126
  });
108
127
  $effect.pre(() => {
109
- layout.$setOptions(lanesProp, gapProp);
110
- store.$update(ACTION_RELAYOUT, undefined);
128
+ store.$update(
129
+ ACTION_RELAYOUT,
130
+ layout.$relayout(lanesProp, gapProp, store.$getVisibleOffset()),
131
+ );
111
132
  });
112
133
 
113
134
  let prevStateVersion: StateVersion | undefined;
@@ -39,6 +39,10 @@ export interface VMasonryProps<T> extends ViewportComponentAttributes {
39
39
  * @defaultValue 200
40
40
  */
41
41
  bufferSize?: number;
42
+ /**
43
+ * List of indexes that should be always mounted, even when off screen.
44
+ */
45
+ keepMounted?: readonly number[];
42
46
  /**
43
47
  * You can restore cache by passing a {@link CacheSnapshot} on mount. This is useful when you want to restore scroll position after navigation. The snapshot can be obtained from {@link VMasonryHandle.getCache}.
44
48
  *
@@ -54,6 +58,10 @@ export interface VMasonryProps<T> extends ViewportComponentAttributes {
54
58
  * Callback invoked when scrolling stops.
55
59
  */
56
60
  onscrollend?: () => void;
61
+ /**
62
+ * Callback invoked when the size of the viewport or the items changes.
63
+ */
64
+ onresize?: () => void;
57
65
  }
58
66
  /**
59
67
  * Methods of {@link VMasonry}.
@@ -6,6 +6,7 @@
6
6
  type ItemsRange,
7
7
  type StateVersion,
8
8
  UPDATE_SCROLL_END_EVENT,
9
+ UPDATE_SIZE_EVENT,
9
10
  UPDATE_SCROLL_EVENT,
10
11
  UPDATE_VIRTUAL_STATE,
11
12
  createContainerDriver,
@@ -44,6 +45,7 @@
44
45
  children,
45
46
  onscroll,
46
47
  onscrollend,
48
+ onresize,
47
49
  }: Props = $props();
48
50
 
49
51
  const layout = createListLayout(data.length, itemSize, cache);
@@ -58,6 +60,9 @@
58
60
  store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
59
61
  onscrollend && onscrollend();
60
62
  });
63
+ store.$subscribe(UPDATE_SIZE_EVENT, () => {
64
+ onresize && onresize();
65
+ });
61
66
 
62
67
  let isSSR = $state(!!ssrCount);
63
68
 
@@ -153,8 +158,10 @@
153
158
  )) satisfies VirtualizerHandle["getScrollSize"] as VirtualizerHandle["getScrollSize"];
154
159
  export const getViewportSize =
155
160
  store.$getViewportSize satisfies VirtualizerHandle["getViewportSize"] as VirtualizerHandle["getViewportSize"];
156
- export const findItemIndex =
157
- store.$findItemIndex satisfies VirtualizerHandle["findItemIndex"] as VirtualizerHandle["findItemIndex"];
161
+ export const findItemIndex = ((offset) =>
162
+ layout.$findIndex(
163
+ offset - store.$getStartSpacerSize(),
164
+ )) satisfies VirtualizerHandle["findItemIndex"] as VirtualizerHandle["findItemIndex"];
158
165
  export const getItemOffset =
159
166
  store.$getItemOffset satisfies VirtualizerHandle["getItemOffset"] as VirtualizerHandle["getItemOffset"];
160
167
  export const getItemSize =
@@ -86,6 +86,10 @@ export interface VirtualizerProps<T> {
86
86
  * Callback invoked when scrolling stops.
87
87
  */
88
88
  onscrollend?: () => void;
89
+ /**
90
+ * Callback invoked when the size of the viewport or the items changes.
91
+ */
92
+ onresize?: () => void;
89
93
  }
90
94
  /**
91
95
  * Methods of {@link Virtualizer}.
@@ -5,6 +5,7 @@
5
5
  type ItemsRange,
6
6
  type StateVersion,
7
7
  UPDATE_SCROLL_END_EVENT,
8
+ UPDATE_SIZE_EVENT,
8
9
  UPDATE_SCROLL_EVENT,
9
10
  UPDATE_VIRTUAL_STATE,
10
11
  createVirtualStore,
@@ -34,6 +35,7 @@
34
35
  children,
35
36
  onscroll,
36
37
  onscrollend,
38
+ onresize,
37
39
  }: Props = $props();
38
40
 
39
41
  const layout = createListLayout(data.length, itemSize, cache);
@@ -49,6 +51,9 @@
49
51
  store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
50
52
  onscrollend && onscrollend();
51
53
  });
54
+ store.$subscribe(UPDATE_SIZE_EVENT, () => {
55
+ onresize && onresize();
56
+ });
52
57
 
53
58
  let isSSR = $state(!!ssrCount);
54
59
 
@@ -112,8 +117,10 @@
112
117
  store.$getScrollOffset satisfies WindowVirtualizerHandle["getScrollOffset"] as WindowVirtualizerHandle["getScrollOffset"];
113
118
  export const getViewportSize =
114
119
  store.$getViewportSize satisfies WindowVirtualizerHandle["getViewportSize"] as WindowVirtualizerHandle["getViewportSize"];
115
- export const findItemIndex =
116
- store.$findItemIndex satisfies WindowVirtualizerHandle["findItemIndex"] as WindowVirtualizerHandle["findItemIndex"];
120
+ export const findItemIndex = ((offset) =>
121
+ layout.$findIndex(
122
+ offset - store.$getStartSpacerSize(),
123
+ )) satisfies WindowVirtualizerHandle["findItemIndex"] as WindowVirtualizerHandle["findItemIndex"];
117
124
  export const getItemOffset =
118
125
  store.$getItemOffset satisfies WindowVirtualizerHandle["getItemOffset"] as WindowVirtualizerHandle["getItemOffset"];
119
126
  export const getItemSize =
@@ -57,6 +57,10 @@ export interface WindowVirtualizerProps<T> {
57
57
  * Callback invoked when scrolling stops.
58
58
  */
59
59
  onscrollend?: () => void;
60
+ /**
61
+ * Callback invoked when the size of the viewport or the items changes.
62
+ */
63
+ onresize?: () => void;
60
64
  }
61
65
  /**
62
66
  * Methods of {@link WindowVirtualizer}.
@@ -1,2 +1,2 @@
1
1
  import { HTMLAttributes } from 'svelte/elements';
2
- export type ViewportComponentAttributes = Omit<HTMLAttributes<HTMLDivElement>, "children" | "onscroll" | "onscrollend">;
2
+ export type ViewportComponentAttributes = Omit<HTMLAttributes<HTMLDivElement>, "children" | "onscroll" | "onscrollend" | "onresize">;
@@ -94,6 +94,10 @@ export interface VGridProps<R = number, C = number> extends PublicProps {
94
94
  * Callback invoked when scrolling stops.
95
95
  */
96
96
  onScrollEnd?: () => void;
97
+ /**
98
+ * Callback invoked when the size of the viewport or the items changes.
99
+ */
100
+ onResize?: () => void;
97
101
  }
98
102
  /**
99
103
  * Methods of {@link VGrid}.
@@ -8,7 +8,7 @@ export interface VListHandle extends VirtualizerHandle {
8
8
  /**
9
9
  * Props of {@link VList}.
10
10
  */
11
- export interface VListProps<T = unknown> extends PublicProps, Pick<VirtualizerProps<T>, "data" | "bufferSize" | "itemSize" | "shift" | "horizontal" | "cache" | "ssrCount" | "itemProps" | "onScroll" | "onScrollEnd" | "keepMounted"> {
11
+ export interface VListProps<T = unknown> extends PublicProps, Pick<VirtualizerProps<T>, "data" | "bufferSize" | "itemSize" | "shift" | "horizontal" | "cache" | "ssrCount" | "itemProps" | "onScroll" | "onScrollEnd" | "onResize" | "keepMounted"> {
12
12
  }
13
13
  interface VListInstance<T = unknown> extends VListHandle {
14
14
  $props: VListProps<T>;
@@ -76,6 +76,10 @@ export interface VMasonryProps<T> extends PublicProps {
76
76
  * @defaultValue 200
77
77
  */
78
78
  bufferSize?: number;
79
+ /**
80
+ * List of indexes that should be always mounted, even when off screen.
81
+ */
82
+ keepMounted?: readonly number[];
79
83
  /**
80
84
  * You can restore cache by passing a {@link CacheSnapshot} on mount. This is useful when you want to restore scroll position after navigation. The snapshot can be obtained from {@link VMasonryHandle.cache}.
81
85
  *
@@ -91,6 +95,10 @@ export interface VMasonryProps<T> extends PublicProps {
91
95
  * Callback invoked when scrolling stops.
92
96
  */
93
97
  onScrollEnd?: () => void;
98
+ /**
99
+ * Callback invoked when the size of the viewport or the items changes.
100
+ */
101
+ onResize?: () => void;
94
102
  }
95
103
  interface VMasonryInstance<T> extends VMasonryHandle {
96
104
  $props: VMasonryProps<T>;
@@ -78,6 +78,10 @@ export interface VirtualizerProps<T = unknown> extends PublicProps {
78
78
  * Callback invoked when scrolling stops.
79
79
  */
80
80
  onScrollEnd?: () => void;
81
+ /**
82
+ * Callback invoked when the size of the viewport or the items changes.
83
+ */
84
+ onResize?: () => void;
81
85
  }
82
86
  interface VirtualizerInstance<T = unknown> extends VirtualizerHandle {
83
87
  $props: VirtualizerProps<T>;
@@ -58,6 +58,10 @@ export interface WindowVirtualizerProps<T = unknown> extends PublicProps {
58
58
  * Callback invoked when scrolling stops.
59
59
  */
60
60
  onScrollEnd?: () => void;
61
+ /**
62
+ * Callback invoked when the size of the viewport or the items changes.
63
+ */
64
+ onResize?: () => void;
61
65
  }
62
66
  interface WindowVirtualizerInstance<T = unknown> extends WindowVirtualizerHandle {
63
67
  $props: WindowVirtualizerProps<T>;