forked from TanStack/virtual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
126 lines (110 loc) · 3.24 KB
/
main.tsx
File metadata and controls
126 lines (110 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import {
elementScroll,
useVirtualizer,
VirtualizerOptions,
} from '@tanstack/react-virtual'
function easeInOutQuint(t) {
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t
}
function App() {
const parentRef = React.useRef<HTMLDivElement>()
const scrollingRef = React.useRef<number>()
const scrollToFn: VirtualizerOptions<any, any>['scrollToFn'] =
React.useCallback((offset, canSmooth, instance) => {
const duration = 1000
const start = parentRef.current.scrollTop
const startTime = (scrollingRef.current = Date.now())
const run = () => {
if (scrollingRef.current !== startTime) return
const now = Date.now()
const elapsed = now - startTime
const progress = easeInOutQuint(Math.min(elapsed / duration, 1))
const interpolated = start + (offset - start) * progress
if (elapsed < duration) {
elementScroll(interpolated, canSmooth, instance)
requestAnimationFrame(run)
} else {
elementScroll(interpolated, canSmooth, instance)
}
}
requestAnimationFrame(run)
}, [])
const rowVirtualizer = useVirtualizer({
count: 10000,
getScrollElement: () => parentRef.current,
estimateSize: () => 35,
overscan: 5,
scrollToFn,
})
const randomIndex = Math.floor(Math.random() * 10000)
return (
<div>
<p>
This smooth scroll example uses the <code>`scrollToFn`</code> to
implement a custom scrolling function for the methods like{' '}
<code>`scrollToIndex`</code> and <code>`scrollToOffset`</code>
</p>
<br />
<br />
<div>
<button onClick={() => rowVirtualizer.scrollToIndex(randomIndex)}>
Scroll To Random Index ({randomIndex})
</button>
</div>
<br />
<br />
<div
ref={parentRef}
className="List"
style={{
height: `200px`,
width: `400px`,
overflow: 'auto',
}}
>
<div
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
width: '100%',
position: 'relative',
}}
>
{rowVirtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.index}
className={virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
Row {virtualRow.index}
</div>
))}
</div>
</div>
<br />
<br />
{process.env.NODE_ENV === 'development' ? (
<p>
<strong>Notice:</strong> You are currently running React in
development mode. Rendering performance will be slightly degraded
until this application is build for production.
</p>
) : null}
</div>
)
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'),
)