From bd9928e6b393a5f70e078fe664dbe8cef3e98ac1 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 11 Jun 2019 10:47:04 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20useScrolling=20hoo?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/useScrolling.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/useScrolling.ts diff --git a/src/useScrolling.ts b/src/useScrolling.ts new file mode 100644 index 0000000000..a84ec99e05 --- /dev/null +++ b/src/useScrolling.ts @@ -0,0 +1,31 @@ +import { RefObject, useEffect, useState } from 'react'; + +const useScrolling = (ref: RefObject): boolean => { + const [scrolling, setScrolling] = useState(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;