Skip to content

Instantly share code, notes, and snippets.

@csenio
Created October 28, 2019 14:50
Show Gist options
  • Save csenio/2bfdd84a308e33f1c4445476b0becb75 to your computer and use it in GitHub Desktop.
Save csenio/2bfdd84a308e33f1c4445476b0becb75 to your computer and use it in GitHub Desktop.
import {useEffect} from 'react'
// lock body scroll when modals are open 🔥
// if isLocked isn't specified it will lock the page when mounted
function useLockBodyScroll(isLocked = true) {
useEffect(() => {
const originalStyle = window.getComputedStyle(document.body).overflow
if (isLocked) {
document.body.style.overflow = 'hidden'
}
return () => {
document.body.style.overflow = originalStyle
}
}, [isLocked])
}
export default useLockBodyScroll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment