universal-virtual-scroll 1.0.0
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/LICENSE +21 -0
- package/README.md +448 -0
- package/dist/index.cjs +128 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +101 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tejas Patil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
# universal-virtual-scroll
|
|
2
|
+
|
|
3
|
+
A lightweight and framework-agnostic virtual scrolling library for efficiently rendering large lists and grids with smooth performance.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install universal-virtual-scroll
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Import
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import {
|
|
19
|
+
UniversalVirtualScroll
|
|
20
|
+
} from 'universal-virtual-scroll';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
# Angular Example
|
|
26
|
+
|
|
27
|
+
## app.component.html
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<div #viewport class="viewport">
|
|
31
|
+
|
|
32
|
+
<div
|
|
33
|
+
#content
|
|
34
|
+
[style.paddingTop.px]="paddingTop"
|
|
35
|
+
[style.paddingBottom.px]="paddingBottom">
|
|
36
|
+
|
|
37
|
+
@for (item of visibleItems; track item.name) {
|
|
38
|
+
|
|
39
|
+
<div class="card">
|
|
40
|
+
|
|
41
|
+
{{ item.name }}
|
|
42
|
+
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
</div>
|
|
48
|
+
|
|
49
|
+
</div>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## app.component.ts
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import {
|
|
58
|
+
AfterViewInit,
|
|
59
|
+
Component,
|
|
60
|
+
ElementRef,
|
|
61
|
+
ViewChild
|
|
62
|
+
} from '@angular/core';
|
|
63
|
+
|
|
64
|
+
import {
|
|
65
|
+
UniversalVirtualScroll
|
|
66
|
+
} from 'universal-virtual-scroll';
|
|
67
|
+
|
|
68
|
+
@Component({
|
|
69
|
+
selector: 'app-root',
|
|
70
|
+
standalone: true,
|
|
71
|
+
templateUrl: './app.component.html',
|
|
72
|
+
styleUrl: './app.component.css'
|
|
73
|
+
})
|
|
74
|
+
export class AppComponent
|
|
75
|
+
implements AfterViewInit {
|
|
76
|
+
|
|
77
|
+
@ViewChild('viewport')
|
|
78
|
+
viewport!: ElementRef;
|
|
79
|
+
|
|
80
|
+
@ViewChild('content')
|
|
81
|
+
content!: ElementRef;
|
|
82
|
+
|
|
83
|
+
visibleItems: any[] = [];
|
|
84
|
+
|
|
85
|
+
paddingTop = 0;
|
|
86
|
+
|
|
87
|
+
paddingBottom = 0;
|
|
88
|
+
|
|
89
|
+
items = Array.from(
|
|
90
|
+
{ length: 100000 },
|
|
91
|
+
(_, i) => ({
|
|
92
|
+
name: `Item ${i + 1}`
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
ngAfterViewInit(): void {
|
|
97
|
+
|
|
98
|
+
new UniversalVirtualScroll({
|
|
99
|
+
|
|
100
|
+
viewport:
|
|
101
|
+
this.viewport.nativeElement,
|
|
102
|
+
|
|
103
|
+
content:
|
|
104
|
+
this.content.nativeElement,
|
|
105
|
+
|
|
106
|
+
items: this.items,
|
|
107
|
+
|
|
108
|
+
itemHeight: 100,
|
|
109
|
+
|
|
110
|
+
itemWidth: 200,
|
|
111
|
+
|
|
112
|
+
onUpdate: (
|
|
113
|
+
items,
|
|
114
|
+
top,
|
|
115
|
+
bottom
|
|
116
|
+
) => {
|
|
117
|
+
|
|
118
|
+
this.visibleItems = items;
|
|
119
|
+
|
|
120
|
+
this.paddingTop = top;
|
|
121
|
+
|
|
122
|
+
this.paddingBottom = bottom;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## app.component.css
|
|
132
|
+
|
|
133
|
+
```css
|
|
134
|
+
.viewport {
|
|
135
|
+
height: 100vh;
|
|
136
|
+
overflow-y: auto;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.card {
|
|
140
|
+
height: 100px;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
# React Example
|
|
147
|
+
|
|
148
|
+
## App.jsx
|
|
149
|
+
|
|
150
|
+
```jsx
|
|
151
|
+
import {
|
|
152
|
+
useEffect,
|
|
153
|
+
useRef,
|
|
154
|
+
useState
|
|
155
|
+
} from 'react';
|
|
156
|
+
|
|
157
|
+
import {
|
|
158
|
+
UniversalVirtualScroll
|
|
159
|
+
} from 'universal-virtual-scroll';
|
|
160
|
+
|
|
161
|
+
function App() {
|
|
162
|
+
|
|
163
|
+
const viewportRef = useRef(null);
|
|
164
|
+
|
|
165
|
+
const contentRef = useRef(null);
|
|
166
|
+
|
|
167
|
+
const [visibleItems, setVisibleItems] =
|
|
168
|
+
useState([]);
|
|
169
|
+
|
|
170
|
+
const [paddingTop, setPaddingTop] =
|
|
171
|
+
useState(0);
|
|
172
|
+
|
|
173
|
+
const [paddingBottom, setPaddingBottom] =
|
|
174
|
+
useState(0);
|
|
175
|
+
|
|
176
|
+
const items = Array.from(
|
|
177
|
+
{ length: 100000 },
|
|
178
|
+
(_, i) => ({
|
|
179
|
+
name: `Item ${i + 1}`
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
|
|
185
|
+
const virtualScroll =
|
|
186
|
+
new UniversalVirtualScroll({
|
|
187
|
+
|
|
188
|
+
viewport:
|
|
189
|
+
viewportRef.current,
|
|
190
|
+
|
|
191
|
+
content:
|
|
192
|
+
contentRef.current,
|
|
193
|
+
|
|
194
|
+
items,
|
|
195
|
+
|
|
196
|
+
itemHeight: 100,
|
|
197
|
+
|
|
198
|
+
itemWidth: 200,
|
|
199
|
+
|
|
200
|
+
onUpdate: (
|
|
201
|
+
items,
|
|
202
|
+
top,
|
|
203
|
+
bottom
|
|
204
|
+
) => {
|
|
205
|
+
|
|
206
|
+
setVisibleItems(items);
|
|
207
|
+
|
|
208
|
+
setPaddingTop(top);
|
|
209
|
+
|
|
210
|
+
setPaddingBottom(bottom);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return () => {
|
|
215
|
+
|
|
216
|
+
virtualScroll.destroy();
|
|
217
|
+
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
}, []);
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
|
|
224
|
+
<div
|
|
225
|
+
ref={viewportRef}
|
|
226
|
+
className="viewport">
|
|
227
|
+
|
|
228
|
+
<div
|
|
229
|
+
ref={contentRef}
|
|
230
|
+
style={{
|
|
231
|
+
paddingTop,
|
|
232
|
+
paddingBottom
|
|
233
|
+
}}>
|
|
234
|
+
|
|
235
|
+
{
|
|
236
|
+
|
|
237
|
+
visibleItems.map(item => (
|
|
238
|
+
|
|
239
|
+
<div
|
|
240
|
+
key={item.name}
|
|
241
|
+
className="card">
|
|
242
|
+
|
|
243
|
+
{item.name}
|
|
244
|
+
|
|
245
|
+
</div>
|
|
246
|
+
|
|
247
|
+
))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
</div>
|
|
251
|
+
|
|
252
|
+
</div>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export default App;
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## App.css
|
|
262
|
+
|
|
263
|
+
```css
|
|
264
|
+
.viewport {
|
|
265
|
+
height: 100vh;
|
|
266
|
+
overflow-y: auto;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.card {
|
|
270
|
+
height: 100px;
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
# Next.js Example
|
|
277
|
+
|
|
278
|
+
## app/page.jsx
|
|
279
|
+
|
|
280
|
+
```jsx
|
|
281
|
+
'use client';
|
|
282
|
+
|
|
283
|
+
import {
|
|
284
|
+
useEffect,
|
|
285
|
+
useRef,
|
|
286
|
+
useState
|
|
287
|
+
} from 'react';
|
|
288
|
+
|
|
289
|
+
import {
|
|
290
|
+
UniversalVirtualScroll
|
|
291
|
+
} from 'universal-virtual-scroll';
|
|
292
|
+
|
|
293
|
+
export default function Home() {
|
|
294
|
+
|
|
295
|
+
const viewportRef = useRef(null);
|
|
296
|
+
|
|
297
|
+
const contentRef = useRef(null);
|
|
298
|
+
|
|
299
|
+
const [visibleItems, setVisibleItems] =
|
|
300
|
+
useState([]);
|
|
301
|
+
|
|
302
|
+
const [paddingTop, setPaddingTop] =
|
|
303
|
+
useState(0);
|
|
304
|
+
|
|
305
|
+
const [paddingBottom, setPaddingBottom] =
|
|
306
|
+
useState(0);
|
|
307
|
+
|
|
308
|
+
const items = Array.from(
|
|
309
|
+
{ length: 100000 },
|
|
310
|
+
(_, i) => ({
|
|
311
|
+
name: `Item ${i + 1}`
|
|
312
|
+
})
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
useEffect(() => {
|
|
316
|
+
|
|
317
|
+
const virtualScroll =
|
|
318
|
+
new UniversalVirtualScroll({
|
|
319
|
+
|
|
320
|
+
viewport:
|
|
321
|
+
viewportRef.current,
|
|
322
|
+
|
|
323
|
+
content:
|
|
324
|
+
contentRef.current,
|
|
325
|
+
|
|
326
|
+
items,
|
|
327
|
+
|
|
328
|
+
itemHeight: 100,
|
|
329
|
+
|
|
330
|
+
itemWidth: 200,
|
|
331
|
+
|
|
332
|
+
onUpdate: (
|
|
333
|
+
items,
|
|
334
|
+
top,
|
|
335
|
+
bottom
|
|
336
|
+
) => {
|
|
337
|
+
|
|
338
|
+
setVisibleItems(items);
|
|
339
|
+
|
|
340
|
+
setPaddingTop(top);
|
|
341
|
+
|
|
342
|
+
setPaddingBottom(bottom);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return () => {
|
|
347
|
+
|
|
348
|
+
virtualScroll.destroy();
|
|
349
|
+
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
}, []);
|
|
353
|
+
|
|
354
|
+
return (
|
|
355
|
+
|
|
356
|
+
<div
|
|
357
|
+
ref={viewportRef}
|
|
358
|
+
className="viewport">
|
|
359
|
+
|
|
360
|
+
<div
|
|
361
|
+
ref={contentRef}
|
|
362
|
+
style={{
|
|
363
|
+
paddingTop,
|
|
364
|
+
paddingBottom
|
|
365
|
+
}}>
|
|
366
|
+
|
|
367
|
+
{
|
|
368
|
+
|
|
369
|
+
visibleItems.map(item => (
|
|
370
|
+
|
|
371
|
+
<div
|
|
372
|
+
key={item.name}
|
|
373
|
+
className="card">
|
|
374
|
+
|
|
375
|
+
{item.name}
|
|
376
|
+
|
|
377
|
+
</div>
|
|
378
|
+
|
|
379
|
+
))
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
</div>
|
|
383
|
+
|
|
384
|
+
</div>
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
---
|
|
390
|
+
|
|
391
|
+
## app/globals.css
|
|
392
|
+
|
|
393
|
+
```css
|
|
394
|
+
.viewport {
|
|
395
|
+
height: 100vh;
|
|
396
|
+
overflow-y: auto;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
.card {
|
|
400
|
+
height: 100px;
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
# Configuration
|
|
407
|
+
|
|
408
|
+
| Option | Type | Description |
|
|
409
|
+
|---|---|---|
|
|
410
|
+
| viewport | HTMLElement | Scroll container |
|
|
411
|
+
| content | HTMLElement | Content wrapper |
|
|
412
|
+
| items | Array | Full dataset |
|
|
413
|
+
| itemHeight | Number | Item height |
|
|
414
|
+
| itemWidth | Number | Item width |
|
|
415
|
+
| onUpdate | Function | Update callback |
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
# Methods
|
|
420
|
+
|
|
421
|
+
## updateItems
|
|
422
|
+
|
|
423
|
+
```js
|
|
424
|
+
virtualScroll.updateItems(newItems);
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
---
|
|
428
|
+
|
|
429
|
+
## destroy
|
|
430
|
+
|
|
431
|
+
```js
|
|
432
|
+
virtualScroll.destroy();
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
---
|
|
436
|
+
|
|
437
|
+
# Important
|
|
438
|
+
|
|
439
|
+
- `viewport` must have fixed height
|
|
440
|
+
- `viewport` must use `overflow-y: auto`
|
|
441
|
+
- `itemHeight` must match actual item height
|
|
442
|
+
- `itemWidth` must match actual item width
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
# License
|
|
447
|
+
|
|
448
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
UniversalVirtualScroll: () => UniversalVirtualScroll
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/virtual-scroll-generic.ts
|
|
28
|
+
var UniversalVirtualScroll = class {
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.viewport = config.viewport;
|
|
31
|
+
this.content = config.content;
|
|
32
|
+
this.items = config.items || [];
|
|
33
|
+
this.itemHeight = config.itemHeight || null;
|
|
34
|
+
this.itemWidth = config.itemWidth || null;
|
|
35
|
+
this.buffer = config.buffer || 5;
|
|
36
|
+
this.onUpdate = config.onUpdate;
|
|
37
|
+
this.scrollListener = () => this.calculate();
|
|
38
|
+
this.resizeListener = () => this.calculate();
|
|
39
|
+
this.viewport.addEventListener(
|
|
40
|
+
"scroll",
|
|
41
|
+
this.scrollListener,
|
|
42
|
+
{ passive: true }
|
|
43
|
+
);
|
|
44
|
+
window.addEventListener(
|
|
45
|
+
"resize",
|
|
46
|
+
this.resizeListener
|
|
47
|
+
);
|
|
48
|
+
requestAnimationFrame(() => {
|
|
49
|
+
this.detectItemSize();
|
|
50
|
+
this.calculate();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
detectItemSize() {
|
|
54
|
+
const firstItem = this.content.querySelector(
|
|
55
|
+
"[data-virtual-item]"
|
|
56
|
+
);
|
|
57
|
+
if (!firstItem) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const rect = firstItem.getBoundingClientRect();
|
|
61
|
+
this.itemHeight || (this.itemHeight = rect.height);
|
|
62
|
+
this.itemWidth || (this.itemWidth = rect.width);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
calculate() {
|
|
66
|
+
if (!this.items.length) {
|
|
67
|
+
this.onUpdate([], 0, 0);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!this.itemHeight || !this.itemWidth) {
|
|
71
|
+
const detected = this.detectItemSize();
|
|
72
|
+
if (!detected) return;
|
|
73
|
+
}
|
|
74
|
+
const viewportHeight = this.viewport.clientHeight;
|
|
75
|
+
const viewportWidth = this.viewport.clientWidth;
|
|
76
|
+
const cols = Math.max(
|
|
77
|
+
1,
|
|
78
|
+
Math.floor(
|
|
79
|
+
viewportWidth / this.itemWidth
|
|
80
|
+
)
|
|
81
|
+
);
|
|
82
|
+
const visibleRows = Math.ceil(
|
|
83
|
+
viewportHeight / this.itemHeight
|
|
84
|
+
);
|
|
85
|
+
const pageSize = (visibleRows + this.buffer) * cols;
|
|
86
|
+
const scrollTop = this.viewport.scrollTop;
|
|
87
|
+
const startRow = Math.floor(
|
|
88
|
+
scrollTop / this.itemHeight
|
|
89
|
+
);
|
|
90
|
+
const start = Math.max(
|
|
91
|
+
0,
|
|
92
|
+
startRow * cols - this.buffer * cols
|
|
93
|
+
);
|
|
94
|
+
const end = Math.min(
|
|
95
|
+
this.items.length,
|
|
96
|
+
start + pageSize
|
|
97
|
+
);
|
|
98
|
+
const slicedList = this.items.slice(start, end);
|
|
99
|
+
const paddingTop = Math.floor(start / cols) * this.itemHeight;
|
|
100
|
+
const remainingItems = this.items.length - end;
|
|
101
|
+
const paddingBottom = Math.ceil(
|
|
102
|
+
remainingItems / cols
|
|
103
|
+
) * this.itemHeight;
|
|
104
|
+
this.onUpdate(
|
|
105
|
+
slicedList,
|
|
106
|
+
paddingTop,
|
|
107
|
+
paddingBottom
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
updateItems(items) {
|
|
111
|
+
this.items = items;
|
|
112
|
+
this.calculate();
|
|
113
|
+
}
|
|
114
|
+
destroy() {
|
|
115
|
+
this.viewport.removeEventListener(
|
|
116
|
+
"scroll",
|
|
117
|
+
this.scrollListener
|
|
118
|
+
);
|
|
119
|
+
window.removeEventListener(
|
|
120
|
+
"resize",
|
|
121
|
+
this.resizeListener
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
UniversalVirtualScroll
|
|
128
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface UniversalVirtualScrollConfig {
|
|
2
|
+
viewport: HTMLElement;
|
|
3
|
+
content: HTMLElement;
|
|
4
|
+
items: any[];
|
|
5
|
+
itemHeight?: number;
|
|
6
|
+
itemWidth?: number;
|
|
7
|
+
buffer?: number;
|
|
8
|
+
onUpdate: (visibleItems: any[], paddingTop: number, paddingBottom: number) => void;
|
|
9
|
+
}
|
|
10
|
+
declare class UniversalVirtualScroll {
|
|
11
|
+
private viewport;
|
|
12
|
+
private content;
|
|
13
|
+
private items;
|
|
14
|
+
private itemHeight;
|
|
15
|
+
private itemWidth;
|
|
16
|
+
private buffer;
|
|
17
|
+
private onUpdate;
|
|
18
|
+
private scrollListener;
|
|
19
|
+
private resizeListener;
|
|
20
|
+
constructor(config: UniversalVirtualScrollConfig);
|
|
21
|
+
private detectItemSize;
|
|
22
|
+
calculate(): void;
|
|
23
|
+
updateItems(items: any[]): void;
|
|
24
|
+
destroy(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { UniversalVirtualScroll };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface UniversalVirtualScrollConfig {
|
|
2
|
+
viewport: HTMLElement;
|
|
3
|
+
content: HTMLElement;
|
|
4
|
+
items: any[];
|
|
5
|
+
itemHeight?: number;
|
|
6
|
+
itemWidth?: number;
|
|
7
|
+
buffer?: number;
|
|
8
|
+
onUpdate: (visibleItems: any[], paddingTop: number, paddingBottom: number) => void;
|
|
9
|
+
}
|
|
10
|
+
declare class UniversalVirtualScroll {
|
|
11
|
+
private viewport;
|
|
12
|
+
private content;
|
|
13
|
+
private items;
|
|
14
|
+
private itemHeight;
|
|
15
|
+
private itemWidth;
|
|
16
|
+
private buffer;
|
|
17
|
+
private onUpdate;
|
|
18
|
+
private scrollListener;
|
|
19
|
+
private resizeListener;
|
|
20
|
+
constructor(config: UniversalVirtualScrollConfig);
|
|
21
|
+
private detectItemSize;
|
|
22
|
+
calculate(): void;
|
|
23
|
+
updateItems(items: any[]): void;
|
|
24
|
+
destroy(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { UniversalVirtualScroll };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/virtual-scroll-generic.ts
|
|
2
|
+
var UniversalVirtualScroll = class {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
this.viewport = config.viewport;
|
|
5
|
+
this.content = config.content;
|
|
6
|
+
this.items = config.items || [];
|
|
7
|
+
this.itemHeight = config.itemHeight || null;
|
|
8
|
+
this.itemWidth = config.itemWidth || null;
|
|
9
|
+
this.buffer = config.buffer || 5;
|
|
10
|
+
this.onUpdate = config.onUpdate;
|
|
11
|
+
this.scrollListener = () => this.calculate();
|
|
12
|
+
this.resizeListener = () => this.calculate();
|
|
13
|
+
this.viewport.addEventListener(
|
|
14
|
+
"scroll",
|
|
15
|
+
this.scrollListener,
|
|
16
|
+
{ passive: true }
|
|
17
|
+
);
|
|
18
|
+
window.addEventListener(
|
|
19
|
+
"resize",
|
|
20
|
+
this.resizeListener
|
|
21
|
+
);
|
|
22
|
+
requestAnimationFrame(() => {
|
|
23
|
+
this.detectItemSize();
|
|
24
|
+
this.calculate();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
detectItemSize() {
|
|
28
|
+
const firstItem = this.content.querySelector(
|
|
29
|
+
"[data-virtual-item]"
|
|
30
|
+
);
|
|
31
|
+
if (!firstItem) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const rect = firstItem.getBoundingClientRect();
|
|
35
|
+
this.itemHeight || (this.itemHeight = rect.height);
|
|
36
|
+
this.itemWidth || (this.itemWidth = rect.width);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
calculate() {
|
|
40
|
+
if (!this.items.length) {
|
|
41
|
+
this.onUpdate([], 0, 0);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!this.itemHeight || !this.itemWidth) {
|
|
45
|
+
const detected = this.detectItemSize();
|
|
46
|
+
if (!detected) return;
|
|
47
|
+
}
|
|
48
|
+
const viewportHeight = this.viewport.clientHeight;
|
|
49
|
+
const viewportWidth = this.viewport.clientWidth;
|
|
50
|
+
const cols = Math.max(
|
|
51
|
+
1,
|
|
52
|
+
Math.floor(
|
|
53
|
+
viewportWidth / this.itemWidth
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
const visibleRows = Math.ceil(
|
|
57
|
+
viewportHeight / this.itemHeight
|
|
58
|
+
);
|
|
59
|
+
const pageSize = (visibleRows + this.buffer) * cols;
|
|
60
|
+
const scrollTop = this.viewport.scrollTop;
|
|
61
|
+
const startRow = Math.floor(
|
|
62
|
+
scrollTop / this.itemHeight
|
|
63
|
+
);
|
|
64
|
+
const start = Math.max(
|
|
65
|
+
0,
|
|
66
|
+
startRow * cols - this.buffer * cols
|
|
67
|
+
);
|
|
68
|
+
const end = Math.min(
|
|
69
|
+
this.items.length,
|
|
70
|
+
start + pageSize
|
|
71
|
+
);
|
|
72
|
+
const slicedList = this.items.slice(start, end);
|
|
73
|
+
const paddingTop = Math.floor(start / cols) * this.itemHeight;
|
|
74
|
+
const remainingItems = this.items.length - end;
|
|
75
|
+
const paddingBottom = Math.ceil(
|
|
76
|
+
remainingItems / cols
|
|
77
|
+
) * this.itemHeight;
|
|
78
|
+
this.onUpdate(
|
|
79
|
+
slicedList,
|
|
80
|
+
paddingTop,
|
|
81
|
+
paddingBottom
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
updateItems(items) {
|
|
85
|
+
this.items = items;
|
|
86
|
+
this.calculate();
|
|
87
|
+
}
|
|
88
|
+
destroy() {
|
|
89
|
+
this.viewport.removeEventListener(
|
|
90
|
+
"scroll",
|
|
91
|
+
this.scrollListener
|
|
92
|
+
);
|
|
93
|
+
window.removeEventListener(
|
|
94
|
+
"resize",
|
|
95
|
+
this.resizeListener
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
export {
|
|
100
|
+
UniversalVirtualScroll
|
|
101
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "universal-virtual-scroll",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight and framework-agnostic virtual scrolling library.",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
16
|
+
"dev": "tsup src/index.ts --watch"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"virtual-scroll",
|
|
20
|
+
"virtualization",
|
|
21
|
+
"infinite-scroll",
|
|
22
|
+
"performance",
|
|
23
|
+
"angular",
|
|
24
|
+
"react",
|
|
25
|
+
"nextjs",
|
|
26
|
+
"typescript",
|
|
27
|
+
"scrolling"
|
|
28
|
+
],
|
|
29
|
+
"author": "Tejas Patil",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/tejaspatil-web/universal-virtual-scroll.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/tejaspatil-web/universal-virtual-scroll#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/tejaspatil-web/universal-virtual-scroll/issues"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"tsup": "^8.0.0",
|
|
41
|
+
"typescript": "^5.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|