useLockBodyScroll
Locks scrolling on document.body (or a target element) with scrollbar gap compensation and reference counting across multiple active locks.
Loading preview...
The useLockBodyScroll hook prevents background scrolling by setting overflow: hidden on document.body (or a custom target element), compensates for the disappearing scrollbar width via padding-right to prevent layout shift, and reference-counts concurrent locks so nested modals unlock safely.
Installation
Usage
Examples
Locking Scroll When a Drawer or Modal Opens
Pass a boolean locked flag so body scrolling is disabled only while the overlay is open.
Locking a Custom Scrollable Container
Pass a target element ref and optionally disable reserveScrollBarGap to lock a specific container element instead of document.body.
API Reference
UseLockBodyScrollOptions
Configuration options for target element and scrollbar gap reservation.
Property
Specification
reserveScrollBarGap?uniondefault:true
Compensate for the hidden scrollbar width by adding
padding-right to prevent layout shiftFull Signature
target?uniondefault:document.body
Target scrollable element to lock
Full Signature