You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.2 KiB

2 months ago
  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. // Gets the entire size of the scrollable document area, even extending outside
  9. // of the `<html>` and `<body>` rect bounds if horizontally scrollable
  10. export default function getDocumentRect(element: HTMLElement): Rect {
  11. const html = getDocumentElement(element);
  12. const winScroll = getWindowScroll(element);
  13. const body = element.ownerDocument?.body;
  14. const width = max(
  15. html.scrollWidth,
  16. html.clientWidth,
  17. body ? body.scrollWidth : 0,
  18. body ? body.clientWidth : 0
  19. );
  20. const height = max(
  21. html.scrollHeight,
  22. html.clientHeight,
  23. body ? body.scrollHeight : 0,
  24. body ? body.clientHeight : 0
  25. );
  26. let x = -winScroll.scrollLeft + getWindowScrollBarX(element);
  27. const y = -winScroll.scrollTop;
  28. if (getComputedStyle(body || html).direction === 'rtl') {
  29. x += max(html.clientWidth, body ? body.clientWidth : 0) - width;
  30. }
  31. return { width, height, x, y };
  32. }