source: imaps-frontend/node_modules/@popperjs/core/lib/dom-utils/getCompositeRect.js.flow

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 2 weeks ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.1 KB
Line 
1// @flow
2import type { Rect, VirtualElement, Window } from '../types';
3import getBoundingClientRect from './getBoundingClientRect';
4import getNodeScroll from './getNodeScroll';
5import getNodeName from './getNodeName';
6import { isHTMLElement } from './instanceOf';
7import getWindowScrollBarX from './getWindowScrollBarX';
8import getDocumentElement from './getDocumentElement';
9import isScrollParent from './isScrollParent';
10import { round } from '../utils/math';
11
12function isElementScaled(element: HTMLElement) {
13 const rect = element.getBoundingClientRect();
14 const scaleX = round(rect.width) / element.offsetWidth || 1;
15 const scaleY = round(rect.height) / element.offsetHeight || 1;
16
17 return scaleX !== 1 || scaleY !== 1;
18}
19
20// Returns the composite rect of an element relative to its offsetParent.
21// Composite means it takes into account transforms as well as layout.
22export default function getCompositeRect(
23 elementOrVirtualElement: Element | VirtualElement,
24 offsetParent: Element | Window,
25 isFixed: boolean = false
26): Rect {
27 const isOffsetParentAnElement = isHTMLElement(offsetParent);
28 const offsetParentIsScaled =
29 isHTMLElement(offsetParent) && isElementScaled(offsetParent);
30 const documentElement = getDocumentElement(offsetParent);
31 const rect = getBoundingClientRect(
32 elementOrVirtualElement,
33 offsetParentIsScaled,
34 isFixed
35 );
36
37 let scroll = { scrollLeft: 0, scrollTop: 0 };
38 let offsets = { x: 0, y: 0 };
39
40 if (isOffsetParentAnElement || (!isOffsetParentAnElement && !isFixed)) {
41 if (
42 getNodeName(offsetParent) !== 'body' ||
43 // https://github.com/popperjs/popper-core/issues/1078
44 isScrollParent(documentElement)
45 ) {
46 scroll = getNodeScroll(offsetParent);
47 }
48
49 if (isHTMLElement(offsetParent)) {
50 offsets = getBoundingClientRect(offsetParent, true);
51 offsets.x += offsetParent.clientLeft;
52 offsets.y += offsetParent.clientTop;
53 } else if (documentElement) {
54 offsets.x = getWindowScrollBarX(documentElement);
55 }
56 }
57
58 return {
59 x: rect.left + scroll.scrollLeft - offsets.x,
60 y: rect.top + scroll.scrollTop - offsets.y,
61 width: rect.width,
62 height: rect.height,
63 };
64}
Note: See TracBrowser for help on using the repository browser.