clear-virtualizer 0.1.5 → 0.1.8
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 +17 -0
- package/bin/clear-virtualizer.mjs +64 -0
- package/lib/index.cjs +1 -1
- package/lib/index.js +1 -1
- package/package.json +8 -2
- package/skills/getting-started/SKILL.md +92 -0
package/README.md
CHANGED
|
@@ -4,6 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
One hook 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.
|
|
6
6
|
|
|
7
|
+
> 👉 TL;DR? **[Try the live playground](https://clear-virtualize-sandbox.vercel.app/)** — no setup, click around.
|
|
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
|
+
|
|
7
24
|
## Install
|
|
8
25
|
|
|
9
26
|
```sh
|
|
@@ -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.1.
|
|
3
|
+
"version": "0.1.8",
|
|
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
|
],
|
|
@@ -92,5 +97,6 @@
|
|
|
92
97
|
"vite-plugin-eslint": "^1.8.1",
|
|
93
98
|
"vite-tsconfig-paths": "^4.2.0",
|
|
94
99
|
"vitest": "^1.6.1"
|
|
95
|
-
}
|
|
100
|
+
},
|
|
101
|
+
"dependencies": {}
|
|
96
102
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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.
|
|
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
|
+
## SSR
|
|
74
|
+
|
|
75
|
+
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.
|
|
76
|
+
|
|
77
|
+
## Limitations
|
|
78
|
+
|
|
79
|
+
- Vertical scrolling only.
|
|
80
|
+
- The internal offset tree keeps one `float64` per row (~8 MB per million rows).
|
|
81
|
+
- Totals above 15,000,000 px are scaled down internally; positions stay consistent, but the mapping scroll position ↔ row is approximate in that regime.
|
|
82
|
+
|
|
83
|
+
## Reference files by task
|
|
84
|
+
|
|
85
|
+
| Task | Source of truth |
|
|
86
|
+
| ------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
87
|
+
| Full API reference, list + table examples | `README.md` in the installed package |
|
|
88
|
+
| Exact types and options (the contract) | `lib/index.d.ts` of the **installed** version — it wins over any report or summary |
|
|
89
|
+
| Behavior details (settle loop, scale, SSR) | `src/hooks/useVirtualizer.ts` and `src/utils.ts` in the repository |
|
|
90
|
+
| Copy-paste list and table components | `README.md` sections "List example" and "Table example" |
|
|
91
|
+
|
|
92
|
+
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.
|