Toggles the scroll lock on the document body to prevent or allow scrolling.
When locking scroll, it sets overflow: hidden on the <body> and adjusts
padding-right to compensate for the scrollbar width, preventing layout shift.
When unlocking, it restores the original styles.
This is useful for modal dialogs, sidebars, or any UI that requires disabling background scroll
while keeping layout stable.
Parameters
lock: boolean
If true, locks scrolling and adjusts padding. If false, restores scrolling and styles.
Returns void
void
Example: Imports
// ES Module import { toggleScrollLock } from'@bnidev/js-utils' // CommonJS
Toggles the scroll lock on the document body to prevent or allow scrolling.
When locking scroll, it sets
overflow: hidden
on the<body>
and adjustspadding-right
to compensate for the scrollbar width, preventing layout shift. When unlocking, it restores the original styles.This is useful for modal dialogs, sidebars, or any UI that requires disabling background scroll while keeping layout stable.