diff --git a/components/ui/__tests__/context-menu-render-passes.test.tsx b/components/ui/__tests__/context-menu-render-passes.test.tsx
new file mode 100644
index 000000000..8390ba7b9
--- /dev/null
+++ b/components/ui/__tests__/context-menu-render-passes.test.tsx
@@ -0,0 +1,36 @@
+import { Profiler } from 'react';
+import { fireEvent, render, screen } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+import { ContextMenu, ContextMenuItem } from '../context-menu';
+
+// A closed menu comes with every message row's hover actions, the rail and
+// the toolbars. Mounting one must not cost a render pass of its own: the perf
+// guard counted two extra passes on every message open for the row's ⋮ menu.
+describe('a closed context menu', () => {
+ it('mounts in the same render pass as what it comes with', () => {
+ const commits: string[] = [];
+ render(
+ commits.push(phase)}>
+ {}}>
+ {}} />
+
+ ,
+ );
+ expect(commits).toEqual(['mount']);
+ });
+
+ it('still opens on its first render once it is asked to', () => {
+ function Host({ open }: { open: boolean }) {
+ return (
+ {}}>
+ {}} />
+
+ );
+ }
+ const { rerender } = render();
+ expect(screen.queryByRole('menu')).toBeNull();
+ rerender();
+ expect(screen.getByRole('menuitem', { name: 'Reply' })).toBeTruthy();
+ fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' });
+ });
+});
diff --git a/components/ui/context-menu.tsx b/components/ui/context-menu.tsx
index ca68b0492..9dbc95a9a 100644
--- a/components/ui/context-menu.tsx
+++ b/components/ui/context-menu.tsx
@@ -101,6 +101,10 @@ export function useContextMenuOpen(): boolean {
return useSyncExternalStore(subscribeContextMenuOpen, isContextMenuOpen, () => false);
}
+const subscribeNothing = () => () => {};
+const clientSnapshot = () => true;
+const serverSnapshot = () => false;
+
// ─── Items of a menu level ─────────────────────────────────────
const ITEM_SELECTOR = [
@@ -270,7 +274,10 @@ function useMenuLevel(recentTouch: () => boolean): MenuLevel {
*/
export const ContextMenu = forwardRef(
({ isOpen, position, onClose, children, label }, ref) => {
- const [mounted, setMounted] = useState(false);
+ // In the browser after hydration: a portal needs document.body. True from
+ // the first render for a menu mounted later, so a closed menu that comes
+ // with a row or a toolbar costs no second render pass.
+ const mounted = useSyncExternalStore(subscribeNothing, clientSnapshot, serverSnapshot);
const [adjustedPosition, setAdjustedPosition] = useState<(Position & { maxHeight?: number }) | null>(null);
const localRef = useRef(null);
const onCloseRef = useRef(onClose);
@@ -287,9 +294,6 @@ export const ContextMenu = forwardRef(
// The desktop app shows this same menu, not a native copy: a native menu
// has no icons, and writes keys with Chromium's names rather than Windows'.
- useEffect(() => {
- setMounted(true);
- }, []);
const anchor = position.anchor;
const anchorKey = anchor ? `${anchor.left},${anchor.top},${anchor.right},${anchor.bottom},${anchor.align ?? ""}` : "";