The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { ContextMenuItem } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m } from "./i18n/messages"; |
| 5 | |
| 6 | const SVG_NS = "http://www.w3.org/2000/svg"; |
| 7 | |
| 8 | const ICON_PATHS: Record<string, ReadonlyArray<{ tag: string; attrs: Record<string, string> }>> = { |
| 9 | archive: [ |
| 10 | { tag: "rect", attrs: { width: "20", height: "5", x: "2", y: "3", rx: "1" } }, |
| 11 | { tag: "path", attrs: { d: "M4 8v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8" } }, |
| 12 | { tag: "path", attrs: { d: "M10 12h4" } }, |
| 13 | ], |
| 14 | check: [{ tag: "path", attrs: { d: "M20 6 9 17l-5-5" } }], |
| 15 | timer: [ |
| 16 | { tag: "line", attrs: { x1: "10", x2: "14", y1: "2", y2: "2" } }, |
| 17 | { tag: "line", attrs: { x1: "12", x2: "15", y1: "14", y2: "11" } }, |
| 18 | { tag: "circle", attrs: { cx: "12", cy: "14", r: "8" } }, |
| 19 | ], |
| 20 | "chevron-right": [{ tag: "path", attrs: { d: "m9 19 7-7-7-7" } }], |
| 21 | "circle-check": [ |
| 22 | { tag: "circle", attrs: { cx: "12", cy: "12", r: "10" } }, |
| 23 | { tag: "path", attrs: { d: "m9 12 2 2 4-4" } }, |
| 24 | ], |
| 25 | clock: [ |
| 26 | { tag: "path", attrs: { d: "M12 6v6l4 2" } }, |
| 27 | { tag: "circle", attrs: { cx: "12", cy: "12", r: "10" } }, |
| 28 | ], |
| 29 | pencil: [ |
| 30 | { |
| 31 | tag: "path", |
| 32 | attrs: { |
| 33 | d: "M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z", |
| 34 | }, |
| 35 | }, |
| 36 | { tag: "path", attrs: { d: "m15 5 4 4" } }, |
| 37 | ], |
| 38 | copy: [ |
| 39 | { tag: "rect", attrs: { width: "14", height: "14", x: "8", y: "8", rx: "2", ry: "2" } }, |
| 40 | { tag: "path", attrs: { d: "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" } }, |
| 41 | ], |
| 42 | folder: [ |
| 43 | { |
| 44 | tag: "path", |
| 45 | attrs: { |
| 46 | d: "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z", |
| 47 | }, |
| 48 | }, |
| 49 | ], |
| 50 | "git-branch": [ |
| 51 | { tag: "line", attrs: { x1: "6", x2: "6", y1: "3", y2: "15" } }, |
| 52 | { tag: "circle", attrs: { cx: "18", cy: "6", r: "3" } }, |
| 53 | { tag: "circle", attrs: { cx: "6", cy: "18", r: "3" } }, |
| 54 | { tag: "path", attrs: { d: "M18 9a9 9 0 0 1-9 9" } }, |
| 55 | ], |
| 56 | hash: [ |
| 57 | { tag: "line", attrs: { x1: "4", x2: "20", y1: "9", y2: "9" } }, |
| 58 | { tag: "line", attrs: { x1: "4", x2: "20", y1: "15", y2: "15" } }, |
| 59 | { tag: "line", attrs: { x1: "10", x2: "8", y1: "3", y2: "21" } }, |
| 60 | { tag: "line", attrs: { x1: "16", x2: "14", y1: "3", y2: "21" } }, |
| 61 | ], |
| 62 | "mail-open": [ |
| 63 | { |
| 64 | tag: "path", |
| 65 | attrs: { |
| 66 | d: "M21.2 8.4c.5.38.8.97.8 1.6v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V10a2 2 0 0 1 .8-1.6l8-6a2 2 0 0 1 2.4 0l8 6Z", |
| 67 | }, |
| 68 | }, |
| 69 | { tag: "path", attrs: { d: "m22 10-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 10" } }, |
| 70 | ], |
| 71 | "message-square": [ |
| 72 | { |
| 73 | tag: "path", |
| 74 | attrs: { |
| 75 | d: "M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z", |
| 76 | }, |
| 77 | }, |
| 78 | ], |
| 79 | "message-square-plus": [ |
| 80 | { |
| 81 | tag: "path", |
| 82 | attrs: { |
| 83 | d: "M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z", |
| 84 | }, |
| 85 | }, |
| 86 | { tag: "path", attrs: { d: "M12 8v6" } }, |
| 87 | { tag: "path", attrs: { d: "M9 11h6" } }, |
| 88 | ], |
| 89 | pin: [ |
| 90 | { tag: "path", attrs: { d: "M12 17v5" } }, |
| 91 | { |
| 92 | tag: "path", |
| 93 | attrs: { |
| 94 | d: "M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z", |
| 95 | }, |
| 96 | }, |
| 97 | ], |
| 98 | "pin-off": [ |
| 99 | { tag: "path", attrs: { d: "M12 17v5" } }, |
| 100 | { tag: "path", attrs: { d: "M15 9.34V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H7.89" } }, |
| 101 | { tag: "path", attrs: { d: "m2 2 20 20" } }, |
| 102 | { |
| 103 | tag: "path", |
| 104 | attrs: { |
| 105 | d: "M9 9v1.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h11", |
| 106 | }, |
| 107 | }, |
| 108 | ], |
| 109 | "refresh-cw": [ |
| 110 | { tag: "path", attrs: { d: "M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" } }, |
| 111 | { tag: "path", attrs: { d: "M21 3v5h-5" } }, |
| 112 | { tag: "path", attrs: { d: "M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" } }, |
| 113 | { tag: "path", attrs: { d: "M8 16H3v5" } }, |
| 114 | ], |
| 115 | settings: [ |
| 116 | { |
| 117 | tag: "path", |
| 118 | attrs: { |
| 119 | d: "M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.38a2 2 0 0 0-.73-2.73l-.15-.09a2 2 0 0 1-1-1.74v-.51a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z", |
| 120 | }, |
| 121 | }, |
| 122 | { tag: "circle", attrs: { cx: "12", cy: "12", r: "3" } }, |
| 123 | ], |
| 124 | "user-plus": [ |
| 125 | { tag: "path", attrs: { d: "M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" } }, |
| 126 | { tag: "circle", attrs: { cx: "9", cy: "7", r: "4" } }, |
| 127 | { tag: "line", attrs: { x1: "19", x2: "19", y1: "8", y2: "14" } }, |
| 128 | { tag: "line", attrs: { x1: "22", x2: "16", y1: "11", y2: "11" } }, |
| 129 | ], |
| 130 | "folder-plus": [ |
| 131 | { tag: "path", attrs: { d: "M12 10v6" } }, |
| 132 | { tag: "path", attrs: { d: "M9 13h6" } }, |
| 133 | { |
| 134 | tag: "path", |
| 135 | attrs: { |
| 136 | d: "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z", |
| 137 | }, |
| 138 | }, |
| 139 | ], |
| 140 | "folder-tree": [ |
| 141 | { |
| 142 | tag: "path", |
| 143 | attrs: { |
| 144 | d: "M20 10a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1h-2.5a1 1 0 0 1-.8-.4l-.9-1.2A1 1 0 0 0 15 3h-2a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1Z", |
| 145 | }, |
| 146 | }, |
| 147 | { |
| 148 | tag: "path", |
| 149 | attrs: { |
| 150 | d: "M20 21a1 1 0 0 0 1-1v-3a1 1 0 0 0-1-1h-2.9a1 1 0 0 1-.88-.55l-.42-.85a1 1 0 0 0-.92-.6H13a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1Z", |
| 151 | }, |
| 152 | }, |
| 153 | { tag: "path", attrs: { d: "M3 5a2 2 0 0 0 2 2h3" } }, |
| 154 | { tag: "path", attrs: { d: "M3 3v13a2 2 0 0 0 2 2h3" } }, |
| 155 | ], |
| 156 | "text-search": [ |
| 157 | { tag: "path", attrs: { d: "M21 5H3" } }, |
| 158 | { tag: "path", attrs: { d: "M10 12H3" } }, |
| 159 | { tag: "path", attrs: { d: "M10 19H3" } }, |
| 160 | { tag: "circle", attrs: { cx: "17", cy: "15", r: "3" } }, |
| 161 | { tag: "path", attrs: { d: "m21 19-1.9-1.9" } }, |
| 162 | ], |
| 163 | trash: [ |
| 164 | { tag: "path", attrs: { d: "M3 6h18" } }, |
| 165 | { tag: "path", attrs: { d: "M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" } }, |
| 166 | { tag: "path", attrs: { d: "M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" } }, |
| 167 | { tag: "line", attrs: { x1: "10", x2: "10", y1: "11", y2: "17" } }, |
| 168 | { tag: "line", attrs: { x1: "14", x2: "14", y1: "11", y2: "17" } }, |
| 169 | ], |
| 170 | }; |
| 171 | |
| 172 | function createIconElement(name: string, tone: "neutral" | "destructive"): SVGSVGElement | null { |
| 173 | const paths = ICON_PATHS[name]; |
| 174 | if (!paths || typeof document.createElementNS !== "function") { |
| 175 | return null; |
| 176 | } |
| 177 | const svg = document.createElementNS(SVG_NS, "svg"); |
| 178 | svg.setAttribute("aria-hidden", "true"); |
| 179 | svg.setAttribute("xmlns", SVG_NS); |
| 180 | svg.setAttribute("viewBox", "0 0 24 24"); |
| 181 | svg.setAttribute("fill", "none"); |
| 182 | svg.setAttribute("stroke", "currentColor"); |
| 183 | svg.setAttribute("stroke-width", "2"); |
| 184 | svg.setAttribute("stroke-linecap", "round"); |
| 185 | svg.setAttribute("stroke-linejoin", "round"); |
| 186 | svg.setAttribute( |
| 187 | "class", |
| 188 | tone === "destructive" |
| 189 | ? "size-4.5 shrink-0 sm:size-4" |
| 190 | : "size-4.5 shrink-0 text-muted-foreground sm:size-4", |
| 191 | ); |
| 192 | for (const node of paths) { |
| 193 | const child = document.createElementNS(SVG_NS, node.tag); |
| 194 | for (const [key, value] of Object.entries(node.attrs)) { |
| 195 | child.setAttribute(key, value); |
| 196 | } |
| 197 | svg.appendChild(child); |
| 198 | } |
| 199 | return svg; |
| 200 | } |
| 201 | |
| 202 | function clampMenuPosition(menu: HTMLDivElement, preferredLeft: number, preferredTop: number) { |
| 203 | const rect = menu.getBoundingClientRect(); |
| 204 | const left = Math.min( |
| 205 | Math.max(4, preferredLeft), |
| 206 | Math.max(4, window.innerWidth - rect.width - 4), |
| 207 | ); |
| 208 | const top = Math.min( |
| 209 | Math.max(4, preferredTop), |
| 210 | Math.max(4, window.innerHeight - rect.height - 4), |
| 211 | ); |
| 212 | menu.style.left = `${left}px`; |
| 213 | menu.style.top = `${top}px`; |
| 214 | } |
| 215 | |
| 216 | function isNodeWithinMenuStack(target: EventTarget | null, menuStack: readonly HTMLDivElement[]) { |
| 217 | if (typeof Node !== "undefined" && target instanceof Node) { |
| 218 | return menuStack.some((menu) => menu.contains(target)); |
| 219 | } |
| 220 | if (!target || typeof target !== "object") { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | let current: unknown = target; |
| 225 | while (current && typeof current === "object") { |
| 226 | if (menuStack.includes(current as HTMLDivElement)) { |
| 227 | return true; |
| 228 | } |
| 229 | current = (current as { parent?: unknown }).parent; |
| 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | let activeContextMenuDismiss: ((restoreFocus?: boolean) => void) | null = null; |
| 235 | |
| 236 | export function isContextMenuOpen(): boolean { |
| 237 | return activeContextMenuDismiss !== null; |
| 238 | } |
| 239 | |
| 240 | export function dismissContextMenu(restoreFocus = true): void { |
| 241 | activeContextMenuDismiss?.(restoreFocus); |
| 242 | activeContextMenuDismiss = null; |
| 243 | } |
| 244 | |
| 245 | export function showContextMenuFallback<T extends string>( |
| 246 | items: readonly ContextMenuItem<T>[], |
| 247 | position?: { x: number; y: number }, |
| 248 | ): Promise<T | null> { |
| 249 | dismissContextMenu(); |
| 250 | return new Promise<T | null>((resolve) => { |
| 251 | let focusedElement = document.activeElement; |
| 252 | while (focusedElement?.shadowRoot?.activeElement) { |
| 253 | focusedElement = focusedElement.shadowRoot.activeElement; |
| 254 | } |
| 255 | const previouslyFocusedElement = focusedElement instanceof HTMLElement ? focusedElement : null; |
| 256 | const menuStack: HTMLDivElement[] = []; |
| 257 | const submenuTriggerStack: Array<HTMLButtonElement | undefined> = []; |
| 258 | let isDisposed = false; |
| 259 | let canDismissFromPointer = false; |
| 260 | |
| 261 | const dismiss = (restoreFocus = true) => cleanup(null, restoreFocus); |
| 262 | |
| 263 | const cleanup = (result: T | null, restoreFocus = true) => { |
| 264 | if (isDisposed) { |
| 265 | return; |
| 266 | } |
| 267 | isDisposed = true; |
| 268 | if (activeContextMenuDismiss === dismiss) { |
| 269 | activeContextMenuDismiss = null; |
| 270 | } |
| 271 | document.removeEventListener("keydown", onKeyDown); |
| 272 | document.removeEventListener("pointerdown", onPointerDown, true); |
| 273 | document.removeEventListener("contextmenu", onContextMenu, true); |
| 274 | document.removeEventListener("focusin", onFocusIn); |
| 275 | window.removeEventListener("blur", onWindowBlur); |
| 276 | const shouldRestoreFocus = |
| 277 | restoreFocus && isNodeWithinMenuStack(document.activeElement, menuStack); |
| 278 | for (const menu of menuStack) { |
| 279 | menu.remove(); |
| 280 | } |
| 281 | if (shouldRestoreFocus && previouslyFocusedElement?.isConnected) { |
| 282 | previouslyFocusedElement.focus({ preventScroll: true }); |
| 283 | } |
| 284 | resolve(result); |
| 285 | }; |
| 286 | |
| 287 | const onKeyDown = (event: KeyboardEvent) => { |
| 288 | if ( |
| 289 | event.defaultPrevented || |
| 290 | event.isComposing || |
| 291 | event.altKey || |
| 292 | event.ctrlKey || |
| 293 | event.metaKey |
| 294 | ) |
| 295 | return; |
| 296 | const level = menuStack.findIndex((menu) => |
| 297 | isNodeWithinMenuStack(document.activeElement, [menu]), |
| 298 | ); |
| 299 | if (level < 0) return; |
| 300 | const menu = menuStack[level]!; |
| 301 | const buttons = [...menu.querySelectorAll<HTMLButtonElement>("button")].filter( |
| 302 | (button) => !button.disabled, |
| 303 | ); |
| 304 | const index = buttons.indexOf(document.activeElement as HTMLButtonElement); |
| 305 | let target: HTMLButtonElement | undefined; |
| 306 | switch (event.key) { |
| 307 | case "ArrowDown": |
| 308 | target = buttons[(index + 1) % buttons.length]; |
| 309 | break; |
| 310 | case "ArrowUp": |
| 311 | target = |
| 312 | buttons[(index < 0 ? buttons.length - 1 : index - 1 + buttons.length) % buttons.length]; |
| 313 | break; |
| 314 | case "Home": |
| 315 | target = buttons[0]; |
| 316 | break; |
| 317 | case "End": |
| 318 | target = buttons.at(-1); |
| 319 | break; |
| 320 | case "ArrowRight": |
| 321 | if (buttons[index]?.getAttribute("aria-haspopup") !== "menu") return; |
| 322 | buttons[index]?.click(); |
| 323 | break; |
| 324 | case " ": |
| 325 | case "Enter": |
| 326 | // Keyboard activation must not depend on the opening pointer gesture's frame. |
| 327 | canDismissFromPointer = true; |
| 328 | buttons[index]?.click(); |
| 329 | break; |
| 330 | case "ArrowLeft": |
| 331 | if (level === 0) return; |
| 332 | // Fall through to cancel this submenu only. |
| 333 | case "Escape": { |
| 334 | const parent = submenuTriggerStack[level]; |
| 335 | if (parent) { |
| 336 | parent.focus({ preventScroll: true }); |
| 337 | closeMenusFromLevel(level); |
| 338 | } else cleanup(null); |
| 339 | break; |
| 340 | } |
| 341 | case "Tab": |
| 342 | // Tab closes the menu and continues from the element that opened it. |
| 343 | cleanup(null); |
| 344 | return; |
| 345 | default: |
| 346 | return; |
| 347 | } |
| 348 | event.preventDefault(); |
| 349 | event.stopPropagation(); |
| 350 | if (target && target !== submenuTriggerStack[level + 1]) closeMenusFromLevel(level + 1); |
| 351 | target?.focus({ preventScroll: true }); |
| 352 | target?.scrollIntoView({ block: "nearest" }); |
| 353 | }; |
| 354 | |
| 355 | const onFocusIn = (event: FocusEvent) => { |
| 356 | if (!isNodeWithinMenuStack(event.target, menuStack)) cleanup(null, false); |
| 357 | }; |
| 358 | const onWindowBlur = () => cleanup(null, false); |
| 359 | |
| 360 | const onPointerDown = (event: PointerEvent) => { |
| 361 | if (!canDismissFromPointer || isNodeWithinMenuStack(event.target, menuStack)) { |
| 362 | return; |
| 363 | } |
| 364 | cleanup(null); |
| 365 | }; |
| 366 | |
| 367 | const onContextMenu = (event: MouseEvent) => { |
| 368 | if (!canDismissFromPointer || isNodeWithinMenuStack(event.target, menuStack)) { |
| 369 | return; |
| 370 | } |
| 371 | event.preventDefault(); |
| 372 | cleanup(null); |
| 373 | }; |
| 374 | |
| 375 | const closeMenusFromLevel = (level: number) => { |
| 376 | while (menuStack.length > level) { |
| 377 | submenuTriggerStack.pop()?.setAttribute("aria-expanded", "false"); |
| 378 | menuStack.pop()?.remove(); |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | const openMenu = ( |
| 383 | entries: readonly ContextMenuItem<T>[], |
| 384 | preferredLeft: number, |
| 385 | preferredTop: number, |
| 386 | level: number, |
| 387 | parentTrigger?: HTMLButtonElement, |
| 388 | ) => { |
| 389 | closeMenusFromLevel(level); |
| 390 | |
| 391 | const menu = document.createElement("div"); |
| 392 | menu.className = |
| 393 | "dropdown-glass fixed z-[10000] min-w-32 max-w-sm overflow-hidden rounded-lg bg-clip-padding text-popover-foreground outline-none"; |
| 394 | menu.style.cssText = |
| 395 | "position:fixed;z-index:10000;min-width:8rem;max-width:24rem;overflow:hidden;border-radius:var(--radius-lg);background-clip:padding-box;color:var(--contrast-popover-foreground);outline:none;pointer-events:auto;"; |
| 396 | menu.style.left = `${preferredLeft}px`; |
| 397 | menu.style.top = `${preferredTop}px`; |
| 398 | menu.dataset.level = String(level); |
| 399 | menu.dataset.contextMenu = "true"; |
| 400 | menu.setAttribute("role", "menu"); |
| 401 | menu.setAttribute("aria-label", parentTrigger?.textContent ?? m(msg("Context menu"))); |
| 402 | menu.tabIndex = -1; |
| 403 | |
| 404 | const inner = document.createElement("div"); |
| 405 | inner.className = |
| 406 | "max-h-[calc(100vh-0.5rem)] min-w-0 max-w-sm overflow-y-auto overflow-x-hidden p-1"; |
| 407 | inner.style.cssText = |
| 408 | "max-height:calc(100vh - 0.5rem);min-width:0;max-width:24rem;overflow-x:hidden;overflow-y:auto;padding:0.25rem;"; |
| 409 | |
| 410 | for (const item of entries) { |
| 411 | if (item.separatorBefore === true && inner.children.length > 0) { |
| 412 | const separator = document.createElement("div"); |
| 413 | separator.className = "mx-2 my-1 h-px bg-border"; |
| 414 | separator.style.cssText = |
| 415 | "height:1px;margin:0.25rem 0.5rem;background:var(--contrast-border);"; |
| 416 | separator.dataset.contextMenuSeparator = "true"; |
| 417 | separator.setAttribute("role", "separator"); |
| 418 | inner.appendChild(separator); |
| 419 | } |
| 420 | |
| 421 | if (item.header === true) { |
| 422 | const header = document.createElement("div"); |
| 423 | header.className = "px-2 py-1.5 font-medium text-muted-foreground text-xs"; |
| 424 | header.textContent = item.label; |
| 425 | inner.appendChild(header); |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | const hasChildren = Array.isArray(item.children) && item.children.length > 0; |
| 430 | const isLeafDestructive = |
| 431 | !hasChildren && (item.destructive === true || item.id === ("delete" as T)); |
| 432 | |
| 433 | const button = document.createElement("button"); |
| 434 | button.type = "button"; |
| 435 | const isDisabled = item.disabled === true; |
| 436 | button.disabled = isDisabled; |
| 437 | button.tabIndex = -1; |
| 438 | button.setAttribute("role", "menuitem"); |
| 439 | button.setAttribute("aria-disabled", String(isDisabled)); |
| 440 | const rowBase = |
| 441 | "flex w-full cursor-default select-none items-center gap-2 rounded-sm px-2 py-1 text-left outline-none transition-colors sm:min-h-7 sm:text-sm min-h-8 text-base"; |
| 442 | button.className = isDisabled |
| 443 | ? `${rowBase} pointer-events-none cursor-not-allowed text-muted-foreground opacity-64` |
| 444 | : isLeafDestructive |
| 445 | ? `${rowBase} text-destructive-foreground hover:bg-destructive/10 hover:text-destructive-foreground` |
| 446 | : `${rowBase} text-foreground hover:bg-accent hover:text-accent-foreground`; |
| 447 | button.style.cssText = |
| 448 | "display:flex;width:100%;min-height:1.75rem;align-items:center;gap:0.5rem;border:0;border-radius:var(--radius-sm);background:transparent;padding:0.25rem 0.5rem;color:var(--contrast-foreground);font-family:var(--font-sans,system-ui,sans-serif);font-size:0.875rem;line-height:1.25rem;text-align:left;cursor:default;"; |
| 449 | if (isLeafDestructive) { |
| 450 | button.style.color = "var(--destructive-foreground)"; |
| 451 | } |
| 452 | if (isDisabled) { |
| 453 | button.style.color = "var(--contrast-muted-foreground)"; |
| 454 | button.style.opacity = "0.64"; |
| 455 | button.style.pointerEvents = "none"; |
| 456 | } |
| 457 | |
| 458 | if (typeof item.checked === "boolean") { |
| 459 | button.setAttribute("role", "menuitemradio"); |
| 460 | button.setAttribute("aria-checked", item.checked ? "true" : "false"); |
| 461 | const check = item.checked ? createIconElement("check", "neutral") : null; |
| 462 | if (check) { |
| 463 | button.appendChild(check); |
| 464 | } else { |
| 465 | const spacer = document.createElement("span"); |
| 466 | spacer.className = "size-4.5 shrink-0 sm:size-4"; |
| 467 | spacer.style.cssText = "display:inline-block;width:1rem;height:1rem;flex-shrink:0;"; |
| 468 | spacer.setAttribute("aria-hidden", "true"); |
| 469 | button.appendChild(spacer); |
| 470 | } |
| 471 | } else if (typeof item.icon === "string") { |
| 472 | const icon = createIconElement(item.icon, isLeafDestructive ? "destructive" : "neutral"); |
| 473 | if (icon) { |
| 474 | button.appendChild(icon); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | const label = document.createElement("span"); |
| 479 | label.className = "min-w-0 flex-1 truncate"; |
| 480 | label.textContent = item.label; |
| 481 | button.appendChild(label); |
| 482 | |
| 483 | if (item.detail) { |
| 484 | // The comma gives screen readers a pause between label and detail. |
| 485 | button.setAttribute("aria-label", `${item.label}, ${item.detail}`); |
| 486 | const detail = document.createElement("span"); |
| 487 | detail.className = "ms-auto shrink-0 ps-4 text-muted-foreground text-xs"; |
| 488 | detail.style.cssText = |
| 489 | "margin-inline-start:auto;flex-shrink:0;padding-inline-start:1rem;color:var(--contrast-muted-foreground);font-size:0.75rem;"; |
| 490 | detail.textContent = item.detail; |
| 491 | button.appendChild(detail); |
| 492 | } |
| 493 | |
| 494 | if (hasChildren) { |
| 495 | button.setAttribute("aria-haspopup", "menu"); |
| 496 | button.setAttribute("aria-expanded", "false"); |
| 497 | const chevron = createIconElement("chevron-right", "neutral"); |
| 498 | if (chevron) { |
| 499 | chevron.setAttribute( |
| 500 | "class", |
| 501 | "-me-0.5 ms-auto size-4.5 shrink-0 text-muted-foreground opacity-80 sm:size-4", |
| 502 | ); |
| 503 | chevron.setAttribute("aria-hidden", "true"); |
| 504 | chevron.dataset.contextMenuChevron = "true"; |
| 505 | button.appendChild(chevron); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (!isDisabled) { |
| 510 | let isHovered = false; |
| 511 | let isFocused = false; |
| 512 | const updateHighlight = () => { |
| 513 | const isHighlighted = isHovered || isFocused; |
| 514 | button.style.background = isHighlighted |
| 515 | ? isLeafDestructive |
| 516 | ? "color-mix(in srgb, var(--destructive) 10%, transparent)" |
| 517 | : "var(--accent)" |
| 518 | : "transparent"; |
| 519 | button.style.color = isHighlighted |
| 520 | ? isLeafDestructive |
| 521 | ? "var(--destructive-foreground)" |
| 522 | : "var(--contrast-accent-foreground)" |
| 523 | : isLeafDestructive |
| 524 | ? "var(--destructive-foreground)" |
| 525 | : "var(--contrast-foreground)"; |
| 526 | }; |
| 527 | button.addEventListener("mouseenter", () => { |
| 528 | button.focus({ preventScroll: true }); |
| 529 | isHovered = true; |
| 530 | updateHighlight(); |
| 531 | }); |
| 532 | button.addEventListener("mouseleave", () => { |
| 533 | isHovered = false; |
| 534 | updateHighlight(); |
| 535 | }); |
| 536 | button.addEventListener("focus", () => { |
| 537 | isFocused = true; |
| 538 | updateHighlight(); |
| 539 | }); |
| 540 | button.addEventListener("blur", () => { |
| 541 | isFocused = false; |
| 542 | updateHighlight(); |
| 543 | }); |
| 544 | |
| 545 | if (hasChildren) { |
| 546 | const openSubmenu = (focusFirstItem = false) => { |
| 547 | const rect = button.getBoundingClientRect(); |
| 548 | const nextLeft = rect.right + 4; |
| 549 | const nextTop = rect.top; |
| 550 | openMenu(item.children!, nextLeft, nextTop, level + 1, button); |
| 551 | button.setAttribute("aria-expanded", "true"); |
| 552 | |
| 553 | const childMenu = menuStack[level + 1]; |
| 554 | if (!childMenu) { |
| 555 | return; |
| 556 | } |
| 557 | if (focusFirstItem) { |
| 558 | [...childMenu.querySelectorAll<HTMLButtonElement>("button")] |
| 559 | .find((childButton) => !childButton.disabled) |
| 560 | ?.focus(); |
| 561 | } |
| 562 | }; |
| 563 | button.addEventListener("mouseenter", () => { |
| 564 | openSubmenu(); |
| 565 | }); |
| 566 | button.addEventListener("click", (event) => { |
| 567 | event.preventDefault(); |
| 568 | openSubmenu(true); |
| 569 | }); |
| 570 | } else { |
| 571 | button.addEventListener("mouseenter", () => { |
| 572 | closeMenusFromLevel(level + 1); |
| 573 | }); |
| 574 | button.addEventListener("click", () => { |
| 575 | if (canDismissFromPointer) cleanup(item.id); |
| 576 | }); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | inner.appendChild(button); |
| 581 | } |
| 582 | |
| 583 | menu.appendChild(inner); |
| 584 | |
| 585 | menu.addEventListener("mouseenter", () => { |
| 586 | closeMenusFromLevel(level + 1); |
| 587 | }); |
| 588 | // Like a native menu, take thumb Back/Forward presses instead of letting Chromium |
| 589 | // run the app router's history underneath. |
| 590 | const swallowThumbButton = (event: MouseEvent) => { |
| 591 | if (event.button === 3 || event.button === 4) event.preventDefault(); |
| 592 | }; |
| 593 | menu.addEventListener("mousedown", swallowThumbButton); |
| 594 | menu.addEventListener("mouseup", swallowThumbButton); |
| 595 | |
| 596 | document.body.appendChild(menu); |
| 597 | menuStack[level] = menu; |
| 598 | submenuTriggerStack[level] = parentTrigger; |
| 599 | |
| 600 | requestAnimationFrame(() => { |
| 601 | if (!parentTrigger) { |
| 602 | clampMenuPosition(menu, preferredLeft, preferredTop); |
| 603 | return; |
| 604 | } |
| 605 | // A fixed menu near the right edge shrinks to the space left; measure its real width. |
| 606 | menu.style.width = "max-content"; |
| 607 | menu.style.maxWidth = "min(24rem, calc(100vw - 0.5rem))"; |
| 608 | const { width } = menu.getBoundingClientRect(); |
| 609 | const roomLeft = parentTrigger.getBoundingClientRect().left - 4; |
| 610 | const roomRight = window.innerWidth - preferredLeft; |
| 611 | const flipLeft = width > roomRight && roomLeft > roomRight; |
| 612 | clampMenuPosition(menu, flipLeft ? roomLeft - width : preferredLeft, preferredTop); |
| 613 | }); |
| 614 | }; |
| 615 | |
| 616 | document.addEventListener("keydown", onKeyDown); |
| 617 | document.addEventListener("pointerdown", onPointerDown, true); |
| 618 | document.addEventListener("contextmenu", onContextMenu, true); |
| 619 | openMenu(items, position?.x ?? 0, position?.y ?? 0, 0); |
| 620 | activeContextMenuDismiss = dismiss; |
| 621 | // A keyboard-opened menu starts on its first action, a pointer-opened one on the menu itself. |
| 622 | // Text fields always match :focus-visible, so they say nothing about how the menu opened. |
| 623 | const firstAction = |
| 624 | previouslyFocusedElement?.matches(":focus-visible") && |
| 625 | !previouslyFocusedElement.matches('input, textarea, select, [contenteditable="true"]') |
| 626 | ? menuStack[0]?.querySelector<HTMLButtonElement>("button:not(:disabled)") |
| 627 | : null; |
| 628 | (firstAction ?? menuStack[0])?.focus({ preventScroll: true }); |
| 629 | document.addEventListener("focusin", onFocusIn); |
| 630 | window.addEventListener("blur", onWindowBlur); |
| 631 | |
| 632 | requestAnimationFrame(() => { |
| 633 | canDismissFromPointer = true; |
| 634 | }); |
| 635 | }); |
| 636 | } |