|
| 1 | +import React from 'react' |
| 2 | +import ReactDOM from 'react-dom/client' |
| 3 | +import { useVirtualizer } from '@tanstack/react-virtual' |
| 4 | + |
| 5 | +function getRandomInt(min: number, max: number) { |
| 6 | + return Math.floor(Math.random() * (max - min + 1)) + min |
| 7 | +} |
| 8 | + |
| 9 | +const randomHeight = (() => { |
| 10 | + const cache = new Map<string, number>() |
| 11 | + return (id: string) => { |
| 12 | + const value = cache.get(id) |
| 13 | + if (value !== undefined) { |
| 14 | + return value |
| 15 | + } |
| 16 | + const v = getRandomInt(25, 100) |
| 17 | + cache.set(id, v) |
| 18 | + return v |
| 19 | + } |
| 20 | +})() |
| 21 | + |
| 22 | +const App = () => { |
| 23 | + const parentRef = React.useRef<HTMLDivElement>(null) |
| 24 | + |
| 25 | + const rowVirtualizer = useVirtualizer({ |
| 26 | + count: 1002, |
| 27 | + getScrollElement: () => parentRef.current, |
| 28 | + estimateSize: () => 50, |
| 29 | + }) |
| 30 | + |
| 31 | + return ( |
| 32 | + <div> |
| 33 | + <div style={{ display: 'flex', gap: 8, marginBottom: 8 }}> |
| 34 | + <button |
| 35 | + id="scroll-to-100" |
| 36 | + onClick={() => |
| 37 | + rowVirtualizer.scrollToIndex(100, { behavior: 'smooth' }) |
| 38 | + } |
| 39 | + > |
| 40 | + Smooth scroll to 100 |
| 41 | + </button> |
| 42 | + <button |
| 43 | + id="scroll-to-500" |
| 44 | + onClick={() => |
| 45 | + rowVirtualizer.scrollToIndex(500, { behavior: 'smooth' }) |
| 46 | + } |
| 47 | + > |
| 48 | + Smooth scroll to 500 |
| 49 | + </button> |
| 50 | + <button |
| 51 | + id="scroll-to-1000" |
| 52 | + onClick={() => |
| 53 | + rowVirtualizer.scrollToIndex(1000, { behavior: 'smooth' }) |
| 54 | + } |
| 55 | + > |
| 56 | + Smooth scroll to 1000 |
| 57 | + </button> |
| 58 | + <button |
| 59 | + id="scroll-to-0" |
| 60 | + onClick={() => |
| 61 | + rowVirtualizer.scrollToIndex(0, { behavior: 'smooth' }) |
| 62 | + } |
| 63 | + > |
| 64 | + Smooth scroll to 0 |
| 65 | + </button> |
| 66 | + <button |
| 67 | + id="scroll-to-500-start" |
| 68 | + onClick={() => |
| 69 | + rowVirtualizer.scrollToIndex(500, { |
| 70 | + behavior: 'smooth', |
| 71 | + align: 'start', |
| 72 | + }) |
| 73 | + } |
| 74 | + > |
| 75 | + Smooth scroll to 500 (start) |
| 76 | + </button> |
| 77 | + <button |
| 78 | + id="scroll-to-500-center" |
| 79 | + onClick={() => |
| 80 | + rowVirtualizer.scrollToIndex(500, { |
| 81 | + behavior: 'smooth', |
| 82 | + align: 'center', |
| 83 | + }) |
| 84 | + } |
| 85 | + > |
| 86 | + Smooth scroll to 500 (center) |
| 87 | + </button> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div |
| 91 | + ref={parentRef} |
| 92 | + id="scroll-container" |
| 93 | + style={{ height: 400, overflow: 'auto' }} |
| 94 | + > |
| 95 | + <div |
| 96 | + style={{ |
| 97 | + height: rowVirtualizer.getTotalSize(), |
| 98 | + position: 'relative', |
| 99 | + }} |
| 100 | + > |
| 101 | + {rowVirtualizer.getVirtualItems().map((v) => ( |
| 102 | + <div |
| 103 | + key={v.key} |
| 104 | + data-testid={`item-${v.index}`} |
| 105 | + ref={rowVirtualizer.measureElement} |
| 106 | + data-index={v.index} |
| 107 | + style={{ |
| 108 | + position: 'absolute', |
| 109 | + top: 0, |
| 110 | + left: 0, |
| 111 | + transform: `translateY(${v.start}px)`, |
| 112 | + width: '100%', |
| 113 | + }} |
| 114 | + > |
| 115 | + <div style={{ height: randomHeight(String(v.key)) }}> |
| 116 | + Row {v.index} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ))} |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +ReactDOM.createRoot(document.getElementById('root')!).render(<App />) |
0 commit comments