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

Allow user-specified click rejection #25

Merged
merged 2 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ var Main = React.createClass({
ReactDOM.render(<Main />, document.getElementById("container"));
```

### Ignoring ghost clicks

When a tap happens, the browser sends a `touchstart` and `touchend`, and then
300ms later, a `click` event. This plugin ignores the click event if it has
been immediately preceeded by a touch event (within 750ms of the last touch
event).

Occasionally, there may be times when the 750ms threshold is exceeded due to
slow rendering or garbage collection, and this causes the dreaded ghost click.

The 750ms threshold is pretty good, but sometimes you might want to override
that behaviour. You can do this by supplying your own `shouldRejectClick`
function when you inject the plugin.

The following example will simply reject all click events, which you might
want to do if you are always using `onTouchTap` and only building for touch
devices:

```js
var React = require('react'),
injectTapEventPlugin = require("react-tap-event-plugin");
injectTapEventPlugin({
shouldRejectClick: function (lastTouchEventTimestamp, clickEventTimestamp) {
return true;
}
});
```

## Build standalone version

Use the demo project and it's README instructions to build a version of React with the tap event plugin included.
108 changes: 55 additions & 53 deletions src/TapEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,61 +109,63 @@ var now = (function() {
}
})();

var TapEventPlugin = {

tapMoveThreshold: tapMoveThreshold,

ignoreMouseThreshold: ignoreMouseThreshold,

eventTypes: eventTypes,

/**
* @param {string} topLevelType Record from `EventConstants`.
* @param {DOMEventTarget} topLevelTarget The listening component root node.
* @param {string} topLevelTargetID ID of `topLevelTarget`.
* @param {object} nativeEvent Native browser event.
* @return {*} An accumulation of synthetic events.
* @see {EventPluginHub.extractEvents}
*/
extractEvents: function(
topLevelType,
topLevelTarget,
topLevelTargetID,
nativeEvent,
nativeEventTarget) {

if (isTouch(topLevelType)) {
lastTouchEvent = now();
} else {
if (lastTouchEvent && (now() - lastTouchEvent) < ignoreMouseThreshold) {
return null;
}
}

if (!isStartish(topLevelType) && !isEndish(topLevelType)) {
return null;
}
var event = null;
var distance = getDistance(startCoords, nativeEvent);
if (isEndish(topLevelType) && distance < tapMoveThreshold) {
event = SyntheticUIEvent.getPooled(
eventTypes.touchTap,
function createTapEventPlugin(shouldRejectClick) {
return {

tapMoveThreshold: tapMoveThreshold,

ignoreMouseThreshold: ignoreMouseThreshold,

eventTypes: eventTypes,

/**
* @param {string} topLevelType Record from `EventConstants`.
* @param {DOMEventTarget} topLevelTarget The listening component root node.
* @param {string} topLevelTargetID ID of `topLevelTarget`.
* @param {object} nativeEvent Native browser event.
* @return {*} An accumulation of synthetic events.
* @see {EventPluginHub.extractEvents}
*/
extractEvents: function(
topLevelType,
topLevelTarget,
topLevelTargetID,
nativeEvent,
nativeEventTarget
);
}
if (isStartish(topLevelType)) {
startCoords.x = getAxisCoordOfEvent(Axis.x, nativeEvent);
startCoords.y = getAxisCoordOfEvent(Axis.y, nativeEvent);
} else if (isEndish(topLevelType)) {
startCoords.x = 0;
startCoords.y = 0;
nativeEventTarget) {

if (isTouch(topLevelType)) {
lastTouchEvent = now();
} else {
if (shouldRejectClick(lastTouchEvent, now())) {
return null;
}
}

if (!isStartish(topLevelType) && !isEndish(topLevelType)) {
return null;
}
var event = null;
var distance = getDistance(startCoords, nativeEvent);
if (isEndish(topLevelType) && distance < tapMoveThreshold) {
event = SyntheticUIEvent.getPooled(
eventTypes.touchTap,
topLevelTargetID,
nativeEvent,
nativeEventTarget
);
}
if (isStartish(topLevelType)) {
startCoords.x = getAxisCoordOfEvent(Axis.x, nativeEvent);
startCoords.y = getAxisCoordOfEvent(Axis.y, nativeEvent);
} else if (isEndish(topLevelType)) {
startCoords.x = 0;
startCoords.y = 0;
}
EventPropagators.accumulateTwoPhaseDispatches(event);
return event;
}
EventPropagators.accumulateTwoPhaseDispatches(event);
return event;
}

};
};
}

module.exports = TapEventPlugin;
module.exports = createTapEventPlugin;
5 changes: 5 additions & 0 deletions src/defaultClickRejectionStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(lastTouchEvent, clickTimestamp) {
if (lastTouchEvent && (clickTimestamp - lastTouchEvent) < 750) {
return null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change this to true to actually ignore the click event.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I recently changed this to work on React 14 and messed up. Should return true so caller can return null.

}
};
9 changes: 7 additions & 2 deletions src/injectTapEventPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = function injectTapEventPlugin () {
var defaultClickRejectionStrategy = require("./defaultClickRejectionStrategy");

module.exports = function injectTapEventPlugin (strategyOverrides) {
strategyOverrides = strategyOverrides || {}
var shouldRejectClick = strategyOverrides.shouldRejectClick || defaultClickRejectionStrategy;

require('react/lib/EventPluginHub').injection.injectEventPluginsByName({
"TapEventPlugin": require('./TapEventPlugin.js')
"TapEventPlugin": require('./TapEventPlugin.js')(shouldRejectClick)
});
};