Skip to content
Source
Sign inSign up
TanStack/virtual
Open History to see the last commit

main.tsx

123 lines3.3 KB

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