react-layout-virtual 0.0.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/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # react-layout-virtual
2
+
3
+ React virtual scrolling component for responsive lists and grids with dynamic item sizes.
4
+
5
+ - Homepage: [https://itihon.github.io/layout-virtual/](https://itihon.github.io/layout-virtual/)
6
+ - GitHub: [https://github.com/itihon/layout-virtual](https://github.com/itihon/layout-virtual)
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @fileoverview VirtualizedList React component.
3
+ * @license MIT
4
+ * @author Alexandr Kalabin
5
+ */
6
+ import React from 'react';
7
+ export interface ListItemProps<T = unknown> {
8
+ data: T;
9
+ ref: React.Ref<HTMLDivElement> | undefined;
10
+ index: number;
11
+ }
12
+ export interface VirtualizedListReactProps<T> {
13
+ scrollerRef?: React.RefObject<HTMLDivElement>;
14
+ overscanHeight?: number;
15
+ data: T[];
16
+ renderItem: (props: ListItemProps<T>) => React.ReactNode;
17
+ }
18
+ export default function VirtualizedListReact<T>(props: VirtualizedListReactProps<T>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @fileoverview ReactRenderer.
3
+ * @license MIT
4
+ * @author Alexandr Kalabin
5
+ */
6
+ import { ScrollableContainer } from "layout-virtual";
7
+ import type { IRangeRenderer, ScrollDirection, IItemStore, IItem, VirtualScrollStructure } from "layout-virtual/types";
8
+ type ReactRendererOptions = {
9
+ itemsSetter: React.Dispatch<React.SetStateAction<React.ReactNode[]>>;
10
+ } & VirtualScrollStructure;
11
+ interface IReactRenderer {
12
+ commit: () => void;
13
+ }
14
+ export default class ReactRenderer implements IRangeRenderer, IReactRenderer {
15
+ private _store;
16
+ private _scrollableContainer;
17
+ private _renderedIndexRegistry;
18
+ private _renderedItemsRegistry;
19
+ private _itemsSetter;
20
+ private _renderedRangeRefPool;
21
+ private _listItems;
22
+ private _flushItems;
23
+ private _getRenderedBoundaryIndex;
24
+ constructor(opts: ReactRendererOptions);
25
+ render(startIndex: number, endIndex: number, direction: ScrollDirection): number;
26
+ renderRange(startIndex: number, endIndex: number, direction: ScrollDirection): void;
27
+ removeRange(startIndex: number, endIndex: number, direction: ScrollDirection): number;
28
+ clear(): void;
29
+ getIndex(item: Element): number | undefined;
30
+ getItem(index: number): Element | undefined;
31
+ get scrollableContainer(): ScrollableContainer;
32
+ attach(store: IItemStore<IItem>): void;
33
+ flush(): Promise<void>;
34
+ commit(): void;
35
+ }
36
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @fileoverview Package entry point
3
+ * @license MIT
4
+ * @author Alexandr Kalabin
5
+ */
6
+ export { default } from './ReactLayoutVirtual';
package/dist/index.js ADDED
@@ -0,0 +1,372 @@
1
+ import re, { useRef as g, useState as te, useLayoutEffect as ne, useEffect as se } from "react";
2
+ import { flushSync as oe, createPortal as ae } from "react-dom";
3
+ import le, { ScrollableContainer as ie, ArrayItemStore as ce, DynamicListLayout as ue } from "layout-virtual";
4
+ var O = { exports: {} }, y = {};
5
+ var V;
6
+ function fe() {
7
+ if (V) return y;
8
+ V = 1;
9
+ var v = /* @__PURE__ */ Symbol.for("react.transitional.element"), t = /* @__PURE__ */ Symbol.for("react.fragment");
10
+ function c(l, n, o) {
11
+ var s = null;
12
+ if (o !== void 0 && (s = "" + o), n.key !== void 0 && (s = "" + n.key), "key" in n) {
13
+ o = {};
14
+ for (var i in n)
15
+ i !== "key" && (o[i] = n[i]);
16
+ } else o = n;
17
+ return n = o.ref, {
18
+ $$typeof: v,
19
+ type: l,
20
+ key: s,
21
+ ref: n !== void 0 ? n : null,
22
+ props: o
23
+ };
24
+ }
25
+ return y.Fragment = t, y.jsx = c, y.jsxs = c, y;
26
+ }
27
+ var S = {};
28
+ var z;
29
+ function de() {
30
+ return z || (z = 1, process.env.NODE_ENV !== "production" && (function() {
31
+ function v(e) {
32
+ if (e == null) return null;
33
+ if (typeof e == "function")
34
+ return e.$$typeof === Q ? null : e.displayName || e.name || null;
35
+ if (typeof e == "string") return e;
36
+ switch (e) {
37
+ case T:
38
+ return "Fragment";
39
+ case j:
40
+ return "Profiler";
41
+ case P:
42
+ return "StrictMode";
43
+ case J:
44
+ return "Suspense";
45
+ case B:
46
+ return "SuspenseList";
47
+ case Z:
48
+ return "Activity";
49
+ }
50
+ if (typeof e == "object")
51
+ switch (typeof e.tag == "number" && console.error(
52
+ "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
53
+ ), e.$$typeof) {
54
+ case w:
55
+ return "Portal";
56
+ case G:
57
+ return e.displayName || "Context";
58
+ case I:
59
+ return (e._context.displayName || "Context") + ".Consumer";
60
+ case H:
61
+ var r = e.render;
62
+ return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
63
+ case X:
64
+ return r = e.displayName || null, r !== null ? r : v(e.type) || "Memo";
65
+ case A:
66
+ r = e._payload, e = e._init;
67
+ try {
68
+ return v(e(r));
69
+ } catch {
70
+ }
71
+ }
72
+ return null;
73
+ }
74
+ function t(e) {
75
+ return "" + e;
76
+ }
77
+ function c(e) {
78
+ try {
79
+ t(e);
80
+ var r = !1;
81
+ } catch {
82
+ r = !0;
83
+ }
84
+ if (r) {
85
+ r = console;
86
+ var u = r.error, d = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
87
+ return u.call(
88
+ r,
89
+ "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
90
+ d
91
+ ), t(e);
92
+ }
93
+ }
94
+ function l(e) {
95
+ if (e === T) return "<>";
96
+ if (typeof e == "object" && e !== null && e.$$typeof === A)
97
+ return "<...>";
98
+ try {
99
+ var r = v(e);
100
+ return r ? "<" + r + ">" : "<...>";
101
+ } catch {
102
+ return "<...>";
103
+ }
104
+ }
105
+ function n() {
106
+ var e = C.A;
107
+ return e === null ? null : e.getOwner();
108
+ }
109
+ function o() {
110
+ return Error("react-stack-top-frame");
111
+ }
112
+ function s(e) {
113
+ if (Y.call(e, "key")) {
114
+ var r = Object.getOwnPropertyDescriptor(e, "key").get;
115
+ if (r && r.isReactWarning) return !1;
116
+ }
117
+ return e.key !== void 0;
118
+ }
119
+ function i(e, r) {
120
+ function u() {
121
+ F || (F = !0, console.error(
122
+ "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
123
+ r
124
+ ));
125
+ }
126
+ u.isReactWarning = !0, Object.defineProperty(e, "key", {
127
+ get: u,
128
+ configurable: !0
129
+ });
130
+ }
131
+ function a() {
132
+ var e = v(this.type);
133
+ return $[e] || ($[e] = !0, console.error(
134
+ "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
135
+ )), e = this.props.ref, e !== void 0 ? e : null;
136
+ }
137
+ function f(e, r, u, d, k, L) {
138
+ var m = u.ref;
139
+ return e = {
140
+ $$typeof: b,
141
+ type: e,
142
+ key: r,
143
+ props: u,
144
+ _owner: d
145
+ }, (m !== void 0 ? m : null) !== null ? Object.defineProperty(e, "ref", {
146
+ enumerable: !1,
147
+ get: a
148
+ }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
149
+ configurable: !1,
150
+ enumerable: !1,
151
+ writable: !0,
152
+ value: 0
153
+ }), Object.defineProperty(e, "_debugInfo", {
154
+ configurable: !1,
155
+ enumerable: !1,
156
+ writable: !0,
157
+ value: null
158
+ }), Object.defineProperty(e, "_debugStack", {
159
+ configurable: !1,
160
+ enumerable: !1,
161
+ writable: !0,
162
+ value: k
163
+ }), Object.defineProperty(e, "_debugTask", {
164
+ configurable: !1,
165
+ enumerable: !1,
166
+ writable: !0,
167
+ value: L
168
+ }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
169
+ }
170
+ function _(e, r, u, d, k, L) {
171
+ var m = r.children;
172
+ if (m !== void 0)
173
+ if (d)
174
+ if (K(m)) {
175
+ for (d = 0; d < m.length; d++)
176
+ R(m[d]);
177
+ Object.freeze && Object.freeze(m);
178
+ } else
179
+ console.error(
180
+ "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
181
+ );
182
+ else R(m);
183
+ if (Y.call(r, "key")) {
184
+ m = v(e);
185
+ var x = Object.keys(r).filter(function(ee) {
186
+ return ee !== "key";
187
+ });
188
+ d = 0 < x.length ? "{key: someKey, " + x.join(": ..., ") + ": ...}" : "{key: someKey}", U[m + d] || (x = 0 < x.length ? "{" + x.join(": ..., ") + ": ...}" : "{}", console.error(
189
+ `A props object containing a "key" prop is being spread into JSX:
190
+ let props = %s;
191
+ <%s {...props} />
192
+ React keys must be passed directly to JSX without using spread:
193
+ let props = %s;
194
+ <%s key={someKey} {...props} />`,
195
+ d,
196
+ m,
197
+ x,
198
+ m
199
+ ), U[m + d] = !0);
200
+ }
201
+ if (m = null, u !== void 0 && (c(u), m = "" + u), s(r) && (c(r.key), m = "" + r.key), "key" in r) {
202
+ u = {};
203
+ for (var M in r)
204
+ M !== "key" && (u[M] = r[M]);
205
+ } else u = r;
206
+ return m && i(
207
+ u,
208
+ typeof e == "function" ? e.displayName || e.name || "Unknown" : e
209
+ ), f(
210
+ e,
211
+ m,
212
+ u,
213
+ n(),
214
+ k,
215
+ L
216
+ );
217
+ }
218
+ function R(e) {
219
+ E(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === A && (e._payload.status === "fulfilled" ? E(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
220
+ }
221
+ function E(e) {
222
+ return typeof e == "object" && e !== null && e.$$typeof === b;
223
+ }
224
+ var p = re, b = /* @__PURE__ */ Symbol.for("react.transitional.element"), w = /* @__PURE__ */ Symbol.for("react.portal"), T = /* @__PURE__ */ Symbol.for("react.fragment"), P = /* @__PURE__ */ Symbol.for("react.strict_mode"), j = /* @__PURE__ */ Symbol.for("react.profiler"), I = /* @__PURE__ */ Symbol.for("react.consumer"), G = /* @__PURE__ */ Symbol.for("react.context"), H = /* @__PURE__ */ Symbol.for("react.forward_ref"), J = /* @__PURE__ */ Symbol.for("react.suspense"), B = /* @__PURE__ */ Symbol.for("react.suspense_list"), X = /* @__PURE__ */ Symbol.for("react.memo"), A = /* @__PURE__ */ Symbol.for("react.lazy"), Z = /* @__PURE__ */ Symbol.for("react.activity"), Q = /* @__PURE__ */ Symbol.for("react.client.reference"), C = p.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Y = Object.prototype.hasOwnProperty, K = Array.isArray, N = console.createTask ? console.createTask : function() {
225
+ return null;
226
+ };
227
+ p = {
228
+ react_stack_bottom_frame: function(e) {
229
+ return e();
230
+ }
231
+ };
232
+ var F, $ = {}, D = p.react_stack_bottom_frame.bind(
233
+ p,
234
+ o
235
+ )(), W = N(l(o)), U = {};
236
+ S.Fragment = T, S.jsx = function(e, r, u) {
237
+ var d = 1e4 > C.recentlyCreatedOwnerStacks++;
238
+ return _(
239
+ e,
240
+ r,
241
+ u,
242
+ !1,
243
+ d ? Error("react-stack-top-frame") : D,
244
+ d ? N(l(e)) : W
245
+ );
246
+ }, S.jsxs = function(e, r, u) {
247
+ var d = 1e4 > C.recentlyCreatedOwnerStacks++;
248
+ return _(
249
+ e,
250
+ r,
251
+ u,
252
+ !0,
253
+ d ? Error("react-stack-top-frame") : D,
254
+ d ? N(l(e)) : W
255
+ );
256
+ };
257
+ })()), S;
258
+ }
259
+ var q;
260
+ function me() {
261
+ return q || (q = 1, process.env.NODE_ENV === "production" ? O.exports = fe() : O.exports = de()), O.exports;
262
+ }
263
+ var h = me();
264
+ class _e {
265
+ _store = null;
266
+ _scrollableContainer;
267
+ _renderedIndexRegistry = /* @__PURE__ */ new Map();
268
+ _renderedItemsRegistry = /* @__PURE__ */ new Map();
269
+ _itemsSetter;
270
+ _renderedRangeRefPool = /* @__PURE__ */ new Map();
271
+ _listItems = [];
272
+ _flushItems = () => {
273
+ this._itemsSetter(this._listItems);
274
+ };
275
+ _getRenderedBoundaryIndex(t) {
276
+ const c = t === "first" ? this._scrollableContainer.getFirstItem() : t === "last" ? this._scrollableContainer.getLastItem() : null;
277
+ if (c)
278
+ return this.getIndex(c);
279
+ }
280
+ constructor(t) {
281
+ this._scrollableContainer = new ie({ ...t }), this._itemsSetter = t.itemsSetter;
282
+ }
283
+ render(t, c, l) {
284
+ const n = this._getRenderedBoundaryIndex("first"), o = this._getRenderedBoundaryIndex("last");
285
+ let s = t, i = c, a = n, f = o, _ = 0;
286
+ return l === "down" ? (console.log("SCROLLING DOWN"), a !== void 0 && o !== void 0 && (f = Math.min(s - 1, o), s = Math.max(o + 1, s), a <= f && (_ = this.removeRange(a, f, l)))) : l === "up" && (console.log("SCROLLING UP"), f !== void 0 && n !== void 0 && (a = Math.max(i + 1, n), i = Math.min(n - 1, i), a <= f && (_ = this.removeRange(a, f, l)))), s < i && this.renderRange(s, i, l), _;
287
+ }
288
+ renderRange(t, c, l) {
289
+ console.log("_renderRange", t, c, l), t > c && console.error("_renderRange", t, c, l);
290
+ const n = this._store, o = this._listItems, s = [], i = this._renderedRangeRefPool;
291
+ if (n) {
292
+ for (let a = t; a <= c; a++) {
293
+ const f = n.getByIndex(a);
294
+ if (f) {
295
+ const _ = f.render, R = i.get(a) || { current: null, idx: a };
296
+ i.set(a, R), s.push(/* @__PURE__ */ h.jsx(_, { data: f.data, ref: R, index: a }, a));
297
+ }
298
+ }
299
+ this._listItems = l === "down" ? o.concat(s) : l === "up" ? s.concat(o) : this._listItems;
300
+ }
301
+ }
302
+ removeRange(t, c, l) {
303
+ const n = this._renderedIndexRegistry, o = this._renderedItemsRegistry;
304
+ let s = 0, i = 1 / 0, a = 0;
305
+ for (let f = t; f <= c; f++) {
306
+ const _ = o.get(f);
307
+ if (_) {
308
+ const { offsetTop: R, offsetHeight: E } = _;
309
+ i = Math.min(i, R), a = Math.max(a, R + E), o.delete(f), n.delete(_), s++;
310
+ }
311
+ }
312
+ return s && (this._listItems = l === "down" ? this._listItems.slice(s) : l === "up" ? this._listItems.slice(0, -s) : this._listItems), console.log("_removeItems startIndex:", t, "endIndex:", c, "removedHeight:", a > i ? a - i : 0, "removedItemsCount:", s), a > i ? a - i : 0;
313
+ }
314
+ clear() {
315
+ this._renderedIndexRegistry.clear(), this._renderedItemsRegistry.clear(), this._listItems = [], this._itemsSetter(this._listItems);
316
+ }
317
+ getIndex(t) {
318
+ return this._renderedIndexRegistry.get(t);
319
+ }
320
+ getItem(t) {
321
+ return this._renderedItemsRegistry.get(t);
322
+ }
323
+ get scrollableContainer() {
324
+ return this._scrollableContainer;
325
+ }
326
+ attach(t) {
327
+ this._store = t;
328
+ }
329
+ flush() {
330
+ return oe(this._flushItems), Promise.resolve();
331
+ }
332
+ commit() {
333
+ const t = this._renderedIndexRegistry, c = this._renderedItemsRegistry, l = this._renderedRangeRefPool;
334
+ for (const n of l.values()) {
335
+ const { idx: o, current: s } = n;
336
+ s && (t.set(s, o), c.set(o, s));
337
+ }
338
+ l.clear();
339
+ }
340
+ }
341
+ function ge(v) {
342
+ const { overscanHeight: t = 200, data: c, renderItem: l, scrollerRef: n } = v, o = n || g(null), s = g(null), i = g(null), a = g(null), f = g(null), _ = g(null), R = g(null), [E, p] = te([]), b = g(void 0);
343
+ ne(() => {
344
+ b.current = new _e({
345
+ container: o.current,
346
+ scrollHeightFiller: s.current,
347
+ viewportContainer: i.current,
348
+ scrollCanvas: a.current,
349
+ topSpacer: f.current,
350
+ contentLayer: _.current,
351
+ bottomSpacer: R.current,
352
+ itemsSetter: p
353
+ });
354
+ const T = new ce(), P = new ue({ overscanHeight: t, renderer: b.current }), j = new le({ store: T, layout: P });
355
+ for (let I = 0; I < c.length; I++)
356
+ j.insert({ data: c[I], render: l }, I);
357
+ }, []), se(() => {
358
+ b.current?.commit();
359
+ }, [E]);
360
+ const w = /* @__PURE__ */ h.jsxs(h.Fragment, { children: [
361
+ /* @__PURE__ */ h.jsx("div", { ref: s }),
362
+ /* @__PURE__ */ h.jsx("div", { ref: i, children: /* @__PURE__ */ h.jsxs("div", { ref: a, children: [
363
+ /* @__PURE__ */ h.jsx("div", { ref: f }),
364
+ /* @__PURE__ */ h.jsx("div", { ref: _, children: E }),
365
+ /* @__PURE__ */ h.jsx("div", { ref: R })
366
+ ] }) })
367
+ ] });
368
+ return n && n.current ? ae(w, n.current) : /* @__PURE__ */ h.jsx("div", { ref: o, children: w });
369
+ }
370
+ export {
371
+ ge as default
372
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "react-layout-virtual",
3
+ "version": "0.0.1",
4
+ "description": "React virtual scrolling component for responsive lists and grids with dynamic item sizes.",
5
+ "keywords": [
6
+ "virtual",
7
+ "virtualized",
8
+ "list",
9
+ "grid",
10
+ "window",
11
+ "scroll",
12
+ "react"
13
+ ],
14
+ "homepage": "https://itihon.github.io/layout-virtual/",
15
+ "bugs": {
16
+ "url": "https://github.com/itihon/layout-virtual/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/itihon/layout-virtual.git"
21
+ },
22
+ "license": "MIT",
23
+ "author": "Alexandr Kalabin",
24
+ "type": "module",
25
+ "main": "dist/index.js",
26
+ "module": "dist/index.js",
27
+ "browser": "dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.js"
36
+ }
37
+ },
38
+ "scripts": {
39
+ "test": "test",
40
+ "build": "npm run build:bundle && npm run build:types",
41
+ "build:bundle": "vite build",
42
+ "build:types": "tsc -p tsconfig.build.json --emitDeclarationOnly"
43
+ },
44
+ "dependencies": {
45
+ "layout-virtual": "0.0.1"
46
+ },
47
+ "peerDependencies": {
48
+ "react": "^19.2.5",
49
+ "react-dom": "^19.2.5"
50
+ },
51
+ "devDependencies": {}
52
+ }