4.0.5 • Published 5 years ago

react-scrolllock-configurable v4.0.5

Weekly downloads
669
License
MIT
Repository
github
Last release
5 years ago

React Scroll Lock Configurable

Prevent scroll on the <body /> when a component is mounted.

Note from developer

This library is a fork of react-scrolllock by jossmac with ability to accept options object which will override some touch handlers. It is useful if you want to have vertical scroll locked while allowing for horizontal scroll or in cases where some of your children require horizontal scroll to work (which original package doesn't allow because of aggressive event capturing and propagation prevention)

Forked Github Repo

Forked NPM Package

Install

yarn add react-scrolllock-configurable

Usage

import ScrollLock, { TouchScrollable } from 'react-scrolllock-configurable';

class Modal extends Component {
  state = { lockScroll: false }
  render() {
    return (
      <div>
        ...
        // the lock accepts a single child element, which supports touch-scrolling.
        <ScrollLock>
          <ElementWithScrollableContent>...</ElementWithScrollableContent>
        </ScrollLock>

        // if your app structure doesn't allow wrapping like above, the `TouchScrollable`
        // component is exposed as a named export.
        <ScrollLock />
        <TouchScrollable>
          <ElementWithScrollableContent>...</ElementWithScrollableContent>
        </TouchScrollable>
        
        // you can also toggle the lock based on some state.
        <ScrollLock isActive={this.state.lockScroll} />
      </div>
    );
  }
}

Props

ScrollLock

PropertyDescription
accountForScrollbars booleanDefault: true -- Whether or not to replace the scrollbar width when active.
isActive booleanDefault: true -- Whether or not the lock is active.
children elementDefault: null -- This element is wrapped internally by the TouchScrollable component.

TouchScrollable

Wrap an element in the TouchScrollable component if you need an area that supports scroll on mobile.

This is necessary because the touchmove event is explicitly cancelled iOS doesn't observe overflow: hidden; when applied to the <body /> element 😢

PropertyDescription
children elementRequired The element that can be scrolled.
options objectOptional Provide an options object
Options
Options object can have `onTouchMove` and `onTouchStart` event handlers if you need to unblock scrolling 
{
  onTouchMove: (event) => {}
  onTouchStart: (event) => {}
}