saleor-dashboard/src/hooks/useWindowScroll.ts

25 lines
544 B
TypeScript
Raw Normal View History

import throttle from "lodash/throttle";
2019-08-09 10:17:04 +00:00
import { useEffect, useState } from "react";
function getPosition() {
return {
x: window.pageXOffset,
y: window.pageYOffset
};
}
function useWindowScroll() {
const [scroll, setScroll] = useState(getPosition);
useEffect(() => {
const handleScroll = throttle(() => setScroll(getPosition()), 100);
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return scroll;
}
export default useWindowScroll;