Tappable component for React. Abstracts touch events to implement onTap and onPress.
The events mimic their native equivalents as closely as possible:
- the baseClass (default:
Tappable) has-activeor-inactiveadded to it to enable pressed-state styling - the pressed state is visually cancelled if the touch moves too far away from the element, but resumes if the touch comes back again
- when you start scrolling a parent element, the touch event is cancelled
- if the
onPressproperty is set, it will cancel the touch event after the press happens
When touch events are not supported, it will fall back to mouse events.
Live demo: jedwatson.github.io/react-tappable
To build the examples locally, run:
npm install
gulp dev
Then open localhost:8000 in a browser.
The easiest way to use React-tappable is to install it from NPM and include it in your own React build process (using Browserify, etc).
You can also use the standalone build by including dist/react-tappable.js in your page. If you use this, make sure you have already included React, and it is available as a global variable.
npm install react-tappable --save
React-tappable generates a React component (defaults to <span>) and binds touch events to it.
To disable default event handling (e.g. scrolling) set the preventDefault prop.
var Tappable = require('react-tappable');
<Tappable onTap={this.handleTapEvent}>Tap me</Tappable>
componentcomponent to render, defaults to'span'classNameoptional class name for the componentclassBasebase to use for the active/inactive classesmoveThresholdpx to allow movement before cancelling a tap; defaults to100pressDelayms delay before a press event is detected, defaults to1000pressMoveThresholdpx to allow movement before ignoring long presses; defaults to5preventDefault(boolean) automatically call preventDefault on all eventsstopPropagation(boolean) automatically call stopPropagation on all events
These are the special events implemented by Tappable.
onTapfired when touchStart or mouseDown is followed by touchEnd or mouseUp within the moveThresholdonPressfired when a touch is held for the specified ms
The following native event handlers can also be specified.
onTouchStartonTouchMoveonTouchEndonMouseDownonMouseUponMouseMoveonMouseOut
Returning false from onTouchStart or onMouseDown handlers will prevent Tappable from handling the event.