@aiquants/virtualscroll 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +192 -0
  3. package/dist/cli.js +28 -0
  4. package/package.json +86 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 fehde-k
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,192 @@
1
+ # @aiquants/virtualscroll
2
+
3
+ High-performance virtual scrolling component for React with variable item heights using Fenwick Tree optimization.
4
+
5
+ ## Features
6
+
7
+ - ⚡ **High Performance**: Optimized for thousands of items with O(log n) operations
8
+ - 📐 **Variable Heights**: Support for items with different heights
9
+ <!-- - 🌲 **Fenwick Tree**: Advanced data structure for efficient prefix sum calculations -->
10
+ - 🎯 **Precise Scrolling**: Accurate scroll positioning and smooth navigation
11
+ <!-- - 🔄 **LRU Caching**: Built-in cache for optimal memory usage -->
12
+ - 📱 **Touch Support**: Full support for touch devices
13
+ - 🎨 **Customizable**: Flexible styling and theming options
14
+ - 🔧 **TypeScript**: Full TypeScript support with comprehensive type definitions
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @aiquants/virtualscroll
20
+ # or
21
+ yarn add @aiquants/virtualscroll
22
+ # or
23
+ pnpm add @aiquants/virtualscroll
24
+ ```
25
+
26
+ ## Basic Usage
27
+
28
+ ```tsx
29
+ import { VirtualScroll } from '@aiquants/virtualscroll'
30
+ import { useCallback } from 'react'
31
+
32
+ type Item = {
33
+ id: number
34
+ text: string
35
+ height: number
36
+ }
37
+
38
+ const items: Item[] = Array.from({ length: 10000 }, (_, i) => ({
39
+ id: i,
40
+ text: `Item ${i}`,
41
+ height: Math.floor(Math.random() * 50) + 30, // Random height between 30-80px
42
+ }))
43
+
44
+ function App() {
45
+ const getItem = useCallback((index: number) => items[index], [])
46
+ const getItemHeight = useCallback((index: number) => items[index].height, [])
47
+
48
+ return (
49
+ <div style={{ height: '400px', width: '100%' }}>
50
+ <VirtualScroll
51
+ itemCount={items.length}
52
+ getItem={getItem}
53
+ getItemHeight={getItemHeight}
54
+ viewportSize={400}
55
+ overscanCount={5}
56
+ className="border border-gray-300"
57
+ >
58
+ {(item, index) => (
59
+ <div
60
+ key={item.id}
61
+ style={{
62
+ height: item.height,
63
+ padding: '8px',
64
+ borderBottom: '1px solid #eee',
65
+ display: 'flex',
66
+ alignItems: 'center',
67
+ }}
68
+ >
69
+ <span>#{index}: {item.text}</span>
70
+ </div>
71
+ )}
72
+ </VirtualScroll>
73
+ </div>
74
+ )
75
+ }
76
+ ```
77
+
78
+ ## API Reference
79
+
80
+ ### VirtualScroll Props
81
+
82
+ | Prop | Type | Required | Description |
83
+ |------|------|----------|-------------|
84
+ | `children` | `(item: T, index: number) => ReactNode` | ✅ | Render function for items |
85
+ | `itemCount` | `number` | ✅ | Total number of items |
86
+ | `getItem` | `(index: number) => T` | ✅ | Function to get item at index |
87
+ | `getItemHeight` | `(index: number) => number` | ✅ | Function to get item height |
88
+ | `viewportSize` | `number` | ✅ | Height of the visible area |
89
+ | `overscanCount` | `number` | ❌ | Number of items to render outside viewport (default: 5) |
90
+ | `className` | `string` | ❌ | CSS class name |
91
+ | `onScroll` | `(position: number, totalHeight: number) => void` | ❌ | Scroll event handler |
92
+ | `onRangeChange` | `(start: number, end: number, visibleStart: number, visibleEnd: number, position: number, totalHeight: number) => void` | ❌ | Range change handler |
93
+ | `background` | `ReactNode` | ❌ | Background element |
94
+ | `initialScrollIndex` | `number` | ❌ | Initial scroll index |
95
+ | `initialScrollOffset` | `number` | ❌ | Initial scroll offset |
96
+
97
+ ### VirtualScrollHandle Methods
98
+
99
+ | Method | Type | Description |
100
+ |--------|------|-------------|
101
+ | `scrollTo` | `(position: number) => void` | Scroll to specific position |
102
+ | `scrollToIndex` | `(index: number) => void` | Scroll to specific item index |
103
+ | `getScrollPosition` | `() => number` | Get current scroll position |
104
+ <!-- | `getContentSize` | `() => number` | Get total content size | -->
105
+ <!-- | `getViewportSize` | `() => number` | Get viewport size | -->
106
+ <!-- | `getFenwickTreeTotalHeight` | `() => number` | Get Fenwick tree total height | -->
107
+ <!-- | `getFenwickSize` | `() => number` | Get Fenwick tree size | -->
108
+
109
+ ## Advanced Usage
110
+
111
+ ### With Ref and Scroll Control
112
+
113
+ ```tsx
114
+ import { VirtualScroll, VirtualScrollHandle } from '@aiquants/virtualscroll'
115
+ import { useRef } from 'react'
116
+
117
+ function AdvancedExample() {
118
+ const virtualScrollRef = useRef<VirtualScrollHandle>(null)
119
+
120
+ const scrollToTop = () => {
121
+ virtualScrollRef.current?.scrollTo(0)
122
+ }
123
+
124
+ const scrollToIndex = (index: number) => {
125
+ virtualScrollRef.current?.scrollToIndex(index)
126
+ }
127
+
128
+ return (
129
+ <div>
130
+ <div>
131
+ <button onClick={scrollToTop}>Scroll to Top</button>
132
+ <button onClick={() => scrollToIndex(500)}>Scroll to Item 500</button>
133
+ </div>
134
+ <VirtualScroll
135
+ ref={virtualScrollRef}
136
+ itemCount={100000}
137
+ getItem={getItem}
138
+ getItemHeight={getItemHeight}
139
+ viewportSize={400}
140
+ onRangeChange={(start, end) => {
141
+ console.log(`Visible range: ${start} - ${end}`)
142
+ }}
143
+ >
144
+ {(item, index) => <ItemComponent item={item} index={index} />}
145
+ </VirtualScroll>
146
+ </div>
147
+ )
148
+ }
149
+ ```
150
+
151
+ ### Custom Scrollbar Styling
152
+
153
+ ```tsx
154
+ <VirtualScroll
155
+ itemCount={items.length}
156
+ getItem={getItem}
157
+ getItemHeight={getItemHeight}
158
+ viewportSize={400}
159
+ className="custom-virtual-scroll"
160
+ >
161
+ {(item, index) => <ItemComponent item={item} index={index} />}
162
+ </VirtualScroll>
163
+
164
+ <style>
165
+ .custom-virtual-scroll .scrollbar {
166
+ background-color: #f0f0f0;
167
+ }
168
+
169
+ .custom-virtual-scroll .scrollbar-thumb {
170
+ background-color: #007acc;
171
+ border-radius: 4px;
172
+ }
173
+ </style>
174
+ ```
175
+
176
+ ## Performance Tips
177
+
178
+ 1. **Memoize callback functions**: Use `useCallback` for `getItem` and `getItemHeight`
179
+ 2. **Optimize item rendering**: Memoize item components when possible
180
+ 3. **Adjust overscan count**: Balance between smooth scrolling and memory usage
181
+ 4. **Consider item height consistency**: More consistent heights provide better performance
182
+
183
+ ## Browser Support
184
+
185
+ - Chrome 88+
186
+ - Firefox 87+
187
+ - Safari 14+
188
+ - Edge 88+
189
+
190
+ ## License
191
+
192
+ MIT
package/dist/cli.js ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from "child_process";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+ const args = process.argv.slice(2);
8
+ if (args[0] === "demo") {
9
+ console.log("Starting demo server...");
10
+ const demoPath = path.join(__dirname, "..", "demo");
11
+ const child = spawn("pnpm", ["run", "dev"], {
12
+ cwd: demoPath,
13
+ stdio: "inherit",
14
+ shell: process.platform === "win32"
15
+ });
16
+ child.on("close", (code) => {
17
+ if (code !== 0) {
18
+ console.error(`Demo server process exited with code ${code}`);
19
+ }
20
+ });
21
+ child.on("error", (err) => {
22
+ console.error("Failed to start demo server:", err);
23
+ });
24
+ } else {
25
+ console.log(`Unknown command: ${args[0]}`);
26
+ console.log("Usage: npx @aiquants/virtualscroll demo");
27
+ process.exit(1);
28
+ }
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@aiquants/virtualscroll",
3
+ "version": "0.1.0",
4
+ "description": "High-performance virtual scrolling component for React with variable item heights",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "scripts": {
21
+ "build": "vite build && vite build --config vite.config.cli.ts",
22
+ "dev": "vite build --watch",
23
+ "demo:dev": "cd demo && pnpm run dev",
24
+ "demo:build": "cd demo && pnpm run build",
25
+ "demo:start": "cd demo && pnpm start",
26
+ "typecheck": "tsc --noEmit",
27
+ "test": "vitest",
28
+ "test:watch": "vitest --watch",
29
+ "test:coverage": "vitest --coverage",
30
+ "lint": "biome check src",
31
+ "lint:fix": "biome check --write src",
32
+ "format": "biome format --write src",
33
+ "format:check": "biome format src",
34
+ "clean": "rimraf dist",
35
+ "prepublishOnly": "npm run clean && npm run typecheck && npm run build",
36
+ "publish:patch": "npm version patch && npm publish",
37
+ "publish:minor": "npm version minor && npm publish",
38
+ "publish:major": "npm version major && npm publish"
39
+ },
40
+ "dependencies": {
41
+ "react": "^19.1.1",
42
+ "tailwind-merge": "^2.6.0"
43
+ },
44
+ "peerDependencies": {
45
+ "react": ">=19.0.0",
46
+ "react-dom": ">=19.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "@biomejs/biome": "^1.9.4",
50
+ "@testing-library/jest-dom": "^6.6.5",
51
+ "@testing-library/react": "^16.3.0",
52
+ "@testing-library/user-event": "^14.5.2",
53
+ "@types/react": "^19.1.11",
54
+ "@types/react-dom": "^19.1.8",
55
+ "@vitejs/plugin-react": "^5.0.1",
56
+ "autoprefixer": "^10.4.21",
57
+ "jsdom": "^26.0.0",
58
+ "postcss": "^8.5.6",
59
+ "react-dom": "^19.1.1",
60
+ "rimraf": "^6.0.1",
61
+ "tailwindcss": "^4.1.12",
62
+ "tailwindcss-animate": "^1.0.7",
63
+ "typescript": "^5.9.2",
64
+ "vite": "^7.1.3",
65
+ "vite-plugin-dts": "^4.5.4",
66
+ "vitest": "^3.2.4"
67
+ },
68
+ "keywords": [
69
+ "react",
70
+ "virtual-scroll",
71
+ "virtual-scrolling",
72
+ "performance",
73
+ "large-list",
74
+ "typescript",
75
+ "fenwick-tree",
76
+ "variable-height"
77
+ ],
78
+ "author": "fehde-k",
79
+ "license": "MIT",
80
+ "bin": {
81
+ "virtualscroll": "./dist/cli.js"
82
+ },
83
+ "publishConfig": {
84
+ "access": "public"
85
+ }
86
+ }