-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathgetBoundaries.ts
More file actions
86 lines (79 loc) · 2.98 KB
/
getBoundaries.ts
File metadata and controls
86 lines (79 loc) · 2.98 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Computed the boundaries limits and return them
*/
import { Offsets } from '../models';
import { findCommonOffsetParent } from './findCommonOffsetParent';
import { getFixedPositionOffsetParent } from './getFixedPositionOffsetParent';
import { getOffsetRectRelativeToArbitraryNode } from './getOffsetRectRelativeToArbitraryNode';
import { getParentNode } from './getParentNode';
import { getScrollParent } from './getScrollParent';
import { getViewportOffsetRectRelativeToArbitraryNode } from './getViewportOffsetRectRelativeToArbitraryNode';
import { getWindowSizes } from './getWindowSizes';
import { isFixed } from './isFixed';
import { isNumber } from './isNumeric';
export function getBoundaries(
target: HTMLElement,
host: HTMLElement,
padding = 0,
boundariesElement: string,
fixedPosition = false
): Partial<Offsets> {
// NOTE: 1 DOM access here
let boundaries: Partial<Offsets> = { top: 0, left: 0 };
const offsetParent = fixedPosition ? getFixedPositionOffsetParent(target) : findCommonOffsetParent(target, host);
// Handle viewport case
if (boundariesElement === 'viewport') {
boundaries = getViewportOffsetRectRelativeToArbitraryNode(offsetParent, fixedPosition);
} else {
// Handle other cases based on DOM element used as boundaries
let boundariesNode;
if (boundariesElement === 'scrollParent') {
boundariesNode = getScrollParent(getParentNode(host));
if (boundariesNode.nodeName === 'BODY') {
boundariesNode = target.ownerDocument.documentElement;
}
} else if (boundariesElement === 'window') {
boundariesNode = target.ownerDocument.documentElement;
} else {
boundariesNode = boundariesElement;
}
const offsets = getOffsetRectRelativeToArbitraryNode(
boundariesNode,
offsetParent,
fixedPosition
);
// In case of HTML, we need a different computation
if (offsets && boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
const { height, width } = getWindowSizes(target.ownerDocument);
if (isNumber(boundaries.top) && isNumber(offsets.top) && isNumber(offsets.marginTop)) {
boundaries.top += offsets.top - offsets.marginTop;
}
if (isNumber(boundaries.top)) {
boundaries.bottom = Number(height) + Number(offsets.top);
}
if (isNumber(boundaries.left) && isNumber(offsets.left) && isNumber(offsets.marginLeft)) {
boundaries.left += offsets.left - offsets.marginLeft;
}
if (isNumber(boundaries.top)) {
boundaries.right = Number(width) + Number(offsets.left);
}
} else if (offsets) {
// for all the other DOM elements, this one is good
boundaries = offsets;
}
}
// Add paddings
if (isNumber(boundaries.left)) {
boundaries.left += padding;
}
if (isNumber(boundaries.top)) {
boundaries.top += padding;
}
if (isNumber(boundaries.right)) {
boundaries.right -= padding;
}
if (isNumber(boundaries.bottom)) {
boundaries.bottom -= padding;
}
return boundaries;
}