import { useEffect, useRef, useState, useLayoutEffect } from 'react'; import { ChevronRight, Search } from 'lucide-react'; export interface ContextMenuItem { label: string; icon?: React.ReactNode; onClick: () => void; danger?: boolean; disabled?: boolean; divider?: boolean; submenu?: ContextMenuItem[]; // When set on an item with a submenu, render a search input above the // submenu items that filters by label (case-insensitive). submenuSearchPlaceholder?: string; title?: string; } interface ContextMenuProps { x: number; y: number; items: ContextMenuItem[]; onClose: () => void; } interface SubmenuPanelProps { items: ContextMenuItem[]; searchPlaceholder?: string; onClose: () => void; className: string; onMouseEnter: () => void; onMouseLeave: () => void; } function SubmenuPanel({ items, searchPlaceholder, onClose, className, onMouseEnter, onMouseLeave, }: SubmenuPanelProps) { const [query, setQuery] = useState(''); const inputRef = useRef(null); useEffect(() => { if (searchPlaceholder) { // Defer focus so it survives the mouse event that opened the submenu. const id = window.setTimeout(() => inputRef.current?.focus(), 0); return () => window.clearTimeout(id); } }, [searchPlaceholder]); const trimmed = query.trim().toLowerCase(); const filteredItems = searchPlaceholder && trimmed ? items.filter((i) => i.label.toLowerCase().includes(trimmed)) : items; return (
{searchPlaceholder && (
setQuery(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { const first = filteredItems.find((i) => !i.disabled); if (first) { first.onClick(); onClose(); } } }} placeholder={searchPlaceholder} className="w-full pl-7 pr-2 py-1 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none" />
)} {filteredItems.map((subItem, subIndex) => ( ))} {searchPlaceholder && filteredItems.length === 0 && (
)}
); } export function ContextMenu({ x, y, items, onClose }: ContextMenuProps) { const menuRef = useRef(null); const [activeSubmenu, setActiveSubmenu] = useState(null); const submenuTimeoutRef = useRef(null); const [position, setPosition] = useState({ x, y, visible: false }); const [openSubmenuLeft, setOpenSubmenuLeft] = useState(false); const [submenuPositions, setSubmenuPositions] = useState>({}); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { onClose(); } }; const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } }; const handleScroll = (e: Event) => { // Internal submenu scroll (overflow-y-auto on the submenu panel) must // not dismiss the menu — only close on scroll outside our own subtree. if (menuRef.current && menuRef.current.contains(e.target as Node)) { return; } onClose(); }; document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleEscape); document.addEventListener('scroll', handleScroll, true); return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleEscape); document.removeEventListener('scroll', handleScroll, true); if (submenuTimeoutRef.current) { clearTimeout(submenuTimeoutRef.current); } }; }, [onClose]); // Adjust position to keep menu in viewport - use useLayoutEffect for synchronous measurement useLayoutEffect(() => { if (menuRef.current) { // Force a reflow to get accurate measurements menuRef.current.style.visibility = 'hidden'; menuRef.current.style.display = 'block'; const rect = menuRef.current.getBoundingClientRect(); const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; const padding = 8; let adjustedX = x; let adjustedY = y; // Adjust horizontal position - if menu would overflow right, shift left if (x + rect.width > viewportWidth - padding) { adjustedX = Math.max(padding, viewportWidth - rect.width - padding); } // Also check if starting position is negative if (adjustedX < padding) { adjustedX = padding; } // Adjust vertical position - if menu would overflow bottom, shift up if (y + rect.height > viewportHeight - padding) { adjustedY = Math.max(padding, viewportHeight - rect.height - padding); } // Also check if starting position is negative if (adjustedY < padding) { adjustedY = padding; } // Check if submenus should open to the left (more space on left than right) const submenuWidth = 180; const spaceOnRight = viewportWidth - adjustedX - rect.width; const spaceOnLeft = adjustedX; // Only open left if there's not enough space on right AND there's enough space on left setOpenSubmenuLeft(spaceOnRight < submenuWidth && spaceOnLeft > submenuWidth); setPosition({ x: adjustedX, y: adjustedY, visible: true }); } }, [x, y]); const handleMouseEnterSubmenu = (index: number, element: HTMLElement) => { if (submenuTimeoutRef.current) { clearTimeout(submenuTimeoutRef.current); submenuTimeoutRef.current = null; } // Calculate if submenu should open upward or downward const rect = element.getBoundingClientRect(); const viewportHeight = window.innerHeight; const submenuMaxHeight = 300; // matches max-h-[300px] const padding = 8; // Check if there's enough space below for the submenu const spaceBelow = viewportHeight - rect.top - padding; const shouldOpenUpward = spaceBelow < submenuMaxHeight && rect.top > submenuMaxHeight; setSubmenuPositions(prev => ({ ...prev, [index]: shouldOpenUpward ? 'bottom' : 'top' })); setActiveSubmenu(index); }; const handleMouseLeaveSubmenu = () => { submenuTimeoutRef.current = window.setTimeout(() => { setActiveSubmenu(null); }, 150); }; return (
{items.map((item, index) => { if (item.divider) { return
; } const hasSubmenu = item.submenu && item.submenu.length > 0; return (
hasSubmenu && handleMouseEnterSubmenu(index, e.currentTarget)} onMouseLeave={() => hasSubmenu && handleMouseLeaveSubmenu()} > {/* Submenu */} {hasSubmenu && activeSubmenu === index && ( { if (submenuTimeoutRef.current) { clearTimeout(submenuTimeoutRef.current); submenuTimeoutRef.current = null; } }} onMouseLeave={() => handleMouseLeaveSubmenu()} /> )}
); })}
); }