├── README.md ├── index.js ├── package.json ├── .gitignore ├── LICENSE └── Tapable.js /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import Tapable from './Tapable'; 2 | export default Tapable; 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-tapable", 3 | "version": "1.0.0", 4 | "description": "React Native Component that handles single, double, or triple tap interactions with the least delay possible", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "github.com/christopherabouabdo/react-native-tapable" 12 | }, 13 | "keywords": [ 14 | "react-native", 15 | "component", 16 | "tap" 17 | ], 18 | "author": "christopherabouabdo", 19 | "license": "MIT" 20 | } 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Christopher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Tapable.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import React, { 4 | Component, 5 | TouchableWithoutFeedback, 6 | View 7 | } from 'react-native'; 8 | 9 | const TAP_DELAY = 250; 10 | const COUNT_HANDLER_MAP = [ 11 | { count: 1, fn: 'onTap' }, 12 | { count: 2, fn: 'onDoubleTap' }, 13 | { count: 3, fn: 'onTripleTap' }, 14 | ] 15 | 16 | class Tapable extends Component { 17 | 18 | constructor() { 19 | super(); 20 | 21 | // Timer Vars 22 | this._timer = null; 23 | this._tapCount = 0; 24 | 25 | // Bind functions to instance 26 | this._bind(); 27 | } 28 | 29 | _bind() { 30 | this.onPress = this.onPress.bind(this); 31 | this._timerExpired = this._timerExpired.bind(this); 32 | } 33 | 34 | reset() { 35 | this._timer = null; 36 | this._tapCount = 0; 37 | } 38 | 39 | onPress() { 40 | this._timer 41 | ? this._handlePress() 42 | : this._handleInitialPress(); 43 | } 44 | 45 | _handlePress() { 46 | this._tapCount++; 47 | this._shouldKeepListening() 48 | ? this._extendTimer() 49 | : this._forceExpire(); 50 | } 51 | 52 | _handleInitialPress() { 53 | this._tapCount = 1; 54 | this._shouldKeepListening() 55 | ? this._startTimer() 56 | : this._forceExpire(); 57 | } 58 | 59 | _startTimer() { 60 | this._timer = setTimeout(this._timerExpired, TAP_DELAY); 61 | } 62 | 63 | _extendTimer() { 64 | clearTimeout(this._timer); 65 | this._startTimer(); 66 | } 67 | 68 | _forceExpire() { 69 | clearTimeout(this._timer); 70 | this._timerExpired(); 71 | } 72 | 73 | _timerExpired() { 74 | // Find and run the proper handler for the tap count 75 | let current = COUNT_HANDLER_MAP.find((row) => row.count === this._tapCount); 76 | this.props[current.fn] && this.props[current.fn](); 77 | // Reset timer and tap count 78 | this.reset(); 79 | } 80 | 81 | _shouldKeepListening() { 82 | // Check if there are any listeners assigned to tapCounts 83 | // higher than the current count 84 | return COUNT_HANDLER_MAP.find((row) => { 85 | return !!(row.count > this._tapCount && this.props[row.fn]); 86 | }); 87 | } 88 | 89 | render() { 90 | return( 91 | 92 | 96 | { this.props.children } 97 | 98 | 99 | ); 100 | } 101 | 102 | } 103 | 104 | Tapable.propTypes = { 105 | onTap: React.PropTypes.func, 106 | onDoubleTap: React.PropTypes.func, 107 | onTripleTap: React.PropTypes.func, 108 | style: View.propTypes.style, 109 | }; 110 | 111 | export default Tapable; 112 | --------------------------------------------------------------------------------