Skip to content

Instantly share code, notes, and snippets.

@hnordt
Created September 18, 2025 13:31
Show Gist options
  • Select an option

  • Save hnordt/633a20477b8a5838a24ca8202e3f7632 to your computer and use it in GitHub Desktop.

Select an option

Save hnordt/633a20477b8a5838a24ca8202e3f7632 to your computer and use it in GitHub Desktop.
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