/react-remove-scroll

Removes and disables ๐Ÿ“œin a "React" way

Primary LanguageTypeScript

react-remove-scroll

NPM version

Disables scroll outside of children node.

  • ๐Ÿ–ฑ mouse and touch devices friendly
  • ๐Ÿ“œ removes document scroll bar maintaining it space
  • โœ… support nested scrollable elements
  • ๐Ÿ•ณ supports react-portals (uses React Event system)

Usage

Just wrap content, which should be scrollable, and everything else would not.

import {RemoveScroll} from 'react-remove-scroll';

<RemoveScroll>
  Only this content would be scrollable
</RemoveScroll>  

RemoveScroll accept following props

  • children
  • [enabled] - activate or deactivate component behaviour without removing it.
  • [noIsolation] - disables outer event capturing
  • [forwardProps] - will forward all props to the children
  • [className] - className for an internal div
  • [removeScrollBar] - to control scroll bar removal. Set to false, if you prefer to keep it (wheel and touch scroll still disabled).

Internal div

But default RemoveScroll will create a div to handle and capture events. You may specify className for it, if you need, or remove it.

The following code samples will produce the same output

<RemoveScroll className="scroll">
  Only this content would be scrollable
</RemoveScroll>
<RemoveScroll forwardProps>
  <div className="scroll"> //RemoveScroll will inject props to this div
    Only this content would be scrollable
  </div>
</RemoveScroll> 

Pick the first one if you don't need a second.

Position:fixed elements

To properly size these elements please add a special className to them.

import {RemoveScroll} from 'react-remove-scroll';

// to make "width: 100%"
<div className={cx(classWithPositionFixed, RemoveScroll.classNames.fullWidth)} />

// to make "right:0"
<div className={cx(classWithPositionFixed, RemoveScroll.classNames.zeroRight)} />

See react-remove-scroll-bar documentation for details.

More than one lock

When stacked more is active (default) only one (last) component would be active.

Performance

To do the job this library setup non passive event listener. Chrome dev tools would complain about it, as a performance no-op.

We have to use synchronous scroll/touch handler, and it may affect scrolling performance.

Consider using noIsolation mode, if you have large scrollable areas.

Size

1.5kb after compression (excluding tslib).

Scroll-Locky

This is a refactoring of another library - react-scroll-locky - to make package smaller and more react-portals friendly.

See also

This package is relative smaller(1), more react friendly(2), works with non zero body margins(3), and has a better "overscroll" management.

License

MIT