Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
962 B · 38 lines
TypeScript
at develop
123456789101112131415161718192021222324252627282930313233343536373839import { type RefObject, useEffect, useRef } from "react";
import useEventListener from "./useEventListener";
export function useRightClick( callback: (e: MouseEvent) => void, element?: RefObject<HTMLElement | null>,) { const mousePosition = useRef<{ x: number; y: number }>(undefined); const callbackRef = useRef<typeof callback>(() => {}); useEffect(() => { callbackRef.current = callback; }, [callback]); useEventListener( "pointerdown", (e: MouseEvent) => { if (e.button === 2) { mousePosition.current = { x: e.clientX, y: e.clientY }; } else { mousePosition.current = undefined; } }, element?.current || undefined, ); useEventListener( "pointerup", (e: MouseEvent) => { if (!mousePosition.current) return; if ( Math.abs(e.clientX - mousePosition.current.x) < 5 && Math.abs(e.clientY - mousePosition.current.y) < 5 ) { callbackRef.current?.(e); } }, element?.current || undefined, );}