UNPKG

@zag-js/popper

Version:

Dynamic positioning logic for ui machines

41 lines (40 loc) 1.04 kB
// src/get-anchor.ts import { isHTMLElement } from "@zag-js/dom-query"; function createDOMRect(x = 0, y = 0, width = 0, height = 0) { if (typeof DOMRect === "function") { return new DOMRect(x, y, width, height); } const rect = { x, y, width, height, top: y, right: x + width, bottom: y + height, left: x }; return { ...rect, toJSON: () => rect }; } function getDOMRect(anchorRect) { if (!anchorRect) return createDOMRect(); const { x, y, width, height } = anchorRect; return createDOMRect(x, y, width, height); } function getAnchorElement(anchorElement, getAnchorRect) { return { contextElement: isHTMLElement(anchorElement) ? anchorElement : anchorElement?.contextElement, getBoundingClientRect: () => { const anchor = anchorElement; const anchorRect = getAnchorRect?.(anchor); if (anchorRect || !anchor) { return getDOMRect(anchorRect); } return anchor.getBoundingClientRect(); } }; } export { createDOMRect, getAnchorElement };