• Stars
    star
    861
  • Rank 52,968 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tappable component for React

React-Tappable

Tappable component for React. Abstracts touch events to implement onTap, onPress, and pinch events.

The events mimic their native equivalents as closely as possible:

  • the baseClass (default: Tappable) has -active or -inactive added 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 onPress property 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. Keyboard events are also supported, emulating the behaviour of native button controls.

Demo & Examples

Live demo: jedwatson.github.io/react-tappable

To build the examples locally, run:

npm install
gulp dev

Then open localhost:8000 in a browser.

Installation

The easiest way to use React-tappable is to install it from npm.

npm install react-tappable --save

Ensure to include it in your own React build process (using Browserify, etc).

You could also use the standalone build by including dist/react-tappable.js in your page; but, if you do this, make sure you have already included React, and that it is available globally.

Usage

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.

import Tappable from 'react-tappable';

<Tappable onTap={this.handleTapEvent}>Tap me</Tappable>

For a lighter component, you can opt-in to just the features you need:

import Tappable from 'react-tappable/lib/Tappable';
import Pinchable from 'react-tappable/lib/Pinchable';
import TapAndPinchable from 'react-tappable/lib/TapAndPinchable';

<Tappable onTap={this.handleTapEvent}>I respond to Tap events</Tappable>
<Pinchable onPinch={this.handlePinch}>I respond to Pinch events</Pinchable>
<TapAndPinchable onTap={this.handleTapEvent} onPinch={this.handlePinch}>In respond to both!</TapAndPinchable>

The TapAndPinchable component is the default one you get when you just import react-tappable.

Properties

  • activeDelay ms delay before the -active class is added, defaults to 0
  • component component to render, defaults to 'span'
  • classes optional object containing active and inactive class names to apply to the component; useful with css-modules
  • classBase base to use for the active/inactive classes
  • className optional class name for the component
  • moveThreshold px to allow movement before cancelling a tap; defaults to 100
  • pressDelay ms delay before a press event is detected, defaults to 1000
  • pressMoveThreshold px to allow movement before ignoring long presses; defaults to 5
  • preventDefault (boolean) automatically call preventDefault on all events
  • stopPropagation (boolean) automatically call stopPropagation on all events
  • style (object) styles to apply to the component

Special Events

These are the special events implemented by Tappable.

  • onTap fired when touchStart or mouseDown is followed by touchEnd or mouseUp within the moveThreshold
  • onPress fired when a touch is held for the specified ms
  • onPinchStart fired when two fingers land on the screen
  • onPinchMove fired on any movement while two fingers are on screen
  • onPinchEnd fired when less than two fingers are left on the screen, onTouchStart is triggerred, if one touch remains

Pinch Events

Pinch events come with a special object with additional data to actually be more useful than the native events:

  • touches: Array of Objects - {identifier, pageX, pageY} - raw data from the event
  • center: Object - {x, y} - Calculated center between the two touch points
  • angle: Degrees - angle of the line connecting the two touch points to the X-axis
  • distance: Number of pixels - beween the two touch points
  • displacement: Object {x, y} - offset of the center since the pinch began
  • displacementVelocity: Object {x, y} : Pixels/ms - Immediate velocity of the displacement
  • rotation: degrees - delta rotation since the beginning of the gesture
  • rotationVelocity: degrees/millisecond - immediate rotational velocity
  • zoom: Number - Zoom factor - ratio between distance between the two touch points now over initial
  • zoomVelocity: zoomFactor/millisecond - immediate velocity of zooming
  • time: milliseconds since epoch - Timestamp

Known Issues

  • The pinch implementation has not been thoroughly tested
  • Any touch event with 3 three or more touches is completely ignored.

Native Events

The following native event handlers can also be specified.

  • onKeyDown
  • onKeyUp
  • onTouchStart
  • onTouchMove
  • onTouchEnd
  • onMouseDown
  • onMouseUp
  • onMouseMove
  • onMouseOut

Returning false from onKeyDown, onMouseDown, or onTouchStart handlers will prevent Tappable from handling the event.

Changelog

See CHANGES.md

License

Copyright (c) 2017 Jed Watson. MIT

More Repositories

1

react-select

The Select Component for React.js
TypeScript
27,612
star
2

classnames

A simple javascript utility for conditionally joining classNames together
JavaScript
17,507
star
3

react-codemirror

Codemirror Component for React.js
JavaScript
1,540
star
4

react-hammerjs

ReactJS / HammerJS integration. Support touch events in your React app.
JavaScript
939
star
5

react-input-autosize

Auto-resizing input field for React
JavaScript
765
star
6

sydjs-site

SydJS Meetup Website
JavaScript
535
star
7

react-md-editor

React.js Markdown Editor Component
JavaScript
508
star
8

exenv

React's ExecutionEnvironment module extracted for use in other packages & components
JavaScript
232
star
9

generator-react-component

React Component Project Generator
JavaScript
220
star
10

react-date-select

A React Date Select / Picker Input Component
CSS
158
star
11

happiness

Standard customised to make me happy
JavaScript
124
star
12

react-component-gulp-tasks

Common Gulp tasks I use across my React Component projects
JavaScript
112
star
13

react-value

Adding Value to React
JavaScript
91
star
14

node-premailer

Node.js wrapper for premailer.dialect.ca
JavaScript
74
star
15

react-container

A flexible container for laying out React apps. Part of TouchstoneJS.
JavaScript
57
star
16

react-domify

Like JSON.strinfigy but uses React to generate DOM elements
JavaScript
54
star
17

picture-tour-app

Example React.js Cordova / PhoneGap App with Babel, Webpack and Hot Reloading
JavaScript
46
star
18

react-fixed

A lightweight component that sticks to the bottom of the window while scrolling. Neat for toolbars.
JavaScript
33
star
19

generator-react-express

React / Express Project Generator
JavaScript
33
star
20

react-alt-text

React.js Component for displaying alternate text when the [alt] key is pressed
JavaScript
32
star
21

store-prototype

Simple class for creating event-driven Data Stores
JavaScript
32
star
22

keystone-forum

The source code for forums.keystonejs.com (in development)
JavaScript
30
star
23

react-scroll-spy

React.js mixin for updating state based on the window's scroll position
JavaScript
29
star
24

react-context-example

An example / test / playpen looking at how Context works in React.js
JavaScript
27
star
25

node-express-workshop-complete

The complete reference implementation for node-express-workshop
JavaScript
18
star
26

react-date-input

A React.js Date Input Component
CSS
16
star
27

react-express-starter

ReactJS / Express Starter Project
CSS
16
star
28

node-mascot

โœจ๐Ÿข๐Ÿš€โœจ
JavaScript
14
star
29

html-stringify

Converts Javascript Objects or Arrays to pretty HTML
JavaScript
14
star
30

model-transform

Common toJSON Transform for KeystoneJS / Mongoose Models
JavaScript
13
star
31

react-component-starter

ReactJS Component Starter Project
JavaScript
12
star
32

react-conf-2016

React Conf 2016 API
JavaScript
10
star
33

randomkey

Lightweight node.js lib for generating random string keys
JavaScript
10
star
34

prisma-day-keystone-workshop

TypeScript
10
star
35

asyncdi

Async Dependency Injection
JavaScript
9
star
36

block-timer

A node.js timer implementation for timing blocks of code that are run one or more times
JavaScript
8
star
37

node-bpay

Node.JS BPAY Customer Reference Number Generator and Validator
JavaScript
7
star
38

talk-sydjs-webcomponents

Resources for my talk on Web Components at SydJS, June 2014
CSS
6
star
39

keystone-next-prisma-demo

TypeScript
6
star
40

babel-plugin-example

JavaScript
5
star
41

react-sydney-2021-stack

Demo content for my talk at React Sydney, May 2021
TypeScript
5
star
42

list-to-array

Simple javascript lib for converting a [comma || space] delimited string to an array
JavaScript
4
star
43

aba

Deprecated; use aba-generator instead
JavaScript
4
star
44

sydney-mongodb-meetup

The resource files for the hand-on KeystoneJS session @ the Sydney MongoDB Meet-up, 25th Feb 2014
CSS
4
star
45

qs-set

Querystring manipulator for node.js and the browser
JavaScript
2
star
46

prototype-mobile-app

Prototype Mobile App
JavaScript
2
star
47

admin-ui-prototype

Starting from scratch
CSS
2
star
48

editor-ui-playground

TypeScript
1
star