1 | // @flow
|
---|
2 | import type { Rect } from '../types';
|
---|
3 | import getDocumentElement from './getDocumentElement';
|
---|
4 | import getComputedStyle from './getComputedStyle';
|
---|
5 | import getWindowScrollBarX from './getWindowScrollBarX';
|
---|
6 | import getWindowScroll from './getWindowScroll';
|
---|
7 | import { max } from '../utils/math';
|
---|
8 |
|
---|
9 | // Gets the entire size of the scrollable document area, even extending outside
|
---|
10 | // of the `<html>` and `<body>` rect bounds if horizontally scrollable
|
---|
11 | export default function getDocumentRect(element: HTMLElement): Rect {
|
---|
12 | const html = getDocumentElement(element);
|
---|
13 | const winScroll = getWindowScroll(element);
|
---|
14 | const body = element.ownerDocument?.body;
|
---|
15 |
|
---|
16 | const width = max(
|
---|
17 | html.scrollWidth,
|
---|
18 | html.clientWidth,
|
---|
19 | body ? body.scrollWidth : 0,
|
---|
20 | body ? body.clientWidth : 0
|
---|
21 | );
|
---|
22 | const height = max(
|
---|
23 | html.scrollHeight,
|
---|
24 | html.clientHeight,
|
---|
25 | body ? body.scrollHeight : 0,
|
---|
26 | body ? body.clientHeight : 0
|
---|
27 | );
|
---|
28 |
|
---|
29 | let x = -winScroll.scrollLeft + getWindowScrollBarX(element);
|
---|
30 | const y = -winScroll.scrollTop;
|
---|
31 |
|
---|
32 | if (getComputedStyle(body || html).direction === 'rtl') {
|
---|
33 | x += max(html.clientWidth, body ? body.clientWidth : 0) - width;
|
---|
34 | }
|
---|
35 |
|
---|
36 | return { width, height, x, y };
|
---|
37 | }
|
---|