-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James
committed
Jun 11, 2019
1 parent
c42553b
commit bd9928e
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |