|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import findNearestClickableElement from "./utils/find-nearest-clickable-element"; |
| 3 | +import getLiveCursorPosition from "./utils/get-live-cursor-position"; |
| 4 | +import calculateNextPosition from "./utils/calculate-next-position"; |
| 5 | +import isElementOutOfViewport from "./utils/is-element-out-of-viewport"; |
| 6 | + |
| 7 | +const ArrowKeys = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]; |
| 8 | + |
| 9 | +function simulateClick({ x, y }: { x: number; y: number }) { |
| 10 | + const element = findNearestClickableElement({ x, y }); |
| 11 | + |
| 12 | + if (element instanceof HTMLElement) return element.click(); |
| 13 | +} |
| 14 | + |
| 15 | +export default function useVirtualCursor({ |
| 16 | + moveMultiplier = 1, |
| 17 | +}: { moveMultiplier?: number }) { |
| 18 | + const cursorRef = useRef<HTMLDivElement>(null); |
| 19 | + const [position, setPosition] = useState({ x: 0, y: 0 }); |
| 20 | + const [canInteract, setcanInteract] = useState<boolean>(false); |
| 21 | + const [cursorDimensions, setCursorDimensions] = useState({ |
| 22 | + width: 0, |
| 23 | + height: 0, |
| 24 | + }); |
| 25 | + |
| 26 | + const handleKeyPress = useCallback( |
| 27 | + (e: KeyboardEvent) => { |
| 28 | + if (!cursorDimensions || !moveMultiplier || !position) return; |
| 29 | + |
| 30 | + const key = e.key; |
| 31 | + |
| 32 | + if (ArrowKeys.includes(key)) |
| 33 | + return setPosition((currentPosition) => { |
| 34 | + const nextPosition = calculateNextPosition({ |
| 35 | + currentPosition, |
| 36 | + cursorDimensions, |
| 37 | + moveMultiplier, |
| 38 | + key, |
| 39 | + }); |
| 40 | + |
| 41 | + if ( |
| 42 | + !nextPosition || |
| 43 | + isElementOutOfViewport({ position: nextPosition, cursorDimensions }) |
| 44 | + ) |
| 45 | + return currentPosition; |
| 46 | + |
| 47 | + return nextPosition; |
| 48 | + }); |
| 49 | + |
| 50 | + if (key === "Enter" && cursorRef.current) |
| 51 | + return simulateClick(getLiveCursorPosition(cursorRef.current)); |
| 52 | + }, |
| 53 | + [cursorDimensions, moveMultiplier, position], |
| 54 | + ); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + window.addEventListener("keydown", handleKeyPress); |
| 58 | + |
| 59 | + return () => window.removeEventListener("keydown", handleKeyPress); |
| 60 | + }, [handleKeyPress]); |
| 61 | + |
| 62 | + // Set the cursor in the center of the screen initially |
| 63 | + useEffect(() => { |
| 64 | + if (!cursorRef.current) return; |
| 65 | + |
| 66 | + const { width, height } = cursorRef.current.getBoundingClientRect(); |
| 67 | + |
| 68 | + setCursorDimensions({ width, height }); |
| 69 | + |
| 70 | + setPosition({ |
| 71 | + x: (window.innerWidth - width) / 2, |
| 72 | + y: (window.innerHeight - height) / 2, |
| 73 | + }); |
| 74 | + }, []); |
| 75 | + |
| 76 | + // Update state of cursor is/isn't over clickable element |
| 77 | + // biome-ignore lint/correctness/useExhaustiveDependencies: triggered by changes to position |
| 78 | + useEffect(() => { |
| 79 | + if (cursorRef.current) { |
| 80 | + const cursorPosition = getLiveCursorPosition(cursorRef.current); |
| 81 | + const element = findNearestClickableElement(cursorPosition); |
| 82 | + |
| 83 | + setcanInteract(Boolean(element)); |
| 84 | + } |
| 85 | + }, [position]); |
| 86 | + |
| 87 | + return { cursorRef, position, setPosition, canInteract, cursorDimensions }; |
| 88 | +} |
0 commit comments