Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(sidebar): portal the submenu out of the scrollable menu
liuzixiangceafc7407924
diff --git a/apps/desktop/src/components/SidebarMenuPanel.tsx b/apps/desktop/src/components/SidebarMenuPanel.tsxindex a6ba5cfc0..7e6823544 100644--- a/apps/desktop/src/components/SidebarMenuPanel.tsx+++ b/apps/desktop/src/components/SidebarMenuPanel.tsx@@ -3,8 +3,11 @@** Submenus open on mouse hover and on click (including touch); keyboard users* open them with ArrowRight/Enter and return with ArrowLeft/Escape. The child- * panel is viewport-positioned beside its trigger so the root menu's scroll- * area cannot clip it.+ * panel is portaled to the body and viewport-positioned beside its trigger: as+ * a DOM child of the root menu it would sit inside a scrollable fixed box, and+ * any ancestor-side effect that creates a containing block (a theme's+ * `backdrop-filter`, a `transform`, `contain`) would offset it by the menu's+ * own position and stretch the menu's scroll area (#1896).*/import {Fragment,@@ -17,6 +20,7 @@ import {type RefObject,} from "react";import { IconChevronRight } from "./icons";+import { portalToBody } from "../lib/portal-visibility";import type {SidebarMenuActionItem,SidebarMenuGroupItem,@@ -113,12 +117,21 @@ export function SidebarMenuPanel({};reposition();- if (typeof ResizeObserver === "undefined" || !submenuRef.current) return;- const observer = new ResizeObserver(reposition);- observer.observe(submenuRef.current);+ const root = rootMenuRef.current;+ const submenu = submenuRef.current;+ // The trigger moves with the menu's own scroll and with async content+ // growth, so the submenu follows both instead of only its own size.+ root?.addEventListener("scroll", reposition, { passive: true });window.addEventListener("resize", reposition);+ let observer: ResizeObserver | undefined;+ if (typeof ResizeObserver !== "undefined") {+ observer = new ResizeObserver(reposition);+ if (submenu) observer.observe(submenu);+ if (root) observer.observe(root);+ }return () => {- observer.disconnect();+ observer?.disconnect();+ root?.removeEventListener("scroll", reposition);window.removeEventListener("resize", reposition);};}, [openSubmenuId, rootMenuRef]);@@ -133,6 +146,20 @@ export function SidebarMenuPanel({return () => cancelAnimationFrame(frame);}, [openSubmenuId, submenuPosition]);+ const handleSubmenuPointerLeave = (event: ReactPointerEvent<HTMLDivElement>) => {+ if (event.pointerType !== "mouse") return;+ const related = event.relatedTarget;+ // Crossing back into the root menu keeps the child open; the row the+ // pointer lands on closes it if that row is not the submenu trigger.+ if (+ related instanceof Node &&+ (submenuRef.current?.contains(related) || rootMenuRef.current?.contains(related))+ ) {+ return;+ }+ closeSubmenu(false);+ };+const handleKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {const target = event.target instanceof Element ? event.target : null;const activeMenu = target?.closest<HTMLElement>('[role="menu"]') ?? rootMenuRef.current;@@ -308,7 +335,15 @@ export function SidebarMenuPanel({onPointerLeave={(event) => {if (event.pointerType !== "mouse") return;const related = event.relatedTarget;- if (related instanceof Node && event.currentTarget.contains(related)) return;+ // The submenu lives in its own portal, so its rows are not DOM+ // descendants here. React's synthetic leave follows the React tree+ // through portals, so this mostly guards native leave sources.+ if (+ related instanceof Node &&+ (event.currentTarget.contains(related) || submenuRef.current?.contains(related))+ ) {+ return;+ }closeSubmenu(false);}}style={{ top: position.top, left: position.left }}@@ -321,29 +356,32 @@ export function SidebarMenuPanel({<p className="sidebar-menu-note" data-sidebar-menu-note={note.id}>{note.text}</p></Fragment>))}- {openSubmenuItem ? (- <div- ref={submenuRef}- id={`sidebar-submenu-${openSubmenuItem.id}`}- className="sidebar-row-menu sidebar-submenu"- role="menu"- aria-label={openSubmenuItem.label}- style={submenuPosition- ? { top: submenuPosition.top, left: submenuPosition.left }- : { top: -10000, left: -10000, visibility: "hidden" }}- >- {openSubmenuItem.items.map((item) => renderAction(item, {- keyPrefix: `${openSubmenuItem.id}-`,- inSubmenu: true,- }))}- {openSubmenuItem.notes?.map((note) => (- <Fragment key={note.id}>- {note.separatorBefore ? <div className="sidebar-menu-separator" role="separator" /> : null}- <p className="sidebar-menu-note" data-sidebar-menu-note={note.id}>{note.text}</p>- </Fragment>- ))}- </div>- ) : null}+ {openSubmenuItem+ ? portalToBody(+ <div+ ref={submenuRef}+ id={`sidebar-submenu-${openSubmenuItem.id}`}+ className="sidebar-row-menu sidebar-submenu"+ role="menu"+ aria-label={openSubmenuItem.label}+ onPointerLeave={handleSubmenuPointerLeave}+ style={submenuPosition+ ? { top: submenuPosition.top, left: submenuPosition.left }+ : { top: -10000, left: -10000, visibility: "hidden" }}+ >+ {openSubmenuItem.items.map((item) => renderAction(item, {+ keyPrefix: `${openSubmenuItem.id}-`,+ inSubmenu: true,+ }))}+ {openSubmenuItem.notes?.map((note) => (+ <Fragment key={note.id}>+ {note.separatorBefore ? <div className="sidebar-menu-separator" role="separator" /> : null}+ <p className="sidebar-menu-note" data-sidebar-menu-note={note.id}>{note.text}</p>+ </Fragment>+ ))}+ </div>,+ )+ : null}</div>);}diff --git a/apps/desktop/test/sidebar-menu-panel.test.mjs b/apps/desktop/test/sidebar-menu-panel.test.mjsindex 7e1c473e0..6bef2f606 100644--- a/apps/desktop/test/sidebar-menu-panel.test.mjs+++ b/apps/desktop/test/sidebar-menu-panel.test.mjs@@ -67,6 +67,41 @@ test("submenu flips left and clamps against viewport edges", () => {);});+test("the submenu is portaled out of the scrollable root menu (#1896)", () => {+ // As a DOM child of the fixed, scrollable root menu the child panel is only+ // viewport-anchored while nothing on the menu creates a containing block: a+ // theme's backdrop-filter (or a transform/contain) offsets it by the menu's+ // own position and stretches the menu's scroll area. Portaling it to the+ // body removes that dependency for good.+ assert.match(panelSource, /openSubmenuItem\s*\n?\s*\?\s*portalToBody\(/);+ assert.match(panelSource, /import \{ portalToBody \} from "\.\.\/lib\/portal-visibility"/);+ const portalCall = panelSource.slice(+ panelSource.indexOf("portalToBody("),+ panelSource.indexOf("className=\"sidebar-row-menu sidebar-submenu\""),+ );+ assert.match(portalCall, /ref=\{submenuRef\}/);+});++test("pointer travel between the menu and its portal keeps the submenu open", () => {+ // Both sides must recognize the other's element: the root menu no longer+ // contains the child panel, so moving onto it would otherwise close it.+ assert.match(+ panelSource,+ /event\.currentTarget\.contains\(related\) \|\| submenuRef\.current\?\.contains\(related\)/,+ );+ assert.match(panelSource, /onPointerLeave=\{handleSubmenuPointerLeave\}/);+ assert.match(+ panelSource,+ /submenuRef\.current\?\.contains\(related\) \|\| rootMenuRef\.current\?\.contains\(related\)/,+ );+});++test("the child panel follows its trigger through menu scroll and growth", () => {+ assert.match(panelSource, /root\?\.addEventListener\("scroll", reposition/);+ assert.match(panelSource, /observer\.observe\(root\)/);+ assert.match(panelSource, /root\?\.removeEventListener\("scroll", reposition\)/);+});+/*The new menu vocabulary is product copy, so every shipped locale carries it;a missing key would silently fall back to English.diff --git a/apps/desktop/test/worktree-menu-user-path.test.mjs b/apps/desktop/test/worktree-menu-user-path.test.mjsindex 2261303c8..0dedf4f61 100644--- a/apps/desktop/test/worktree-menu-user-path.test.mjs+++ b/apps/desktop/test/worktree-menu-user-path.test.mjs@@ -235,7 +235,31 @@ window.menuProbe = async () => {}));await settle();check(submenuTrigger.getAttribute("aria-expanded") === "true", "hover opens the submenu");- check(submenuHost.querySelector('#sidebar-submenu-git [data-action="fork"]'), "hover shows child actions");+ // The child panel is portaled to the body, so it is a sibling of the host+ // rather than its descendant (#1896).+ check(document.querySelector('#sidebar-submenu-git [data-action="fork"]'), "hover shows child actions");++ // Travel from the menu onto its portaled child: the pointer leaves the root+ // menu's subtree, but it is still inside the menu the user sees (#1896).+ const forkRow = document.querySelector('#sidebar-submenu-git [data-action="fork"]');+ submenuTrigger.dispatchEvent(new PointerEvent("pointerout", {+ bubbles: true,+ pointerType: "mouse",+ relatedTarget: forkRow,+ }));+ await settle();+ check(+ document.querySelector("#sidebar-submenu-git") && submenuTrigger.getAttribute("aria-expanded") === "true",+ "moving onto the portaled submenu keeps it open",+ );+ // Leaving both panels still closes it.+ forkRow.dispatchEvent(new PointerEvent("pointerout", {+ bubbles: true,+ pointerType: "mouse",+ relatedTarget: document.body,+ }));+ await settle();+ check(!document.querySelector("#sidebar-submenu-git"), "leaving both panels closes the child menu");submenuTrigger.focus();submenuTrigger.dispatchEvent(new KeyboardEvent("keydown", {@@ -251,13 +275,13 @@ window.menuProbe = async () => {cancelable: true,}));await settle();- check(!submenuHost.querySelector("#sidebar-submenu-git"), "Escape closes the child menu");+ check(!document.querySelector("#sidebar-submenu-git"), "Escape closes the child menu");check(document.activeElement === submenuTrigger, "closing restores focus to its trigger");submenuTrigger.dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 1 }));await settle();check(submenuTrigger.getAttribute("aria-expanded") === "true", "click opens the submenu without hover");- submenuHost.querySelector('[data-action="reveal"]').click();+ document.querySelector('#sidebar-submenu-git [data-action="reveal"]').click();check(JSON.stringify(selectedSubmenuActions) === JSON.stringify(["reveal"]), "child action executes");flushSync(() => submenuRoot.unmount());submenuHost.remove();