Skip to content

Commit

Permalink
feat: 🎸 add useScrolling hook
Browse files Browse the repository at this point in the history
  • Loading branch information
James committed Jun 11, 2019
1 parent c42553b commit bd9928e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/useScrolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RefObject, useEffect, useState } from 'react';

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);
};

ref.current.addEventListener('scroll', handleScroll, false);
return () => ref.current.removeEventListener('scroll', handleScroll, false);
}
},
[ref.current],
);

return scrolling;
};

export default useScrolling;

0 comments on commit bd9928e

Please sign in to comment.