Blame view

node_modules/@popperjs/core/lib/dom-utils/getBoundingClientRect.js.flow 1.45 KB
4cd4fd28   郭伟龙   feat: 初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// @flow
import type { ClientRectObject, VirtualElement } from '../types';
import { isElement, isHTMLElement } from './instanceOf';
import { round } from '../utils/math';
import getWindow from './getWindow';
import isLayoutViewport from './isLayoutViewport';

export default function getBoundingClientRect(
  element: Element | VirtualElement,
  includeScale: boolean = false,
  isFixedStrategy: boolean = false
): ClientRectObject {
  const clientRect = element.getBoundingClientRect();
  let scaleX = 1;
  let scaleY = 1;

  if (includeScale && isHTMLElement(element)) {
    scaleX =
      (element: HTMLElement).offsetWidth > 0
        ? round(clientRect.width) / (element: HTMLElement).offsetWidth || 1
        : 1;
    scaleY =
      (element: HTMLElement).offsetHeight > 0
        ? round(clientRect.height) / (element: HTMLElement).offsetHeight || 1
        : 1;
  }

  const { visualViewport } = isElement(element) ? getWindow(element) : window;
  const addVisualOffsets = !isLayoutViewport() && isFixedStrategy;

  const x =
    (clientRect.left +
      (addVisualOffsets && visualViewport ? visualViewport.offsetLeft : 0)) /
    scaleX;
  const y =
    (clientRect.top +
      (addVisualOffsets && visualViewport ? visualViewport.offsetTop : 0)) /
    scaleY;
  const width = clientRect.width / scaleX;
  const height = clientRect.height / scaleY;

  return {
    width,
    height,
    top: y,
    right: x + width,
    bottom: y + height,
    left: x,
    x,
    y,
  };
}