Skip to content

Instantly share code, notes, and snippets.

@garmjs
Created April 21, 2020 23:19
Show Gist options
  • Save garmjs/62104998a01df285424798e1a367e102 to your computer and use it in GitHub Desktop.
Save garmjs/62104998a01df285424798e1a367e102 to your computer and use it in GitHub Desktop.
Modal Prevent scroll/remember scroll position on Safari Mobile
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])
@garmjs
Copy link
Author

garmjs commented May 18, 2020

body[class="prevent-scroll"]{overflow:hidden !important;}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment