From 356cbbb8d136707b9b8ebaf59c8582663b7b7e82 Mon Sep 17 00:00:00 2001 From: Linus Rath <139418639+rathlinus@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:00:55 +0100 Subject: [PATCH] fix: mount a closed menu without a render pass of its own, so opening or searching mail costs no extra passes for the row's tag menu --- .../context-menu-render-passes.test.tsx | 36 +++++++++++++++++++ components/ui/context-menu.tsx | 12 ++++--- 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 components/ui/__tests__/context-menu-render-passes.test.tsx 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 ?? ""}` : "";