import { useState, type PointerEvent as ReactPointerEvent } from "react"; type CursorState = { visible: boolean; active: boolean; x: number; y: number; }; export function useMobileCursor() { const debug = typeof window !== "undefined" && new URLSearchParams(window.location.search).has("cursorDebug"); const [cursor, setCursor] = useState({ visible: false, active: false, x: 0, y: 0, }); const updatePosition = (event: ReactPointerEvent) => { const bounds = event.currentTarget.getBoundingClientRect(); const localX = bounds.width === 0 ? 0 : ((event.clientX - bounds.left) / bounds.width) * event.currentTarget.offsetWidth; const localY = bounds.height === 0 ? 0 : ((event.clientY - bounds.top) / bounds.height) * event.currentTarget.offsetHeight; setCursor((current) => ({ ...current, visible: true, x: localX, y: localY, })); }; return { cursorHandlers: { onPointerEnter: updatePosition, onPointerMove: updatePosition, onPointerDown: (event: ReactPointerEvent) => { updatePosition(event); setCursor((current) => ({ ...current, active: true })); }, onPointerUp: (event: ReactPointerEvent) => { updatePosition(event); setCursor((current) => ({ ...current, active: false })); }, onPointerCancel: () => { setCursor((current) => ({ ...current, active: false, visible: false })); }, onPointerLeave: () => { setCursor((current) => ({ ...current, active: false, visible: false })); }, }, cursorDebug: debug, cursorElement: (
{debug ?
), }; }