Created
September 18, 2025 13:31
-
-
Save hnordt/633a20477b8a5838a24ca8202e3f7632 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React from "react"; | |
| function usePress(onEnd: (isCancelled: boolean) => void, onStart?: () => void) { | |
| return { | |
| onPointerUp(e: React.PointerEvent<Element>) { | |
| const rect = e.currentTarget.getBoundingClientRect(); | |
| onEnd( | |
| !( | |
| e.clientX >= rect.left && | |
| e.clientX <= rect.right && | |
| e.clientY >= rect.top && | |
| e.clientY <= rect.bottom | |
| ) | |
| ); | |
| }, | |
| onPointerDown: onStart, | |
| }; | |
| } | |
| function Usage() { | |
| const press = usePress( | |
| (isCancelled) => console.log(isCancelled ? "press cancelled" : "press end"), | |
| () => console.log("press start") | |
| ); | |
| return <button {...press} />; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment