@tanstack/react-virtual 3.13.26 → 3.14.1
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/dist/cjs/index.cjs +82 -7
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +44 -2
- package/dist/esm/index.d.ts +44 -2
- package/dist/esm/index.js +82 -7
- package/dist/esm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.tsx +164 -10
package/dist/cjs/index.cjs
CHANGED
|
@@ -23,24 +23,96 @@ const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
|
23
23
|
const useIsomorphicLayoutEffect = typeof document !== "undefined" ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
24
24
|
function useVirtualizerBase({
|
|
25
25
|
useFlushSync = true,
|
|
26
|
+
directDomUpdates = false,
|
|
27
|
+
directDomUpdatesMode = "transform",
|
|
26
28
|
...options
|
|
27
29
|
}) {
|
|
28
30
|
const rerender = React__namespace.useReducer((x) => x + 1, 0)[1];
|
|
31
|
+
const directRef = React__namespace.useRef({
|
|
32
|
+
enabled: directDomUpdates,
|
|
33
|
+
mode: directDomUpdatesMode,
|
|
34
|
+
container: null,
|
|
35
|
+
lastSize: null,
|
|
36
|
+
// Keyed by the element itself so a remounted node (same key, new DOM
|
|
37
|
+
// node — e.g. when `enabled` is toggled off then on) is treated as fresh
|
|
38
|
+
// and gets its style written.
|
|
39
|
+
lastPositions: /* @__PURE__ */ new WeakMap(),
|
|
40
|
+
prevRange: null
|
|
41
|
+
});
|
|
42
|
+
directRef.current.enabled = directDomUpdates;
|
|
43
|
+
directRef.current.mode = directDomUpdatesMode;
|
|
44
|
+
const applyDirectStyles = (instance2) => {
|
|
45
|
+
const state = directRef.current;
|
|
46
|
+
if (!state.enabled) return;
|
|
47
|
+
const totalSize = instance2.getTotalSize();
|
|
48
|
+
if (state.container && totalSize !== state.lastSize) {
|
|
49
|
+
state.lastSize = totalSize;
|
|
50
|
+
const sizeAxis = instance2.options.horizontal ? "width" : "height";
|
|
51
|
+
state.container.style[sizeAxis] = `${totalSize}px`;
|
|
52
|
+
}
|
|
53
|
+
const horizontal = !!instance2.options.horizontal;
|
|
54
|
+
const useTransform = state.mode === "transform";
|
|
55
|
+
const posAxis = horizontal ? "left" : "top";
|
|
56
|
+
const scrollMargin = instance2.options.scrollMargin;
|
|
57
|
+
const items = instance2.getVirtualItems();
|
|
58
|
+
for (const item of items) {
|
|
59
|
+
const next = item.start - scrollMargin;
|
|
60
|
+
const el = instance2.elementsCache.get(item.key);
|
|
61
|
+
if (!el) continue;
|
|
62
|
+
if (state.lastPositions.get(el) === next) continue;
|
|
63
|
+
state.lastPositions.set(el, next);
|
|
64
|
+
if (useTransform) {
|
|
65
|
+
el.style.transform = horizontal ? `translate3d(${next}px, 0, 0)` : `translate3d(0, ${next}px, 0)`;
|
|
66
|
+
} else {
|
|
67
|
+
el.style[posAxis] = `${next}px`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
29
71
|
const resolvedOptions = {
|
|
30
72
|
...options,
|
|
31
73
|
onChange: (instance2, sync) => {
|
|
32
74
|
var _a;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
75
|
+
const state = directRef.current;
|
|
76
|
+
let shouldRerender = true;
|
|
77
|
+
if (state.enabled) {
|
|
78
|
+
applyDirectStyles(instance2);
|
|
79
|
+
const range = instance2.range;
|
|
80
|
+
const prev = state.prevRange;
|
|
81
|
+
shouldRerender = !prev || prev.isScrolling !== instance2.isScrolling || prev.startIndex !== (range == null ? void 0 : range.startIndex) || prev.endIndex !== (range == null ? void 0 : range.endIndex);
|
|
82
|
+
if (shouldRerender) {
|
|
83
|
+
state.prevRange = range ? {
|
|
84
|
+
startIndex: range.startIndex,
|
|
85
|
+
endIndex: range.endIndex,
|
|
86
|
+
isScrolling: instance2.isScrolling
|
|
87
|
+
} : null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (shouldRerender) {
|
|
91
|
+
if (useFlushSync && sync) {
|
|
92
|
+
reactDom.flushSync(rerender);
|
|
93
|
+
} else {
|
|
94
|
+
rerender();
|
|
95
|
+
}
|
|
37
96
|
}
|
|
38
97
|
(_a = options.onChange) == null ? void 0 : _a.call(options, instance2, sync);
|
|
39
98
|
}
|
|
40
99
|
};
|
|
41
|
-
const [instance] = React__namespace.useState(
|
|
42
|
-
|
|
43
|
-
|
|
100
|
+
const [instance] = React__namespace.useState(() => {
|
|
101
|
+
const v = new virtualCore.Virtualizer(resolvedOptions);
|
|
102
|
+
return Object.assign(v, {
|
|
103
|
+
containerRef: (node) => {
|
|
104
|
+
const state = directRef.current;
|
|
105
|
+
state.container = node;
|
|
106
|
+
state.lastSize = null;
|
|
107
|
+
if (node && state.enabled) {
|
|
108
|
+
const total = v.getTotalSize();
|
|
109
|
+
state.lastSize = total;
|
|
110
|
+
const axis = v.options.horizontal ? "width" : "height";
|
|
111
|
+
node.style[axis] = `${total}px`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
44
116
|
instance.setOptions(resolvedOptions);
|
|
45
117
|
useIsomorphicLayoutEffect(() => {
|
|
46
118
|
return instance._didMount();
|
|
@@ -48,6 +120,9 @@ function useVirtualizerBase({
|
|
|
48
120
|
useIsomorphicLayoutEffect(() => {
|
|
49
121
|
return instance._willUpdate();
|
|
50
122
|
});
|
|
123
|
+
useIsomorphicLayoutEffect(() => {
|
|
124
|
+
applyDirectStyles(instance);
|
|
125
|
+
});
|
|
51
126
|
return instance;
|
|
52
127
|
}
|
|
53
128
|
function useVirtualizer(options) {
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): Virtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n if (useFlushSync && sync) {\n flushSync(rerender)\n } else {\n rerender()\n }\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(\n () => new Virtualizer<TScrollElement, TItemElement>(resolvedOptions),\n )\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n return instance._willUpdate()\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Virtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): Virtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["React","instance","flushSync","Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAeA,MAAM,4BACJ,OAAO,aAAa,cAAcA,iBAAM,kBAAkBA,iBAAM;AASlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,GAAG;AACL,GAGE;AACA,QAAM,WAAWA,iBAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC,EAAE,CAAC;AAE5D,QAAM,kBAAoE;AAAA,IACxE,GAAG;AAAA,IACH,UAAU,CAACC,WAAU,SAAS;;AAC5B,UAAI,gBAAgB,MAAM;AACxBC,iBAAAA,UAAU,QAAQ;AAAA,MACpB,OAAO;AACL,iBAAA;AAAA,MACF;AACA,oBAAQ,aAAR,iCAAmBD,WAAU;AAAA,IAC/B;AAAA,EAAA;AAGF,QAAM,CAAC,QAAQ,IAAID,iBAAM;AAAA,IACvB,MAAM,IAAIG,YAAAA,YAA0C,eAAe;AAAA,EAAA;AAGrE,WAAS,WAAW,eAAe;AAEnC,4BAA0B,MAAM;AAC9B,WAAO,SAAS,UAAA;AAAA,EAClB,GAAG,CAAA,CAAE;AAEL,4BAA0B,MAAM;AAC9B,WAAO,SAAS,YAAA;AAAA,EAClB,CAAC;AAED,SAAO;AACT;AAEO,SAAS,eAId,SAI2C;AAC3C,SAAO,mBAAiD;AAAA,IAAA,oBACtDC,YAAAA;AAAAA,IAAA,sBACAC,YAAAA;AAAAA,IACA,YAAYC,YAAAA;AAAAA,IACZ,GAAG;AAAA,EAAA,CACJ;AACH;AAEO,SAAS,qBACd,SAOmC;AACnC,SAAO,mBAAyC;AAAA,IAC9C,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,IACpE,oBAAoBC,YAAAA;AAAAA,IACpB,sBAAsBC,YAAAA;AAAAA,IACtB,YAAYC,YAAAA;AAAAA,IACZ,eAAe,MAAO,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,IACzE,GAAG;AAAA,EAAA,CACJ;AACH;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner sized container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner sized container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled) return\n\n const totalSize = instance.getTotalSize()\n if (state.container && totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n\n const horizontal = !!instance.options.horizontal\n const useTransform = state.mode === 'transform'\n const posAxis = horizontal ? 'left' : 'top'\n const scrollMargin = instance.options.scrollMargin\n const items = instance.getVirtualItems()\n for (const item of items) {\n const next = item.start - scrollMargin\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (!el) continue\n if (state.lastPositions.get(el) === next) continue\n state.lastPositions.set(el, next)\n if (useTransform) {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[posAxis] = `${next}px`\n }\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n if (useFlushSync && sync) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n if (node && state.enabled) {\n const total = v.getTotalSize()\n state.lastSize = total\n const axis = v.options.horizontal ? 'width' : 'height'\n node.style[axis] = `${total}px`\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["React","instance","flushSync","Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAeA,MAAM,4BACJ,OAAO,aAAa,cAAcA,iBAAM,kBAAkBA,iBAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAAM,WAAWA,iBAAM,WAAW,CAAC,MAAc,IAAI,GAA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,SAAS,eAId,SAIgD;AAChD,SAAO,mBAAiD;AAAA,IAAA,oBACtDC,YAAAA;AAAAA,IAAA,sBACAC,YAAAA;AAAAA,IACA,YAAYC,YAAAA;AAAAA,IACZ,GAAG;AAAA,EAAA,CACJ;AACH;AAEO,SAAS,qBACd,SAOwC;AACxC,SAAO,mBAAyC;AAAA,IAC9C,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,IACpE,oBAAoBC,YAAAA;AAAAA,IACpB,sBAAsBC,YAAAA;AAAAA,IACtB,YAAYC,YAAAA;AAAAA,IACZ,eAAe,MAAO,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,IACzE,GAAG;AAAA,EAAA,CACJ;AACH;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,7 +1,49 @@
|
|
|
1
1
|
import { Virtualizer, PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core';
|
|
2
2
|
export * from '@tanstack/virtual-core';
|
|
3
|
+
export type ReactVirtualizer<TScrollElement extends Element | Window, TItemElement extends Element> = Virtualizer<TScrollElement, TItemElement> & {
|
|
4
|
+
/**
|
|
5
|
+
* Ref callback for the inner sized container element. Only meaningful when
|
|
6
|
+
* `directDomUpdates: true` — the virtualizer writes the container's
|
|
7
|
+
* main-axis size (`height` or `width`) directly to skip React re-renders.
|
|
8
|
+
*/
|
|
9
|
+
containerRef: (node: HTMLElement | null) => void;
|
|
10
|
+
};
|
|
3
11
|
export type ReactVirtualizerOptions<TScrollElement extends Element | Window, TItemElement extends Element> = VirtualizerOptions<TScrollElement, TItemElement> & {
|
|
4
12
|
useFlushSync?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Skip React re-renders for scroll-only updates. The virtualizer writes
|
|
15
|
+
* item positions (`top`/`left`) and the container size (`height`/`width`)
|
|
16
|
+
* directly to the DOM, and only re-renders when the visible index range
|
|
17
|
+
* or `isScrolling` changes.
|
|
18
|
+
*
|
|
19
|
+
* Requirements when enabled:
|
|
20
|
+
* - Item elements must be `position: absolute`; in `'transform'` mode they
|
|
21
|
+
* must also be anchored with `top: 0` / `left: 0`.
|
|
22
|
+
* - Item elements must NOT set the main-axis position in their style — the
|
|
23
|
+
* virtualizer owns `top` / `left` in `'position'` mode and `transform` in
|
|
24
|
+
* `'transform'` mode.
|
|
25
|
+
* - The inner sized container must receive `virtualizer.containerRef` and
|
|
26
|
+
* must NOT set `height` / `width` in its style.
|
|
27
|
+
* - For multi-lane layouts (grids / masonry), the cross-axis position
|
|
28
|
+
* (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and
|
|
29
|
+
* must still be set in your JSX — only the main axis is automated.
|
|
30
|
+
*
|
|
31
|
+
* This flag is intended to be set once at mount. Toggling it (or
|
|
32
|
+
* `directDomUpdatesMode`) at runtime can leave stale inline styles on
|
|
33
|
+
* items and the container.
|
|
34
|
+
*/
|
|
35
|
+
directDomUpdates?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* How `directDomUpdates` positions item elements.
|
|
38
|
+
* - `'transform'` (default): writes `transform: translate3d(...)`.
|
|
39
|
+
* Promotes items to their own compositor layer — usually smoother on long
|
|
40
|
+
* lists, but creates a stacking context and can interfere with
|
|
41
|
+
* `position: fixed` descendants. Item elements must still be anchored with
|
|
42
|
+
* `position: absolute`, `top: 0`, and `left: 0`.
|
|
43
|
+
* - `'position'`: writes `top` / `left`. Item elements must be
|
|
44
|
+
* `position: absolute`.
|
|
45
|
+
*/
|
|
46
|
+
directDomUpdatesMode?: 'position' | 'transform';
|
|
5
47
|
};
|
|
6
|
-
export declare function useVirtualizer<TScrollElement extends Element, TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>):
|
|
7
|
-
export declare function useWindowVirtualizer<TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<Window, TItemElement>, 'getScrollElement' | 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>):
|
|
48
|
+
export declare function useVirtualizer<TScrollElement extends Element, TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): ReactVirtualizer<TScrollElement, TItemElement>;
|
|
49
|
+
export declare function useWindowVirtualizer<TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<Window, TItemElement>, 'getScrollElement' | 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): ReactVirtualizer<Window, TItemElement>;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,49 @@
|
|
|
1
1
|
import { Virtualizer, PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core';
|
|
2
2
|
export * from '@tanstack/virtual-core';
|
|
3
|
+
export type ReactVirtualizer<TScrollElement extends Element | Window, TItemElement extends Element> = Virtualizer<TScrollElement, TItemElement> & {
|
|
4
|
+
/**
|
|
5
|
+
* Ref callback for the inner sized container element. Only meaningful when
|
|
6
|
+
* `directDomUpdates: true` — the virtualizer writes the container's
|
|
7
|
+
* main-axis size (`height` or `width`) directly to skip React re-renders.
|
|
8
|
+
*/
|
|
9
|
+
containerRef: (node: HTMLElement | null) => void;
|
|
10
|
+
};
|
|
3
11
|
export type ReactVirtualizerOptions<TScrollElement extends Element | Window, TItemElement extends Element> = VirtualizerOptions<TScrollElement, TItemElement> & {
|
|
4
12
|
useFlushSync?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Skip React re-renders for scroll-only updates. The virtualizer writes
|
|
15
|
+
* item positions (`top`/`left`) and the container size (`height`/`width`)
|
|
16
|
+
* directly to the DOM, and only re-renders when the visible index range
|
|
17
|
+
* or `isScrolling` changes.
|
|
18
|
+
*
|
|
19
|
+
* Requirements when enabled:
|
|
20
|
+
* - Item elements must be `position: absolute`; in `'transform'` mode they
|
|
21
|
+
* must also be anchored with `top: 0` / `left: 0`.
|
|
22
|
+
* - Item elements must NOT set the main-axis position in their style — the
|
|
23
|
+
* virtualizer owns `top` / `left` in `'position'` mode and `transform` in
|
|
24
|
+
* `'transform'` mode.
|
|
25
|
+
* - The inner sized container must receive `virtualizer.containerRef` and
|
|
26
|
+
* must NOT set `height` / `width` in its style.
|
|
27
|
+
* - For multi-lane layouts (grids / masonry), the cross-axis position
|
|
28
|
+
* (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and
|
|
29
|
+
* must still be set in your JSX — only the main axis is automated.
|
|
30
|
+
*
|
|
31
|
+
* This flag is intended to be set once at mount. Toggling it (or
|
|
32
|
+
* `directDomUpdatesMode`) at runtime can leave stale inline styles on
|
|
33
|
+
* items and the container.
|
|
34
|
+
*/
|
|
35
|
+
directDomUpdates?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* How `directDomUpdates` positions item elements.
|
|
38
|
+
* - `'transform'` (default): writes `transform: translate3d(...)`.
|
|
39
|
+
* Promotes items to their own compositor layer — usually smoother on long
|
|
40
|
+
* lists, but creates a stacking context and can interfere with
|
|
41
|
+
* `position: fixed` descendants. Item elements must still be anchored with
|
|
42
|
+
* `position: absolute`, `top: 0`, and `left: 0`.
|
|
43
|
+
* - `'position'`: writes `top` / `left`. Item elements must be
|
|
44
|
+
* `position: absolute`.
|
|
45
|
+
*/
|
|
46
|
+
directDomUpdatesMode?: 'position' | 'transform';
|
|
5
47
|
};
|
|
6
|
-
export declare function useVirtualizer<TScrollElement extends Element, TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>):
|
|
7
|
-
export declare function useWindowVirtualizer<TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<Window, TItemElement>, 'getScrollElement' | 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>):
|
|
48
|
+
export declare function useVirtualizer<TScrollElement extends Element, TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): ReactVirtualizer<TScrollElement, TItemElement>;
|
|
49
|
+
export declare function useWindowVirtualizer<TItemElement extends Element>(options: PartialKeys<ReactVirtualizerOptions<Window, TItemElement>, 'getScrollElement' | 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): ReactVirtualizer<Window, TItemElement>;
|
package/dist/esm/index.js
CHANGED
|
@@ -5,24 +5,96 @@ export * from "@tanstack/virtual-core";
|
|
|
5
5
|
const useIsomorphicLayoutEffect = typeof document !== "undefined" ? React.useLayoutEffect : React.useEffect;
|
|
6
6
|
function useVirtualizerBase({
|
|
7
7
|
useFlushSync = true,
|
|
8
|
+
directDomUpdates = false,
|
|
9
|
+
directDomUpdatesMode = "transform",
|
|
8
10
|
...options
|
|
9
11
|
}) {
|
|
10
12
|
const rerender = React.useReducer((x) => x + 1, 0)[1];
|
|
13
|
+
const directRef = React.useRef({
|
|
14
|
+
enabled: directDomUpdates,
|
|
15
|
+
mode: directDomUpdatesMode,
|
|
16
|
+
container: null,
|
|
17
|
+
lastSize: null,
|
|
18
|
+
// Keyed by the element itself so a remounted node (same key, new DOM
|
|
19
|
+
// node — e.g. when `enabled` is toggled off then on) is treated as fresh
|
|
20
|
+
// and gets its style written.
|
|
21
|
+
lastPositions: /* @__PURE__ */ new WeakMap(),
|
|
22
|
+
prevRange: null
|
|
23
|
+
});
|
|
24
|
+
directRef.current.enabled = directDomUpdates;
|
|
25
|
+
directRef.current.mode = directDomUpdatesMode;
|
|
26
|
+
const applyDirectStyles = (instance2) => {
|
|
27
|
+
const state = directRef.current;
|
|
28
|
+
if (!state.enabled) return;
|
|
29
|
+
const totalSize = instance2.getTotalSize();
|
|
30
|
+
if (state.container && totalSize !== state.lastSize) {
|
|
31
|
+
state.lastSize = totalSize;
|
|
32
|
+
const sizeAxis = instance2.options.horizontal ? "width" : "height";
|
|
33
|
+
state.container.style[sizeAxis] = `${totalSize}px`;
|
|
34
|
+
}
|
|
35
|
+
const horizontal = !!instance2.options.horizontal;
|
|
36
|
+
const useTransform = state.mode === "transform";
|
|
37
|
+
const posAxis = horizontal ? "left" : "top";
|
|
38
|
+
const scrollMargin = instance2.options.scrollMargin;
|
|
39
|
+
const items = instance2.getVirtualItems();
|
|
40
|
+
for (const item of items) {
|
|
41
|
+
const next = item.start - scrollMargin;
|
|
42
|
+
const el = instance2.elementsCache.get(item.key);
|
|
43
|
+
if (!el) continue;
|
|
44
|
+
if (state.lastPositions.get(el) === next) continue;
|
|
45
|
+
state.lastPositions.set(el, next);
|
|
46
|
+
if (useTransform) {
|
|
47
|
+
el.style.transform = horizontal ? `translate3d(${next}px, 0, 0)` : `translate3d(0, ${next}px, 0)`;
|
|
48
|
+
} else {
|
|
49
|
+
el.style[posAxis] = `${next}px`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
11
53
|
const resolvedOptions = {
|
|
12
54
|
...options,
|
|
13
55
|
onChange: (instance2, sync) => {
|
|
14
56
|
var _a;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
57
|
+
const state = directRef.current;
|
|
58
|
+
let shouldRerender = true;
|
|
59
|
+
if (state.enabled) {
|
|
60
|
+
applyDirectStyles(instance2);
|
|
61
|
+
const range = instance2.range;
|
|
62
|
+
const prev = state.prevRange;
|
|
63
|
+
shouldRerender = !prev || prev.isScrolling !== instance2.isScrolling || prev.startIndex !== (range == null ? void 0 : range.startIndex) || prev.endIndex !== (range == null ? void 0 : range.endIndex);
|
|
64
|
+
if (shouldRerender) {
|
|
65
|
+
state.prevRange = range ? {
|
|
66
|
+
startIndex: range.startIndex,
|
|
67
|
+
endIndex: range.endIndex,
|
|
68
|
+
isScrolling: instance2.isScrolling
|
|
69
|
+
} : null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (shouldRerender) {
|
|
73
|
+
if (useFlushSync && sync) {
|
|
74
|
+
flushSync(rerender);
|
|
75
|
+
} else {
|
|
76
|
+
rerender();
|
|
77
|
+
}
|
|
19
78
|
}
|
|
20
79
|
(_a = options.onChange) == null ? void 0 : _a.call(options, instance2, sync);
|
|
21
80
|
}
|
|
22
81
|
};
|
|
23
|
-
const [instance] = React.useState(
|
|
24
|
-
|
|
25
|
-
|
|
82
|
+
const [instance] = React.useState(() => {
|
|
83
|
+
const v = new Virtualizer(resolvedOptions);
|
|
84
|
+
return Object.assign(v, {
|
|
85
|
+
containerRef: (node) => {
|
|
86
|
+
const state = directRef.current;
|
|
87
|
+
state.container = node;
|
|
88
|
+
state.lastSize = null;
|
|
89
|
+
if (node && state.enabled) {
|
|
90
|
+
const total = v.getTotalSize();
|
|
91
|
+
state.lastSize = total;
|
|
92
|
+
const axis = v.options.horizontal ? "width" : "height";
|
|
93
|
+
node.style[axis] = `${total}px`;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
26
98
|
instance.setOptions(resolvedOptions);
|
|
27
99
|
useIsomorphicLayoutEffect(() => {
|
|
28
100
|
return instance._didMount();
|
|
@@ -30,6 +102,9 @@ function useVirtualizerBase({
|
|
|
30
102
|
useIsomorphicLayoutEffect(() => {
|
|
31
103
|
return instance._willUpdate();
|
|
32
104
|
});
|
|
105
|
+
useIsomorphicLayoutEffect(() => {
|
|
106
|
+
applyDirectStyles(instance);
|
|
107
|
+
});
|
|
33
108
|
return instance;
|
|
34
109
|
}
|
|
35
110
|
function useVirtualizer(options) {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): Virtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n if (useFlushSync && sync) {\n flushSync(rerender)\n } else {\n rerender()\n }\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(\n () => new Virtualizer<TScrollElement, TItemElement>(resolvedOptions),\n )\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n return instance._willUpdate()\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Virtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): Virtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["instance"],"mappings":";;;;AAeA,MAAM,4BACJ,OAAO,aAAa,cAAc,MAAM,kBAAkB,MAAM;AASlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,GAAG;AACL,GAGE;AACA,QAAM,WAAW,MAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC,EAAE,CAAC;AAE5D,QAAM,kBAAoE;AAAA,IACxE,GAAG;AAAA,IACH,UAAU,CAACA,WAAU,SAAS;;AAC5B,UAAI,gBAAgB,MAAM;AACxB,kBAAU,QAAQ;AAAA,MACpB,OAAO;AACL,iBAAA;AAAA,MACF;AACA,oBAAQ,aAAR,iCAAmBA,WAAU;AAAA,IAC/B;AAAA,EAAA;AAGF,QAAM,CAAC,QAAQ,IAAI,MAAM;AAAA,IACvB,MAAM,IAAI,YAA0C,eAAe;AAAA,EAAA;AAGrE,WAAS,WAAW,eAAe;AAEnC,4BAA0B,MAAM;AAC9B,WAAO,SAAS,UAAA;AAAA,EAClB,GAAG,CAAA,CAAE;AAEL,4BAA0B,MAAM;AAC9B,WAAO,SAAS,YAAA;AAAA,EAClB,CAAC;AAED,SAAO;AACT;AAEO,SAAS,eAId,SAI2C;AAC3C,SAAO,mBAAiD;AAAA,IACtD;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,GAAG;AAAA,EAAA,CACJ;AACH;AAEO,SAAS,qBACd,SAOmC;AACnC,SAAO,mBAAyC;AAAA,IAC9C,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,IACpE,oBAAoB;AAAA,IACpB,sBAAsB;AAAA,IACtB,YAAY;AAAA,IACZ,eAAe,MAAO,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,IACzE,GAAG;AAAA,EAAA,CACJ;AACH;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport { flushSync } from 'react-dom'\nimport {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nconst useIsomorphicLayoutEffect =\n typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type ReactVirtualizer<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = Virtualizer<TScrollElement, TItemElement> & {\n /**\n * Ref callback for the inner sized container element. Only meaningful when\n * `directDomUpdates: true` — the virtualizer writes the container's\n * main-axis size (`height` or `width`) directly to skip React re-renders.\n */\n containerRef: (node: HTMLElement | null) => void\n}\n\nexport type ReactVirtualizerOptions<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> = VirtualizerOptions<TScrollElement, TItemElement> & {\n useFlushSync?: boolean\n /**\n * Skip React re-renders for scroll-only updates. The virtualizer writes\n * item positions (`top`/`left`) and the container size (`height`/`width`)\n * directly to the DOM, and only re-renders when the visible index range\n * or `isScrolling` changes.\n *\n * Requirements when enabled:\n * - Item elements must be `position: absolute`; in `'transform'` mode they\n * must also be anchored with `top: 0` / `left: 0`.\n * - Item elements must NOT set the main-axis position in their style — the\n * virtualizer owns `top` / `left` in `'position'` mode and `transform` in\n * `'transform'` mode.\n * - The inner sized container must receive `virtualizer.containerRef` and\n * must NOT set `height` / `width` in its style.\n * - For multi-lane layouts (grids / masonry), the cross-axis position\n * (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and\n * must still be set in your JSX — only the main axis is automated.\n *\n * This flag is intended to be set once at mount. Toggling it (or\n * `directDomUpdatesMode`) at runtime can leave stale inline styles on\n * items and the container.\n */\n directDomUpdates?: boolean\n /**\n * How `directDomUpdates` positions item elements.\n * - `'transform'` (default): writes `transform: translate3d(...)`.\n * Promotes items to their own compositor layer — usually smoother on long\n * lists, but creates a stacking context and can interfere with\n * `position: fixed` descendants. Item elements must still be anchored with\n * `position: absolute`, `top: 0`, and `left: 0`.\n * - `'position'`: writes `top` / `left`. Item elements must be\n * `position: absolute`.\n */\n directDomUpdatesMode?: 'position' | 'transform'\n}\n\nfunction useVirtualizerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n>({\n useFlushSync = true,\n directDomUpdates = false,\n directDomUpdatesMode = 'transform',\n ...options\n}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<\n TScrollElement,\n TItemElement\n> {\n const rerender = React.useReducer((x: number) => x + 1, 0)[1]\n\n // Mutable across renders so the onChange closure captured by setOptions\n // always reads the latest values without us having to re-create it.\n const directRef = React.useRef({\n enabled: directDomUpdates,\n mode: directDomUpdatesMode,\n container: null as HTMLElement | null,\n lastSize: null as number | null,\n // Keyed by the element itself so a remounted node (same key, new DOM\n // node — e.g. when `enabled` is toggled off then on) is treated as fresh\n // and gets its style written.\n lastPositions: new WeakMap<HTMLElement, number>(),\n prevRange: null as {\n startIndex: number\n endIndex: number\n isScrolling: boolean\n } | null,\n })\n directRef.current.enabled = directDomUpdates\n directRef.current.mode = directDomUpdatesMode\n\n // Writes container size + item positions to the DOM. Idempotent — guarded\n // by lastSize / lastPositions. Called from onChange (covers scroll-driven\n // updates) and from a layout effect (covers post-render commits when refs\n // have just registered new items in elementsCache).\n const applyDirectStyles = (\n instance: Virtualizer<TScrollElement, TItemElement>,\n ) => {\n const state = directRef.current\n if (!state.enabled) return\n\n const totalSize = instance.getTotalSize()\n if (state.container && totalSize !== state.lastSize) {\n state.lastSize = totalSize\n const sizeAxis = instance.options.horizontal ? 'width' : 'height'\n state.container.style[sizeAxis] = `${totalSize}px`\n }\n\n const horizontal = !!instance.options.horizontal\n const useTransform = state.mode === 'transform'\n const posAxis = horizontal ? 'left' : 'top'\n const scrollMargin = instance.options.scrollMargin\n const items = instance.getVirtualItems()\n for (const item of items) {\n const next = item.start - scrollMargin\n const el = instance.elementsCache.get(item.key) as HTMLElement | undefined\n if (!el) continue\n if (state.lastPositions.get(el) === next) continue\n state.lastPositions.set(el, next)\n if (useTransform) {\n el.style.transform = horizontal\n ? `translate3d(${next}px, 0, 0)`\n : `translate3d(0, ${next}px, 0)`\n } else {\n el.style[posAxis] = `${next}px`\n }\n }\n }\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n const state = directRef.current\n let shouldRerender = true\n\n if (state.enabled) {\n applyDirectStyles(instance)\n\n // Only re-render on range / isScrolling changes\n const range = instance.range\n const prev = state.prevRange\n shouldRerender =\n !prev ||\n prev.isScrolling !== instance.isScrolling ||\n prev.startIndex !== range?.startIndex ||\n prev.endIndex !== range?.endIndex\n if (shouldRerender) {\n state.prevRange = range\n ? {\n startIndex: range.startIndex,\n endIndex: range.endIndex,\n isScrolling: instance.isScrolling,\n }\n : null\n }\n }\n\n if (shouldRerender) {\n if (useFlushSync && sync) {\n flushSync(rerender)\n } else {\n rerender()\n }\n }\n\n options.onChange?.(instance, sync)\n },\n }\n\n const [instance] = React.useState(() => {\n const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)\n return Object.assign(v, {\n containerRef: (node: HTMLElement | null) => {\n const state = directRef.current\n state.container = node\n state.lastSize = null\n if (node && state.enabled) {\n const total = v.getTotalSize()\n state.lastSize = total\n const axis = v.options.horizontal ? 'width' : 'height'\n node.style[axis] = `${total}px`\n }\n },\n })\n })\n\n instance.setOptions(resolvedOptions)\n\n useIsomorphicLayoutEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n return instance._willUpdate()\n })\n\n // After every render commit, newly mounted item refs have registered in\n // elementsCache; write their positions to the DOM so the user doesn't see\n // them at (0, 0) until the next onChange.\n useIsomorphicLayoutEffect(() => {\n applyDirectStyles(instance)\n })\n\n return instance\n}\n\nexport function useVirtualizer<\n TScrollElement extends Element,\n TItemElement extends Element,\n>(\n options: PartialKeys<\n ReactVirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): ReactVirtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement extends Element>(\n options: PartialKeys<\n ReactVirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): ReactVirtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),\n ...options,\n })\n}\n"],"names":["instance"],"mappings":";;;;AAeA,MAAM,4BACJ,OAAO,aAAa,cAAc,MAAM,kBAAkB,MAAM;AAuDlE,SAAS,mBAGP;AAAA,EACA,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,uBAAuB;AAAA,EACvB,GAAG;AACL,GAGE;AACA,QAAM,WAAW,MAAM,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC,EAAE,CAAC;AAI5D,QAAM,YAAY,MAAM,OAAO;AAAA,IAC7B,SAAS;AAAA,IACT,MAAM;AAAA,IACN,WAAW;AAAA,IACX,UAAU;AAAA;AAAA;AAAA;AAAA,IAIV,mCAAmB,QAAA;AAAA,IACnB,WAAW;AAAA,EAAA,CAKZ;AACD,YAAU,QAAQ,UAAU;AAC5B,YAAU,QAAQ,OAAO;AAMzB,QAAM,oBAAoB,CACxBA,cACG;AACH,UAAM,QAAQ,UAAU;AACxB,QAAI,CAAC,MAAM,QAAS;AAEpB,UAAM,YAAYA,UAAS,aAAA;AAC3B,QAAI,MAAM,aAAa,cAAc,MAAM,UAAU;AACnD,YAAM,WAAW;AACjB,YAAM,WAAWA,UAAS,QAAQ,aAAa,UAAU;AACzD,YAA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
|
4
4
|
"description": "Headless UI for virtualizing scrollable elements in React",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"src"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@tanstack/virtual-core": "3.16.
|
|
48
|
+
"@tanstack/virtual-core": "3.16.1"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@testing-library/react": "^16.3.0",
|
package/src/index.tsx
CHANGED
|
@@ -16,11 +16,57 @@ export * from '@tanstack/virtual-core'
|
|
|
16
16
|
const useIsomorphicLayoutEffect =
|
|
17
17
|
typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
18
18
|
|
|
19
|
+
export type ReactVirtualizer<
|
|
20
|
+
TScrollElement extends Element | Window,
|
|
21
|
+
TItemElement extends Element,
|
|
22
|
+
> = Virtualizer<TScrollElement, TItemElement> & {
|
|
23
|
+
/**
|
|
24
|
+
* Ref callback for the inner sized container element. Only meaningful when
|
|
25
|
+
* `directDomUpdates: true` — the virtualizer writes the container's
|
|
26
|
+
* main-axis size (`height` or `width`) directly to skip React re-renders.
|
|
27
|
+
*/
|
|
28
|
+
containerRef: (node: HTMLElement | null) => void
|
|
29
|
+
}
|
|
30
|
+
|
|
19
31
|
export type ReactVirtualizerOptions<
|
|
20
32
|
TScrollElement extends Element | Window,
|
|
21
33
|
TItemElement extends Element,
|
|
22
34
|
> = VirtualizerOptions<TScrollElement, TItemElement> & {
|
|
23
35
|
useFlushSync?: boolean
|
|
36
|
+
/**
|
|
37
|
+
* Skip React re-renders for scroll-only updates. The virtualizer writes
|
|
38
|
+
* item positions (`top`/`left`) and the container size (`height`/`width`)
|
|
39
|
+
* directly to the DOM, and only re-renders when the visible index range
|
|
40
|
+
* or `isScrolling` changes.
|
|
41
|
+
*
|
|
42
|
+
* Requirements when enabled:
|
|
43
|
+
* - Item elements must be `position: absolute`; in `'transform'` mode they
|
|
44
|
+
* must also be anchored with `top: 0` / `left: 0`.
|
|
45
|
+
* - Item elements must NOT set the main-axis position in their style — the
|
|
46
|
+
* virtualizer owns `top` / `left` in `'position'` mode and `transform` in
|
|
47
|
+
* `'transform'` mode.
|
|
48
|
+
* - The inner sized container must receive `virtualizer.containerRef` and
|
|
49
|
+
* must NOT set `height` / `width` in its style.
|
|
50
|
+
* - For multi-lane layouts (grids / masonry), the cross-axis position
|
|
51
|
+
* (e.g. `left: ${(item.lane * 100) / lanes}%`) is stable per item and
|
|
52
|
+
* must still be set in your JSX — only the main axis is automated.
|
|
53
|
+
*
|
|
54
|
+
* This flag is intended to be set once at mount. Toggling it (or
|
|
55
|
+
* `directDomUpdatesMode`) at runtime can leave stale inline styles on
|
|
56
|
+
* items and the container.
|
|
57
|
+
*/
|
|
58
|
+
directDomUpdates?: boolean
|
|
59
|
+
/**
|
|
60
|
+
* How `directDomUpdates` positions item elements.
|
|
61
|
+
* - `'transform'` (default): writes `transform: translate3d(...)`.
|
|
62
|
+
* Promotes items to their own compositor layer — usually smoother on long
|
|
63
|
+
* lists, but creates a stacking context and can interfere with
|
|
64
|
+
* `position: fixed` descendants. Item elements must still be anchored with
|
|
65
|
+
* `position: absolute`, `top: 0`, and `left: 0`.
|
|
66
|
+
* - `'position'`: writes `top` / `left`. Item elements must be
|
|
67
|
+
* `position: absolute`.
|
|
68
|
+
*/
|
|
69
|
+
directDomUpdatesMode?: 'position' | 'transform'
|
|
24
70
|
}
|
|
25
71
|
|
|
26
72
|
function useVirtualizerBase<
|
|
@@ -28,28 +74,129 @@ function useVirtualizerBase<
|
|
|
28
74
|
TItemElement extends Element,
|
|
29
75
|
>({
|
|
30
76
|
useFlushSync = true,
|
|
77
|
+
directDomUpdates = false,
|
|
78
|
+
directDomUpdatesMode = 'transform',
|
|
31
79
|
...options
|
|
32
|
-
}: ReactVirtualizerOptions<TScrollElement, TItemElement>):
|
|
80
|
+
}: ReactVirtualizerOptions<TScrollElement, TItemElement>): ReactVirtualizer<
|
|
33
81
|
TScrollElement,
|
|
34
82
|
TItemElement
|
|
35
83
|
> {
|
|
36
84
|
const rerender = React.useReducer((x: number) => x + 1, 0)[1]
|
|
37
85
|
|
|
86
|
+
// Mutable across renders so the onChange closure captured by setOptions
|
|
87
|
+
// always reads the latest values without us having to re-create it.
|
|
88
|
+
const directRef = React.useRef({
|
|
89
|
+
enabled: directDomUpdates,
|
|
90
|
+
mode: directDomUpdatesMode,
|
|
91
|
+
container: null as HTMLElement | null,
|
|
92
|
+
lastSize: null as number | null,
|
|
93
|
+
// Keyed by the element itself so a remounted node (same key, new DOM
|
|
94
|
+
// node — e.g. when `enabled` is toggled off then on) is treated as fresh
|
|
95
|
+
// and gets its style written.
|
|
96
|
+
lastPositions: new WeakMap<HTMLElement, number>(),
|
|
97
|
+
prevRange: null as {
|
|
98
|
+
startIndex: number
|
|
99
|
+
endIndex: number
|
|
100
|
+
isScrolling: boolean
|
|
101
|
+
} | null,
|
|
102
|
+
})
|
|
103
|
+
directRef.current.enabled = directDomUpdates
|
|
104
|
+
directRef.current.mode = directDomUpdatesMode
|
|
105
|
+
|
|
106
|
+
// Writes container size + item positions to the DOM. Idempotent — guarded
|
|
107
|
+
// by lastSize / lastPositions. Called from onChange (covers scroll-driven
|
|
108
|
+
// updates) and from a layout effect (covers post-render commits when refs
|
|
109
|
+
// have just registered new items in elementsCache).
|
|
110
|
+
const applyDirectStyles = (
|
|
111
|
+
instance: Virtualizer<TScrollElement, TItemElement>,
|
|
112
|
+
) => {
|
|
113
|
+
const state = directRef.current
|
|
114
|
+
if (!state.enabled) return
|
|
115
|
+
|
|
116
|
+
const totalSize = instance.getTotalSize()
|
|
117
|
+
if (state.container && totalSize !== state.lastSize) {
|
|
118
|
+
state.lastSize = totalSize
|
|
119
|
+
const sizeAxis = instance.options.horizontal ? 'width' : 'height'
|
|
120
|
+
state.container.style[sizeAxis] = `${totalSize}px`
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const horizontal = !!instance.options.horizontal
|
|
124
|
+
const useTransform = state.mode === 'transform'
|
|
125
|
+
const posAxis = horizontal ? 'left' : 'top'
|
|
126
|
+
const scrollMargin = instance.options.scrollMargin
|
|
127
|
+
const items = instance.getVirtualItems()
|
|
128
|
+
for (const item of items) {
|
|
129
|
+
const next = item.start - scrollMargin
|
|
130
|
+
const el = instance.elementsCache.get(item.key) as HTMLElement | undefined
|
|
131
|
+
if (!el) continue
|
|
132
|
+
if (state.lastPositions.get(el) === next) continue
|
|
133
|
+
state.lastPositions.set(el, next)
|
|
134
|
+
if (useTransform) {
|
|
135
|
+
el.style.transform = horizontal
|
|
136
|
+
? `translate3d(${next}px, 0, 0)`
|
|
137
|
+
: `translate3d(0, ${next}px, 0)`
|
|
138
|
+
} else {
|
|
139
|
+
el.style[posAxis] = `${next}px`
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
38
144
|
const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {
|
|
39
145
|
...options,
|
|
40
146
|
onChange: (instance, sync) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
147
|
+
const state = directRef.current
|
|
148
|
+
let shouldRerender = true
|
|
149
|
+
|
|
150
|
+
if (state.enabled) {
|
|
151
|
+
applyDirectStyles(instance)
|
|
152
|
+
|
|
153
|
+
// Only re-render on range / isScrolling changes
|
|
154
|
+
const range = instance.range
|
|
155
|
+
const prev = state.prevRange
|
|
156
|
+
shouldRerender =
|
|
157
|
+
!prev ||
|
|
158
|
+
prev.isScrolling !== instance.isScrolling ||
|
|
159
|
+
prev.startIndex !== range?.startIndex ||
|
|
160
|
+
prev.endIndex !== range?.endIndex
|
|
161
|
+
if (shouldRerender) {
|
|
162
|
+
state.prevRange = range
|
|
163
|
+
? {
|
|
164
|
+
startIndex: range.startIndex,
|
|
165
|
+
endIndex: range.endIndex,
|
|
166
|
+
isScrolling: instance.isScrolling,
|
|
167
|
+
}
|
|
168
|
+
: null
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (shouldRerender) {
|
|
173
|
+
if (useFlushSync && sync) {
|
|
174
|
+
flushSync(rerender)
|
|
175
|
+
} else {
|
|
176
|
+
rerender()
|
|
177
|
+
}
|
|
45
178
|
}
|
|
179
|
+
|
|
46
180
|
options.onChange?.(instance, sync)
|
|
47
181
|
},
|
|
48
182
|
}
|
|
49
183
|
|
|
50
|
-
const [instance] = React.useState(
|
|
51
|
-
|
|
52
|
-
|
|
184
|
+
const [instance] = React.useState(() => {
|
|
185
|
+
const v = new Virtualizer<TScrollElement, TItemElement>(resolvedOptions)
|
|
186
|
+
return Object.assign(v, {
|
|
187
|
+
containerRef: (node: HTMLElement | null) => {
|
|
188
|
+
const state = directRef.current
|
|
189
|
+
state.container = node
|
|
190
|
+
state.lastSize = null
|
|
191
|
+
if (node && state.enabled) {
|
|
192
|
+
const total = v.getTotalSize()
|
|
193
|
+
state.lastSize = total
|
|
194
|
+
const axis = v.options.horizontal ? 'width' : 'height'
|
|
195
|
+
node.style[axis] = `${total}px`
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
})
|
|
53
200
|
|
|
54
201
|
instance.setOptions(resolvedOptions)
|
|
55
202
|
|
|
@@ -61,6 +208,13 @@ function useVirtualizerBase<
|
|
|
61
208
|
return instance._willUpdate()
|
|
62
209
|
})
|
|
63
210
|
|
|
211
|
+
// After every render commit, newly mounted item refs have registered in
|
|
212
|
+
// elementsCache; write their positions to the DOM so the user doesn't see
|
|
213
|
+
// them at (0, 0) until the next onChange.
|
|
214
|
+
useIsomorphicLayoutEffect(() => {
|
|
215
|
+
applyDirectStyles(instance)
|
|
216
|
+
})
|
|
217
|
+
|
|
64
218
|
return instance
|
|
65
219
|
}
|
|
66
220
|
|
|
@@ -72,7 +226,7 @@ export function useVirtualizer<
|
|
|
72
226
|
ReactVirtualizerOptions<TScrollElement, TItemElement>,
|
|
73
227
|
'observeElementRect' | 'observeElementOffset' | 'scrollToFn'
|
|
74
228
|
>,
|
|
75
|
-
):
|
|
229
|
+
): ReactVirtualizer<TScrollElement, TItemElement> {
|
|
76
230
|
return useVirtualizerBase<TScrollElement, TItemElement>({
|
|
77
231
|
observeElementRect: observeElementRect,
|
|
78
232
|
observeElementOffset: observeElementOffset,
|
|
@@ -89,7 +243,7 @@ export function useWindowVirtualizer<TItemElement extends Element>(
|
|
|
89
243
|
| 'observeElementOffset'
|
|
90
244
|
| 'scrollToFn'
|
|
91
245
|
>,
|
|
92
|
-
):
|
|
246
|
+
): ReactVirtualizer<Window, TItemElement> {
|
|
93
247
|
return useVirtualizerBase<Window, TItemElement>({
|
|
94
248
|
getScrollElement: () => (typeof document !== 'undefined' ? window : null),
|
|
95
249
|
observeElementRect: observeWindowRect,
|