@vuesax-alpha/nightly
Version:
A Component Library for Vue 3
54 lines (51 loc) • 2.19 kB
JavaScript
import { createCoords, rectToClientRect } from '../../utils/index.mjs';
import { getWindow, getComputedStyle } from '../../utils/dom.mjs';
import { getScale } from '../platform/get-scale.mjs';
import { isElement } from '../platform/is-element.mjs';
import { shouldAddVisualOffsets, getVisualOffsets } from './get-visual-offsets.mjs';
import { unwrapElement } from './unwrap-element.mjs';
function getBoundingClientRect(element, includeScale = false, isFixedStrategy = false, offsetParent) {
const clientRect = element.getBoundingClientRect();
const domElement = unwrapElement(element);
let scale = createCoords(1);
if (includeScale) {
if (offsetParent) {
if (isElement(offsetParent)) {
scale = getScale(offsetParent);
}
} else {
scale = getScale(element);
}
}
const visualOffsets = shouldAddVisualOffsets(
domElement,
isFixedStrategy,
offsetParent
) ? getVisualOffsets(domElement) : createCoords(0);
let x = (clientRect.left + visualOffsets.x) / scale.x;
let y = (clientRect.top + visualOffsets.y) / scale.y;
let width = clientRect.width / scale.x;
let height = clientRect.height / scale.y;
if (domElement) {
const win = getWindow(domElement);
const offsetWin = offsetParent && isElement(offsetParent) ? getWindow(offsetParent) : offsetParent;
let currentIFrame = win.frameElement;
while (currentIFrame && offsetParent && offsetWin !== win) {
const iframeScale = getScale(currentIFrame);
const iframeRect = currentIFrame.getBoundingClientRect();
const css = getComputedStyle(currentIFrame);
const left = iframeRect.left + (currentIFrame.clientLeft + Number.parseFloat(css.paddingLeft)) * iframeScale.x;
const top = iframeRect.top + (currentIFrame.clientTop + Number.parseFloat(css.paddingTop)) * iframeScale.y;
x *= iframeScale.x;
y *= iframeScale.y;
width *= iframeScale.x;
height *= iframeScale.y;
x += left;
y += top;
currentIFrame = getWindow(currentIFrame).frameElement;
}
}
return rectToClientRect({ width, height, x, y });
}
export { getBoundingClientRect };
//# sourceMappingURL=get-bounding-client-rect.mjs.map