Skip to content

Commit

Permalink
fix: mobile context menu behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lewxdev committed Aug 4, 2024
1 parent 99096f1 commit e17bd19
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/hooks/use-long-press.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,38 @@ export function useLongPress({
}: Options) {
const timeoutRef = useRef<NodeJS.Timeout | null>(null);

const handleStart = (event: React.TouchEvent) => {
event.preventDefault();
const handleStart = () => {
timeoutRef.current = setTimeout(() => {
onLongPress();
navigator.vibrate?.(50);
timeoutRef.current = null;
}, threshold);
};

const handleEnd = (callback?: () => void) => (event: React.TouchEvent) => {
event.preventDefault();
if (event.cancelable) {
// prevent click event if tapped
event.preventDefault();
}
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
callback?.();
}
};

const handleRightClick = (event: React.MouseEvent) => {
// prevent context menu
event.preventDefault();
// only trigger with secondary mouse button
if (event.button === 2) {
onLongPress();
}
};

return {
onClick: onPress,
onContextMenu: onLongPress,
onContextMenu: handleRightClick,
onTouchStart: handleStart,
onTouchEnd: handleEnd(onPress),
onTouchCancel: handleEnd(),
Expand Down

0 comments on commit e17bd19

Please sign in to comment.