UNPKG

@redocly/theme

Version:

Shared UI components lib

22 lines (18 loc) 645 B
import { useEffect } from 'react'; export function useModalScrollLock(isOpen: boolean) { useEffect(() => { const originalOverflow = document.body.style.overflow; const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth; if (isOpen) { document.body.style.overflow = 'hidden'; document.body.style.marginRight = `${scrollbarWidth}px`; } else { document.body.style.overflow = originalOverflow; document.body.style.marginRight = ''; } return () => { document.body.style.overflow = originalOverflow; document.body.style.marginRight = ''; }; }, [isOpen]); }