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(ui): tab caret menu flips at the viewport edges too (shared useMenuFlip)

enes5ae5ecbd0085
diff --git a/src/renderer/components/ContextMenu.tsx b/src/renderer/components/ContextMenu.tsx
index bfd8bd55c..5c8424142 100644
--- a/src/renderer/components/ContextMenu.tsx
+++ b/src/renderer/components/ContextMenu.tsx
@@ -1,6 +1,7 @@
-import { useLayoutEffect, useRef, useState, type ReactNode } from 'react'
+import { useState, type ReactNode } from 'react'
import { createPortal } from 'react-dom'
import { NODE_COLORS } from '../state/workspace'
+import { useMenuFlip } from '../ui/useMenuFlip'
export type MenuItem =
| {
@@ -49,26 +50,13 @@ export function ContextMenu({ x, y, items, onClose, zIndex, scroll }: ContextMen
// Keep the menu one above its backdrop (matches the default 46/45 CSS ordering).
const backdropStyle = zIndex != null ? { zIndex } : undefined
// Flip away from the viewport edges: a right-click near the bottom (or right) edge used to
- // open the menu DOWNWARD off-screen, cutting the tail rows off. Measured after render but
- // BEFORE paint (useLayoutEffect — no visible jump): if the menu would overflow below, it
- // opens upward from the cursor (native-menu behavior); same flip horizontally. The margin
- // clamp is the last resort for a menu taller than the viewport itself.
- const menuRef = useRef<HTMLDivElement>(null)
- const [pos, setPos] = useState({ top: y, left: x })
- useLayoutEffect(() => {
- const el = menuRef.current
- if (!el) return
- const M = 6
- const w = el.offsetWidth
- const h = el.offsetHeight
- const top = y + h > window.innerHeight - M ? Math.max(M, y - h) : y
- const left = x + w > window.innerWidth - M ? Math.max(M, x - w) : x
- // Identity-guarded: `items` often gets a fresh array identity per parent render, and an
- // unconditional setPos would re-render forever.
- setPos((p) => (p.top === top && p.left === left ? p : { top, left }))
- }, [x, y, items])
+ // open the menu DOWNWARD off-screen, cutting the tail rows off. See useMenuFlip.
+ const flip = useMenuFlip(y, x)
+ const menuRef = flip.ref
const menuStyle =
- zIndex != null ? { top: pos.top, left: pos.left, zIndex: zIndex + 1 } : { top: pos.top, left: pos.left }
+ zIndex != null
+ ? { top: flip.top, left: flip.left, zIndex: zIndex + 1 }
+ : { top: flip.top, left: flip.left }
// Index of the row whose submenu flyout is currently open (hover-driven).
const [openSub, setOpenSub] = useState<number | null>(null)
return createPortal(
diff --git a/src/renderer/components/TabBar.tsx b/src/renderer/components/TabBar.tsx
index f3087e75e..f2f204b6d 100644
--- a/src/renderer/components/TabBar.tsx
+++ b/src/renderer/components/TabBar.tsx
@@ -10,6 +10,7 @@ import { sshAutoModeHint } from '../state/permissionMode'
import { useSystemAccount } from '../state/systemAccount'
import { sessionCount, sessionForProject, useProjectSession } from '../session/session'
import { tabClickAction } from '../session/relay-tab'
+import { useMenuFlip } from '../ui/useMenuFlip'
import { IconCanvasView, IconKanban } from './icons'
import {
ALL_PERMISSION_MODES,
@@ -89,7 +90,11 @@ export function TabBar({
})
const unreadSet = useMemo(() => new Set(unreadIds.split('|').filter(Boolean)), [unreadIds])
const [menuId, setMenuId] = useState<string | null>(null)
- const [menuPos, setMenuPos] = useState<{ top: number; left: number } | null>(null)
+ // `flipBase` is the ANCHOR's top edge: when the menu would overflow the bottom of the window,
+ // it opens upward from the caret button instead (see useMenuFlip below).
+ const [menuPos, setMenuPos] = useState<{ top: number; left: number; flipBase: number } | null>(
+ null
+ )
const [editingId, setEditingId] = useState<string | null>(null)
const [draft, setDraft] = useState('')
// Tab drag-reorder: the project id being dragged + the current drop target ('' = end zone).
@@ -146,9 +151,15 @@ export function TabBar({
const openMenu = (id: string, anchor: HTMLElement) => {
const r = anchor.getBoundingClientRect()
setMenuId(id)
- setMenuPos({ top: r.bottom + 4, left: r.left })
+ setMenuPos({ top: r.bottom + 4, left: r.left, flipBase: r.top - 4 })
}
+ // Viewport-edge flip for the caret menu — same behavior as the right-click ContextMenu. The
+ // hook runs unconditionally (menuPos may be null while closed; the ref is simply unattached
+ // then) and re-measures on size changes, so EXPANDING the account/permission sub-lists near
+ // the bottom edge lifts the menu instead of growing it off-screen.
+ const menuFlip = useMenuFlip(menuPos?.top ?? 0, menuPos?.left ?? 0, menuPos?.flipBase)
+
const startRename = (id: string, current: string) => {
setEditingId(id)
setDraft(current)
@@ -352,8 +363,9 @@ export function TabBar({
menuProject &&
createPortal(
<div
+ ref={menuFlip.ref}
className="tab-menu"
- style={{ top: menuPos.top, left: menuPos.left }}
+ style={{ top: menuFlip.top, left: menuFlip.left }}
onClick={(e) => e.stopPropagation()}
>
<button onClick={() => startRename(menuProject.id, menuProject.name)}>Rename</button>
diff --git a/src/renderer/ui/useMenuFlip.ts b/src/renderer/ui/useMenuFlip.ts
new file mode 100644
index 000000000..5c4a8e71c
--- /dev/null
+++ b/src/renderer/ui/useMenuFlip.ts
@@ -0,0 +1,41 @@
+import { useLayoutEffect, useRef, useState, type RefObject } from 'react'
+
+/**
+ * Viewport-edge flip for fixed-position menus (context menus, the tab caret menu): measured
+ * after render but BEFORE paint (useLayoutEffect — no visible jump), and re-measured on size
+ * changes (ResizeObserver — the tab menu GROWS while open when its account/permission-mode
+ * sub-lists expand, and a grown menu near the bottom edge must lift itself).
+ *
+ * - Vertically: if the menu would overflow below, its BOTTOM is anchored at `flipBase`
+ * (default = `top`; a dropdown passes its anchor's TOP edge so the flip opens above the
+ * anchor, not above the dropdown gap). The margin clamp is the last resort for a menu
+ * taller than the viewport.
+ * - Horizontally: clamped so the right edge stays inside the viewport (native-menu behavior
+ * for both cursor menus and anchored dropdowns).
+ */
+export function useMenuFlip(
+ top: number,
+ left: number,
+ flipBase?: number
+): { ref: RefObject<HTMLDivElement>; top: number; left: number } {
+ const ref = useRef<HTMLDivElement>(null)
+ const [pos, setPos] = useState({ top, left })
+ useLayoutEffect(() => {
+ const el = ref.current
+ if (!el) return
+ const M = 6
+ const measure = (): void => {
+ const w = el.offsetWidth
+ const h = el.offsetHeight
+ const t = top + h > window.innerHeight - M ? Math.max(M, (flipBase ?? top) - h) : top
+ const l = Math.max(M, Math.min(left, window.innerWidth - w - M))
+ // Identity-guarded so a parent re-render (or an RO tick with no real change) can't loop.
+ setPos((p) => (p.top === t && p.left === l ? p : { top: t, left: l }))
+ }
+ measure()
+ const ro = new ResizeObserver(measure)
+ ro.observe(el)
+ return () => ro.disconnect()
+ }, [top, left, flipBase])
+ return { ref, top: pos.top, left: pos.left }
+}