Skip to content

Commit

Permalink
[fix] use getBoundingClientRect to measure layout
Browse files Browse the repository at this point in the history
Fix #1037
Fix #1151
  • Loading branch information
necolas committed Jan 1, 2019
1 parent 77c778a commit 93eb3f0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
20 changes: 8 additions & 12 deletions packages/react-native-web/src/exports/UIManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@
* @noflow
*/

import getBoundingClientRect from '../../modules/getBoundingClientRect';
import setValueForStyles from '../../vendor/react-dom/setValueForStyles';

const getRect = node => {
const height = node.offsetHeight;
// Unlike the DOM's getBoundingClientRect, React Native layout measurements
// for "height" and "width" ignore scale transforms.
// https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements
const { x, y, top, left } = getBoundingClientRect(node);
const width = node.offsetWidth;
let left = node.offsetLeft;
let top = node.offsetTop;
node = node.offsetParent;

while (node && node.nodeType === 1 /* Node.ELEMENT_NODE */) {
left += node.offsetLeft - node.scrollLeft;
top += node.offsetTop - node.scrollTop;
node = node.offsetParent;
}
return { height, left, top, width };
const height = node.offsetHeight;
return { x, y, width, height, top, left };
};

const measureLayout = (node, relativeToNativeNode, callback) => {
const relativeNode = relativeToNativeNode || (node && node.parentNode);
if (node && relativeNode) {
setTimeout(() => {
const relativeRect = getRect(relativeNode);
const relativeRect = getBoundingClientRect(relativeNode);
const { height, left, top, width } = getRect(node);
const x = left - relativeRect.left;
const y = top - relativeRect.top;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2015-present, Nicolas Gallagher.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

/* global HTMLElement */

const getBoundingClientRect = (node: HTMLElement) => {
if (node) {
const isElement = node.nodeType === 1; /* Node.ELEMENT_NODE */
if (isElement && typeof node.getBoundingClientRect === 'function') {
return node.getBoundingClientRect();
}
}
};

export default getBoundingClientRect;
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@
* @flow
*/

import getBoundingClientRect from '../getBoundingClientRect';

const emptyArray = [];
const emptyFunction = () => {};

const getRect = node => {
if (node) {
const isElement = node.nodeType === 1 /* Node.ELEMENT_NODE */;
if (isElement && typeof node.getBoundingClientRect === 'function') {
return node.getBoundingClientRect();
}
}
};

// Mobile Safari re-uses touch objects, so we copy the properties we want and normalize the identifier
const normalizeTouches = touches => {
if (!touches) {
Expand All @@ -35,13 +28,13 @@ const normalizeTouches = touches => {
clientY: touch.clientY,
force: touch.force,
get locationX() {
rect = rect || getRect(touch.target);
rect = rect || getBoundingClientRect(touch.target);
if (rect) {
return touch.pageX - rect.left;
}
},
get locationY() {
rect = rect || getRect(touch.target);
rect = rect || getBoundingClientRect(touch.target);
if (rect) {
return touch.pageY - rect.top;
}
Expand Down Expand Up @@ -121,13 +114,13 @@ function normalizeMouseEvent(nativeEvent) {
force: nativeEvent.force,
identifier: 0,
get locationX() {
rect = rect || getRect(nativeEvent.target);
rect = rect || getBoundingClientRect(nativeEvent.target);
if (rect) {
return nativeEvent.pageX - rect.left;
}
},
get locationY() {
rect = rect || getRect(nativeEvent.target);
rect = rect || getBoundingClientRect(nativeEvent.target);
if (rect) {
return nativeEvent.pageY - rect.top;
}
Expand Down

0 comments on commit 93eb3f0

Please sign in to comment.