Last active
May 24, 2020 16:19
-
-
Save aandrewww/5ea5e2c071240e07fb3636c69cfc7d32 to your computer and use it in GitHub Desktop.
This file contains 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
import { useCallback } from 'react'; | |
export const usePreventPageScroll = () => { | |
const enable = useCallback(() => { | |
// Prevent scrolling | |
document.body.style.position = 'fixed'; | |
document.body.style.top = `-${window.scrollY}px`; | |
}, []); | |
const disable = useCallback(() => { | |
// Re-enable scrolling | |
document.body.style.position = ''; | |
document.body.style.top = ''; | |
window.scrollTo(0, parseInt(window.getComputedStyle(document.body).top || '0', 10) * -1); | |
}, []); | |
return { | |
enable, | |
disable, | |
}; | |
}; |
Author
aandrewww
commented
May 24, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment