|
7 | 7 | * @noflow |
8 | 8 | */ |
9 | 9 |
|
10 | | -import getBoundingClientRect from '../../modules/getBoundingClientRect'; |
11 | 10 | import setValueForStyles from '../../modules/setValueForStyles'; |
12 | 11 |
|
13 | 12 | 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; |
19 | 13 | 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 }; |
21 | 29 | }; |
22 | 30 |
|
23 | 31 | const measureLayout = (node, relativeToNativeNode, callback) => { |
24 | 32 | const relativeNode = relativeToNativeNode || (node && node.parentNode); |
25 | 33 | if (node && relativeNode) { |
26 | 34 | 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 | + } |
32 | 42 | }, 0); |
33 | 43 | } |
34 | 44 | }; |
|
0 commit comments