@pyreon/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/LICENSE +21 -0
- package/README.md +184 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/index.js +143 -0
- package/lib/index.js.map +1 -0
- package/lib/types/index.d.ts +143 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +54 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +55 -0
- package/src/index.ts +37 -0
- package/src/tests/virtual.test.ts +619 -0
- package/src/use-virtualizer.ts +124 -0
- package/src/use-window-virtualizer.ts +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Vit Bokisch
|
|
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,184 @@
|
|
|
1
|
+
# @pyreon/virtual
|
|
2
|
+
|
|
3
|
+
Pyreon adapter for TanStack Virtual. Efficient rendering of large lists with reactive `virtualItems`, `totalSize`, and `isScrolling` signals.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @pyreon/virtual @tanstack/virtual-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { signal } from "@pyreon/reactivity"
|
|
15
|
+
import { useVirtualizer } from "@pyreon/virtual"
|
|
16
|
+
|
|
17
|
+
function VirtualList() {
|
|
18
|
+
const parentRef = signal<HTMLElement | null>(null)
|
|
19
|
+
const items = Array.from({ length: 10000 }, (_, i) => `Item ${i + 1}`)
|
|
20
|
+
|
|
21
|
+
const { virtualItems, totalSize } = useVirtualizer(() => ({
|
|
22
|
+
count: items.length,
|
|
23
|
+
getScrollElement: () => parentRef(),
|
|
24
|
+
estimateSize: () => 40,
|
|
25
|
+
overscan: 5,
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
return () => (
|
|
29
|
+
<div ref={(el) => parentRef.set(el)} style="height: 400px; overflow-y: auto;">
|
|
30
|
+
<div style={`height: ${totalSize()}px; position: relative;`}>
|
|
31
|
+
{virtualItems().map((row) => (
|
|
32
|
+
<div
|
|
33
|
+
key={row.key}
|
|
34
|
+
style={`position: absolute; top: 0; width: 100%; height: ${row.size}px; transform: translateY(${row.start}px);`}
|
|
35
|
+
>
|
|
36
|
+
{items[row.index]}
|
|
37
|
+
</div>
|
|
38
|
+
))}
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `useVirtualizer(options)`
|
|
48
|
+
|
|
49
|
+
Create a reactive virtualizer for element-based scrolling. Options are passed as a function so reactive signals can be read inside, and the virtualizer updates automatically when they change.
|
|
50
|
+
|
|
51
|
+
| Parameter | Type | Description |
|
|
52
|
+
| --- | --- | --- |
|
|
53
|
+
| `options` | `() => UseVirtualizerOptions` | Function returning virtualizer config |
|
|
54
|
+
|
|
55
|
+
Options extend `VirtualizerOptions` from `@tanstack/virtual-core` with `observeElementRect`, `observeElementOffset`, and `scrollToFn` pre-filled (overridable).
|
|
56
|
+
|
|
57
|
+
**Returns:** `UseVirtualizerResult` with:
|
|
58
|
+
|
|
59
|
+
| Property | Type | Description |
|
|
60
|
+
| --- | --- | --- |
|
|
61
|
+
| `instance` | `Virtualizer` | The underlying TanStack Virtualizer instance |
|
|
62
|
+
| `virtualItems` | `Signal<VirtualItem[]>` | Currently visible virtual items |
|
|
63
|
+
| `totalSize` | `Signal<number>` | Total scrollable size in pixels |
|
|
64
|
+
| `isScrolling` | `Signal<boolean>` | Whether the user is currently scrolling |
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const parentRef = signal<HTMLDivElement | null>(null)
|
|
68
|
+
const count = signal(1000)
|
|
69
|
+
|
|
70
|
+
const { virtualItems, totalSize, isScrolling, instance } = useVirtualizer(() => ({
|
|
71
|
+
count: count(),
|
|
72
|
+
getScrollElement: () => parentRef(),
|
|
73
|
+
estimateSize: () => 35,
|
|
74
|
+
overscan: 5,
|
|
75
|
+
}))
|
|
76
|
+
|
|
77
|
+
// Scroll programmatically:
|
|
78
|
+
instance.scrollToIndex(500)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### `useWindowVirtualizer(options)`
|
|
82
|
+
|
|
83
|
+
Create a reactive virtualizer for window-based scrolling. The scroll element is automatically set to `window`. SSR-safe — checks for `window` and `document` availability.
|
|
84
|
+
|
|
85
|
+
| Parameter | Type | Description |
|
|
86
|
+
| --- | --- | --- |
|
|
87
|
+
| `options` | `() => UseWindowVirtualizerOptions` | Function returning config (no `getScrollElement` needed) |
|
|
88
|
+
|
|
89
|
+
**Returns:** `UseWindowVirtualizerResult` — same shape as `UseVirtualizerResult` but typed with `Window` as the scroll element.
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
function WindowList() {
|
|
93
|
+
const items = Array.from({ length: 50000 }, (_, i) => `Row ${i}`)
|
|
94
|
+
|
|
95
|
+
const { virtualItems, totalSize } = useWindowVirtualizer(() => ({
|
|
96
|
+
count: items.length,
|
|
97
|
+
estimateSize: () => 40,
|
|
98
|
+
}))
|
|
99
|
+
|
|
100
|
+
return () => (
|
|
101
|
+
<div style={`height: ${totalSize()}px; position: relative;`}>
|
|
102
|
+
{virtualItems().map((row) => (
|
|
103
|
+
<div
|
|
104
|
+
key={row.key}
|
|
105
|
+
style={`position: absolute; top: 0; width: 100%; height: ${row.size}px; transform: translateY(${row.start}px);`}
|
|
106
|
+
>
|
|
107
|
+
{items[row.index]}
|
|
108
|
+
</div>
|
|
109
|
+
))}
|
|
110
|
+
</div>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Patterns
|
|
116
|
+
|
|
117
|
+
### Dynamic Item Sizes
|
|
118
|
+
|
|
119
|
+
Use `measureElement` for variable-height items that are measured after render.
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
import { measureElement } from "@pyreon/virtual"
|
|
123
|
+
|
|
124
|
+
const { virtualItems, totalSize, instance } = useVirtualizer(() => ({
|
|
125
|
+
count: items.length,
|
|
126
|
+
getScrollElement: () => parentRef(),
|
|
127
|
+
estimateSize: () => 50,
|
|
128
|
+
measureElement,
|
|
129
|
+
}))
|
|
130
|
+
|
|
131
|
+
// In render, set the ref on each item:
|
|
132
|
+
virtualItems().map((row) => (
|
|
133
|
+
<div
|
|
134
|
+
key={row.key}
|
|
135
|
+
ref={(el) => instance.measureElement(el)}
|
|
136
|
+
data-index={row.index}
|
|
137
|
+
>
|
|
138
|
+
{items[row.index]}
|
|
139
|
+
</div>
|
|
140
|
+
))
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Horizontal Lists
|
|
144
|
+
|
|
145
|
+
Set the `horizontal` option for horizontal virtualization.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const { virtualItems, totalSize } = useVirtualizer(() => ({
|
|
149
|
+
count: columns.length,
|
|
150
|
+
getScrollElement: () => parentRef(),
|
|
151
|
+
estimateSize: () => 120,
|
|
152
|
+
horizontal: true,
|
|
153
|
+
}))
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Reactive Count
|
|
157
|
+
|
|
158
|
+
Since options are a function, changing the count signal re-calculates virtual items automatically.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
const filteredItems = signal(allItems)
|
|
162
|
+
const { virtualItems } = useVirtualizer(() => ({
|
|
163
|
+
count: filteredItems().length,
|
|
164
|
+
getScrollElement: () => parentRef(),
|
|
165
|
+
estimateSize: () => 40,
|
|
166
|
+
}))
|
|
167
|
+
|
|
168
|
+
// Updating filteredItems triggers recalculation
|
|
169
|
+
filteredItems.set(allItems.filter(i => i.includes(search())))
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Re-exports from `@tanstack/virtual-core`
|
|
173
|
+
|
|
174
|
+
**Runtime:** `defaultKeyExtractor`, `defaultRangeExtractor`, `observeElementOffset`, `observeElementRect`, `observeWindowOffset`, `observeWindowRect`, `elementScroll`, `windowScroll`, `measureElement`, `Virtualizer`
|
|
175
|
+
|
|
176
|
+
**Types:** `VirtualizerOptions`, `VirtualItem`, `Range`, `Rect`, `ScrollToOptions`
|
|
177
|
+
|
|
178
|
+
## Gotchas
|
|
179
|
+
|
|
180
|
+
- Options must be a function `() => opts` for reactive tracking. The virtualizer re-calculates when signals read inside the function change.
|
|
181
|
+
- The `instance` is the raw TanStack Virtualizer — use it for imperative methods like `scrollToIndex()` and `scrollToOffset()`.
|
|
182
|
+
- `virtualItems`, `totalSize`, and `isScrolling` are Pyreon signals updated via `batch()` for efficient reactive notifications.
|
|
183
|
+
- The virtualizer's DOM observers are mounted via `onMount` and cleaned up via `onUnmount`. The component must be mounted for scroll observation to work.
|
|
184
|
+
- `useWindowVirtualizer` checks for `window` availability and provides a safe fallback for SSR.
|