React Perfect Scrollbar

1.5.8 · active · verified Sun Apr 19

react-perfect-scrollbar is a React component that wraps the 'perfect-scrollbar' library, providing a highly customizable and performant scrollbar solution for web applications. The current stable version is 1.5.8. Releases appear to be less frequent than monthly, but consistent enough to indicate active maintenance, with recent updates addressing bug fixes, new features like `onSync`, and improved TypeScript definitions. Its primary differentiator is offering a polished, native-feeling scroll experience that avoids the limitations and inconsistencies of default browser scrollbars, especially useful in complex UIs where standard scrollbars might be visually jarring or functionally inadequate. It achieves this by taking control of scroll behavior and rendering custom scrollbars that blend seamlessly with application design, while still relying on the underlying perfect-scrollbar library for the core functionality.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of PerfectScrollbar, including importing the component and its styles, wrapping content, configuring options, accessing the containerRef, and handling scroll events in a functional React component.

import React, { useRef, useEffect } from 'react';
import PerfectScrollbar from 'react-perfect-scrollbar';
import 'react-perfect-scrollbar/dist/css/styles.css';

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

  useEffect(() => {
    // Example: Scroll to bottom on mount
    if (scrollRef.current) {
      scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
    }
  }, []);

  return (
    <div style={{ width: '300px', height: '200px', border: '1px solid #ccc' }}>
      <PerfectScrollbar
        containerRef={ref => (scrollRef.current = ref)}
        options={{ suppressScrollX: false }}
        onScrollY={container => console.log(`Scrolled Y: ${container.scrollTop}`)}
        style={{ maxHeight: '100%', maxWidth: '100%' }}
      >
        <div style={{ height: '500px', padding: '10px' }}>
          {Array.from({ length: 50 }).map((_, i) => (
            <p key={i}>Item {i + 1}: This is some scrollable content.</p>
          ))}
        </div>
      </PerfectScrollbar>
    </div>
  );
}

// To render this:
// ReactDOM.render(<MyScrollableComponent />, document.getElementById('root'));

view raw JSON →