Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(menu): open a submenu leftwards when the right edge would clip it

enes8697fb027dc0
diff --git a/src/renderer/components/ContextMenu.submenu-side.test.tsx b/src/renderer/components/ContextMenu.submenu-side.test.tsx
new file mode 100644
index 000000000..55b9aa98f
--- /dev/null
+++ b/src/renderer/components/ContextMenu.submenu-side.test.tsx
@@ -0,0 +1,74 @@
+// @vitest-environment jsdom
+//
+// MEASURES that the flyout actually changes side, rather than asserting the hook was called.
+//
+// `useSubmenuFlip` reads the flyout's `offsetParent` and `offsetWidth`, and jsdom reports 0 for
+// both — so a test that renders the menu and expects a flip would pass or fail for reasons that
+// have nothing to do with the decision. Both are stubbed here with the geometry of the reported
+// case (a menu opened near the right edge of the window), which is the smallest thing that makes
+// the render path real: the hook runs in a layout effect, reads the ROW's rect, and stamps
+// `data-side` that the stylesheet anchors on.
+import { act } from 'react'
+import { createRoot } from 'react-dom/client'
+import { afterEach, describe, expect, it } from 'vitest'
+import { ContextMenu, type MenuItem } from './ContextMenu'
+
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+class NoopResizeObserver {
+ observe(): void {}
+ unobserve(): void {}
+ disconnect(): void {}
+}
+;(globalThis as { ResizeObserver?: unknown }).ResizeObserver ??= NoopResizeObserver
+
+const noop = () => {}
+const items: MenuItem[] = [
+ { type: 'submenu', label: 'New agent', children: [{ label: 'Claude', onClick: noop }] }
+]
+
+/** Place the submenu ROW at `rowLeft` in a 1000px-wide window and give the flyout `width`. */
+function renderAt(rowLeft: number, width: number): HTMLElement {
+ window.innerWidth = 1000
+ const host = document.createElement('div')
+ document.body.appendChild(host)
+ // The flyout's offsetParent IS its row (the row is `position: relative`), which is what the
+ // hook measures — its own rect already carries the side being decided.
+ Object.defineProperty(HTMLElement.prototype, 'offsetParent', {
+ configurable: true,
+ get(this: HTMLElement) {
+ return this.classList.contains('ctx-submenu') ? this.parentElement : null
+ }
+ })
+ Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
+ configurable: true,
+ get(this: HTMLElement) {
+ return this.classList.contains('ctx-submenu') ? width : 0
+ }
+ })
+ act(() => {
+ createRoot(host).render(<ContextMenu x={rowLeft} y={0} items={items} onClose={noop} />)
+ })
+ const row = document.querySelector('.ctx-item--submenu') as HTMLElement
+ row.getBoundingClientRect = () =>
+ ({ left: rowLeft, right: rowLeft + 200, top: 0, bottom: 30, width: 200, height: 30 }) as DOMRect
+ act(() => {
+ row.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }))
+ })
+ return document.querySelector('.ctx-submenu') as HTMLElement
+}
+
+afterEach(() => {
+ document.body.innerHTML = ''
+})
+
+describe('ContextMenu submenu side', () => {
+ it('opens to the right when there is room — the unchanged case', () => {
+ expect(renderAt(100, 240).getAttribute('data-side')).toBe('right')
+ })
+
+ it('opens to the LEFT near the right edge, instead of off-screen', () => {
+ // Row ends at 900; a 240px flyout would reach 1136 in a 1000px window.
+ expect(renderAt(700, 240).getAttribute('data-side')).toBe('left')
+ })
+})
diff --git a/src/renderer/components/ContextMenu.tsx b/src/renderer/components/ContextMenu.tsx
index ce05a8e85..a02b08e3d 100644
--- a/src/renderer/components/ContextMenu.tsx
+++ b/src/renderer/components/ContextMenu.tsx
@@ -2,6 +2,7 @@ import { useState, type ReactNode } from 'react'
import { createPortal } from 'react-dom'
import { NodeColorSwatches } from './NodeColorSwatches'
import { useMenuFlip } from '../ui/useMenuFlip'
+import { useSubmenuFlip } from '../ui/useSubmenuFlip'
export type MenuItem =
| {
@@ -99,7 +100,7 @@ export function ContextMenu({ x, y, items, onClose, zIndex, scroll }: ContextMen
<span className="ctx-icon">{item.icon}</span>
{item.label}
{openSub === i && (
- <div className="ctx-menu ctx-submenu" onClick={(e) => e.stopPropagation()}>
+ <SubmenuFlyout>
{item.children.map((child, j) => {
if (child.type === 'separator') return <div key={j} className="ctx-sep" />
if (child.type === 'label')
@@ -121,7 +122,7 @@ export function ContextMenu({ x, y, items, onClose, zIndex, scroll }: ContextMen
</button>
)
})}
- </div>
+ </SubmenuFlyout>
)}
</div>
)
@@ -147,3 +148,26 @@ export function ContextMenu({ x, y, items, onClose, zIndex, scroll }: ContextMen
document.body
)
}
+
+/**
+ * One submenu flyout, measured so it opens AWAY from the viewport edge.
+ *
+ * Its own component because the measurement is per-flyout state (a ref plus a side) and only the
+ * open one exists at a time: mounting it with the row keeps the hook's lifetime exactly the
+ * flyout's, so a menu re-opened near the other edge measures afresh instead of inheriting the
+ * last decision. `data-side` is what the stylesheet anchors on; the default stays `right`, so a
+ * flyout with room renders exactly as it always did.
+ */
+function SubmenuFlyout({ children }: { children: ReactNode }): JSX.Element {
+ const { ref, side } = useSubmenuFlip()
+ return (
+ <div
+ ref={ref}
+ className="ctx-menu ctx-submenu"
+ data-side={side}
+ onClick={(e) => e.stopPropagation()}
+ >
+ {children}
+ </div>
+ )
+}
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index cd35c5241..e16fe8a9b 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -3531,6 +3531,10 @@ body,
}
.ctx-submenu {
position: absolute;
+ /* Default side. `useSubmenuFlip` measures the open flyout and stamps `data-side` — the rule
+ below opens it leftwards when the row sits too close to the viewport's right edge. The
+ 4px is the overlap that keeps the cursor on a hovered surface the whole way across
+ (SUBMENU_OVERLAP_PX). */
left: calc(100% - 4px);
top: -6px;
/* Absolutely positioned inside a ~menu-width row, shrink-to-fit would clamp the flyout to
@@ -3540,6 +3544,10 @@ body,
max-height: min(70vh, 520px);
overflow-y: auto;
}
+.ctx-submenu[data-side='left'] {
+ left: auto;
+ right: calc(100% - 4px);
+}
.ctx-submenu .ctx-item {
/* Keep the label and its default ✓ on one line (labels ellipsize past max-width). */
white-space: nowrap;
diff --git a/src/renderer/ui/useSubmenuFlip.test.ts b/src/renderer/ui/useSubmenuFlip.test.ts
new file mode 100644
index 000000000..fc2104256
--- /dev/null
+++ b/src/renderer/ui/useSubmenuFlip.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from 'vitest'
+import { submenuSide, SUBMENU_OVERLAP_PX } from './useSubmenuFlip'
+
+/** A 200px-wide submenu row, placed by its left edge in a 1000px viewport. */
+const row = (left: number): { rowLeft: number; rowRight: number } => ({
+ rowLeft: left,
+ rowRight: left + 200
+})
+const W = 1000
+
+describe('submenuSide', () => {
+ it('opens to the right when the flyout fits there', () => {
+ expect(submenuSide({ ...row(100), width: 240, viewportWidth: W })).toBe('right')
+ })
+
+ it('opens to the LEFT when the right edge would clip it', () => {
+ // Row ends at 900; a 240px flyout would reach 1136 in a 1000px viewport.
+ expect(submenuSide({ ...row(700), width: 240, viewportWidth: W })).toBe('left')
+ })
+
+ it('counts the overlap, so a flyout that fits only because of it stays right', () => {
+ // Right edge lands exactly on the margin: 800 - 4 + 190 = 986 = 1000 - 6 - 8… one px either
+ // way is what this pins, hence the exact arithmetic rather than a round number.
+ const width = W - 6 - (900 - SUBMENU_OVERLAP_PX)
+ expect(submenuSide({ ...row(700), width, viewportWidth: W })).toBe('right')
+ expect(submenuSide({ ...row(700), width: width + 1, viewportWidth: W })).toBe('left')
+ })
+
+ it('takes the roomier side when neither fits, rather than refusing', () => {
+ // Row hugs the right edge: left has 700px of room, right has ~100.
+ expect(submenuSide({ ...row(700), width: 900, viewportWidth: W })).toBe('left')
+ // Row hugs the left edge: the roomier side is now the right one.
+ expect(submenuSide({ ...row(0), width: 900, viewportWidth: W })).toBe('right')
+ })
+
+ it('never opens left into the viewport edge', () => {
+ // A row at the very left: opening left would start at a negative x, so right wins.
+ expect(submenuSide({ ...row(10), width: 240, viewportWidth: W })).toBe('right')
+ })
+})
diff --git a/src/renderer/ui/useSubmenuFlip.ts b/src/renderer/ui/useSubmenuFlip.ts
new file mode 100644
index 000000000..70132fd79
--- /dev/null
+++ b/src/renderer/ui/useSubmenuFlip.ts
@@ -0,0 +1,76 @@
+import { useLayoutEffect, useRef, useState, type RefObject } from 'react'
+
+/** Which side of its row a submenu flyout opens towards. */
+export type SubmenuSide = 'right' | 'left'
+
+/** How far the flyout overlaps its row, so the cursor crosses no gap (the `- 4px` in the CSS). */
+export const SUBMENU_OVERLAP_PX = 4
+/** Viewport margin, the same one `useMenuFlip` keeps. */
+const M = 6
+
+/**
+ * Which side a submenu should open towards, given where its row sits and how wide the flyout is.
+ *
+ * Native menus (and the root menu here, via `useMenuFlip`) never let a menu run off screen; the
+ * submenu flyout was the one surface that did — it was pinned to `left: calc(100% - 4px)` with no
+ * measurement at all, so a context menu opened near the right edge of the window pushed its
+ * flyout off-screen entirely. A submenu cannot use `useMenuFlip`'s clamp because it is positioned
+ * against its ROW, not the viewport: the fix is a side, not an offset.
+ *
+ * When neither side fits (a narrow window, a wide flyout) the roomier side wins rather than a
+ * refusal — the flyout's own `max-width` then bounds it, so part of it stays reachable either way.
+ * Pure so the decision is testable without a DOM.
+ */
+export function submenuSide(input: {
+ /** The submenu ROW's viewport rect edges. */
+ rowLeft: number
+ rowRight: number
+ /** Measured flyout width. */
+ width: number
+ viewportWidth: number
+ margin?: number
+}): SubmenuSide {
+ const margin = input.margin ?? M
+ const rightEdge = input.rowRight - SUBMENU_OVERLAP_PX + input.width
+ if (rightEdge <= input.viewportWidth - margin) return 'right'
+ const leftEdge = input.rowLeft + SUBMENU_OVERLAP_PX - input.width
+ if (leftEdge >= margin) return 'left'
+ const roomRight = input.viewportWidth - margin - (input.rowRight - SUBMENU_OVERLAP_PX)
+ const roomLeft = input.rowLeft + SUBMENU_OVERLAP_PX - margin
+ return roomLeft > roomRight ? 'left' : 'right'
+}
+
+/**
+ * Measure an open submenu flyout and answer which side it should open towards.
+ *
+ * Measured after render but BEFORE paint (`useLayoutEffect`), so a flyout never appears on the
+ * wrong side first and jumps. Re-measured on size changes for the same reason `useMenuFlip` is: a
+ * flyout that grows must be able to change its mind.
+ *
+ * The rect read is the flyout's OFFSET PARENT — the submenu row — because the flyout's own rect
+ * already carries the side we are deciding.
+ */
+export function useSubmenuFlip(): { ref: RefObject<HTMLDivElement>; side: SubmenuSide } {
+ const ref = useRef<HTMLDivElement>(null)
+ const [side, setSide] = useState<SubmenuSide>('right')
+ useLayoutEffect(() => {
+ const el = ref.current
+ const row = el?.offsetParent as HTMLElement | null
+ if (!el || !row) return
+ const measure = (): void => {
+ const r = row.getBoundingClientRect()
+ const next = submenuSide({
+ rowLeft: r.left,
+ rowRight: r.right,
+ width: el.offsetWidth,
+ viewportWidth: window.innerWidth
+ })
+ setSide((cur) => (cur === next ? cur : next))
+ }
+ measure()
+ const ro = new ResizeObserver(measure)
+ ro.observe(el)
+ return () => ro.disconnect()
+ }, [])
+ return { ref, side }
+}