import React, { forwardRef, useRef } from 'react'; import { useTableContext } from '../TableContext'; import { useVirtualScrollPadding } from './utils/useVirtualScrollPadding'; import { paddingBottomStyles, paddingTopStyles } from './TableBody.styles'; import { TableBodyProps } from './TableBody.types'; const TableBody = forwardRef( ({ children, ...rest }: TableBodyProps, fwdRef) => { const { isVirtual, virtualTable } = useTableContext(); const { paddingTop, paddingBottom } = useVirtualScrollPadding( isVirtual, virtualTable, ); const topRef = useRef(null); const bottomRef = useRef(null); if (isVirtual) { topRef.current && topRef.current.style.setProperty( '--virtual-padding-top', `${paddingTop}px`, ); bottomRef.current && bottomRef.current.style.setProperty( '--virtual-padding-bottom', `${paddingBottom}px`, ); } return ( <> {/* As the user scrolls down, the paddingTop grows bigger, creating the effect of virtual scrolling */} {paddingTop > 0 && ( )} {children} {/* As the user scrolls down, the paddingBottom gets smaller, creating the effect of virtual scrolling */} {paddingBottom > 0 && ( )} ); }, ); TableBody.displayName = 'TableBody'; export default TableBody;