OverlayScrollbars for React

0.5.6 · active · verified Sun Apr 19

OverlayScrollbars-react is the official React wrapper for the highly customizable OverlayScrollbars library, providing native-like scrollbar functionality with enhanced styling capabilities and flexible configuration. The current stable version, 0.5.6, is built to be compatible with OverlayScrollbars v2.x. It offers both a component-based approach via `OverlayScrollbarsComponent` and a hook-based API with `useOverlayScrollbars`, catering to different integration preferences within React applications. Releases are frequent, often mirroring updates and feature additions in the core OverlayScrollbars library, ensuring an actively maintained and up-to-date solution. Key differentiators include its robust performance, comprehensive accessibility features (especially in scenarios where native scrollbars are problematic), and a focus on deferred initialization to optimize application load times and user experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate OverlayScrollbars into a React component using `OverlayScrollbarsComponent`, apply custom options, handle scroll events, and ensure performance with deferred initialization.

import { useRef } from 'react';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-react';
import 'overlayscrollbars/overlayscrollbars.css';

function MyScrollableContent() {
  const scrollRef = useRef(null);

  const handleScroll = (instance, event) => {
    console.log('Scroll event!', instance.state().scrollOffsetElement.scrollTop);
  };

  return (
    <OverlayScrollbarsComponent
      element="div"
      options={{
        scrollbars: { autoHide: 'scroll', theme: 'os-theme-dark' },
        overflow: { x: 'hidden', y: 'scroll' }
      }}
      events={{ scroll: handleScroll }}
      defer
      style={{ maxHeight: '200px', width: '300px', border: '1px solid #ccc' }}
      ref={scrollRef}
    >
      <div style={{ padding: '15px', height: '500px' }}>
        <p>This is a long piece of content that will require scrolling.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        <p>More content to ensure the scrollbar appears. Keep scrolling!</p>
      </div>
    </OverlayScrollbarsComponent>
  );
}

export default MyScrollableContent;

view raw JSON →