0

Tried all these option, two frameworks, none works:

import preventScroll from 'prevent-scroll'
import {
    disableBodyScroll,
    enableBodyScroll,
    clearAllBodyScrollLocks,
} from 'body-scroll-lock'

    useEffect(() => {
        document.body.style.overflow = 'hidden'
        document.body.style.touchAction = 'none'

        document.addEventListener('scroll', function (event) {
            event.preventDefault()
        })

        disableBodyScroll()
        preventScroll.on()

        // other approach from here: https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/
        document.body.style.position = 'fixed'
        document.body.style.top = `-${window.scrollY}px`

        return () => {
            document.body.style.overflow = 'auto'
            document.body.style.touchAction = 'auto'

            document.removeEventListener('scroll', function (event) {
                event.preventDefault()
            })
            preventScroll.off()

            clearAllBodyScrollLocks()

            document.body.style.position = ''
            document.body.style.top = ''
        }
    }, [])

The last approach comes from here will show a black stripe here: https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/ Keyboard is not visible on screen shot.

enter image description here

János
  • 32,867
  • 38
  • 193
  • 353
  • Does this answer your question? [Prevent BODY from scrolling when a modal is opened](https://stackoverflow.com/questions/9538868/prevent-body-from-scrolling-when-a-modal-is-opened) – Konrad Dec 31 '22 at 17:21
  • I have not seen any now approach what I listed here, most of the answer try to set only `overflow` to `hidden` and back to `none`. But this does not work on Safari. – János Dec 31 '22 at 18:20
  • Should work on safari as well – Konrad Dec 31 '22 at 18:31
  • No, it does not work in on iPhone Safari – János Jan 01 '23 at 02:58

0 Answers0