useScrollLock Hook Fixes iOS Safari and Multi-Overlay Scroll Bugs in React
A common React pattern of setting document.body.style.overflow to 'hidden' fails in several real-world scenarios, including iOS Safari, where touch-based rubber-band scrolling persists despite the CSS rule. Conflicts also arise when multiple overlays such as drawers and lightboxes simultaneously control body scroll, often leaving pages permanently frozen if closed in the wrong order. The useScrollLock hook from the @reactuses/core library addresses these issues by restoring the exact inline overflow value it replaced rather than defaulting to an empty string. It also attaches a touchmove event guard specifically for iOS while still allowing scroll within the modal's own content. The hook exposes the lock as genuine React state, making it compatible with dependency arrays, aria attributes, and keyboard handlers without additional wiring.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in