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

This commit is contained in:
Linus Rath
2026-11-04 21:00:55 +01:00
parent a948e7fc26
commit 356cbbb8d1
2 changed files with 44 additions and 4 deletions
@@ -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(
<Profiler id="row" onRender={(_id, phase) => commits.push(phase)}>
<ContextMenu isOpen={false} position={{ x: 0, y: 0 }} onClose={() => {}}>
<ContextMenuItem label="Reply" onClick={() => {}} />
</ContextMenu>
</Profiler>,
);
expect(commits).toEqual(['mount']);
});
it('still opens on its first render once it is asked to', () => {
function Host({ open }: { open: boolean }) {
return (
<ContextMenu isOpen={open} position={{ x: 10, y: 10 }} onClose={() => {}}>
<ContextMenuItem label="Reply" onClick={() => {}} />
</ContextMenu>
);
}
const { rerender } = render(<Host open={false} />);
expect(screen.queryByRole('menu')).toBeNull();
rerender(<Host open />);
expect(screen.getByRole('menuitem', { name: 'Reply' })).toBeTruthy();
fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' });
});
});
+8 -4
View File
@@ -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<HTMLDivElement, ContextMenuProps>(
({ 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<HTMLDivElement | null>(null);
const onCloseRef = useRef(onClose);
@@ -287,9 +294,6 @@ export const ContextMenu = forwardRef<HTMLDivElement, ContextMenuProps>(
// 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 ?? ""}` : "";