1 | import getBoundingClientRect from "./getBoundingClientRect.js";
|
---|
2 | import getNodeScroll from "./getNodeScroll.js";
|
---|
3 | import getNodeName from "./getNodeName.js";
|
---|
4 | import { isHTMLElement } from "./instanceOf.js";
|
---|
5 | import getWindowScrollBarX from "./getWindowScrollBarX.js";
|
---|
6 | import getDocumentElement from "./getDocumentElement.js";
|
---|
7 | import isScrollParent from "./isScrollParent.js";
|
---|
8 | import { round } from "../utils/math.js";
|
---|
9 |
|
---|
10 | function isElementScaled(element) {
|
---|
11 | var rect = element.getBoundingClientRect();
|
---|
12 | var scaleX = round(rect.width) / element.offsetWidth || 1;
|
---|
13 | var scaleY = round(rect.height) / element.offsetHeight || 1;
|
---|
14 | return scaleX !== 1 || scaleY !== 1;
|
---|
15 | } // Returns the composite rect of an element relative to its offsetParent.
|
---|
16 | // Composite means it takes into account transforms as well as layout.
|
---|
17 |
|
---|
18 |
|
---|
19 | export default function getCompositeRect(elementOrVirtualElement, offsetParent, isFixed) {
|
---|
20 | if (isFixed === void 0) {
|
---|
21 | isFixed = false;
|
---|
22 | }
|
---|
23 |
|
---|
24 | var isOffsetParentAnElement = isHTMLElement(offsetParent);
|
---|
25 | var offsetParentIsScaled = isHTMLElement(offsetParent) && isElementScaled(offsetParent);
|
---|
26 | var documentElement = getDocumentElement(offsetParent);
|
---|
27 | var rect = getBoundingClientRect(elementOrVirtualElement, offsetParentIsScaled, isFixed);
|
---|
28 | var scroll = {
|
---|
29 | scrollLeft: 0,
|
---|
30 | scrollTop: 0
|
---|
31 | };
|
---|
32 | var offsets = {
|
---|
33 | x: 0,
|
---|
34 | y: 0
|
---|
35 | };
|
---|
36 |
|
---|
37 | if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
|
---|
38 | if (getNodeName(offsetParent) !== 'body' || // https://github.com/popperjs/popper-core/issues/1078
|
---|
39 | isScrollParent(documentElement)) {
|
---|
40 | scroll = getNodeScroll(offsetParent);
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (isHTMLElement(offsetParent)) {
|
---|
44 | offsets = getBoundingClientRect(offsetParent, true);
|
---|
45 | offsets.x += offsetParent.clientLeft;
|
---|
46 | offsets.y += offsetParent.clientTop;
|
---|
47 | } else if (documentElement) {
|
---|
48 | offsets.x = getWindowScrollBarX(documentElement);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | return {
|
---|
53 | x: rect.left + scroll.scrollLeft - offsets.x,
|
---|
54 | y: rect.top + scroll.scrollTop - offsets.y,
|
---|
55 | width: rect.width,
|
---|
56 | height: rect.height
|
---|
57 | };
|
---|
58 | } |
---|