diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-17 18:31:21 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-17 18:31:21 +0200 |
| commit | 9a186b357b32325cf77fdce39a6c6c9aff76d8a5 (patch) | |
| tree | bfeb790f3cc82f753ac74c9b0130f43189502803 /src/utils | |
| parent | c77c58e18143233be042c4980a6ed08ae9beac52 (diff) | |
chore: listen scroll to hide/show back to top button
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/hooks/use-scroll-position.tsx | 15 |
1 files changed, 15 insertions, 0 deletions
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; |
