/* Copyright © 2020 Splunk Inc.
SPLUNK CONFIDENTIAL – Use or disclosure of this material in whole or
in part without a valid written license from Splunk Inc. is PROHIBITED. */
import {
  react_exports
} from "./chunk-V3S5WUKN.js";

// src/useDimensions.ts
var useDimensions = () => {
  const elementRef = (0, react_exports.useRef)(null);
  const [dimensions, setDimensions] = (0, react_exports.useState)({
    width: 0,
    height: 0
  });
  const dimensionsRef = (0, react_exports.useRef)(dimensions);
  const getElementRefDimensions = () => {
    if (!elementRef.current) {
      return { width: 0, height: 0 };
    }
    const { width, height } = elementRef.current.getBoundingClientRect();
    return { width, height };
  };
  const updateDimensionsFromElementRef = (0, react_exports.useCallback)(() => {
    const nextDimensions = getElementRefDimensions();
    if (nextDimensions.width !== dimensionsRef.current.width || nextDimensions.height !== dimensionsRef.current.height) {
      setDimensions(getElementRefDimensions);
    }
  }, [setDimensions]);
  (0, react_exports.useEffect)(() => {
    window.addEventListener("resize", updateDimensionsFromElementRef);
    return () => {
      window.removeEventListener("resize", updateDimensionsFromElementRef);
    };
  }, [updateDimensionsFromElementRef]);
  (0, react_exports.useLayoutEffect)(() => {
    dimensionsRef.current = dimensions;
    updateDimensionsFromElementRef();
  });
  return { ref: elementRef, dimensions };
};

export {
  useDimensions
};
//# sourceMappingURL=chunk-DYC7NOL7.js.map
