Package Exports
- use-scroll-direction
- use-scroll-direction/dist/index.js
- use-scroll-direction/dist/use-scroll-direction.esm.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-scroll-direction) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
use-scroll-direction
A simple, performant, and cross-browser hook for handling scroll in your next react app.
Use it to detect scroll direction in X or Y axis. On the browser window as well as on the custom component with usage of ref.
If you need more quick or lazy response, play with wait option.
Installation
npm i use-scroll-directionUsage
The hook returns the object with the actual scroll direction which could be one of three states: 'NONE', 'DOWN', 'UP', 'LEFT', 'RIGHT' and useful booleans.
Use on window object
import {useScrollDirection} from "use-scroll-direction";
export const WindowExample = () => {
const {
scrollDirection,
isScrolling,
isScrollingUp,
isScrollingDown,
isScrollingLeft,
isScrollingRight
} = useScrollDirection();
useEffect(() => {
console.log(scrollDirection)
}, [scrollDirection]);
return (
<div>{...}</div>
)
};
Use on the specific element
import {useScrollDirection} from "use-scroll-direction";
export const ComponentRefExample = () => {
const elementRef = useRef(null);
const {scrollDirection} = useScrollDirection({ref: elementRef});
useEffect(() => {
console.log(scrollDirection)
}, [scrollDirection]);
return (
//The content needs to overflow a container
<div ref={elementRef} style={{height: '100vh', overflowY: 'scroll'}} >
<div>{...}</div>
</div>
)
};Available options
| Name | Type | Description |
|---|---|---|
wait |
?number |
Time in ms for throttling of scroll handler (default: 50) |
timeToReset |
?number |
Time in ms after last scroll event to reset the state (default: 150) |
ref |
?string |
When passed, the listener will be attached to element instead of window object |
