"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useElementOffset = useElementOffset; var _react = require("react"); function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } var EPS = 1; /** * Stores the dimensions and position of a DOM element as returned by `getBoundingClientRect()`. * * Values are viewport-relative and may be fractional (subpixel precision). * * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect} */ /** * Callback ref setter returned by {@link useElementOffset}. * * Pass this to a DOM element's `ref` prop to start observing its layout. * * @param node - the DOM element to observe, or `null` when the element unmounts */ /** * Checks whether two ElementOffset values differ by more than `EPS` (1px) in any dimension. * * @param a - the first ElementOffset to compare * @param b - the second ElementOffset to compare * @returns true if any dimension differs by more than 1px */ function hasSignificantChange(a, b) { return Math.abs(a.height - b.height) > EPS || Math.abs(a.left - b.left) > EPS || Math.abs(a.top - b.top) > EPS || Math.abs(a.width - b.width) > EPS; } /** * Reads the current bounding box of a DOM element using `getBoundingClientRect()`. * * @param node - the DOM element to measure * @returns an ElementOffset with the element's current dimensions and viewport-relative position */ function readElementOffset(node) { var rect = node.getBoundingClientRect(); return { height: rect.height, left: rect.left, top: rect.top, width: rect.width }; } /** * Use this to listen to element layout changes. * * Very useful for reading actual sizes of DOM elements relative to the viewport. * * Uses ResizeObserver to automatically detect size changes of the observed element. * * @param extraDependencies use this to trigger new DOM dimensions read when any of these change. Good for things like payload and label, that will re-render something down in the children array, but you want to read the layout box of a parent. * @returns [lastElementOffset, updateElementOffset] most recent value, and setter. Pass the setter to a DOM element ref like this: `