Created
April 8, 2026 06:38
-
-
Save khasky/8be10c4b1cbb874a13d7e925b1941815 to your computer and use it in GitHub Desktop.
useDisableBodyScroll React Hook
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
| // When `open` is true, sets `document.body.style.overflow` to `hidden` so | |
| // the page behind modals/drawers does not scroll; restores `unset` when closed. | |
| import { useEffect } from 'react'; | |
| export const useDisableBodyScroll = (open: boolean) => { | |
| useEffect(() => { | |
| if (open) { | |
| document.body.style.overflow = 'hidden'; | |
| } else { | |
| document.body.style.overflow = 'unset'; | |
| } | |
| }, [open]); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment