useScrollLock
function useScrollLock(locked?: boolean): void; // default true
Stops the page from scrolling while locked is true, for example while a modal or drawer is
open. Scrolling inside an element with its own overflow, such as the modal, still works.
- It hides the overflow of
<html>, and pads it by the scrollbar's width so the content doesn't jump sideways when the scrollbar disappears. - Locks are counted: with a dialog open over another dialog, the page stays locked until both
release it. The page's own inline
overflowandpadding-rightare restored after the last. - It does nothing during server rendering.
Live demo
Example
import { useScrollLock } from "react-honey-hooks";
function Drawer({ open, children }: { open: boolean; children: React.ReactNode }) {
useScrollLock(open);
return open ? <aside className="drawer">{children}</aside> : null;
}