Skip to content

Commit 3914e84

Browse files
committed
examples: add scroll-padding
1 parent f24c8fe commit 3914e84

File tree

11 files changed

+1001
-0
lines changed

11 files changed

+1001
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example
2+
3+
To run this example:
4+
5+
- `npm install` or `yarn`
6+
- `npm run start` or `yarn start`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Vite App</title>
7+
<script type="module" src="https://cdn.skypack.dev/twind/shim"></script>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "tanstack-react-virtual-example-scroll-padding",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"serve": "vite preview --port 3001",
9+
"start": "vite"
10+
},
11+
"dependencies": {
12+
"@react-hookz/web": "^14.2.2",
13+
"@tanstack/react-virtual": "3.0.0-beta.0",
14+
"react": "^17.0.2",
15+
"react-dom": "^17.0.2"
16+
},
17+
"devDependencies": {
18+
"@rollup/plugin-replace": "^4.0.0",
19+
"@vitejs/plugin-react": "^1.2.0",
20+
"vite": "^2.8.6"
21+
}
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
html {
2+
font-family: sans-serif;
3+
font-size: 14px;
4+
}
5+
6+
.List table {
7+
background-color: #fff;
8+
border: 1px solid #e6e4dc;
9+
max-width: 100%;
10+
border-collapse: collapse;
11+
12+
display: flex;
13+
flex-direction: column;
14+
align-items: stretch;
15+
position: relative;
16+
}
17+
18+
.List thead {
19+
display: flex;
20+
flex-direction: column;
21+
background-color: #fff;
22+
23+
position: sticky;
24+
top: 0;
25+
z-index: 1;
26+
}
27+
28+
.List thead tr {
29+
height: 70px;
30+
}
31+
32+
.List tr {
33+
display: flex;
34+
flex-direction: row;
35+
}
36+
37+
.List td,
38+
.List th {
39+
display: flex;
40+
flex-direction: row;
41+
align-items: center;
42+
justify-content: center;
43+
width: 180px;
44+
}
45+
46+
.ListItemEven {
47+
background-color: #e6e4dc;
48+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
4+
import './index.css'
5+
6+
import { useMeasure } from '@react-hookz/web/esm'
7+
import { useVirtualizer } from '@tanstack/react-virtual'
8+
9+
function App() {
10+
const parentRef = React.useRef<HTMLDivElement>()
11+
const [theadSize, theadRef] = useMeasure<HTMLTableSectionElement>()
12+
13+
const rowVirtualizer = useVirtualizer({
14+
count: 10000,
15+
getScrollElement: () => parentRef.current,
16+
estimateSize: React.useCallback(() => 35, []),
17+
overscan: 5,
18+
paddingStart: theadSize?.height ?? 0,
19+
scrollPaddingStart: theadSize?.height ?? 0,
20+
})
21+
22+
return (
23+
<>
24+
<div className="flex gap-2">
25+
<button
26+
onClick={() => {
27+
rowVirtualizer.scrollToIndex(40)
28+
}}
29+
className="border border-black"
30+
>
31+
Scroll to index 40
32+
</button>
33+
<button
34+
onClick={() => {
35+
rowVirtualizer.scrollToIndex(20)
36+
}}
37+
className="border border-black"
38+
>
39+
Then scroll to index 20
40+
</button>
41+
</div>
42+
43+
<br />
44+
<br />
45+
46+
<div
47+
ref={parentRef}
48+
className="List"
49+
style={{
50+
height: `200px`,
51+
width: `400px`,
52+
overflow: 'auto',
53+
}}
54+
>
55+
<table
56+
style={{
57+
height: `${rowVirtualizer.getTotalSize()}px`,
58+
width: '100%',
59+
}}
60+
>
61+
<thead ref={theadRef}>
62+
<tr>
63+
<th>Index</th>
64+
<th>Key</th>
65+
</tr>
66+
</thead>
67+
<tbody>
68+
{rowVirtualizer.getVirtualItems().map((virtualRow) => (
69+
<tr
70+
key={virtualRow.index}
71+
className={
72+
virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'
73+
}
74+
style={{
75+
position: 'absolute',
76+
top: 0,
77+
left: 0,
78+
width: '100%',
79+
height: `${virtualRow.size}px`,
80+
transform: `translateY(${virtualRow.start}px)`,
81+
}}
82+
>
83+
<td>#{virtualRow.index}</td>
84+
<td>{virtualRow.key}</td>
85+
</tr>
86+
))}
87+
</tbody>
88+
</table>
89+
</div>
90+
{process.env.NODE_ENV === 'development' ? (
91+
<p>
92+
<strong>Notice:</strong> You are currently running React in
93+
development mode. Rendering performance will be slightly degraded
94+
until this application is build for production.
95+
</p>
96+
) : null}
97+
</>
98+
)
99+
}
100+
101+
ReactDOM.render(
102+
<React.StrictMode>
103+
<App />
104+
</React.StrictMode>,
105+
document.getElementById('root'),
106+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"composite": true,
3+
"extends": "../../../tsconfig.base.json",
4+
"compilerOptions": {
5+
"outDir": "./build/types"
6+
},
7+
"files": ["src/main.tsx"],
8+
"include": [
9+
"src"
10+
// "__tests__/**/*.test.*"
11+
]
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as path from 'path'
2+
import { defineConfig } from 'vite'
3+
import react from '@vitejs/plugin-react'
4+
import rollupReplace from '@rollup/plugin-replace'
5+
6+
// https://vitejs.dev/config/
7+
export default defineConfig({
8+
plugins: [
9+
rollupReplace({
10+
preventAssignment: true,
11+
values: {
12+
__DEV__: JSON.stringify(true),
13+
'process.env.NODE_ENV': JSON.stringify('development'),
14+
},
15+
}),
16+
react(),
17+
],
18+
})

0 commit comments

Comments
 (0)