/** * ContextMenu - 右键菜单组件 */ import { useEffect, useRef } from 'react' export interface MenuItem { icon: React.ReactNode label: string action: () => void danger?: boolean separator?: boolean } interface ContextMenuProps { x: number y: number items: MenuItem[] onClose: () => void } export function ContextMenu({ x, y, items, onClose }: ContextMenuProps) { const menuRef = useRef(null) useEffect(() => { const handleClick = (e: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { onClose() } } const handleKeydown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } document.addEventListener('mousedown', handleClick) document.addEventListener('keydown', handleKeydown) return () => { document.removeEventListener('mousedown', handleClick) document.removeEventListener('keydown', handleKeydown) } }, [onClose]) return (
{items.map((item, index) => (
{item.separator && index > 0 && (
)}
))}
) }