react-use/src/useScrolling.ts
Renovate Bot a27f09fd36
chore: refactoring and rearrangement.
More DRY code. Also move non-hooks to separate directories.

BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint`
BREAKING CHANGE: `comps` directory renamed to `component`
2021-01-30 23:30:26 +03:00

35 lines
829 B
TypeScript

import { RefObject, useEffect, useState } from 'react';
import { off, on } from './misc/util';
const useScrolling = (ref: RefObject<HTMLElement>): boolean => {
const [scrolling, setScrolling] = useState<boolean>(false);
useEffect(() => {
if (ref.current) {
let scrollingTimeout;
const handleScrollEnd = () => {
setScrolling(false);
};
const handleScroll = () => {
setScrolling(true);
clearTimeout(scrollingTimeout);
scrollingTimeout = setTimeout(() => handleScrollEnd(), 150);
};
on(ref.current, 'scroll', handleScroll, false);
return () => {
if (ref.current) {
off(ref.current, 'scroll', handleScroll, false);
}
};
}
return () => {};
}, [ref]);
return scrolling;
};
export default useScrolling;