Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
627 B · 20 lines
TypeScript
at main
123456789101112131415161718192021import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
/** * Returns current scroll left position in pixels. * * @returns {number | null} Scroll left position in pixels */export default function useScrollLeft(): number | null { const [scrollLeft, setScrollLeft] = useState(isBrowser ? window.scrollX : null);
const getScrollLeft = useCallback(() => setScrollLeft(window.scrollX), []);
useEventListener(isBrowser ? document : null, 'scroll', getScrollLeft);
return scrollLeft;}