From 9a186b357b32325cf77fdce39a6c6c9aff76d8a5 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 17 May 2022 18:31:21 +0200 Subject: chore: listen scroll to hide/show back to top button --- src/utils/hooks/use-scroll-position.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/utils/hooks/use-scroll-position.tsx (limited to 'src/utils/hooks/use-scroll-position.tsx') diff --git a/src/utils/hooks/use-scroll-position.tsx b/src/utils/hooks/use-scroll-position.tsx new file mode 100644 index 0000000..47cfdd0 --- /dev/null +++ b/src/utils/hooks/use-scroll-position.tsx @@ -0,0 +1,15 @@ +import { useEffect } from 'react'; + +/** + * Execute the given function based on scroll position. + * + * @param scrollHandler - A callback function. + */ +const useScrollPosition = (scrollHandler: () => void) => { + useEffect(() => { + window.addEventListener('scroll', scrollHandler); + return () => window.removeEventListener('scroll', scrollHandler); + }, [scrollHandler]); +}; + +export default useScrollPosition; -- cgit v1.2.3