UNPKG

@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.

69 lines (63 loc) 2.63 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { useState, useEffect } from "react"; import boundingClientRect from "../../utils/boundingClientRect"; var useDimensions = function useDimensions(_ref) { var containerRef = _ref.containerRef, popover = _ref.popover, content = _ref.content, fixed = _ref.fixed, scrollableParent = _ref.scrollableParent, children = _ref.children; var _useState = useState({ containerTop: 0, containerPureTop: 0, containerLeft: 0, containerHeight: 0, containerWidth: 0, popoverHeight: 0, popoverWidth: 0, windowScrollTop: 0, windowWidth: 0, windowHeight: 0, contentHeight: 0, documentHeight: 0 }), _useState2 = _slicedToArray(_useState, 2), positions = _useState2[0], setPositions = _useState2[1]; useEffect(function () { var calculate = function calculate() { var containerDimensions = boundingClientRect(containerRef); var popoverDimensions = boundingClientRect(popover); var contentDimensions = boundingClientRect(content); if (containerDimensions && popoverDimensions && contentDimensions && typeof window !== "undefined") { var documentDimensions = window.document.body.getBoundingClientRect(); setPositions({ containerTop: containerDimensions.top, containerLeft: containerDimensions.left, containerHeight: containerDimensions.height, containerWidth: containerDimensions.width, containerPureTop: containerDimensions.pureTop, popoverHeight: popoverDimensions.height, popoverWidth: popoverDimensions.width, windowScrollTop: window.scrollY || window.pageYOffset, windowWidth: window.innerWidth, windowHeight: window.innerHeight, contentHeight: contentDimensions.height, documentHeight: documentDimensions.height }); } }; calculate(); window.addEventListener("resize", calculate); if (fixed) window.addEventListener("scroll", calculate); if (scrollableParent !== document.body && scrollableParent) scrollableParent.addEventListener("scroll", calculate); return function () { window.removeEventListener("resize", calculate); if (fixed) window.removeEventListener("scroll", calculate); if (scrollableParent !== document.body && scrollableParent) scrollableParent.removeEventListener("scroll", calculate); }; }, [containerRef, content, popover, fixed, scrollableParent, children]); return positions; }; export default useDimensions;