clear-virtualizer 0.1.7 → 0.2.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/README.md +24 -1
- package/bin/clear-virtualizer.mjs +64 -0
- package/lib/index.cjs +1 -1
- package/lib/index.js +1 -1
- package/package.json +7 -4
- package/skills/getting-started/SKILL.md +99 -0
package/README.md
CHANGED
|
@@ -6,6 +6,21 @@ One hook for vertical virtualized lists and tables with dynamic row heights. Row
|
|
|
6
6
|
|
|
7
7
|
> 👉 TL;DR? **[Try the live playground](https://clear-virtualize-sandbox.vercel.app/)** — no setup, click around.
|
|
8
8
|
|
|
9
|
+
This library ships an agent skill: `skills/getting-started/SKILL.md` — setup, measurement rules, `scrollToIndex`/settle semantics, `headerHeight`, SSR, and reference files by task. Point your coding agent at it before writing code against this library:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx clear-virtualizer skill # print SKILL.md (stamped with the package version)
|
|
13
|
+
npx clear-virtualizer skill install # copy it to .claude/skills/clear-virtualizer/ (--to <dir> for other agents)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
If the skill was not picked up, the agent contract is this README plus the `lib/index.d.ts` of the installed version — they win over any report or summary, and automation claims ("this hook invalidates X") are verified there, not assumed. The fastest traps:
|
|
17
|
+
|
|
18
|
+
- **A fixed `height` on rows** breaks auto-measurement — let rows size naturally.
|
|
19
|
+
- **Wrong positioning**: rows must be absolutely positioned inside a `position: relative` spacer of `height: scrollHeight` and moved with `transform: translateY(item.start)`, not `top`/margins.
|
|
20
|
+
- **Reusing one `getMeasureRef(0)` for all rows** corrupts measurements — call `getMeasureRef(index)` per row.
|
|
21
|
+
- **Assuming `scrollToIndex` is synchronous**: it settles over several frames after a jump, and a user scroll during the settle cancels it.
|
|
22
|
+
- **Horizontal scrolling**: out of scope — vertical only, no sticky columns.
|
|
23
|
+
|
|
9
24
|
## Install
|
|
10
25
|
|
|
11
26
|
```sh
|
|
@@ -229,12 +244,20 @@ scrollToIndex(34567, { align: 'end' });
|
|
|
229
244
|
| `center` | In the middle of the viewport |
|
|
230
245
|
| `end` | At the bottom of the viewport |
|
|
231
246
|
|
|
232
|
-
If the row is far away and its neighbors were never measured, the hook first jumps to the estimated position, measures the rows around the target, and corrects the position. A manual scroll during that correction cancels it and hands control back to the user.
|
|
247
|
+
If the row is far away and its neighbors were never measured, the hook first jumps to the estimated position, measures the rows around the target, and corrects the position. A manual scroll during that correction cancels it and hands control back to the user. An index outside `[0, count)` is a silent no-op (no error, no clamping) — clamp it yourself if you need clamping semantics.
|
|
233
248
|
|
|
234
249
|
## estimateSize
|
|
235
250
|
|
|
236
251
|
`estimateSize` is only a starting guess until real measurements arrive. The closer it is to reality, the less rows visually shift on fast scroll through unmeasured regions: underestimated rows overlap until measured, overestimated ones leave temporary gaps. Pass a value near your average row height.
|
|
237
252
|
|
|
253
|
+
## Dynamic data (growing counts, filtering, reordering)
|
|
254
|
+
|
|
255
|
+
`count` may change at any time — appending rows, streaming, filtering — the hook handles it:
|
|
256
|
+
|
|
257
|
+
- **Measured heights survive count changes.** When `count` grows or shrinks, real measurements are kept and re-applied by index; `estimateSize` is called only for indices that did not exist before. Appending rows at the end is therefore seamless.
|
|
258
|
+
- **Measurements are bound to the index, not to the item.** If you filter or reorder the underlying data, index `i` now holds a different item and inherits the previous item's measured height for one or two frames, until `ResizeObserver` re-measures it. This self-corrects with no overlapping rows in the settled state, but expect a brief visual shift right after a filter or sort change.
|
|
259
|
+
- **Row keys.** The examples use `key={item.index}`, which is right for static data. If your data mutates (filtering, sorting, prepending), prefer a stable key from the item itself (`item.id`) — React then reuses the row's DOM node instead of remounting it on every change.
|
|
260
|
+
|
|
238
261
|
## SSR
|
|
239
262
|
|
|
240
263
|
Server-side rendering is supported: on the server the hook renders an empty container (`virtualItems: []`, `scrollHeight: 0`) without touching browser APIs. Rows appear after hydration once the scroll container is attached and `ResizeObserver` reports real heights.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Prints or installs the agent skill shipped with clear-virtualizer.
|
|
3
|
+
//
|
|
4
|
+
// clear-virtualizer skill print SKILL.md (stamped with the package version)
|
|
5
|
+
// clear-virtualizer skill install copy it to .claude/skills/clear-virtualizer/
|
|
6
|
+
// clear-virtualizer skill install --to <dir> copy it to <dir>/clear-virtualizer/
|
|
7
|
+
|
|
8
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
9
|
+
import { createRequire } from 'node:module';
|
|
10
|
+
import { dirname, join, resolve } from 'node:path';
|
|
11
|
+
|
|
12
|
+
const require = createRequire(import.meta.url);
|
|
13
|
+
const pkgRoot = dirname(require.resolve('../package.json'));
|
|
14
|
+
|
|
15
|
+
const USAGE = `Usage:
|
|
16
|
+
clear-virtualizer skill print SKILL.md (stamped with the package version)
|
|
17
|
+
clear-virtualizer skill install copy it to .claude/skills/clear-virtualizer/ (--to <dir> for other agents)`;
|
|
18
|
+
|
|
19
|
+
async function readSkill() {
|
|
20
|
+
const [pkg, raw] = await Promise.all([
|
|
21
|
+
readFile(join(pkgRoot, 'package.json'), 'utf8').then(JSON.parse),
|
|
22
|
+
readFile(join(pkgRoot, 'skills', 'getting-started', 'SKILL.md'), 'utf8'),
|
|
23
|
+
]);
|
|
24
|
+
return { version: pkg.version, skill: raw.replaceAll('{{VERSION}}', pkg.version) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function main() {
|
|
28
|
+
const [, , command, subcommand, ...rest] = process.argv;
|
|
29
|
+
|
|
30
|
+
if (command !== 'skill' || (subcommand && subcommand !== 'install')) {
|
|
31
|
+
console.error(USAGE);
|
|
32
|
+
process.exit(command === '--help' || command === '-h' ? 0 : 1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { skill } = await readSkill();
|
|
36
|
+
|
|
37
|
+
if (!subcommand) {
|
|
38
|
+
console.log(skill);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const toFlag = rest.indexOf('--to');
|
|
43
|
+
let targetRoot = resolve('.claude', 'skills');
|
|
44
|
+
if (toFlag !== -1) {
|
|
45
|
+
const dir = rest[toFlag + 1];
|
|
46
|
+
if (!dir) {
|
|
47
|
+
console.error('--to requires a directory argument');
|
|
48
|
+
console.error(USAGE);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
targetRoot = resolve(dir);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const targetDir = join(targetRoot, 'clear-virtualizer');
|
|
55
|
+
const targetFile = join(targetDir, 'SKILL.md');
|
|
56
|
+
await mkdir(targetDir, { recursive: true });
|
|
57
|
+
await writeFile(targetFile, skill);
|
|
58
|
+
console.log(`Skill written to ${targetFile}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
main().catch(error => {
|
|
62
|
+
console.error(error?.message ?? error);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
});
|
package/lib/index.cjs
CHANGED
|
@@ -208,7 +208,7 @@ function useVirtualizer({ count, estimateSize, overscan = DEFAULT_OVERSCAN, head
|
|
|
208
208
|
if (typeof ResizeObserver === "undefined")
|
|
209
209
|
return;
|
|
210
210
|
const observer = new ResizeObserver(([entry]) => {
|
|
211
|
-
const height = entry?.borderBoxSize[0]?.blockSize;
|
|
211
|
+
const height = entry?.borderBoxSize?.[0]?.blockSize;
|
|
212
212
|
if (height == null || !fenwickRef.current)
|
|
213
213
|
return;
|
|
214
214
|
const tree = fenwickRef.current;
|
package/lib/index.js
CHANGED
|
@@ -206,7 +206,7 @@ function useVirtualizer({ count, estimateSize, overscan = DEFAULT_OVERSCAN, head
|
|
|
206
206
|
if (typeof ResizeObserver === "undefined")
|
|
207
207
|
return;
|
|
208
208
|
const observer = new ResizeObserver(([entry]) => {
|
|
209
|
-
const height = entry?.borderBoxSize[0]?.blockSize;
|
|
209
|
+
const height = entry?.borderBoxSize?.[0]?.blockSize;
|
|
210
210
|
if (height == null || !fenwickRef.current)
|
|
211
211
|
return;
|
|
212
212
|
const tree = fenwickRef.current;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear-virtualizer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A lightweight React hook for virtualized lists and tables with dynamic row heights, measured automatically",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.cjs",
|
|
@@ -13,8 +13,13 @@
|
|
|
13
13
|
"require": "./lib/index.cjs"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"clear-virtualizer": "bin/clear-virtualizer.mjs"
|
|
18
|
+
},
|
|
16
19
|
"files": [
|
|
20
|
+
"bin",
|
|
17
21
|
"lib",
|
|
22
|
+
"skills",
|
|
18
23
|
"README.md",
|
|
19
24
|
"LICENSE"
|
|
20
25
|
],
|
|
@@ -93,7 +98,5 @@
|
|
|
93
98
|
"vite-tsconfig-paths": "^4.2.0",
|
|
94
99
|
"vitest": "^1.6.1"
|
|
95
100
|
},
|
|
96
|
-
"dependencies": {
|
|
97
|
-
"clear-virtualizer": "^0.1.5"
|
|
98
|
-
}
|
|
101
|
+
"dependencies": {}
|
|
99
102
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: clear-virtualizer
|
|
3
|
+
description: Setup and usage of clear-virtualizer, a React hook for virtualized lists and tables with dynamic row heights measured automatically via ResizeObserver. Read this before writing code against the library: install, API, measurement rules, scrollToIndex settle semantics, headerHeight, SSR, and pitfalls.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# clear-virtualizer — getting started (agent skill)
|
|
7
|
+
|
|
8
|
+
Package version: {{VERSION}}
|
|
9
|
+
|
|
10
|
+
One hook, `useVirtualizer`, for vertical virtualized lists and tables with dynamic row heights. Rows are measured automatically via `ResizeObserver` — no manual measuring, no fixed heights. Works for an unlimited number of rows.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm i clear-virtualizer
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
React >= 16.8 is the only peer dependency. ESM and CJS builds ship in `lib/`.
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { useVirtualizer, type Options, type ScrollAlign, type VirtualItem } from 'clear-virtualizer';
|
|
24
|
+
|
|
25
|
+
const { virtualItems, scrollHeight, scrollRef, getMeasureRef, scrollToIndex } = useVirtualizer({
|
|
26
|
+
count: 100000,
|
|
27
|
+
estimateSize: () => 44,
|
|
28
|
+
overscan: 10, // default 3
|
|
29
|
+
headerHeight: 0, // sticky header height excluded from the visible window, default 0
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `virtualItems: VirtualItem[]` — rows to render: `{ index, start, size, end }` in px.
|
|
34
|
+
- `scrollHeight: number` — total scrollable height in px; put it on the inner spacer `div` height.
|
|
35
|
+
- `scrollRef(el)` — callback ref for the scroll container (`overflow: auto`).
|
|
36
|
+
- `getMeasureRef(index)` — returns a callback ref; attach it to each rendered row for measuring.
|
|
37
|
+
- `scrollToIndex(index, { align })` — `align` is `'start'` (default), `'center'`, or `'end'`.
|
|
38
|
+
|
|
39
|
+
## Required DOM structure
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
<div ref={scrollRef} style={{ height: 550, overflow: 'auto' }}>
|
|
43
|
+
<div style={{ position: 'relative', height: scrollHeight }}>
|
|
44
|
+
{virtualItems.map(item => (
|
|
45
|
+
<div
|
|
46
|
+
key={item.index}
|
|
47
|
+
ref={getMeasureRef(item.index)}
|
|
48
|
+
style={{
|
|
49
|
+
position: 'absolute',
|
|
50
|
+
top: 0,
|
|
51
|
+
left: 0,
|
|
52
|
+
width: '100%',
|
|
53
|
+
transform: `translateY(${item.start}px)`,
|
|
54
|
+
}}
|
|
55
|
+
>
|
|
56
|
+
{renderItem(item.index)}
|
|
57
|
+
</div>
|
|
58
|
+
))}
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Rules that must not be broken
|
|
64
|
+
|
|
65
|
+
1. **Never set a fixed `height` on rows.** Measuring relies on natural sizing; a fixed height defeats the whole point.
|
|
66
|
+
2. **Position rows with `transform: translateY(item.start)`,** inside a `position: relative` spacer of `height: scrollHeight`. Do not use `top` with `absolute` + margins.
|
|
67
|
+
3. **Call `getMeasureRef(index)` per row index** (it returns a stable callback per index). Attaching one ref to every row corrupts measurements.
|
|
68
|
+
4. **Pass a realistic `estimateSize`** (average row height). It is only a starting guess until real measurements arrive; underestimates overlap, overestimates leave temporary gaps.
|
|
69
|
+
5. **`scrollToIndex` is not synchronous.** For far-away targets the hook jumps to the estimated position, measures rows around the target, and corrects over the following frames. A user scroll during that settle cancels it and hands control back. Do not assert on `scrollTop` in the same tick. An index outside `[0, count)` is a silent no-op.
|
|
70
|
+
6. **Vertical scrolling only.** No horizontal virtualization, no sticky columns; style wide tables with per-column widths and `flexShrink: 0` so the header and body stay aligned.
|
|
71
|
+
7. **`headerHeight` shifts the visible window** below a `position: sticky` header; item `start` values stay absolute relative to the content top either way.
|
|
72
|
+
|
|
73
|
+
## Dynamic data (growing counts, filtering, reordering)
|
|
74
|
+
|
|
75
|
+
`count` may change at any time — appending rows, streaming, filtering:
|
|
76
|
+
|
|
77
|
+
- Measured heights survive count changes: they are kept and re-applied by index, and `estimateSize` is called only for indices that did not exist before. Appending at the end is seamless.
|
|
78
|
+
- Measurements are bound to the **index, not the item**. After a filter or reorder, an index inherits the previous item's height for one or two frames until `ResizeObserver` re-measures; it self-corrects. Prefer stable item keys (`item.id`) over `key={item.index}` when data mutates.
|
|
79
|
+
|
|
80
|
+
## SSR
|
|
81
|
+
|
|
82
|
+
On the server the hook renders an empty container (`virtualItems: []`, `scrollHeight: 0`) without touching browser APIs. Rows appear after hydration once the scroll container is attached and `ResizeObserver` reports real heights. Initial HTML is deterministic and hydration-safe.
|
|
83
|
+
|
|
84
|
+
## Limitations
|
|
85
|
+
|
|
86
|
+
- Vertical scrolling only.
|
|
87
|
+
- The internal offset tree keeps one `float64` per row (~8 MB per million rows).
|
|
88
|
+
- Totals above 15,000,000 px are scaled down internally; positions stay consistent, but the mapping scroll position ↔ row is approximate in that regime.
|
|
89
|
+
|
|
90
|
+
## Reference files by task
|
|
91
|
+
|
|
92
|
+
| Task | Source of truth |
|
|
93
|
+
| ------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| Full API reference, list + table examples | `README.md` in the installed package |
|
|
95
|
+
| Exact types and options (the contract) | `lib/index.d.ts` of the **installed** version — it wins over any report or summary |
|
|
96
|
+
| Behavior details (settle loop, scale, SSR) | `src/hooks/useVirtualizer.ts` and `src/utils.ts` in the repository |
|
|
97
|
+
| Copy-paste list and table components | `README.md` sections "List example" and "Table example" |
|
|
98
|
+
|
|
99
|
+
If the skill was not picked up, the agent contract is the package `README.md` plus the `lib/index.d.ts` of the installed version — they win over any report or summary, and automation claims ("this hook invalidates X") are verified there, not assumed.
|