Skip to content

The agent that grows with you

perf(desktop): defer closed action menus until their content mounts

brooklyn!fd304509ec48
diff --git a/apps/desktop/src/components/ui/actions-menu.test.tsx b/apps/desktop/src/components/ui/actions-menu.test.tsx
new file mode 100644
index 00000000000..3cb3ce14588
--- /dev/null
+++ b/apps/desktop/src/components/ui/actions-menu.test.tsx
@@ -0,0 +1,39 @@
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { ActionsContextMenu, ActionsMenu, type MenuKit } from './actions-menu'
+
+afterEach(cleanup)
+
+it.each(['dropdown', 'context'] as const)('defers %s items until opened and reads the latest actions', async kind => {
+ const selected = vi.fn()
+ const items = vi.fn((kit: MenuKit) => <kit.Item onSelect={selected}>Original action</kit.Item>)
+ const nextItems = vi.fn((kit: MenuKit) => <kit.Item onSelect={selected}>Latest action</kit.Item>)
+ const Menu = kind === 'dropdown' ? ActionsMenu : ActionsContextMenu
+
+ const view = (renderItems: typeof items) => (
+ <Menu items={renderItems}>
+ <button type="button">Actions</button>
+ </Menu>
+ )
+
+ const { rerender } = render(view(items))
+ expect(items).not.toHaveBeenCalled()
+ rerender(view(nextItems))
+ expect(nextItems).not.toHaveBeenCalled()
+
+ const trigger = screen.getByRole('button', { name: 'Actions' })
+
+ if (kind === 'dropdown') {
+ fireEvent.pointerDown(trigger, { button: 0, pointerType: 'mouse' })
+ } else {
+ fireEvent.contextMenu(trigger, { clientX: 20, clientY: 20 })
+ }
+
+ fireEvent.click(await screen.findByRole('menuitem', { name: 'Latest action' }))
+ expect(selected).toHaveBeenCalledTimes(1)
+ await waitFor(() => expect(screen.queryByRole('menu')).toBeNull())
+ const callsAfterClose = nextItems.mock.calls.length
+ rerender(view(nextItems))
+ expect(nextItems).toHaveBeenCalledTimes(callsAfterClose)
+})
diff --git a/apps/desktop/src/components/ui/actions-menu.tsx b/apps/desktop/src/components/ui/actions-menu.tsx
index f46cc04f999..2f02f304869 100644
--- a/apps/desktop/src/components/ui/actions-menu.tsx
+++ b/apps/desktop/src/components/ui/actions-menu.tsx
@@ -111,6 +111,12 @@ interface ActionsMenuProps extends Pick<
onOpenChange?: (open: boolean) => void
}
+// Let the existing presence boundary decide when item construction is needed.
+// Calling `items` in the wrapper builds every closed row menu on each refresh.
+function ActionItems({ items, kit }: { items: ActionsMenuProps['items']; kit: MenuKit }) {
+ return <>{items(kit)}</>
+}
+
/**
* A kebab dropdown menu. Pair it with `ActionsContextMenu` using the same
* `items` render function so the two menus stay identical. No tip on the
@@ -139,7 +145,7 @@ export function ActionsMenu({
side={side}
sideOffset={sideOffset}
>
- {items(DROPDOWN_KIT)}
+ <ActionItems items={items} kit={DROPDOWN_KIT} />
</DropdownMenuContent>
</DropdownMenu>
)
@@ -177,7 +183,7 @@ export function ActionsContextMenu({
<ContextMenu>
<ContextMenuTrigger asChild>{children}</ContextMenuTrigger>
<ContextMenuContent aria-label={ariaLabel} className={contentClassName} onCloseAutoFocus={onCloseAutoFocus}>
- {items(CONTEXT_KIT)}
+ <ActionItems items={items} kit={CONTEXT_KIT} />
</ContextMenuContent>
</ContextMenu>
)