
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-scroll-blocker
Advanced tools
A modern React 18 compatible scroll lock component for preventing body scroll
🔒 A modern, React 18 compatible library for preventing body scroll when components are mounted.
This library was created as a modern alternative to react-scrolllock, which is no longer maintained and doesn't support React 18. React Scroll Blocker provides the same functionality with modern React practices, TypeScript support, and React 18 compatibility.
npm install react-scroll-blocker
# or
yarn add react-scroll-blocker
# or
pnpm add react-scroll-blocker
import React, { useState } from 'react'
import ScrollBlocker from 'react-scroll-blocker'
function MyModal() {
const [isOpen, setIsOpen] = useState(false)
return (
<div>
<button onClick={() => setIsOpen(true)}>Open Modal</button>
{isOpen && (
<>
{/* This will prevent body scroll while the modal is open */}
<ScrollBlocker />
<div
className='modal-overlay'
onClick={() => setIsOpen(false)}>
<div
className='modal-content'
onClick={(e) => e.stopPropagation()}>
<h2>Modal Title</h2>
<p>Modal content here...</p>
<button onClick={() => setIsOpen(false)}>Close</button>
</div>
</div>
</>
)}
</div>
)
}
The main component that prevents body scroll when mounted.
import ScrollBlocker from 'react-scroll-blocker'
;<ScrollBlocker
isActive={true} // Optional: whether the blocker is active (default: true)
accountForScrollbars={true} // Optional: prevent layout shift (default: true)
>
{/* Optional: scrollable content for mobile */}
</ScrollBlocker>
| Prop | Type | Default | Description |
|---|---|---|---|
isActive | boolean | true | Whether the scroll blocker is active |
accountForScrollbars | boolean | true | Whether to add padding to compensate for removed scrollbar |
children | ReactNode | undefined | Child elements that should remain scrollable on touch devices |
A React hook that provides programmatic control over scroll blocking.
import { useScrollBlocker } from 'react-scroll-blocker'
function MyComponent() {
const { blockScroll, unblockScroll, isBlocked } = useScrollBlocker()
return (
<div>
<button onClick={blockScroll}>Block Scroll</button>
<button onClick={unblockScroll}>Unblock Scroll</button>
<p>Scroll is {isBlocked ? 'blocked' : 'unblocked'}</p>
</div>
)
}
| Parameter | Type | Default | Description |
|---|---|---|---|
isBlocked | boolean | false | Whether scroll should be blocked |
accountForScrollbars | boolean | true | Whether to account for scrollbar width |
| Property | Type | Description |
|---|---|---|
blockScroll | () => void | Function to manually block scroll |
unblockScroll | () => void | Function to manually unblock scroll |
isBlocked | boolean | Current block state |
A component that allows its children to remain scrollable even when ScrollBlocker is active. This is particularly useful for mobile devices.
import { ScrollBlocker, TouchScrollable } from 'react-scroll-blocker'
function ScrollableModal() {
return (
<>
<ScrollBlocker />
<TouchScrollable>
<div className='scrollable-content'>
{/* This content will be scrollable on mobile */}
</div>
</TouchScrollable>
</>
)
}
When you pass children to ScrollBlocker, they are automatically wrapped in TouchScrollable:
<ScrollBlocker>
<div className='scrollable-content'>
{/* Automatically scrollable on mobile */}
</div>
</ScrollBlocker>
import React, { useState } from 'react'
import ScrollBlocker from 'react-scroll-blocker'
function Modal() {
const [isOpen, setIsOpen] = useState(false)
return (
<div>
<button onClick={() => setIsOpen(true)}>Open Modal</button>
{isOpen && (
<>
<ScrollBlocker />
<div
style={
{
/* modal styles */
}
}>
<h2>Modal Content</h2>
<button onClick={() => setIsOpen(false)}>Close</button>
</div>
</>
)}
</div>
)
}
import ScrollBlocker from 'react-scroll-blocker'
function ConditionalLock({ shouldLock }) {
return (
<div>
<ScrollBlocker isActive={shouldLock} />
<p>Scroll is {shouldLock ? 'locked' : 'unlocked'}</p>
</div>
)
}
import { useScrollBlocker } from 'react-scroll-blocker'
function HookExample() {
const [isModalOpen, setIsModalOpen] = useState(false)
const { lockScroll, unlockScroll } = useScrollBlocker(isModalOpen)
return (
<div>
<button onClick={() => setIsModalOpen(true)}>Open Modal</button>
{/* Modal JSX */}
</div>
)
}
import ScrollBlocker from 'react-scroll-blocker'
function ScrollableModal() {
return (
<ScrollBlocker>
<div
style={{
position: 'fixed',
top: '10%',
bottom: '10%',
left: '10%',
right: '10%',
overflow: 'auto', // This content will be scrollable
}}>
<div style={{ height: '200vh' }}>Long scrollable content...</div>
</div>
</ScrollBlocker>
)
}
import ScrollBlocker from 'react-scroll-blocker'
function NestedModals() {
const [modal1Open, setModal1Open] = useState(false)
const [modal2Open, setModal2Open] = useState(false)
return (
<div>
{modal1Open && <ScrollBlocker />}
{modal2Open && <ScrollBlocker />}
{/* Both modals can be open simultaneously */}
{/* Scroll is locked while any modal is open */}
{/* Scroll is restored only when both are closed */}
</div>
)
}
React Scroll Blocker is designed as a drop-in replacement for react-scrolllock:
// Before (react-scrolllock)
import ScrollLock, { TouchScrollable } from 'react-scrolllock'
// After (react-scroll-blocker)
import ScrollBlocker, { TouchScrollable } from 'react-scroll-blocker'
// Note: Component name changed from ScrollLock to ScrollBlocker
We welcome contributions! Please see our Contributing Guide for details.
MIT © lomelo-x
This library is inspired by and serves as a modern replacement for react-scrolllock by Joss Mackison. Thanks for the original implementation and inspiration!
FAQs
A modern React 18 compatible scroll lock component for preventing body scroll
We found that react-scroll-blocker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.