28 lines
599 B
TypeScript
28 lines
599 B
TypeScript
import { useState, useCallback } from 'react';
|
|||
|
|
|
||
|
|
interface ContextMenuState {
|
||
|
|
visible: boolean;
|
||
|
|
x: number;
|
||
|
|
y: number;
|
||
|
|
nodeId: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useContextMenu() {
|
||
|
|
const [menuState, setMenuState] = useState<ContextMenuState>({
|
||
|
|
visible: false,
|
||
|
|
x: 0,
|
||
|
|
y: 0,
|
||
|
|
nodeId: null,
|
||
|
|
});
|
||
|
|
|
||
|
|
const show = useCallback((x: number, y: number, nodeId: string | null) => {
|
||
|
|
setMenuState({ visible: true, x, y, nodeId });
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const hide = useCallback(() => {
|
||
|
|
setMenuState((prev) => ({ ...prev, visible: false }));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return { menuState, show, hide };
|
||
|
|
}
|