Created
October 25, 2020 03:51
-
-
Save nihlton/cfeb311119998c075e1fbc48c4dba1dc to your computer and use it in GitHub Desktop.
long Press hook-like
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
export default function longPressEvents(callback, ms = 500) { | |
let timeout = null | |
const start = () => (timeout = setTimeout(callback, ms)) | |
const stop = () => timeout && window.clearTimeout(timeout) | |
return callback ? { | |
onMouseDown: start, | |
onMouseUp: stop, | |
onMouseLeave: stop, | |
onTouchStart: start, | |
onTouchMove: stop, | |
onTouchEnd: stop, | |
} : {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've seen useLongPress hooks floating around. they aren't great, since you have to call hooks from the main function body.
can't do stuff like this:
items.map(item => <button {...useLongPress(handleClick)}>{item}</button>)
this simple vanilla JS side steps the limitations of hooks.