Created
April 21, 2020 23:19
-
-
Save garmjs/62104998a01df285424798e1a367e102 to your computer and use it in GitHub Desktop.
Modal Prevent scroll/remember scroll position on Safari Mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
if (isActive) { | |
document.querySelector("body").classList.add("prevent-scroll") | |
// When the modal is shown, we want a fixed body | |
const scrollY = document.documentElement.style.getPropertyValue( | |
"--scroll-y" | |
) | |
const body = document.body | |
body.style.position = "fixed" | |
body.style.top = `-${scrollY}` | |
} else { | |
const body = document.body | |
const scrollY = body.style.top | |
body.style.position = "" | |
body.style.top = "" | |
window.scrollTo(0, parseInt(scrollY || "0") * -1) | |
document.querySelector("body").classList.remove("prevent-scroll") | |
} | |
}, [isActive]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
body[class="prevent-scroll"]{overflow:hidden !important;}