|
| 1 | +import { cx } from 'flairup'; |
| 2 | +import React, { useState, useCallback, useMemo } from 'react'; |
| 3 | + |
| 4 | +import { stylesheet } from '../../Stylesheet/stylesheet'; |
| 5 | +import useMeasure from '../../hooks/useMeasure'; |
| 6 | + |
| 7 | +const sum = (arr: number[]) => arr.reduce((a, b) => a + b, 0); |
| 8 | + |
| 9 | +const Virtualise = React.memo(({ children, itemHeights, overscan = 0, className }: { |
| 10 | + children: React.ReactNode[]; |
| 11 | + itemHeights: number[]; |
| 12 | + overscan?: number; |
| 13 | + className?: string; |
| 14 | +}) => { |
| 15 | + const [containerMeasure, { height: containerHeight }] = useMeasure<HTMLDivElement>(); |
| 16 | + const [scrollOffset, setScrollOffset] = useState(0); |
| 17 | + const totalHeight = useMemo(() => sum(itemHeights), [itemHeights]); |
| 18 | + |
| 19 | + const calculateVisibleItems = useCallback(() => { |
| 20 | + let start = 0; |
| 21 | + let currentHeight = 0; |
| 22 | + |
| 23 | + // Find the first item in the viewport |
| 24 | + for (let i = 0; i < itemHeights.length; i++) { |
| 25 | + if (currentHeight + itemHeights[i] > scrollOffset) { |
| 26 | + start = Math.max(i - overscan, 0); |
| 27 | + break; |
| 28 | + } |
| 29 | + currentHeight += itemHeights[i]; |
| 30 | + } |
| 31 | + |
| 32 | + let end = start; |
| 33 | + currentHeight = sum(itemHeights.slice(0, start)); |
| 34 | + |
| 35 | + // Find the last item in the viewport |
| 36 | + for (let i = start; i < itemHeights.length; i++) { |
| 37 | + currentHeight += itemHeights[i]; |
| 38 | + if (currentHeight > scrollOffset + containerHeight || i === itemHeights.length - 1) { |
| 39 | + end = Math.min(i + overscan + 1, itemHeights.length); |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Adjust end to include the last element if we're near the bottom |
| 45 | + if (scrollOffset + containerHeight >= totalHeight) { |
| 46 | + end = itemHeights.length; |
| 47 | + } |
| 48 | + |
| 49 | + return itemHeights.slice(start, end).map((_, index) => { |
| 50 | + const itemIndex = start + index; |
| 51 | + const top = sum(itemHeights.slice(0, itemIndex)); |
| 52 | + return { |
| 53 | + index: itemIndex, |
| 54 | + top, |
| 55 | + height: itemHeights[itemIndex], |
| 56 | + element: children[itemIndex], |
| 57 | + }; |
| 58 | + }); |
| 59 | + }, [scrollOffset, containerHeight, itemHeights, overscan, children, totalHeight]); |
| 60 | + |
| 61 | + const visibleItems = useMemo(() => calculateVisibleItems(), [calculateVisibleItems]); |
| 62 | + |
| 63 | + const handleScroll: React.UIEventHandler<HTMLDivElement> = useCallback((e) => { |
| 64 | + setScrollOffset(e.currentTarget.scrollTop); |
| 65 | + }, []); |
| 66 | + |
| 67 | + return ( |
| 68 | + <div |
| 69 | + ref={containerMeasure} |
| 70 | + onScroll={handleScroll} |
| 71 | + style={{ position: 'relative', height: '100%', overflowY: 'auto' }} |
| 72 | + className={cx(styles.virtualizeWrapper)} |
| 73 | + > |
| 74 | + <div className={cx(styles.virtualise)} style={{ height: totalHeight, position: 'relative' }}> |
| 75 | + <ul className={className} style={{ margin: 0, padding: 0 }}> |
| 76 | + {visibleItems.map(({ index, top, height, element }) => ( |
| 77 | + <div |
| 78 | + key={index} |
| 79 | + style={{ |
| 80 | + position: 'absolute', |
| 81 | + top: `${top}px`, |
| 82 | + height: `${height}px`, |
| 83 | + width: '100%', |
| 84 | + }} |
| 85 | + > |
| 86 | + {element} |
| 87 | + </div> |
| 88 | + ))} |
| 89 | + </ul> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +const styles = stylesheet.create({ |
| 96 | + virtualizeWrapper: { |
| 97 | + '.': 'epr-virutalise-wrapper', |
| 98 | + }, |
| 99 | + virtualise: { |
| 100 | + '.': 'epr-virtualise', |
| 101 | + }, |
| 102 | +}); |
| 103 | + |
| 104 | +Virtualise.displayName = 'Virtualise'; |
| 105 | +export default Virtualise; |
0 commit comments