Right-click or long-press the d20 button (per-combatant or Roll All) to open a context menu with Advantage and Disadvantage options. Normal left-click behavior is unchanged. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
33 lines
777 B
TypeScript
33 lines
777 B
TypeScript
import { useCallback, useRef } from "react";
|
|
|
|
const LONG_PRESS_MS = 500;
|
|
|
|
export function useLongPress(onLongPress: (e: React.TouchEvent) => void) {
|
|
const timerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
const firedRef = useRef(false);
|
|
|
|
const onTouchStart = useCallback(
|
|
(e: React.TouchEvent) => {
|
|
firedRef.current = false;
|
|
timerRef.current = setTimeout(() => {
|
|
firedRef.current = true;
|
|
onLongPress(e);
|
|
}, LONG_PRESS_MS);
|
|
},
|
|
[onLongPress],
|
|
);
|
|
|
|
const onTouchEnd = useCallback((e: React.TouchEvent) => {
|
|
clearTimeout(timerRef.current);
|
|
if (firedRef.current) {
|
|
e.preventDefault();
|
|
}
|
|
}, []);
|
|
|
|
const onTouchMove = useCallback(() => {
|
|
clearTimeout(timerRef.current);
|
|
}, []);
|
|
|
|
return { onTouchStart, onTouchEnd, onTouchMove };
|
|
}
|