|
| 1 | +import { Virtualizer } from "@tanstack/react-virtual"; |
| 2 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 3 | + |
| 4 | +const adjustTableHeight = ( |
| 5 | + tableRef: React.RefObject<HTMLTableElement>, |
| 6 | + virtualHeight: number, |
| 7 | + onPseudoHeightChange: useVirtualizationTableHeightProps["onPseudoHeightChange"], |
| 8 | +) => { |
| 9 | + if (!tableRef.current) return; |
| 10 | + |
| 11 | + // calculate the height for the pseudo element after the table |
| 12 | + const existingPseudoElement = window.getComputedStyle(tableRef.current, "::after"); |
| 13 | + const existingPseudoHeight = parseFloat(existingPseudoElement.height) || 0; |
| 14 | + const tableHeight = tableRef.current.clientHeight - existingPseudoHeight; |
| 15 | + const pseudoHeight = Math.max(virtualHeight - tableHeight, 0); |
| 16 | + onPseudoHeightChange?.(pseudoHeight); |
| 17 | + |
| 18 | + return pseudoHeight; |
| 19 | +}; |
| 20 | + |
| 21 | +export interface useVirtualizationTableHeightProps { |
| 22 | + parentRef: React.RefObject<HTMLDivElement>; |
| 23 | + virtualizer: Virtualizer<HTMLDivElement, Element>; |
| 24 | + enabled: boolean; |
| 25 | + onPseudoHeightChange?: (height: number) => void; |
| 26 | +} |
| 27 | + |
| 28 | +// https://github.com/TanStack/virtual/issues/640 |
| 29 | +const useVirtualizationTableHeight = (props: useVirtualizationTableHeightProps) => { |
| 30 | + const { parentRef, virtualizer, enabled, onPseudoHeightChange } = props; |
| 31 | + const scrollableRef = useRef<HTMLDivElement>(null); |
| 32 | + const tableRef = useRef<HTMLTableElement>(null); |
| 33 | + const [isScrollNearBottom, setIsScrollNearBottom] = useState(false); |
| 34 | + |
| 35 | + // avoid calling virtualizer methods when virtualization is disabled |
| 36 | + const virtualItems = enabled ? virtualizer.getVirtualItems() : []; |
| 37 | + const virtualSize = enabled ? virtualizer.getTotalSize() : 0; |
| 38 | + |
| 39 | + // callback to adjust the height of the pseudo element |
| 40 | + const handlePseudoResize = useCallback( |
| 41 | + () => adjustTableHeight(tableRef, virtualSize, onPseudoHeightChange), |
| 42 | + [tableRef, virtualSize, onPseudoHeightChange], |
| 43 | + ); |
| 44 | + |
| 45 | + // callback to handle scrolling, checking if we are near the bottom |
| 46 | + const handleScroll = useCallback(() => { |
| 47 | + if (parentRef.current) { |
| 48 | + const scrollPosition = parentRef.current?.scrollTop; |
| 49 | + const visibleHeight = parentRef.current?.clientHeight; |
| 50 | + setIsScrollNearBottom(scrollPosition > virtualSize * 0.95 - visibleHeight); |
| 51 | + } |
| 52 | + }, [parentRef, virtualSize]); |
| 53 | + |
| 54 | + // add an event listener on the scrollable parent container and resize the |
| 55 | + // pseudo element whenever the table renders with new data |
| 56 | + useEffect(() => { |
| 57 | + if (!enabled) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const scrollable = parentRef.current; |
| 62 | + if (scrollable) scrollable.addEventListener("scroll", handleScroll); |
| 63 | + handlePseudoResize(); |
| 64 | + |
| 65 | + return () => { |
| 66 | + if (scrollable) scrollable.removeEventListener("scroll", handleScroll); |
| 67 | + }; |
| 68 | + }, [handleScroll, handlePseudoResize, parentRef, enabled]); |
| 69 | + |
| 70 | + // if we are near the bottom of the table, resize the pseudo element each time |
| 71 | + // the length of virtual items changes (which is effectively the number of table |
| 72 | + // rows rendered to the DOM). This ensures we don't scroll too far or too short. |
| 73 | + useEffect(() => { |
| 74 | + if (!enabled) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (isScrollNearBottom) handlePseudoResize(); |
| 79 | + }, [isScrollNearBottom, virtualItems.length, handlePseudoResize, enabled]); |
| 80 | + |
| 81 | + return { scrollableRef, tableRef }; |
| 82 | +}; |
| 83 | + |
| 84 | +export { useVirtualizationTableHeight }; |
0 commit comments