Blog>
Snippets

Customizing Scrollbar with TanStack Virtual

Provide an example of customizing the scrollbar in a virtualized table using TanStack Virtual, enhancing the user interface and experience.
import { Virtuoso } from '@tanstack/react-virtuoso';

function CustomScrollTable() {
  return (
    <Virtuoso
      style={{ height: '400px' }}
      totalCount={1000}
      itemContent={(index) => <div>Item {index}</div>}
      components={{
        ScrollSeekPlaceholder: () => <div style={{ textAlign: 'center' }}>Loading...</div>,
        Scroller: React.forwardRef(({ style, children, ...props }, ref) => (
          <div
            ref={ref}
            {...props}
            style={{
              ...style,
              overflowY: 'scroll',
              scrollbarWidth: 'none', /* For Firefox */
              msOverflowStyle: 'none', /* For Internet Explorer and Edge */
              '::-webkit-scrollbar': { display: 'none' } /* For Chrome, Safari, and Opera */
            }}
          >
            {children}
          </div>
        )),
      }}
    />
  );
}
This example demonstrates how to customize the scrollbar of a virtualized table using @tanstack/react-virtuoso. The 'Scroller' component is customized to hide the default scrollbar across various browsers, enhancing the UI and providing a smoother scrolling experience. This is achieved through specific styles like 'scrollbarWidth', 'msOverflowStyle', and '::-webkit-scrollbar'. Additionally, a placeholder is shown during scroll seeking with 'ScrollSeekPlaceholder', improving user feedback during data loading.