-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
getClippingRect.ts
205 lines (182 loc) · 6.06 KB
/
getClippingRect.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type {
Boundary,
ClientRectObject,
Rect,
RootBoundary,
Strategy,
} from '@floating-ui/core';
import {rectToClientRect} from '@floating-ui/core';
import {createCoords, max, min} from '@floating-ui/utils';
import {
getComputedStyle,
getDocumentElement,
getNodeName,
getOverflowAncestors,
getParentNode,
isContainingBlock,
isHTMLElement,
isLastTraversableNode,
isOverflowElement,
} from '@floating-ui/utils/dom';
import type {Platform, ReferenceElement} from '../types';
import {getBoundingClientRect} from '../utils/getBoundingClientRect';
import {getDocumentRect} from '../utils/getDocumentRect';
import {getViewportRect} from '../utils/getViewportRect';
import {getVisualOffsets} from '../utils/getVisualOffsets';
import {getScale} from './getScale';
import {isElement} from './isElement';
import {isTopLayer} from '../utils/isTopLayer';
type PlatformWithCache = Platform & {
_c: Map<ReferenceElement, Element[]>;
};
// Returns the inner client rect, subtracting scrollbars if present.
function getInnerBoundingClientRect(
element: Element,
strategy: Strategy,
): Rect {
const clientRect = getBoundingClientRect(element, true, strategy === 'fixed');
const top = clientRect.top + element.clientTop;
const left = clientRect.left + element.clientLeft;
const scale = isHTMLElement(element) ? getScale(element) : createCoords(1);
const width = element.clientWidth * scale.x;
const height = element.clientHeight * scale.y;
const x = left * scale.x;
const y = top * scale.y;
return {
width,
height,
x,
y,
};
}
function getClientRectFromClippingAncestor(
element: Element,
clippingAncestor: Element | RootBoundary,
strategy: Strategy,
): ClientRectObject {
let rect: Rect;
if (clippingAncestor === 'viewport') {
rect = getViewportRect(element, strategy);
} else if (clippingAncestor === 'document') {
rect = getDocumentRect(getDocumentElement(element));
} else if (isElement(clippingAncestor)) {
rect = getInnerBoundingClientRect(clippingAncestor, strategy);
} else {
const visualOffsets = getVisualOffsets(element);
rect = {
...clippingAncestor,
x: clippingAncestor.x - visualOffsets.x,
y: clippingAncestor.y - visualOffsets.y,
};
}
return rectToClientRect(rect);
}
function hasFixedPositionAncestor(element: Element, stopNode: Node): boolean {
const parentNode = getParentNode(element);
if (
parentNode === stopNode ||
!isElement(parentNode) ||
isLastTraversableNode(parentNode)
) {
return false;
}
return (
getComputedStyle(parentNode).position === 'fixed' ||
hasFixedPositionAncestor(parentNode, stopNode)
);
}
// A "clipping ancestor" is an `overflow` element with the characteristic of
// clipping (or hiding) child elements. This returns all clipping ancestors
// of the given element up the tree.
function getClippingElementAncestors(
element: Element,
cache: PlatformWithCache['_c'],
): Array<Element> {
const cachedResult = cache.get(element);
if (cachedResult) {
return cachedResult;
}
let result = getOverflowAncestors(element, [], false).filter(
(el) => isElement(el) && getNodeName(el) !== 'body',
) as Array<Element>;
let currentContainingBlockComputedStyle: CSSStyleDeclaration | null = null;
const elementIsFixed = getComputedStyle(element).position === 'fixed';
let currentNode: Node | null = elementIsFixed
? getParentNode(element)
: element;
// https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
while (isElement(currentNode) && !isLastTraversableNode(currentNode)) {
const computedStyle = getComputedStyle(currentNode);
const currentNodeIsContaining = isContainingBlock(currentNode);
if (!currentNodeIsContaining && computedStyle.position === 'fixed') {
currentContainingBlockComputedStyle = null;
}
const shouldDropCurrentNode = elementIsFixed
? !currentNodeIsContaining && !currentContainingBlockComputedStyle
: (!currentNodeIsContaining &&
computedStyle.position === 'static' &&
!!currentContainingBlockComputedStyle &&
['absolute', 'fixed'].includes(
currentContainingBlockComputedStyle.position,
)) ||
(isOverflowElement(currentNode) &&
!currentNodeIsContaining &&
hasFixedPositionAncestor(element, currentNode));
if (shouldDropCurrentNode) {
// Drop non-containing blocks.
result = result.filter((ancestor) => ancestor !== currentNode);
} else {
// Record last containing block for next iteration.
currentContainingBlockComputedStyle = computedStyle;
}
currentNode = getParentNode(currentNode);
}
cache.set(element, result);
return result;
}
// Gets the maximum area that the element is visible in due to any number of
// clipping ancestors.
export function getClippingRect(
this: PlatformWithCache,
{
element,
boundary,
rootBoundary,
strategy,
}: {
element: Element;
boundary: Boundary;
rootBoundary: RootBoundary;
strategy: Strategy;
},
): Rect {
const elementClippingAncestors =
boundary === 'clippingAncestors'
? isTopLayer(element)
? []
: getClippingElementAncestors(element, this._c)
: [].concat(boundary);
const clippingAncestors = [...elementClippingAncestors, rootBoundary];
const firstClippingAncestor = clippingAncestors[0];
const clippingRect = clippingAncestors.reduce(
(accRect: ClientRectObject, clippingAncestor) => {
const rect = getClientRectFromClippingAncestor(
element,
clippingAncestor,
strategy,
);
accRect.top = max(rect.top, accRect.top);
accRect.right = min(rect.right, accRect.right);
accRect.bottom = min(rect.bottom, accRect.bottom);
accRect.left = max(rect.left, accRect.left);
return accRect;
},
getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy),
);
return {
width: clippingRect.right - clippingRect.left,
height: clippingRect.bottom - clippingRect.top,
x: clippingRect.left,
y: clippingRect.top,
};
}