Giter Club home page Giter Club logo

Comments (5)

willmcpo avatar willmcpo commented on May 21, 2024

from body-scroll-lock.

maeertin avatar maeertin commented on May 21, 2024

Hey, thanks @willmcpo for the quick response!

Elaborating on my scenario
So I did some more digging into this and there seems to have been more to it then I thought. So the problem starts already when I call disableBodyScroll as this will set the restore values that you mention. In my case these saved restore values are incorrect due to material-ui/Dialog setting overflow: hidden to document.body before disableBodyScroll is fired, and therefore calling enableBodyScroll does not work. Don't really know how to go about this issue but I somehow expected that enableBodyScroll would enable body scrolling regardless of when I might have called disableBodyScroll.

The issue
Incorrect restore values being saved and no way to correct them.

Suggestion
What if the library exposed a function to either update the previousBody... vars or one that set some similar initialBody... vars to be used instead if defined?

Demo
I recreated a condensed example of the issue in this CodeSandbox where I use your library within the ScrollLock component.
https://codesandbox.io/s/50nkpljo9l

from body-scroll-lock.

maeertin avatar maeertin commented on May 21, 2024

Since this is a compatibility issue with handling of overflow: hidden on document.body, another solution would be to let lib users opt in/out of the css/js solutions of scroll canceling.

from body-scroll-lock.

maeertin avatar maeertin commented on May 21, 2024

I see that you already forward the options object to the function setOverflowHidden. What if this function was updated to something like below to allow control of the restore values.

const setOverflowHidden = (options = {}) => {
  const {
    reserveScrollBarGap,
    restoreBodyPaddingRight: optionsBPR,
    restoreBodyOverflowSetting: optionsBOS,
  } = options

  // Setting overflow on body/documentElement synchronously in Desktop Safari slows down
  // the responsiveness for some reason. Setting within a setTimeout fixes this.
  setTimeout(() => {
    // If previousBodyPaddingRight is already set, don't set it again.
    if (previousBodyPaddingRight === undefined) {
      const scrollBarGap = window.innerWidth - document.documentElement.clientWidth

      if (reserveScrollBarGap && scrollBarGap > 0) {
        // type-check against undefined to allow falsy values
        previousBodyPaddingRight = optionsBPR !== undefined
            ? optionsBPR
            : document.body.style.paddingRight
        document.body.style.paddingRight = `${scrollBarGap}px`
      }
    }

    // If previousBodyOverflowSetting is already set, don't set it again.
    if (previousBodyOverflowSetting === undefined) {
      // type-check against undefined to allow falsy values
      previousBodyOverflowSetting = optionsBOS !== undefined
        ? optionsBOS
        : document.body.style.overflow
      document.body.style.overflow = 'hidden'
    }
  })
}

from body-scroll-lock.

willmcpo avatar willmcpo commented on May 21, 2024

from a quick glimpse, seems ok and intuitive.

would you mind putting that in a PR ? =) 👍

from body-scroll-lock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.