Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure touch events are properly handled for pageX and pageY #15587

Merged
merged 4 commits into from May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 36 additions & 10 deletions packages/react-events/src/Press.js
Expand Up @@ -190,14 +190,13 @@ function dispatchLongPressChangeEvent(

function activate(event, context, props, state) {
const nativeEvent: any = event.nativeEvent;
const pageX = nativeEvent.pageX;
const pageY = nativeEvent.pageY;
const {x, y} = getEventPageCoords(nativeEvent);
const wasActivePressed = state.isActivePressed;
state.isActivePressed = true;
if (pageX != null && pageY != null) {
if (x !== null && y !== null) {
state.activationPosition = {
pageX: nativeEvent.pageX,
pageY: nativeEvent.pageY,
pageX: x,
pageY: y,
};
}

Expand Down Expand Up @@ -402,12 +401,39 @@ function calculateResponderRegion(target: Element, props: PressProps) {
};
}

function isTouchEvent(nativeEvent: Event): boolean {
return Array.isArray((nativeEvent: any).changedTouches);
}

function getTouchFromPressEvent(nativeEvent: TouchEvent): Touch {
const {changedTouches, touches} = nativeEvent;
return changedTouches.length > 0
? changedTouches[0]
: touches.length > 0
? touches[0]
: (nativeEvent: any);
}

function getEventPageCoords(
nativeEvent: Event,
): {x: null | number, y: null | number} {
let eventObject = (nativeEvent: any);
if (isTouchEvent(eventObject)) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
eventObject = getTouchFromPressEvent(eventObject);
}
const pageX = eventObject.pageX;
const pageY = eventObject.pageY;
return {
x: pageX != null ? pageX : null,
y: pageY != null ? pageY : null,
};
}

function isPressWithinResponderRegion(
nativeEvent: $PropertyType<ReactResponderEvent, 'nativeEvent'>,
state: PressState,
): boolean {
const {responderRegionOnActivation, responderRegionOnDeactivation} = state;
const event = (nativeEvent: any);
let left, top, right, bottom;

if (responderRegionOnActivation != null) {
Expand All @@ -423,16 +449,16 @@ function isPressWithinResponderRegion(
bottom = Math.max(bottom, responderRegionOnDeactivation.bottom);
}
}
const {x, y} = getEventPageCoords(((nativeEvent: any): Event));

return (
left != null &&
right != null &&
top != null &&
bottom != null &&
(event.pageX >= left &&
event.pageX <= right &&
event.pageY >= top &&
event.pageY <= bottom)
x !== null &&
y !== null &&
(x >= left && x <= right && y >= top && y <= bottom)
);
}

Expand Down