react-remove-scroll is a utility component for React applications that effectively disables scrolling outside of a specified child node. It provides robust scroll blocking for both mouse and touch devices, handles vertical and horizontal scrolling, and adeptly removes the document scrollbar while preserving layout space. Currently stable at version 2.7.2, the library maintains an active release cadence, frequently delivering bug fixes and minor features. Key differentiators include its support for nested scrollable elements, compatibility with React Portals, and a highly optimized bundle size, achieved with a significant 80% reduction in version 2.0.0. It also offers a sidecar pattern for lazy loading the scroll-blocking logic, allowing for further performance optimization.
npm install react-remove-scrollVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the RemoveScroll component to lock the body scroll while a modal-like element is open, allowing only the modal's content to be scrollable. A button toggles the scroll lock state.
Upgrade your React and ReactDOM peer dependencies to version 16.8.0 or newer.
Avoid using the `inert` prop unless absolutely necessary and thoroughly test its impact on event handling, especially with portals. Consider `noIsolation` for less aggressive event handling.
If pinch-to-zoom is required, set `allowPinchZoom={true}`, but be aware that it might compromise the complete scroll isolation effect. Test thoroughly on target devices.Apply `RemoveScroll.classNames.fullWidth` to `position: fixed` elements that need their width adjusted. For elements needing `right: 0` adjustment, use `RemoveScroll.classNames.zeroRight`.
If you need to pass props directly to a child or avoid an extra wrapper `div`, use the `forwardProps` prop on `RemoveScroll` and provide your own child element. Otherwise, use `className` directly on `RemoveScroll` for its internal div.
Upgrade your `react` and `react-dom` peer dependencies to version 16.8.0 or newer.
Ensure `RemoveScroll` is correctly wrapping the *only* intended scrollable content. Check if `allowPinchZoom` needs to be explicitly `false`. Inspect CSS for conflicting `overflow` properties or `position: fixed` elements not handled with `RemoveScroll.classNames`.
Remove the `inert` prop from `RemoveScroll` unless absolutely necessary. For simpler event isolation, consider using `noIsolation` if event capturing is problematic with specific shadow DOM or third-party libraries.
Apply `className={RemoveScroll.classNames.fullWidth}` to any `position: fixed` elements (like modals or headers) that are expected to span 100% width, to ensure they maintain correct sizing when the scrollbar is hidden.