Skip to content

Instantly share code, notes, and snippets.

@khasky
Created April 8, 2026 06:38
Show Gist options
  • Select an option

  • Save khasky/8be10c4b1cbb874a13d7e925b1941815 to your computer and use it in GitHub Desktop.

Select an option

Save khasky/8be10c4b1cbb874a13d7e925b1941815 to your computer and use it in GitHub Desktop.
useDisableBodyScroll React Hook
// 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