
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-use-scroll-direction
Advanced tools
A react hook for detecting the scroll direction of the scrolling window or target element
A react hook for detecting the scroll direction of the scrolling window or target element
import { useScrollDirection } from 'react-use-scroll-direction'
function SomeComponent () {
const {
isScrolling,
isScrollingX,
isScrollingY,
isScrollingUp,
isScrollingDown,
isScrollingLeft,
isScrollingRight,
scrollDirection,
} = useScrollDirection()
console.log(scrollDirection) // -> UP | DOWN | LEFT | RIGHT | null
return (
<div>
{isScrolling ? 'Scrolling' : 'Not scrolling'}
{isScrollingX && 'Scrolling horizontally'}
{isScrollingY && 'Scrolling vertically'}
{isScrollingUp && "Up"}
{isScrollingDown && "Down"}
{isScrollingLeft && "Left"}
{isScrollingRight && "Right"}
</div>
)
}
It's possible for the scroll direction to be null
and this occurs when the view port or target element stops being scrolled.
Hence why the hook returns an object with properties for determining each direction. Just because we are not scrolling down,
doesn't mean that we are scrolling up.
npm install react-use-scroll-direction
import { useScrollDirection } from 'react-use-scroll-direction'
function SomeComponent () {
const { isScrollingUp } = useScrollDirection()
return (
<div>
{isScrollingUp && "Up"}
</div>
)
}
import { useScrollDirection } from 'react-use-scroll-direction'
function SomeComponent () {
const { isScrollingUp, scrollTargetRef } = useScrollDirection()
return (
<div>
{isScrollingUp && "Up"}
<ScrollableElement ref={scrollTarget}>
<SomeLongContent />
</ScrollableElement>
</div>
)
}
Or if you already have a reference to a target element, you can supply it as an arg as follows:
useScrollDirection(targetElement)
FAQs
A react hook for detecting the scroll direction of the scrolling window or target element
The npm package react-use-scroll-direction receives a total of 7,173 weekly downloads. As such, react-use-scroll-direction popularity was classified as popular.
We found that react-use-scroll-direction demonstrated a not healthy version release cadence and project activity because the last version was released 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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.