()
| 11 | } |
| 12 | |
| 13 | function 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 |
nothing calls this directly
no test coverage detected