@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
38 lines (33 loc) • 961 B
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import * as React from "react";
const useBoundingRect = initialValue => {
const [state, setState] = React.useState(() => _objectSpread({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0
}, initialValue));
const ref = React.useRef(null);
React.useEffect(() => {
const calculate = () => {
if (ref && ref.current) {
const dimensions = ref.current.getBoundingClientRect();
/* $FlowFixMe
Somehow the Flow is confused and returns type ClientRect instead of DOMRect and therefore it expects that it's a function.
*/
setState(dimensions);
}
};
calculate();
window.addEventListener("resize", calculate);
return () => {
window.removeEventListener("resize", calculate);
};
}, []); // $FlowFixMe
return [state, ref];
};
export default useBoundingRect;