mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-01-12 22:51:09 +08:00
@@ -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;
|
||||
|
||||
21
packages/react-native-web/src/modules/getBoundingClientRect/index.js
vendored
Normal file
21
packages/react-native-web/src/modules/getBoundingClientRect/index.js
vendored
Normal file
@@ -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;
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user