UNPKG

@wordpress/components

Version:
100 lines (99 loc) • 4.02 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); // packages/components/src/utils/element-rect.ts /** * An `ElementOffsetRect` object with all values set to zero. */ const NULL_ELEMENT_OFFSET_RECT = { element: void 0, top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0 }; /** * Returns the position and dimensions of an element, relative to its offset * parent, with subpixel precision. Values reflect the real measures before any * potential scaling distortions along the X and Y axes. * * Useful in contexts where plain `getBoundingClientRect` calls or `ResizeObserver` * entries are not suitable, such as when the element is transformed, and when * `element.offset<Top|Left|Width|Height>` methods are not precise enough. * * **Note:** in some contexts, like when the scale is 0, this method will fail * because it's impossible to calculate a scaling ratio. When that happens, it * will return `undefined`. */ function getElementOffsetRect(element) { const rect = element.getBoundingClientRect(); if (rect.width === 0 || rect.height === 0) return; const offsetParent = element.offsetParent; const offsetParentRect = offsetParent?.getBoundingClientRect() ?? NULL_ELEMENT_OFFSET_RECT; const offsetParentScrollX = offsetParent?.scrollLeft ?? 0; const offsetParentScrollY = offsetParent?.scrollTop ?? 0; const computedWidth = parseFloat(getComputedStyle(element).width); const computedHeight = parseFloat(getComputedStyle(element).height); const scaleX = computedWidth / rect.width; const scaleY = computedHeight / rect.height; return { element, top: (rect.top - offsetParentRect?.top) * scaleY + offsetParentScrollY, right: (offsetParentRect?.right - rect.right) * scaleX - offsetParentScrollX, bottom: (offsetParentRect?.bottom - rect.bottom) * scaleY - offsetParentScrollY, left: (rect.left - offsetParentRect?.left) * scaleX + offsetParentScrollX, width: computedWidth, height: computedHeight }; } const POLL_RATE = 100; /** * Tracks the position and dimensions of an element, relative to its offset * parent. The element can be changed dynamically. * * When no element is provided (`null` or `undefined`), the hook will return * a "null" rect, in which all values are `0` and `element` is `undefined`. * * **Note:** sometimes, the measurement will fail (see `getElementOffsetRect`'s * documentation for more details). When that happens, this hook will attempt * to measure again after a frame, and if that fails, it will poll every 100 * milliseconds until it succeeds. */ function useTrackElementOffsetRect(targetElement, deps = []) { const [indicatorPosition, setIndicatorPosition] = (0, _wordpress_element.useState)(NULL_ELEMENT_OFFSET_RECT); const intervalRef = (0, _wordpress_element.useRef)(void 0); const measure = (0, _wordpress_compose.useEvent)(() => { if (targetElement && targetElement.isConnected) { const elementOffsetRect = getElementOffsetRect(targetElement); if (elementOffsetRect) { setIndicatorPosition(elementOffsetRect); clearInterval(intervalRef.current); return true; } } else clearInterval(intervalRef.current); return false; }); const setElement = (0, _wordpress_compose.useResizeObserver)(() => { if (!measure()) requestAnimationFrame(() => { if (!measure()) intervalRef.current = setInterval(measure, POLL_RATE); }); }); (0, _wordpress_element.useLayoutEffect)(() => { setElement(targetElement); if (!targetElement) setIndicatorPosition(NULL_ELEMENT_OFFSET_RECT); }, [setElement, targetElement]); (0, _wordpress_element.useLayoutEffect)(() => { measure(); }, deps); return indicatorPosition; } exports.NULL_ELEMENT_OFFSET_RECT = NULL_ELEMENT_OFFSET_RECT; exports.getElementOffsetRect = getElementOffsetRect; exports.useTrackElementOffsetRect = useTrackElementOffsetRect; //# sourceMappingURL=element-rect.cjs.map