|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import type { ElementType } from "react"; |
| 5 | + |
| 6 | +export interface ContextMenuAction { |
| 7 | + label: string; |
| 8 | + icon: ElementType; |
| 9 | + onClick: () => void; |
| 10 | + danger?: boolean; |
| 11 | + disabled?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +interface ContextMenuProps { |
| 15 | + position: { x: number; y: number }; |
| 16 | + actions: ContextMenuAction[]; |
| 17 | + onClose: () => void; |
| 18 | +} |
| 19 | + |
| 20 | +export default function ContextMenu({ position, actions, onClose }: ContextMenuProps) { |
| 21 | + const menuRef = useRef<HTMLDivElement>(null); |
| 22 | + const [menuPosition, setMenuPosition] = useState(position); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + const adjustPosition = () => { |
| 26 | + const menuWidth = menuRef.current?.offsetWidth ?? 200; |
| 27 | + const menuHeight = menuRef.current?.offsetHeight ?? actions.length * 40; |
| 28 | + |
| 29 | + let top = position.y; |
| 30 | + let left = position.x; |
| 31 | + |
| 32 | + if (top + menuHeight > window.innerHeight) { |
| 33 | + top = Math.max(0, window.innerHeight - menuHeight - 8); |
| 34 | + } |
| 35 | + if (left + menuWidth > window.innerWidth) { |
| 36 | + left = Math.max(0, window.innerWidth - menuWidth - 8); |
| 37 | + } |
| 38 | + |
| 39 | + setMenuPosition({ x: left, y: top }); |
| 40 | + }; |
| 41 | + |
| 42 | + adjustPosition(); |
| 43 | + }, [position, actions.length]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const handleScroll = () => onClose(); |
| 47 | + const handleResize = () => onClose(); |
| 48 | + |
| 49 | + window.addEventListener("scroll", handleScroll, true); |
| 50 | + window.addEventListener("resize", handleResize); |
| 51 | + |
| 52 | + return () => { |
| 53 | + window.removeEventListener("scroll", handleScroll, true); |
| 54 | + window.removeEventListener("resize", handleResize); |
| 55 | + }; |
| 56 | + }, [onClose]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <div |
| 60 | + ref={menuRef} |
| 61 | + className="fixed z-[999] w-52 rounded-lg border border-gray-200 bg-white shadow-lg" |
| 62 | + style={{ top: menuPosition.y, left: menuPosition.x }} |
| 63 | + role="menu" |
| 64 | + onClick={(e) => e.stopPropagation()} |
| 65 | + onContextMenu={(e) => e.preventDefault()} |
| 66 | + > |
| 67 | + {actions.map((action) => { |
| 68 | + const Icon = action.icon; |
| 69 | + return ( |
| 70 | + <button |
| 71 | + key={action.label} |
| 72 | + type="button" |
| 73 | + onClick={(e) => { |
| 74 | + e.stopPropagation(); |
| 75 | + if (action.disabled) return; |
| 76 | + action.onClick(); |
| 77 | + }} |
| 78 | + className={`w-full flex items-center gap-3 px-4 py-2.5 text-sm text-left transition-colors ${ |
| 79 | + action.danger |
| 80 | + ? "text-red-600 hover:bg-red-50" |
| 81 | + : action.disabled |
| 82 | + ? "text-gray-400 cursor-not-allowed" |
| 83 | + : "text-gray-700 hover:bg-gray-100" |
| 84 | + }`} |
| 85 | + role="menuitem" |
| 86 | + disabled={action.disabled} |
| 87 | + > |
| 88 | + <Icon |
| 89 | + className={`h-5 w-5 ${ |
| 90 | + action.danger ? "text-red-500" : action.disabled ? "text-gray-300" : "text-gray-500" |
| 91 | + }`} |
| 92 | + /> |
| 93 | + <span>{action.label}</span> |
| 94 | + </button> |
| 95 | + ); |
| 96 | + })} |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | + |
0 commit comments