question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent Body Scrolling (iOS)

See original GitHub issue

There a lot of threads online about how to lock the body especially on iOS, but I haven’t found a good answer.

body { overflow: hidden; } Doesn’t work on iOS.

My question is: how can I prevent Body Scrolling on iOS with react-modal?

Note: I found this package that it might work, but the problem is that I can’t find a way to make it work with react-modal and hooks. React Modal doesn’t accept a ref using useRef.

https://github.com/willmcpo/body-scroll-lock

Any help?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

17reactions
ursnjcommented, Dec 10, 2020

Simple solution would be 👍

onAfterOpen={() => document.body.style.overflow = 'hidden'}
onAfterClose={() => document.body.style.overflow = 'unset'}
5reactions
trungtincommented, Oct 19, 2020

@Alfrex92 you could try this.

<ReactModal
  onAfterOpen={() => {
    document.body.style.top = `-${window.scrollY}px`
    document.body.style.position = 'fixed'
  }}
  onAfterClose={() => {
    const scrollY = document.body.style.top
    document.body.style.position = ''
    document.body.style.top = ''
    window.scrollTo(0, parseInt(scrollY || '0') * -1)
  }}
  ...other props
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Prevent Scrolling The Page On iOS Safari 15 - PQINA
If you're browsing this page on iOS Safari, scroll up and down a bit to see the height stays fixed no matter of...
Read more >
iOS - css/js - Overlay scroll but prevent body scroll
There is no way around this right now. As of iOS 9.3 there's still no good way to prevent the scroll on the...
Read more >
Disable Body Scrolling For Open Modals on iOS Devices
1. Stop everything from scrolling, both body background and everything inside modal. 2. Stop the background from scrolling, while allowing ...
Read more >
Preventing body scroll for modals in iOS - Today I Learned
Preventing body scroll for modals in iOS. IOS does not respect overflow: hidden set on body element, and allows for scrolling anyway, which...
Read more >
Preventing body scroll for modals in iOS - Ben Frain
iOS doesn't prevent users from scrolling past the modal if there is content that exceeds the viewport height, despite you adding that condition ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found