mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-01 18:15:13 +00:00
[fix] only call 'getBoundingClientRect' if nativeEvent.location{X,Y} is accessed
Calculating the `location{X,Y}` values for events requires a call to
`getBoundingClientRect`. To prevent unnecessary performance costs, these values
are implemented as getters and will only make the DOM API call when accessed in
application code.
Close #1157
This commit is contained in:
committed by
Nicolas Gallagher
parent
9888c2a3c6
commit
40c433c6df
+10
-3
@@ -696,9 +696,16 @@ const TouchableMixin = {
|
|||||||
const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent);
|
const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent);
|
||||||
const pageX = touch && touch.pageX;
|
const pageX = touch && touch.pageX;
|
||||||
const pageY = touch && touch.pageY;
|
const pageY = touch && touch.pageY;
|
||||||
const locationX = touch && touch.locationX;
|
this.pressInLocation = {
|
||||||
const locationY = touch && touch.locationY;
|
pageX,
|
||||||
this.pressInLocation = { pageX, pageY, locationX, locationY };
|
pageY,
|
||||||
|
get locationX() {
|
||||||
|
return touch && touch.locationX;
|
||||||
|
},
|
||||||
|
get locationY() {
|
||||||
|
return touch && touch.locationY;
|
||||||
|
}
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
_getDistanceBetweenPoints: function(aX: number, aY: number, bX: number, bY: number) {
|
_getDistanceBetweenPoints: function(aX: number, aY: number, bX: number, bY: number) {
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ function normalizeTouchEvent(nativeEvent) {
|
|||||||
typeof nativeEvent.stopPropagation === 'function'
|
typeof nativeEvent.stopPropagation === 'function'
|
||||||
? nativeEvent.stopPropagation.bind(nativeEvent)
|
? nativeEvent.stopPropagation.bind(nativeEvent)
|
||||||
: emptyFunction;
|
: emptyFunction;
|
||||||
|
const singleChangedTouch = changedTouches[0];
|
||||||
|
|
||||||
const event = {
|
const event = {
|
||||||
_normalized: true,
|
_normalized: true,
|
||||||
@@ -85,11 +86,15 @@ function normalizeTouchEvent(nativeEvent) {
|
|||||||
cancelable: nativeEvent.cancelable,
|
cancelable: nativeEvent.cancelable,
|
||||||
changedTouches,
|
changedTouches,
|
||||||
defaultPrevented: nativeEvent.defaultPrevented,
|
defaultPrevented: nativeEvent.defaultPrevented,
|
||||||
identifier: undefined,
|
identifier: singleChangedTouch ? singleChangedTouch.identifier : undefined,
|
||||||
locationX: undefined,
|
get locationX() {
|
||||||
locationY: undefined,
|
return singleChangedTouch ? singleChangedTouch.locationX : undefined;
|
||||||
pageX: nativeEvent.pageX,
|
},
|
||||||
pageY: nativeEvent.pageY,
|
get locationY() {
|
||||||
|
return singleChangedTouch ? singleChangedTouch.locationY : undefined;
|
||||||
|
},
|
||||||
|
pageX: singleChangedTouch ? singleChangedTouch.pageX : nativeEvent.pageX,
|
||||||
|
pageY: singleChangedTouch ? singleChangedTouch.pageY : nativeEvent.pageY,
|
||||||
preventDefault,
|
preventDefault,
|
||||||
stopImmediatePropagation,
|
stopImmediatePropagation,
|
||||||
stopPropagation,
|
stopPropagation,
|
||||||
@@ -102,14 +107,6 @@ function normalizeTouchEvent(nativeEvent) {
|
|||||||
which: nativeEvent.which
|
which: nativeEvent.which
|
||||||
};
|
};
|
||||||
|
|
||||||
if (changedTouches[0]) {
|
|
||||||
event.identifier = changedTouches[0].identifier;
|
|
||||||
event.pageX = changedTouches[0].pageX;
|
|
||||||
event.pageY = changedTouches[0].pageY;
|
|
||||||
event.locationX = changedTouches[0].locationX;
|
|
||||||
event.locationY = changedTouches[0].locationY;
|
|
||||||
}
|
|
||||||
|
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,8 +161,12 @@ function normalizeMouseEvent(nativeEvent) {
|
|||||||
changedTouches: touches,
|
changedTouches: touches,
|
||||||
defaultPrevented: nativeEvent.defaultPrevented,
|
defaultPrevented: nativeEvent.defaultPrevented,
|
||||||
identifier: touches[0].identifier,
|
identifier: touches[0].identifier,
|
||||||
locationX: touches[0].locationX,
|
get locationX() {
|
||||||
locationY: touches[0].locationY,
|
return touches[0].locationX;
|
||||||
|
},
|
||||||
|
get locationY() {
|
||||||
|
return touches[0].locationY;
|
||||||
|
},
|
||||||
pageX: nativeEvent.pageX,
|
pageX: nativeEvent.pageX,
|
||||||
pageY: nativeEvent.pageY,
|
pageY: nativeEvent.pageY,
|
||||||
preventDefault,
|
preventDefault,
|
||||||
|
|||||||
Reference in New Issue
Block a user