A local-first multi-agent app development platform, built on PenguinHarness
fix(web): dismiss an anchored menu only for scrolls that moved it (#491)
Yaowei Zhengd00537a70509
diff --git a/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.md b/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.mdnew file mode 100644index 00000000..5925f826--- /dev/null+++ b/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.md@@ -0,0 +1,18 @@+# A Session row's context menu survives a streaming conversation++- **Date:** 2026-08-26+- **Type:** fix+- **Scope:** `web`+- **PR:** [#491](https://github.com/Prism-Shadow/penguin-harness/pull/491)++[中文版](2026-08-26-context-menu-scroll-dismiss.zh.md)++Opening the context menu on a sidebar Session row while a conversation was streaming closed it again immediately, and kept closing it for as long as output kept arriving. A pointer-anchored panel now dismisses only for a scroll that moved the content it was anchored to.++## Details++- The panel listens for `scroll` in the capture phase, because scroll events do not bubble. That reaches every scrolling element in the document, including ones the panel is nowhere near: the message list scrolls itself on every streamed chunk, and each of those scrolls dismissed a menu anchored in the sidebar, which had not moved.+- The dismissal itself is kept. A pointer anchor is a position rather than an element, so once the content under that position moves there is nothing left for the panel to follow. `Dropdown` gained an `anchorOwner` accessor naming the element the anchor was measured from, and dismisses only when the scrolled container contains it — so scrolling the sidebar's own list still closes the menu.+- A page-level scroll targets `document`, which contains every node in the page, so full-page scrolling still dismisses without a case of its own.+- A window resize still dismisses unconditionally: it moves every anchor point on the page.+- A caller that supplies no `anchorOwner` keeps dismissing on any scroll, unchanged.diff --git a/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.zh.md b/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.zh.mdnew file mode 100644index 00000000..22e12941--- /dev/null+++ b/changelog/unreleased/2026-08-26-context-menu-scroll-dismiss.zh.md@@ -0,0 +1,18 @@+# 对话流式输出时会话行的右键菜单不再被清掉++- **Date:** 2026-08-26+- **Type:** fix+- **Scope:** `web`+- **PR:** [#491](https://github.com/Prism-Shadow/penguin-harness/pull/491)++[English](2026-08-26-context-menu-scroll-dismiss.md)++在对话流式输出期间打开侧边栏会话行的右键菜单,菜单会立刻被关掉,并且只要输出还在持续就一直关不住。现在指针锚定的面板只在滚动确实移动了它所锚定的内容时才关闭。++## 细节++- 面板在捕获阶段监听 `scroll`,因为滚动事件不冒泡。代价是它能听到文档里**每一个**滚动容器,包括与该面板毫无关系的:消息列表在每个流式片段到达时都会自行滚动,而这些滚动逐一关掉了锚定在侧边栏、根本没有移动过的菜单。+- 关闭行为本身保留。指针锚点是一个位置而不是一个元素,一旦该位置下方的内容移动,面板就没有可跟随的对象了。`Dropdown` 新增 `anchorOwner` 访问器,用来指明锚点是从哪个元素测得的,只有在发生滚动的容器包含该元素时才关闭——因此滚动侧边栏自己的列表依然会关掉菜单。+- 整页滚动的事件目标是 `document`,它包含页面中的每一个节点,因此整页滚动仍然会关闭菜单,无需为其单开分支。+- 窗口尺寸变化依旧无条件关闭:它会移动页面上的每一个锚点。+- 未提供 `anchorOwner` 的调用方维持原状,任何滚动都会关闭面板。diff --git a/packages/web/src/components/layout/sidebar.tsx b/packages/web/src/components/layout/sidebar.tsxindex ae0f6657..4b1bdcdf 100644--- a/packages/web/src/components/layout/sidebar.tsx+++ b/packages/web/src/components/layout/sidebar.tsx@@ -2788,6 +2788,7 @@ function SessionRow({setOpen={ctx.setOpen}portal={{ direction: "down", align: "left" }}anchorRect={ctx.anchor}+ anchorOwner={ctx.anchorOwner}returnFocus={ctx.returnFocus}className="contents"menuClass="w-36"diff --git a/packages/web/src/components/ui/context-menu.tsx b/packages/web/src/components/ui/context-menu.tsxindex 7a7dd344..e6651c48 100644--- a/packages/web/src/components/ui/context-menu.tsx+++ b/packages/web/src/components/ui/context-menu.tsx@@ -7,8 +7,8 @@* context menu inherits — rather than re-implements — that primitive's dismiss stack* (Escape through the shared esc-layer, outside click, scroll), its focus handling* (opening focuses the first item, Escape hands focus back), and its viewport clamping- * and flip. The only thing the Dropdown cannot know is where the pointer was, which is- * what the anchor this hook produces supplies.+ * and flip. What the Dropdown cannot know is where the pointer was, and which element that+ * point sat in — the anchor and its owner, both produced here.** Native-menu suppression is scoped by construction: `preventDefault` is called inside* the row's own `onContextMenu`, so it only ever runs for events that originated in that@@ -57,6 +57,13 @@ export interface RowContextMenu {setOpen: (v: boolean) => void;/** Viewport box to place the panel against; null while closed. */anchor: AnchorRect | null;+ /**+ * The row the anchor was measured against, for the panel's scroll dismissal: only a+ * scroll that moved this row moved the point the menu hangs off, and the panel hears+ * scrolls from every container on the page. An accessor rather than the node, so the+ * list's frequent re-renders do not re-register those listeners.+ */+ anchorOwner: () => HTMLElement | null;/** Focus target when Escape closes the panel — the row's own button. */returnFocus: () => HTMLElement | null;/**@@ -192,6 +199,7 @@ export function useRowContextMenu(): RowContextMenu {dispatch({ kind: "dismiss", at: Date.now() });},anchor,+ anchorOwner: () => row.current,returnFocus: () => row.current?.querySelector<HTMLElement>("button") ?? null,consumeLongPressClick: () => dispatch({ kind: "click" }).swallow,openAt: setAnchor,diff --git a/packages/web/src/components/ui/dropdown.tsx b/packages/web/src/components/ui/dropdown.tsxindex 3266c056..9b3945e1 100644--- a/packages/web/src/components/ui/dropdown.tsx+++ b/packages/web/src/components/ui/dropdown.tsx@@ -27,14 +27,17 @@* viewport box instead of the container's own, so the menu can hang off the point a* secondary click landed on while everything else — the dismiss stack, focus handling,* edge clamping and flip — stays shared with every other menu. A virtual anchor is a- * position rather than an element, so scrolling **dismisses** such a panel instead of- * re-placing it: the point it was anchored to has moved out from under it, and there is- * no trigger left to follow.+ * position rather than an element, so a scroll that moves the content under that point+ * **dismisses** such a panel instead of re-placing it: what it was anchored to has moved+ * out from under it, and there is no trigger left to follow. Which scrolls those are is+ * decided by `anchorOwner` — the listener is capture-phase and therefore hears every+ * scroller in the document, most of which move nothing (see scrollMovesAnchor).*/import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";import { createPortal } from "react-dom";import type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, ReactNode } from "react";import { isTopEscLayer, popEscLayer, pushEscLayer } from "./modal";+import { scrollMovesAnchor } from "../../lib/context-menu";/** Gap between the trigger and the portaled panel, and the panel's minimum distance from the viewport edge (px). */const PANEL_GAP = 4;@@ -56,6 +59,7 @@ export function Dropdown({className,portal,anchorRect,+ anchorOwner,returnFocus,onEscape,focusOnOpen,@@ -75,9 +79,19 @@ export function Dropdown({/*** Place the panel against this viewport box instead of the container's own — a context* menu opened at the pointer, where the "trigger" is a whole row rather than a button.- * Requires `portal`. While set, scrolling dismisses the panel rather than re-placing it.+ * Requires `portal`. While set, a scroll that moved the anchored content dismisses the+ * panel rather than re-placing it — see `anchorOwner` for which scrolls those are.*/anchorRect?: { top: number; bottom: number; left: number; right: number } | null;+ /**+ * The element the `anchorRect` was measured from. A scroll then dismisses the panel only+ * when this element moved with it, which is what keeps a context menu in the sidebar+ * open while an unrelated container — a streaming message list — scrolls itself.+ * Supplied as an accessor rather than as the node so a caller holding it in a ref can+ * answer from whatever the ref points at when the scroll actually arrives. Omitting it+ * keeps the older, blunter rule: any scroll anywhere dismisses.+ */+ anchorOwner?: () => HTMLElement | null;/*** Element to focus when Escape closes the panel. Defaults to the first button inside* the container, which is the trigger for an ordinary dropdown; a context menu has no@@ -169,6 +183,10 @@ export function Dropdown({setOpenRef.current = setOpen;const onEscapeRef = useRef(onEscape);onEscapeRef.current = onEscape;+ // Read through a ref, one effect further down: the scroll listener asks for the owner at+ // event time, so the accessor's identity never has to reach that effect's dependencies.+ const anchorOwnerRef = useRef(anchorOwner);+ anchorOwnerRef.current = anchorOwner;useEffect(() => {if (!open) return;@@ -260,12 +278,18 @@ export function Dropdown({const onScroll = (e: Event) => {if (panelRef.current?.contains(e.target as Node)) return;// A pointer-anchored panel has nothing left to follow once the content under the- // pointer moves, so it dismisses instead (the usual context-menu behavior).- if (anchorRect) setOpenRef.current(false);- else place();+ // pointer moves, so it dismisses instead (the usual context-menu behavior) — but only+ // for a scroll that actually moved that content (see scrollMovesAnchor).+ if (anchorRect) {+ if (scrollMovesAnchor(e.target as Node | null, anchorOwnerRef.current?.() ?? null))+ setOpenRef.current(false);+ return;+ }+ place();};// A resize moves the same anchor point out from under the panel, so it dismisses for- // the same reason a scroll does.+ // the same reason a scroll does — and unconditionally, with no ownership test: every+ // anchor point on the page moves when the viewport changes size.const onResize = () => {if (anchorRect) setOpenRef.current(false);else place();diff --git a/packages/web/src/lib/context-menu.ts b/packages/web/src/lib/context-menu.tsindex f8ab47ee..fac070c9 100644--- a/packages/web/src/lib/context-menu.ts+++ b/packages/web/src/lib/context-menu.ts@@ -58,6 +58,29 @@ export function contextMenuAnchor(e: ContextMenuEventLike, row: AnchorRect): Ancreturn isPointerContextMenu(e) ? pointerAnchor(e.clientX, e.clientY) : row;}+/**+ * Does a scroll of `target` move the content `owner` sits in — the question a+ * pointer-anchored panel has to answer before dismissing itself?+ *+ * The panel's scroll listener runs in the capture phase, because scroll events do not+ * bubble. The price is that it hears **every** scrolling element in the document, not only+ * the ones the anchor lies inside: a streaming conversation scrolls its message list on+ * every chunk, and taking each of those as a reason to dismiss wiped a context menu opened+ * in the sidebar — a part of the page that had not moved at all. Containment is the whole+ * test, and the page itself needs no case of its own, because a full-page scroll targets+ * `document`, which contains every node in it.+ *+ * A caller that names no owner still dismisses on any scroll, which is what every anchored+ * panel did before this rule existed.+ */+export function scrollMovesAnchor(target: Node | null, owner: Node | null): boolean {+ if (owner === null || target === null) return true;+ // An Element and the Document both answer `contains`; a target that does not is not+ // something this rule can judge, so it dismisses rather than pin the panel to content+ // that may well have moved.+ return typeof target.contains === "function" ? target.contains(owner) : true;+}+/*** Shift+F10 — the platform chord for "open the context menu for the focused element".* Handled explicitly rather than left to the browser: Windows and Linux browsersdiff --git a/packages/web/test/context-menu.test.ts b/packages/web/test/context-menu.test.tsindex 54521647..1c76265c 100644--- a/packages/web/test/context-menu.test.ts+++ b/packages/web/test/context-menu.test.ts@@ -9,7 +9,11 @@* neither hover nor a secondary button, which is exactly why* `design/specs/06-PROTOTYPE.md` requires touch to keep a path of its own.*- * The second half pins **suppression scope** against the source text. Preventing the+ * **Dismissal** is the third: the panel listens for scroll in the capture phase, which+ * reaches every scrolling container in the document, so the rule that closes a menu whose+ * anchor point has moved must not close one anchored somewhere that did not move.+ *+ * The last part pins **suppression scope** against the source text. Preventing the* browser's own menu is correct on the row and wrong everywhere else, and nothing in a* node-only suite (`environment: "node"`, no jsdom) would notice a `preventDefault` that* had crept onto a document-level listener — so the scan asserts there is no global@@ -32,6 +36,7 @@ import {longPressMoved,pointerAnchor,reduceHold,+ scrollMovesAnchor,withinSettleWindow,} from "../src/lib/context-menu";import type { HoldEvent } from "../src/lib/context-menu";@@ -225,6 +230,63 @@ describe("reduceHold", () => {});});+/**+ * Scroll dismissal (`components/ui/dropdown.tsx`). A pointer anchor is a position rather+ * than an element, so once the content under it moves the panel dismisses instead of+ * following — but the listener that decides this is capture-phase, and therefore hears+ * every scroller in the document. The reported failure was a Session row's menu that+ * "kept getting wiped": while a conversation streams, the message list scrolls itself on+ * every chunk, and each of those scrolls was closing a menu opened in the sidebar.+ */+describe("scrollMovesAnchor", () => {+ /** A stand-in for a DOM node, implementing the containment the rule reads off one. */+ interface FakeNode {+ parent: FakeNode | null;+ contains: (n: FakeNode | null) => boolean;+ }+ /** `contains` is inclusive, exactly as the DOM's is: a node contains itself. */+ const node = (parent: FakeNode | null): FakeNode => {+ const self: FakeNode = {+ parent,+ contains: (n) => {+ for (let at = n; at !== null; at = at.parent) if (at === self) return true;+ return false;+ },+ };+ return self;+ };+ // The suite is node-only, so the rule is fed stand-ins rather than real elements; it+ // reads nothing off a node but `contains`, which is what these implement.+ const asNode = (n: FakeNode) => n as unknown as Node;++ // The page as the bug found it: a sidebar that scrolls its Session list, a chat pane+ // that scrolls its messages, and no relation between the two but the document.+ const doc = node(null);+ const sidebarScroller = node(doc);+ const row = node(sidebarScroller);+ const messageList = node(doc);++ it("ignores a scroll in an unrelated container, so streaming output leaves the menu open", () => {+ expect(scrollMovesAnchor(asNode(messageList), asNode(row))).toBe(false);+ });++ it("dismisses when the container holding the anchored row scrolls", () => {+ expect(scrollMovesAnchor(asNode(sidebarScroller), asNode(row))).toBe(true);+ });++ it("dismisses when the anchored row is itself what scrolled", () => {+ expect(scrollMovesAnchor(asNode(row), asNode(row))).toBe(true);+ });++ it("dismisses on a page-level scroll, which targets the document and needs no case of its own", () => {+ expect(scrollMovesAnchor(asNode(doc), asNode(row))).toBe(true);+ });++ it("dismisses on any scroll when the caller names no owner, as anchored panels always did", () => {+ expect(scrollMovesAnchor(asNode(messageList), null)).toBe(true);+ });+});+/*** Every .ts/.tsx under packages/web/src, as [relative path, source] pairs.*@@ -267,6 +329,22 @@ describe("sourceFiles", () => {});});+describe("anchored dismissal wiring", () => {+ it("asks scrollMovesAnchor before dismissing an anchored panel", () => {+ // The rule's own cases are covered above against stand-in nodes. What a node-only suite+ // cannot reach is the component consulting it at all, and that is where this silently+ // breaks: drop the call and every assertion above still passes while the reported bug —+ // a streaming message list wiping a menu opened in the sidebar — comes straight back.+ const dropdown = sourceFiles().find(([path]) => path === "components/ui/dropdown.tsx");+ expect(dropdown).toBeDefined();+ const onScroll = /const onScroll = \(e: Event\) => \{([\s\S]*?)\n {4}\};/.exec(dropdown![1]);+ expect(onScroll, "onScroll should be declared in dropdown.tsx").not.toBeNull();+ expect(onScroll![1]).toContain("scrollMovesAnchor(");+ // The owner is the element the anchorRect was measured from, read at event time.+ expect(onScroll![1]).toContain("anchorOwnerRef.current");+ });+});+describe("native-menu suppression scope", () => {it("registers no global contextmenu listener, so the browser's own menu survives off the row", () => {// Matched on the TARGET, not on the call: suppressing the native menu page-wide is the@@ -314,4 +392,11 @@ describe("native-menu suppression scope", () => {expect(sidebar![1]).toContain("{...ctx.rowProps}");expect(sidebar![1]).toContain("ctx.consumeLongPressClick()");});++ it("names the anchor's owner alongside the anchor, so the scroll rule has one to test", () => {+ // Without the wiring, scrollMovesAnchor is asked about a null owner on every scroll and+ // answers "dismiss" — the rule above would still pass while the bug was back.+ const sidebar = sourceFiles().find(([path]) => path === "components/layout/sidebar.tsx");+ expect(sidebar![1]).toContain("anchorOwner={ctx.anchorOwner}");+ });});