npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-scroll-locky

v1.5.0

Published

Smartly prevents page from being scrolled

Downloads

4,738

Readme

📜🔒 react-scroll-locky


📜 Prevents page from being scrolled. Or any other "not permitted" container.

💡 Hides scrollbars, preserving page width.

🤘 Works on any desktop or mobile browser.

📦 All features are hidden inside.

👫 Stands more that one instance.

🤔 Supports nested locks

🔥 Supports nested scrollable elements.

Just wrap your content with <ScrollLocky /> and it block any iterations with the rest of a page.

This is one line solution for the problem.

All due to React-Locky it uses underneath

this could be dangerous for focus state management. Consider use more composite library - react-focus-on - to handle the every edge case.

  • focus
  • scroll
  • aria (inert)

API

Just wrap anything with ScrollLocky, which accepts only one prop - "enabled"

There is only a few pros to configure

  • noRelative - do not apply "position:relative" on body.
  • noImportant - do not apply "!important" to any rules.
  • className - className for a internal div
  • headless - enables "no-div" mode (will pick the first DOM node-inside)
  • enabled - allows to disable Lock behavior (CSS modification and Locky), keeping it rendered.
  • isolation - allows to disable event isolation, preventing only scroll events, not everything outside target node (default behaviour). Use isolation:false if you have some "shadow" underneath modal, to catch and redirect all events.
  • gapMode=[padding|margin(default)] - gap policy, to control the way scrollLocky generate the gap instead of scrollbars. This option affects how absolutely positioned elements will work:
    • gapMode="padding" - "right:0" will be on window right (will jump on scroll removal)
    • gapMode="margin" - "right:0" will be in constant position (will not jump, but leave a gap)
import {ScrollLocky} from 'react-scroll-locky';

<Modal>
 <ScrollLocky>
   <MyContent>
     Anything!
   </MyContent>
 </ScrollLocky>
</Modal>   

Hide scrollbars only

To hide body scrollbars only (does not disable scroll on scrollable container, or body scroll on iOS) use HideBodyScroll component

import {HideBodyScroll} from 'react-scroll-locky';

<HideBodyScroll noRelative noImportant gapMode/> // body scrollbar is hidden

The order

You may have more than one active Lock on the page:

  • Only first Lock is real. Only it hides the scrollbars.
  • Only the last Lock is active. Only last-mounted Locky is working, silencing the rest of a page.
  • To have more that one active lock - consider using HideBodyScroll + react-locky directly.

Gap modes

  • "padding" - for the simple use. It will keep scroll-bar-gap for the normal elements, letting absolutely or fixed positioned elements hit the right-most window edge.
  • "margin" - for the advanced use. Will always preserve the gap, letting only the fixed positioned elements fill the while page(preffered mode)

Default Gap Mode is "margin", it would fit for almost anyone. But if you have another margin on your body (please dont), or have width:100% on the body - it would not.

Then, and only then use gapMode="padding", and dont forget to add box-sizing: border-box; to include paddings, we are going to set, to your width. (and don't send paddings on body, then).

To fill the gap with absolute positioned elements - use another exposed component.

Special component - ScrollLockyPane will help maintain the right "right" position. Alternatively - use react-scroll-locky-edge-right class, to set proper right border for a container.

import {ScrollLocky, ScrollLockyPane} from 'react-scroll-locky';

// position:absolute, left:0, right: -"gap"px
<ScrollLockyPane>
  // your modal inside
  <Modal>
     <ScrollLocky>
        <MyContent/>  
     </ScrollLocky>
  </Modal> 
</ScrollLockyPane>

ScrollLockyPane will "return" the "consumed" width to the component.

Multiple locks

  • If you need multiple locks to be active in a same time - just do it. They will work together.

Article

There is a medium article about preventing the body scroll - How to fight the scroll

More

For a good modals you also need a proper Focus Management Library. Use react-focus-lock to complete the picture.

See also

Licence

MIT