|
| 1 | +// Copyright 2026, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { |
| 5 | + autoUpdate, |
| 6 | + flip, |
| 7 | + FloatingPortal, |
| 8 | + offset, |
| 9 | + shift, |
| 10 | + type Placement, |
| 11 | + type VirtualElement, |
| 12 | + useFloating, |
| 13 | +} from "@floating-ui/react"; |
| 14 | +import { cn } from "@/util/util"; |
| 15 | +import { memo, useEffect, useMemo, useRef, useState } from "react"; |
| 16 | + |
| 17 | +type PreviewContextMenuState = { |
| 18 | + items: ContextMenuItem[]; |
| 19 | + x: number; |
| 20 | + y: number; |
| 21 | +}; |
| 22 | + |
| 23 | +type PreviewContextMenuPanelProps = { |
| 24 | + items: ContextMenuItem[]; |
| 25 | + point?: { x: number; y: number }; |
| 26 | + referenceElement?: HTMLElement; |
| 27 | + placement: Placement; |
| 28 | + depth: number; |
| 29 | + parentPath: number[]; |
| 30 | + openPath: number[]; |
| 31 | + setOpenPath: (path: number[]) => void; |
| 32 | + closeMenu: () => void; |
| 33 | +}; |
| 34 | + |
| 35 | +type PreviewContextMenuItemProps = { |
| 36 | + item: ContextMenuItem; |
| 37 | + itemPath: number[]; |
| 38 | + depth: number; |
| 39 | + parentPath: number[]; |
| 40 | + openPath: number[]; |
| 41 | + setOpenPath: (path: number[]) => void; |
| 42 | + closeMenu: () => void; |
| 43 | +}; |
| 44 | + |
| 45 | +let previewContextMenuListener: ((state: PreviewContextMenuState) => void) | null = null; |
| 46 | +const previewContextMenuItemIds = new WeakMap<ContextMenuItem, string>(); |
| 47 | + |
| 48 | +function makeVirtualElement(x: number, y: number): VirtualElement { |
| 49 | + return { |
| 50 | + getBoundingClientRect() { |
| 51 | + return { |
| 52 | + x, |
| 53 | + y, |
| 54 | + width: 0, |
| 55 | + height: 0, |
| 56 | + top: y, |
| 57 | + right: x, |
| 58 | + bottom: y, |
| 59 | + left: x, |
| 60 | + toJSON: () => ({}), |
| 61 | + } as DOMRect; |
| 62 | + }, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +function isPathOpen(openPath: number[], path: number[]): boolean { |
| 67 | + if (path.length > openPath.length) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + return path.every((segment, index) => openPath[index] === segment); |
| 71 | +} |
| 72 | + |
| 73 | +function getVisibleItems(items: ContextMenuItem[]): ContextMenuItem[] { |
| 74 | + return items.filter((item) => item.visible !== false); |
| 75 | +} |
| 76 | + |
| 77 | +function activateItem(item: ContextMenuItem, closeMenu: () => void): void { |
| 78 | + closeMenu(); |
| 79 | + item.click?.(); |
| 80 | +} |
| 81 | + |
| 82 | +function getPreviewContextMenuItemId(item: ContextMenuItem): string { |
| 83 | + const existingId = previewContextMenuItemIds.get(item); |
| 84 | + if (existingId != null) { |
| 85 | + return existingId; |
| 86 | + } |
| 87 | + const newId = crypto.randomUUID(); |
| 88 | + previewContextMenuItemIds.set(item, newId); |
| 89 | + return newId; |
| 90 | +} |
| 91 | + |
| 92 | +const PreviewContextMenuItem = memo( |
| 93 | + ({ item, itemPath, depth, parentPath, openPath, setOpenPath, closeMenu }: PreviewContextMenuItemProps) => { |
| 94 | + const rowRef = useRef<HTMLDivElement>(null); |
| 95 | + const submenuItems = getVisibleItems(item.submenu ?? []); |
| 96 | + const hasSubmenu = submenuItems.length > 0; |
| 97 | + const isDisabled = item.enabled === false; |
| 98 | + const isHeader = item.type === "header"; |
| 99 | + const isSeparator = item.type === "separator"; |
| 100 | + const isChecked = item.type === "checkbox" || item.type === "radio" ? item.checked === true : false; |
| 101 | + const isSubmenuOpen = hasSubmenu && isPathOpen(openPath, itemPath); |
| 102 | + |
| 103 | + if (isSeparator) { |
| 104 | + return <div className="my-0.5 border-t border-border" role="separator" />; |
| 105 | + } |
| 106 | + |
| 107 | + const handleMouseEnter = () => { |
| 108 | + if (hasSubmenu) { |
| 109 | + setOpenPath(itemPath); |
| 110 | + return; |
| 111 | + } |
| 112 | + setOpenPath(parentPath); |
| 113 | + }; |
| 114 | + |
| 115 | + const handleClick = (e: React.MouseEvent<HTMLDivElement>) => { |
| 116 | + e.stopPropagation(); |
| 117 | + if (isDisabled || isHeader) { |
| 118 | + return; |
| 119 | + } |
| 120 | + if (hasSubmenu) { |
| 121 | + setOpenPath(itemPath); |
| 122 | + return; |
| 123 | + } |
| 124 | + activateItem(item, closeMenu); |
| 125 | + }; |
| 126 | + |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <div |
| 130 | + ref={rowRef} |
| 131 | + role={item.type === "checkbox" ? "menuitemcheckbox" : item.type === "radio" ? "menuitemradio" : "menuitem"} |
| 132 | + aria-disabled={isDisabled} |
| 133 | + aria-checked={item.type === "checkbox" || item.type === "radio" ? isChecked : undefined} |
| 134 | + data-context-menu-item={item.label ?? item.type ?? "item"} |
| 135 | + className={cn( |
| 136 | + "flex min-h-7 items-center gap-2 px-2.5 text-xs text-foreground select-none", |
| 137 | + !isHeader && "cursor-pointer", |
| 138 | + isHeader && "px-2.5 py-0.5 text-[10px] uppercase tracking-[0.08em] text-muted", |
| 139 | + !isHeader && !isDisabled && "hover:bg-hoverbg", |
| 140 | + isDisabled && "text-muted", |
| 141 | + isSubmenuOpen && "bg-hoverbg" |
| 142 | + )} |
| 143 | + onMouseEnter={handleMouseEnter} |
| 144 | + onClick={handleClick} |
| 145 | + > |
| 146 | + {isHeader ? ( |
| 147 | + <span className="truncate">{item.label}</span> |
| 148 | + ) : ( |
| 149 | + <> |
| 150 | + <span className="flex w-3.5 items-center justify-center text-center text-[10px]"> |
| 151 | + {isChecked ? <i className="fa fa-check" /> : null} |
| 152 | + </span> |
| 153 | + <div className="flex min-w-0 flex-1 flex-col"> |
| 154 | + <span className="truncate">{item.label}</span> |
| 155 | + {item.sublabel ? <span className="truncate text-[10px] text-muted">{item.sublabel}</span> : null} |
| 156 | + </div> |
| 157 | + {hasSubmenu ? ( |
| 158 | + <span className="ml-2 text-[10px] text-muted"> |
| 159 | + <i className="fa fa-chevron-right" /> |
| 160 | + </span> |
| 161 | + ) : null} |
| 162 | + </> |
| 163 | + )} |
| 164 | + </div> |
| 165 | + {hasSubmenu && isSubmenuOpen && rowRef.current != null ? ( |
| 166 | + <PreviewContextMenuPanel |
| 167 | + items={submenuItems} |
| 168 | + referenceElement={rowRef.current} |
| 169 | + placement="right-start" |
| 170 | + depth={depth + 1} |
| 171 | + parentPath={itemPath} |
| 172 | + openPath={openPath} |
| 173 | + setOpenPath={setOpenPath} |
| 174 | + closeMenu={closeMenu} |
| 175 | + /> |
| 176 | + ) : null} |
| 177 | + </> |
| 178 | + ); |
| 179 | + } |
| 180 | +); |
| 181 | + |
| 182 | +PreviewContextMenuItem.displayName = "PreviewContextMenuItem"; |
| 183 | + |
| 184 | +const PreviewContextMenuPanel = memo( |
| 185 | + ({ items, point, referenceElement, placement, depth, parentPath, openPath, setOpenPath, closeMenu }: PreviewContextMenuPanelProps) => { |
| 186 | + const visibleItems = getVisibleItems(items); |
| 187 | + const virtualReference = useMemo(() => { |
| 188 | + if (point == null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + return makeVirtualElement(point.x, point.y); |
| 192 | + }, [point]); |
| 193 | + const { refs, floatingStyles } = useFloating({ |
| 194 | + open: true, |
| 195 | + placement, |
| 196 | + strategy: "fixed", |
| 197 | + whileElementsMounted: autoUpdate, |
| 198 | + middleware: [ |
| 199 | + offset(depth === 0 ? 4 : { mainAxis: -4, crossAxis: -4 }), |
| 200 | + flip({ padding: 8 }), |
| 201 | + shift({ padding: 8 }), |
| 202 | + ], |
| 203 | + }); |
| 204 | + |
| 205 | + useEffect(() => { |
| 206 | + if (referenceElement != null) { |
| 207 | + refs.setReference(referenceElement); |
| 208 | + return; |
| 209 | + } |
| 210 | + refs.setPositionReference(virtualReference); |
| 211 | + }, [referenceElement, refs, virtualReference]); |
| 212 | + |
| 213 | + if (visibleItems.length === 0) { |
| 214 | + return null; |
| 215 | + } |
| 216 | + |
| 217 | + return ( |
| 218 | + <div |
| 219 | + ref={refs.setFloating} |
| 220 | + style={floatingStyles} |
| 221 | + className="min-w-[180px] overflow-visible rounded-md border border-border bg-modalbg py-0.5 shadow-2xl" |
| 222 | + role="menu" |
| 223 | + > |
| 224 | + {visibleItems.map((item, index) => ( |
| 225 | + <PreviewContextMenuItem |
| 226 | + key={getPreviewContextMenuItemId(item)} |
| 227 | + item={item} |
| 228 | + itemPath={[...parentPath, index]} |
| 229 | + depth={depth} |
| 230 | + parentPath={parentPath} |
| 231 | + openPath={openPath} |
| 232 | + setOpenPath={setOpenPath} |
| 233 | + closeMenu={closeMenu} |
| 234 | + /> |
| 235 | + ))} |
| 236 | + </div> |
| 237 | + ); |
| 238 | + } |
| 239 | +); |
| 240 | + |
| 241 | +PreviewContextMenuPanel.displayName = "PreviewContextMenuPanel"; |
| 242 | + |
| 243 | +export function showPreviewContextMenu(menu: ContextMenuItem[], e: React.MouseEvent): void { |
| 244 | + e.stopPropagation(); |
| 245 | + e.preventDefault(); |
| 246 | + previewContextMenuListener?.({ |
| 247 | + items: menu, |
| 248 | + x: e.clientX, |
| 249 | + y: e.clientY, |
| 250 | + }); |
| 251 | +} |
| 252 | + |
| 253 | +export const PreviewContextMenu = memo(() => { |
| 254 | + const [menuState, setMenuState] = useState<PreviewContextMenuState | null>(null); |
| 255 | + const [openPath, setOpenPath] = useState<number[]>([]); |
| 256 | + const portalRef = useRef<HTMLDivElement>(null); |
| 257 | + |
| 258 | + const closeMenu = () => { |
| 259 | + setMenuState(null); |
| 260 | + setOpenPath([]); |
| 261 | + }; |
| 262 | + |
| 263 | + useEffect(() => { |
| 264 | + previewContextMenuListener = (state) => { |
| 265 | + setMenuState(state); |
| 266 | + setOpenPath([]); |
| 267 | + }; |
| 268 | + return () => { |
| 269 | + previewContextMenuListener = null; |
| 270 | + }; |
| 271 | + }, []); |
| 272 | + |
| 273 | + useEffect(() => { |
| 274 | + if (menuState == null) { |
| 275 | + return; |
| 276 | + } |
| 277 | + |
| 278 | + const handlePointerDown = (event: PointerEvent) => { |
| 279 | + if (portalRef.current?.contains(event.target as Node)) { |
| 280 | + return; |
| 281 | + } |
| 282 | + closeMenu(); |
| 283 | + }; |
| 284 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 285 | + if (event.key === "Escape") { |
| 286 | + closeMenu(); |
| 287 | + } |
| 288 | + }; |
| 289 | + |
| 290 | + document.addEventListener("pointerdown", handlePointerDown, true); |
| 291 | + document.addEventListener("keydown", handleKeyDown); |
| 292 | + window.addEventListener("blur", closeMenu); |
| 293 | + window.addEventListener("resize", closeMenu); |
| 294 | + window.addEventListener("scroll", closeMenu, true); |
| 295 | + return () => { |
| 296 | + document.removeEventListener("pointerdown", handlePointerDown, true); |
| 297 | + document.removeEventListener("keydown", handleKeyDown); |
| 298 | + window.removeEventListener("blur", closeMenu); |
| 299 | + window.removeEventListener("resize", closeMenu); |
| 300 | + window.removeEventListener("scroll", closeMenu, true); |
| 301 | + }; |
| 302 | + }, [menuState]); |
| 303 | + |
| 304 | + if (menuState == null) { |
| 305 | + return null; |
| 306 | + } |
| 307 | + |
| 308 | + return ( |
| 309 | + <FloatingPortal> |
| 310 | + <div ref={portalRef}> |
| 311 | + <PreviewContextMenuPanel |
| 312 | + items={menuState.items} |
| 313 | + point={{ x: menuState.x, y: menuState.y }} |
| 314 | + placement="bottom-start" |
| 315 | + depth={0} |
| 316 | + parentPath={[]} |
| 317 | + openPath={openPath} |
| 318 | + setOpenPath={setOpenPath} |
| 319 | + closeMenu={closeMenu} |
| 320 | + /> |
| 321 | + </div> |
| 322 | + </FloatingPortal> |
| 323 | + ); |
| 324 | +}); |
| 325 | + |
| 326 | +PreviewContextMenu.displayName = "PreviewContextMenu"; |
0 commit comments