saleor-dashboard/src/hooks/useWindowScroll.ts

25 lines
547 B
TypeScript
Raw Normal View History

2019-08-09 10:17:04 +00:00
import throttle from "lodash-es/throttle";
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;