summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-17 18:31:21 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-17 18:31:21 +0200
commit9a186b357b32325cf77fdce39a6c6c9aff76d8a5 (patch)
treebfeb790f3cc82f753ac74c9b0130f43189502803 /src/utils
parentc77c58e18143233be042c4980a6ed08ae9beac52 (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.tsx15
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;