Last active
April 30, 2023 19:28
-
-
Save MoOx/12726d85a3343d84ee3c to your computer and use it in GitHub Desktop.
Double touch tap workaround for React based on onTouchTap (react-tap-event-plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dblTouchTapMaxDelay = 300 | |
let latestTouchTap = { | |
time: 0, | |
target: null, | |
} | |
export default function isDblTouchTap(event) { | |
const touchTap = { | |
time: new Date().getTime(), | |
target: event.currentTarget, | |
} | |
const isFastDblTouchTap = ( | |
touchTap.target === latestTouchTap.target && | |
touchTap.time - latestTouchTap.time < dblTouchTapMaxDelay | |
) | |
latestTouchTap = touchTap | |
return isFastDblTouchTap | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add onTouchTap support to avoid 300ms onClick delay on touchscreen (like iOS) | |
import injectTapEventPlugin from "react-tap-event-plugin" | |
injectTapEventPlugin() | |
import isDblTouchTap from "./isDblTouchTap" | |
// ... | |
<div | |
onTouchTap={ (e) => { | |
if (isDblTouchTap(e)) { | |
// == onDblTouchTap | |
} | |
} } | |
> | |
... | |
</div> |
On click seems to be working fine with react
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks! 👍