Welcome to mirror list, hosted at ThFree Co, Russian Federation.

getBoundingClientRect.js.flow « dom-utils « lib « package « popperjs - github.com/gohugoio/hugo-mod-jslibs-dist.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1cae0f270db18713289534f249d23a5978ea67b6 (plain)
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
// @flow
import type { ClientRectObject, VirtualElement } from '../types';
import { isHTMLElement } from './instanceOf';
import { round } from '../utils/math';

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

  if (isHTMLElement(element) && includeScale) {
    const offsetHeight = element.offsetHeight;
    const offsetWidth = element.offsetWidth;

    // Do not attempt to divide by 0, otherwise we get `Infinity` as scale
    // Fallback to 1 in case both values are `0`
    if (offsetWidth > 0) {
      scaleX = round(rect.width) / offsetWidth || 1;
    }
    if (offsetHeight > 0) {
      scaleY = round(rect.height) / offsetHeight || 1;
    }
  }

  return {
    width: rect.width / scaleX,
    height: rect.height / scaleY,
    top: rect.top / scaleY,
    right: rect.right / scaleX,
    bottom: rect.bottom / scaleY,
    left: rect.left / scaleX,
    x: rect.left / scaleX,
    y: rect.top / scaleY,
  };
}