Skip to content

Commit 03ddd95

Browse files
LucioChavezFuentesnecolas
authored andcommitted
[fix] layout measurement API consistency with React Native
* Ignore CSS transforms in measurement. * Cancel measurement if elements are unmounted. Close #2501 Fix #1254
1 parent 782a19a commit 03ddd95

File tree

1 file changed

+22
-12
lines changed
  • packages/react-native-web/src/exports/UIManager

1 file changed

+22
-12
lines changed

packages/react-native-web/src/exports/UIManager/index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,38 @@
77
* @noflow
88
*/
99

10-
import getBoundingClientRect from '../../modules/getBoundingClientRect';
1110
import setValueForStyles from '../../modules/setValueForStyles';
1211

1312
const getRect = (node) => {
14-
// Unlike the DOM's getBoundingClientRect, React Native layout measurements
15-
// for "height" and "width" ignore scale transforms.
16-
// https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements
17-
const { x, y, top, left } = getBoundingClientRect(node);
18-
const width = node.offsetWidth;
1913
const height = node.offsetHeight;
20-
return { x, y, width, height, top, left };
14+
const width = node.offsetWidth;
15+
let left = node.offsetLeft;
16+
let top = node.offsetTop;
17+
node = node.offsetParent;
18+
19+
while (node && node.nodeType === 1 /* Node.ELEMENT_NODE */) {
20+
left += node.offsetLeft + node.clientLeft - node.scrollLeft;
21+
top += node.offsetTop + node.clientTop - node.scrollTop;
22+
node = node.offsetParent;
23+
}
24+
25+
top -= window.scrollY;
26+
left -= window.scrollX;
27+
28+
return { width, height, top, left };
2129
};
2230

2331
const measureLayout = (node, relativeToNativeNode, callback) => {
2432
const relativeNode = relativeToNativeNode || (node && node.parentNode);
2533
if (node && relativeNode) {
2634
setTimeout(() => {
27-
const relativeRect = getBoundingClientRect(relativeNode);
28-
const { height, left, top, width } = getRect(node);
29-
const x = left - relativeRect.left;
30-
const y = top - relativeRect.top;
31-
callback(x, y, width, height, left, top);
35+
if (node.isConnected && relativeNode.isConnected) {
36+
const relativeRect = getRect(relativeNode);
37+
const { height, left, top, width } = getRect(node);
38+
const x = left - relativeRect.left;
39+
const y = top - relativeRect.top;
40+
callback(x, y, width, height, left, top);
41+
}
3242
}, 0);
3343
}
3444
};

0 commit comments

Comments
 (0)