Skip to content

Commit

Permalink
Fixing touch/mouse issues with TapEventPlugin
Browse files Browse the repository at this point in the history
On ios device, browser simulates mouse events, but does that with
a delay, because of double tap gesture. The problem is that
TapEventPlugins listens to both types of events, so it fires twice

Everytime there is a touch event, we should ignore mouse events that
follow it.  This way, we still respond to both mouse and touch events,
just ignore the device generated ones.
  • Loading branch information
Tienchai authored and zpao committed Oct 28, 2014
1 parent 1c241b3 commit ff12423
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/browser/eventPlugins/TapEventPlugin.js
Expand Up @@ -62,13 +62,15 @@ var dependencies = [
topLevelTypes.topMouseUp
];

var touchDependencies = [
topLevelTypes.topTouchStart,
topLevelTypes.topTouchCancel,
topLevelTypes.topTouchEnd,
topLevelTypes.topTouchMove
];

if (EventPluginUtils.useTouchEvents) {
dependencies.push(
topLevelTypes.topTouchCancel,
topLevelTypes.topTouchEnd,
topLevelTypes.topTouchStart,
topLevelTypes.topTouchMove
);
dependencies = dependencies.concat(touchDependencies);
}

var eventTypes = {
Expand All @@ -81,6 +83,10 @@ var eventTypes = {
}
};

var usedTouch = false;
var usedTouchTime = 0;
var TOUCH_DELAY = 500;

var TapEventPlugin = {

tapMoveThreshold: tapMoveThreshold,
Expand All @@ -103,6 +109,17 @@ var TapEventPlugin = {
if (!isStartish(topLevelType) && !isEndish(topLevelType)) {
return null;
}
// on ios, there is a delay after touch event and synthetic
// mouse events, so that user can perform double tap
// solution: ignore mouse events following touchevent within small timeframe
if (touchDependencies.indexOf(topLevelType) !== -1) {
usedTouch = true;
usedTouchTime = Date.now();
} else {
if (usedTouch && (Date.now() - usedTouchTime < TOUCH_DELAY)) {
return;
}
}
var event = null;
var distance = getDistance(startCoords, nativeEvent);
if (isEndish(topLevelType) && distance < tapMoveThreshold) {
Expand Down

0 comments on commit ff12423

Please sign in to comment.