summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-scroll-position.tsx
blob: 47cfdd0cbe968e8d02749822477465d662ea4ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;