diff --git a/src/client/changes/GitLens.tsx b/src/client/changes/GitLens.tsx index 01574f1e..48c0bf60 100644 --- a/src/client/changes/GitLens.tsx +++ b/src/client/changes/GitLens.tsx @@ -19,6 +19,7 @@ import type { GitLineCounts, GitLogEntry, GitStatusEntry, GitStatusResult, GitWo import { api, SidebarApiError } from '../api.ts' import { builtinFileIcon, builtinFolderIcon } from '../file-icons.tsx' import { usePolling } from '../use-polling.ts' +import { useSubmenuFlip } from '../menu-flip.ts' import { baseName, relativeTo } from '../paths.ts' import { resolveSidebarPath } from '../paths.ts' import { isZh, t } from '../locales.ts' @@ -403,7 +404,17 @@ export function GitLens(props: GitLensProps) { const [graphRefresh, setGraphRefresh] = useState(0) const [changesOpen, setChangesOpen] = useState(true) const [graphOpen, setGraphOpen] = useState(true) - const [moreOpen, setMoreOpen] = useState(false) + /** + * The open More Actions menu: the trigger's viewport corner (null = closed). + * Its repo/worktree rows carry nested submenus, which the primitives anchor + * with static CSS growing RIGHT of the card — past the right edge of this + * docked panel. This menu is button-anchored (no cursor point to publish), + * so open/close keeps the measured corner and hands it to the same flip + * mechanism as the file-tree row menu. + */ + const [moreMenu, setMoreMenu] = useState<{ x: number; y: number } | null>(null) + const moreOpen = moreMenu !== null + useSubmenuFlip(moreMenu) const [branchOpen, setBranchOpen] = useState(false) /** Commit drafts, one per scope: the Commit button asks only for a message * and a staged row, so a draft typed against another project would submit @@ -557,7 +568,7 @@ export function GitLens(props: GitLensProps) { setFileMenu(null) setHistoryMenu(null) setConfirm(null) - setMoreOpen(false) + setMoreMenu(null) setBranchOpen(false) } @@ -1092,9 +1103,15 @@ export function GitLens(props: GitLensProps) { disabled={!canCommit} onClick={commit} /> } label={t('refresh')} disabled={busy || suggesting} onClick={() => { invalidateGitStatus(scope.sessionId); void refresh(false) }} /> - { setMoreOpen(false) }} portal compact align="end" autoFocus + { setMoreMenu(null) }} portal compact align="end" autoFocus anchor={} label={t('gitMoreActions')} - active={moreOpen} onClick={() => { setMoreOpen(open => !open) }} />} + active={moreOpen} onClick={(event) => { + if (moreOpen) { setMoreMenu(null); return } + // The menu hangs off this button, so its corner IS the anchor the + // flip heuristic needs (a keyboard activation has no cursor point). + const rect = event.currentTarget.getBoundingClientRect() + setMoreMenu({ x: rect.right, y: rect.top }) + }} />} items={[ { id: 'pull', label: t('pull'), disabled: busy || suggesting || snapshot?.detached === true }, { id: 'push', label: t('push'), disabled: busy || suggesting || snapshot?.detached === true }, @@ -1108,7 +1125,7 @@ export function GitLens(props: GitLensProps) { }] : []), ]} onSelect={id => { - setMoreOpen(false) + setMoreMenu(null) if (id === 'push') push() else if (id === 'pull') pull() else if (id.startsWith('repo:')) chooseRepo(id.slice(5)) diff --git a/src/client/layout.css b/src/client/layout.css index 3a3e798d..b28fb0ab 100644 --- a/src/client/layout.css +++ b/src/client/layout.css @@ -92,8 +92,8 @@ body[data-dsh-sidebar-dragging] #root [data-dsh-center-col] { * * menu-flip.ts publishes `data-dsh-sidebar-submenu` on — with the * words "down" and/or "left" — for exactly the lifetime of one of OUR - * submenu-bearing menus (FileTree row menu, TabBar tab menu). The tokens - * flip the growth direction; the attribute's absence restores the + * submenu-bearing menus (FileTree row menu, Git panel's More Actions menu). + * The tokens flip the growth direction; the attribute's absence restores the * primitive's own geometry untouched, and host-owned menus are never * affected because they never open while the attribute exists (each side * closes on the other's outside pointerdown). diff --git a/tests/git-view-worktree.spec.tsx b/tests/git-view-worktree.spec.tsx index 37e835e2..93a1eec0 100644 --- a/tests/git-view-worktree.spec.tsx +++ b/tests/git-view-worktree.spec.tsx @@ -19,6 +19,7 @@ import { createSidebarStore } from '../src/client/state.ts' import { api, type GitStatusEntry, type GitStatusResult, type GitWorktree } from '../src/client/api.ts' import type { BetterSidebarService } from '../src/client/service.ts' import { t } from '../src/client/locales.ts' +import { SUBMENU_FLIP_ATTR } from '../src/client/menu-flip.ts' import type { Context } from '../src/context-types.ts' import { setupReactAct } from './test-utils.ts' @@ -493,6 +494,51 @@ describe('GitLens (changes tab, git lens) linked-worktree consistency', () => { }) }) +/** + * Issue #988: the More Actions menu's repo/worktree rows open nested + * submenus. The button-anchored menu used to skip the body attribute the + * file-tree row menu publishes (menu-flip.ts), so layout.css never had the + * tokens it needs to flip those submenus back inside the viewport — in a + * right-docked sidebar they opened past the right edge and were unreachable. + */ +describe('GitLens (changes tab, git lens) More Actions submenu flip (#988)', () => { + it('publishes the flip geometry while the menu is open and clears it on close', async () => { + vi.spyOn(api, 'gitWorktrees').mockResolvedValue(inventories) + vi.spyOn(api, 'gitStatus').mockImplementation(async (_scope, target) => statusFor(target)) + vi.spyOn(api, 'gitBranch').mockImplementation(async (_scope, target) => ({ + current: target === AGENT ? 'agent' : 'main', + names: target === AGENT ? ['agent'] : ['main'], + })) + vi.spyOn(api, 'gitGraph').mockResolvedValue(graphFor(AGENT)) + + const { container, root } = makeRoot() + try { + mountGit(root) + await flushEffects() + // The attribute's lifetime IS the mechanism's scope: closed menu, no + // attribute (layout.css must not touch host-owned menus). + expect(document.body.hasAttribute(SUBMENU_FLIP_ATTR)).toBe(false) + + // The trigger of a right-docked sidebar, above the viewport's midpoint. + // Those coordinates have to produce BOTH flips: left (no right-hand room + // for card + submenu) and down (more room below the top-edge row menu). + const trigger = container.querySelector(`button[aria-label="${t('gitMoreActions')}"]`) + if (trigger === null) throw new Error('More Actions button not found') + vi.spyOn(trigger, 'getBoundingClientRect').mockReturnValue(new DOMRect(976, 100, 24, 24)) + + expect(openMoreSubmenu(container, t('worktree')).length).toBeGreaterThan(0) + expect(document.body.getAttribute(SUBMENU_FLIP_ATTR)).toBe('down left') + + // Closing the menu drops the tokens: the next menu gets its own geometry. + act(() => { trigger.click() }) + expect(document.body.hasAttribute(SUBMENU_FLIP_ATTR)).toBe(false) + } finally { + act(() => { root.unmount() }) + container.remove() + } + }) +}) + describe('GitLens (changes tab, git lens) change tree', () => { /** Mount with one changed-file inventory; every test picks its own entries. */ async function mountTree(