Skip to content

The agent that grows with you

fix(desktop): keep menu search focus while hovering rows

brooklyn!c0c7e95b168f
diff --git a/apps/desktop/src/app/shell/model-menu-panel.test.tsx b/apps/desktop/src/app/shell/model-menu-panel.test.tsx
index 76ceec51082..870b51ec47d 100644
--- a/apps/desktop/src/app/shell/model-menu-panel.test.tsx
+++ b/apps/desktop/src/app/shell/model-menu-panel.test.tsx
@@ -268,6 +268,37 @@ describe('ModelMenuPanel search', () => {
})
})
+ it('hovering a model row leaves focus in the search field and arrows still drive the list (#53980)', async () => {
+ const { content, onSelectModel } = renderPanel()
+
+ await content.findByText('DeepSeek')
+
+ const input = screen.getByRole('textbox', { name: 'Search models' })
+ input.focus()
+ fireEvent.change(input, { target: { value: 'gemini' } })
+
+ await vi.waitFor(() => {
+ expect(rowWithText(content, /Gemini 2\.5 Pro/i)).not.toBeNull()
+ })
+
+ // A real hand on the mouse: wake the rows, then move over one.
+ fireEvent.mouseMove(window)
+ fireEvent.pointerMove(rowWithText(content, /Gemini 2\.5 Pro/i)!, { pointerType: 'mouse' })
+
+ expect(input.ownerDocument.activeElement).toBe(input)
+
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
+ fireEvent.keyDown(input, { key: 'Enter' })
+
+ await vi.waitFor(() => {
+ expect(onSelectModel).toHaveBeenCalledWith({
+ model: 'gemini-2.5-flash',
+ provider: 'google',
+ sessionId: 'runtime-1'
+ })
+ })
+ })
+
it('with no query the selection sits on the current model, so Enter closes without switching', async () => {
$currentProvider.set('google')
$currentModel.set('gemini-3.1-pro')
diff --git a/apps/desktop/src/components/ui/dropdown-menu.test.tsx b/apps/desktop/src/components/ui/dropdown-menu.test.tsx
new file mode 100644
index 00000000000..561f22279cd
--- /dev/null
+++ b/apps/desktop/src/components/ui/dropdown-menu.test.tsx
@@ -0,0 +1,134 @@
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
+
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuRadioGroup,
+ DropdownMenuRadioItem,
+ DropdownMenuSearch,
+ DropdownMenuSub,
+ DropdownMenuSubContent,
+ DropdownMenuSubTrigger
+} from './dropdown-menu'
+
+// Radix menus use pointer capture and scrollIntoView; jsdom has neither.
+beforeAll(() => {
+ Element.prototype.hasPointerCapture ??= () => false
+ Element.prototype.releasePointerCapture ??= () => undefined
+ Element.prototype.scrollIntoView ??= () => undefined
+})
+
+afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+})
+
+const hover = (element: Element) => fireEvent.pointerMove(element, { pointerType: 'mouse' })
+const unhover = (element: Element) => fireEvent.pointerLeave(element, { pointerType: 'mouse' })
+
+function SearchableMenu({ withSearch = true }: { withSearch?: boolean }) {
+ return (
+ <DropdownMenu open>
+ <DropdownMenuContent>
+ {withSearch && <DropdownMenuSearch aria-label="Search" />}
+ <DropdownMenuItem>Plain row</DropdownMenuItem>
+ <DropdownMenuRadioGroup value="a">
+ <DropdownMenuRadioItem value="a">Radio row</DropdownMenuRadioItem>
+ </DropdownMenuRadioGroup>
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger>Sub row</DropdownMenuSubTrigger>
+ <DropdownMenuSubContent>
+ <DropdownMenuItem>Sub option</DropdownMenuItem>
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )
+}
+
+describe('DropdownMenuSearch hover focus', () => {
+ it('keeps focus in the search field while the mouse moves over rows (#53980)', () => {
+ render(<SearchableMenu />)
+
+ const search = screen.getByRole('textbox', { name: 'Search' })
+ search.focus()
+ expect(search.ownerDocument.activeElement).toBe(search)
+
+ for (const name of ['Plain row', 'Radio row', 'Sub row']) {
+ const row = screen.getByText(name).closest('[role^="menuitem"]')!
+ hover(row)
+ expect(search.ownerDocument.activeElement).toBe(search)
+ unhover(row)
+ expect(search.ownerDocument.activeElement).toBe(search)
+ }
+ })
+
+ it('still highlights the hovered row without taking focus', () => {
+ render(<SearchableMenu />)
+
+ screen.getByRole('textbox', { name: 'Search' }).focus()
+ const row = screen.getByRole('menuitem', { name: 'Plain row' })
+
+ hover(row)
+ expect(row.hasAttribute('data-highlighted')).toBe(true)
+
+ unhover(row)
+ expect(row.hasAttribute('data-highlighted')).toBe(false)
+ })
+
+ it('opens a hovered submenu and closes it when another row is hovered, without taking focus', () => {
+ vi.useFakeTimers()
+ render(<SearchableMenu />)
+
+ const search = screen.getByRole('textbox', { name: 'Search' })
+ search.focus()
+
+ hover(screen.getByRole('menuitem', { name: 'Sub row' }))
+ act(() => vi.advanceTimersByTime(200))
+
+ expect(screen.queryByRole('menuitem', { name: 'Sub option' })).not.toBeNull()
+ expect(search.ownerDocument.activeElement).toBe(search)
+
+ hover(screen.getByRole('menuitem', { name: 'Plain row' }))
+ act(() => vi.advanceTimersByTime(500))
+
+ expect(screen.queryByRole('menuitem', { name: 'Sub option' })).toBeNull()
+ expect(search.ownerDocument.activeElement).toBe(search)
+ })
+
+ it('drops a pending submenu hover-open once the user types, so the submenu cannot take the caret', () => {
+ vi.useFakeTimers()
+ render(<SearchableMenu />)
+
+ const search = screen.getByRole('textbox', { name: 'Search' })
+ search.focus()
+
+ hover(screen.getByRole('menuitem', { name: 'Sub row' }))
+ fireEvent.keyDown(search, { key: 'g' })
+ act(() => vi.advanceTimersByTime(200))
+
+ expect(screen.queryByRole('menuitem', { name: 'Sub option' })).toBeNull()
+ expect(search.ownerDocument.activeElement).toBe(search)
+ })
+
+ it('keeps Radix hover-to-focus once focus has left the search field', () => {
+ render(<SearchableMenu />)
+
+ const row = screen.getByRole('menuitem', { name: 'Plain row' })
+ screen.getByRole('menuitemradio', { name: 'Radio row' }).focus()
+
+ hover(row)
+ expect(row.ownerDocument.activeElement).toBe(row)
+ })
+
+ it('leaves menus without a search field on Radix hover-to-focus', () => {
+ render(<SearchableMenu withSearch={false} />)
+
+ const row = screen.getByRole('menuitem', { name: 'Plain row' })
+
+ hover(row)
+ expect(row.ownerDocument.activeElement).toBe(row)
+ })
+})
diff --git a/apps/desktop/src/components/ui/dropdown-menu.tsx b/apps/desktop/src/components/ui/dropdown-menu.tsx
index 5da802b6902..5a63fdecc71 100644
--- a/apps/desktop/src/components/ui/dropdown-menu.tsx
+++ b/apps/desktop/src/components/ui/dropdown-menu.tsx
@@ -23,6 +23,148 @@ export const dropdownMenuSectionLabel = 'px-2.5 pt-1 pb-0.5 text-[0.625rem] font
// is a filter keystroke and is stopped so the menu's typeahead doesn't hijack it.
const DROPDOWN_NAV_KEYS = new Set(['ArrowDown', 'ArrowUp', 'Enter', 'Escape', 'Tab'])
+// Radix highlights a row by FOCUSING it on hover (MenuItemImpl's pointermove
+// calls item.focus()). In a menu with a DropdownMenuSearch, that pulls the
+// caret out of the field mid-word (#53980). So while the field holds focus,
+// rows cancel the pointermove (Radix skips its handler when the event is
+// defaultPrevented) and set `data-highlighted` themselves. Once focus is
+// elsewhere, e.g. arrowed onto a row, Radix hover applies as usual.
+function searchHoldsFocus(row: HTMLElement): boolean {
+ const active = row.ownerDocument.activeElement
+
+ return Boolean(
+ active?.closest('[data-slot="dropdown-menu-search"]') && row.closest('[role="menu"]')?.contains(active)
+ )
+}
+
+const searchHoverClass = 'data-[highlighted]:bg-(--ui-control-active-background) data-[highlighted]:text-foreground'
+
+type RowPointerHandlers = Pick<React.HTMLAttributes<HTMLDivElement>, 'onPointerLeave' | 'onPointerMove'>
+
+function useSearchSafeHover(
+ { onPointerLeave, onPointerMove }: RowPointerHandlers,
+ { enter, leave }: { enter?: () => void; leave?: () => void } = {}
+) {
+ const [hovered, setHovered] = React.useState(false)
+
+ return {
+ ...(hovered ? { 'data-highlighted': '' } : {}),
+ onPointerLeave: (event: React.PointerEvent<HTMLDivElement>) => {
+ onPointerLeave?.(event)
+
+ if (event.defaultPrevented || event.pointerType !== 'mouse') {
+ return
+ }
+
+ setHovered(false)
+
+ // Radix's leave handler focuses the menu surface: same theft.
+ if (searchHoldsFocus(event.currentTarget)) {
+ event.preventDefault()
+ leave?.()
+ }
+ },
+ onPointerMove: (event: React.PointerEvent<HTMLDivElement>) => {
+ onPointerMove?.(event)
+
+ if (event.defaultPrevented || event.pointerType !== 'mouse') {
+ return
+ }
+
+ const held = searchHoldsFocus(event.currentTarget)
+
+ setHovered(held)
+
+ if (held) {
+ event.preventDefault()
+ enter?.()
+ }
+ }
+ }
+}
+
+// Cancelling the pointermove also skips Radix's submenu hover-open, and Radix
+// closes an open submenu only when focus moves to another row, which no longer
+// happens. The menu re-creates both with Radix's delay as the travel grace:
+// one hover-opened submenu at a time, closed when another row is hovered
+// unless the pointer reaches the submenu first.
+const SUBMENU_HOVER_DELAY_MS = 100
+
+type SetSubOpen = (open: boolean) => void
+
+function createHoverSubmenus() {
+ let current: SetSubOpen | null = null
+ let pending: SetSubOpen | null = null
+ let openTimer = 0
+ let closeTimer = 0
+
+ const cancelOpen = () => {
+ window.clearTimeout(openTimer)
+ pending = null
+ }
+
+ const keep = () => {
+ window.clearTimeout(closeTimer)
+ closeTimer = 0
+ }
+
+ return {
+ cancelOpen,
+ dispose: () => {
+ cancelOpen()
+ keep()
+ },
+ enterSub: (setOpen: SetSubOpen, open: boolean) => {
+ keep()
+
+ if (open) {
+ cancelOpen()
+ current = setOpen
+
+ return
+ }
+
+ if (pending === setOpen) {
+ return
+ }
+
+ cancelOpen()
+ pending = setOpen
+ openTimer = window.setTimeout(() => {
+ pending = null
+
+ if (current !== setOpen) {
+ current?.(false)
+ }
+
+ current = setOpen
+ setOpen(true)
+ }, SUBMENU_HOVER_DELAY_MS)
+ },
+ enterRow: () => {
+ cancelOpen()
+
+ if (current && !closeTimer) {
+ closeTimer = window.setTimeout(() => {
+ closeTimer = 0
+ current?.(false)
+ current = null
+ }, SUBMENU_HOVER_DELAY_MS)
+ }
+ },
+ keep
+ }
+}
+
+const HoverSubmenusContext = React.createContext<ReturnType<typeof createHoverSubmenus> | null>(null)
+const SubOpenContext = React.createContext<{ open: boolean; setOpen: SetSubOpen } | null>(null)
+
+function useRowSearchHover(props: RowPointerHandlers) {
+ const hoverSubmenus = React.useContext(HoverSubmenusContext)
+
+ return useSearchSafeHover(props, { enter: () => hoverSubmenus?.enterRow() })
+}
+
function DropdownMenu({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Root>) {
return <DropdownMenuPrimitive.Root data-slot="dropdown-menu" {...props} />
}
@@ -49,6 +191,8 @@ function DropdownMenuSearch({
}: Omit<React.ComponentProps<'input'>, 'type'> & {
onValueChange?: (value: string) => void
}) {
+ const hoverSubmenus = React.useContext(HoverSubmenusContext)
+
return (
<div className="px-2.5 py-1.5" data-slot="dropdown-menu-search">
<input
@@ -66,6 +210,10 @@ function DropdownMenuSearch({
event.stopPropagation()
}
+ // Radix focuses a submenu that opens after a keypress; a hover-open
+ // still pending while the user types would take the caret with it.
+ hoverSubmenus?.cancelOpen()
+
onKeyDown?.(event)
}}
// Search fields here filter ids, slugs, and model names — dictionary
@@ -92,23 +240,28 @@ function DropdownMenuContent({
// lives outside the dialog's React context. Nested menus still inherit the
// enclosing dialog; everything else falls back to document.body.
const container = usePopoverPortalContainer(portalContainer)
+ const [hoverSubmenus] = React.useState(createHoverSubmenus)
+
+ React.useEffect(() => hoverSubmenus.dispose, [hoverSubmenus])
return (
<DropdownMenuPrimitive.Portal container={container}>
- <DropdownMenuPrimitive.Content
- className={cn(
- menuSurfaceClass,
- menuMotionClass,
- 'z-50 max-h-(--radix-dropdown-menu-content-available-height) min-w-36 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto',
- className
- )}
- // Keep the menu inside the viewport: Radix flips/shifts away from edges
- // (avoidCollisions defaults on); the padding stops it kissing the edge.
- collisionPadding={collisionPadding}
- data-slot="dropdown-menu-content"
- sideOffset={sideOffset}
- {...props}
- />
+ <HoverSubmenusContext.Provider value={hoverSubmenus}>
+ <DropdownMenuPrimitive.Content
+ className={cn(
+ menuSurfaceClass,
+ menuMotionClass,
+ 'z-50 max-h-(--radix-dropdown-menu-content-available-height) min-w-36 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto',
+ className
+ )}
+ // Keep the menu inside the viewport: Radix flips/shifts away from edges
+ // (avoidCollisions defaults on); the padding stops it kissing the edge.
+ collisionPadding={collisionPadding}
+ data-slot="dropdown-menu-content"
+ sideOffset={sideOffset}
+ {...props}
+ />
+ </HoverSubmenusContext.Provider>
</DropdownMenuPrimitive.Portal>
)
}
@@ -126,11 +279,14 @@ function DropdownMenuItem({
inset?: boolean
variant?: 'default' | 'destructive'
}) {
+ const hoverProps = useRowSearchHover(props)
+
return (
<DropdownMenuPrimitive.Item
className={cn(
menuItemClass,
menuItemFocusClass,
+ searchHoverClass,
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[inset]:pl-7 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 [&_svg:not([class*='text-'])]:text-(--ui-text-tertiary) data-[variant=destructive]:*:[svg]:text-destructive!",
className
)}
@@ -138,6 +294,7 @@ function DropdownMenuItem({
data-slot="dropdown-menu-item"
data-variant={variant}
{...props}
+ {...hoverProps}
/>
)
}
@@ -148,17 +305,21 @@ function DropdownMenuCheckboxItem({
checked,
...props
}: React.ComponentProps<typeof DropdownMenuPrimitive.CheckboxItem>) {
+ const hoverProps = useRowSearchHover(props)
+
return (
<DropdownMenuPrimitive.CheckboxItem
checked={checked}
className={cn(
menuItemClass,
menuItemFocusClass,
+ searchHoverClass,
'data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
className
)}
data-slot="dropdown-menu-checkbox-item"
{...props}
+ {...hoverProps}
>
{children}
<DropdownMenuPrimitive.ItemIndicator className="ml-auto flex items-center pl-2 text-foreground">
@@ -177,16 +338,20 @@ function DropdownMenuRadioItem({
children,
...props
}: React.ComponentProps<typeof DropdownMenuPrimitive.RadioItem>) {
+ const hoverProps = useRowSearchHover(props)
+
return (
<DropdownMenuPrimitive.RadioItem
className={cn(
menuItemClass,
menuItemFocusClass,
+ searchHoverClass,
'data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
className
)}
data-slot="dropdown-menu-radio-item"
{...props}
+ {...hoverProps}
>
{children}
<DropdownMenuPrimitive.ItemIndicator className="ml-auto flex items-center pl-2 text-foreground">
@@ -233,8 +398,34 @@ function DropdownMenuShortcut({ className, ...props }: React.ComponentProps<'spa
)
}
-function DropdownMenuSub({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Sub>) {
- return <DropdownMenuPrimitive.Sub data-slot="dropdown-menu-sub" {...props} />
+function DropdownMenuSub({
+ defaultOpen = false,
+ onOpenChange,
+ open: openProp,
+ ...props
+}: React.ComponentProps<typeof DropdownMenuPrimitive.Sub>) {
+ // Owned here (still honouring a controlled `open`) so a sub trigger can
+ // hover-open it while a menu search keeps focus; see createHoverSubmenus.
+ const [openState, setOpenState] = React.useState(defaultOpen)
+ const open = openProp ?? openState
+ const onOpenChangeRef = React.useRef(onOpenChange)
+
+ React.useEffect(() => {
+ onOpenChangeRef.current = onOpenChange
+ })
+
+ const setOpen = React.useCallback((next: boolean) => {
+ setOpenState(next)
+ onOpenChangeRef.current?.(next)
+ }, [])
+
+ const sub = React.useMemo(() => ({ open, setOpen }), [open, setOpen])
+
+ return (
+ <SubOpenContext.Provider value={sub}>
+ <DropdownMenuPrimitive.Sub data-slot="dropdown-menu-sub" onOpenChange={setOpen} open={open} {...props} />
+ </SubOpenContext.Provider>
+ )
}
function DropdownMenuSubTrigger({
@@ -248,15 +439,25 @@ function DropdownMenuSubTrigger({
/** Suppress the trailing caret — for triggers that own their right-side affordance. */
hideChevron?: boolean
}) {
+ const sub = React.useContext(SubOpenContext)
+ const hoverSubmenus = React.useContext(HoverSubmenusContext)
+
+ const hoverProps = useSearchSafeHover(props, {
+ enter: () => sub && hoverSubmenus?.enterSub(sub.setOpen, sub.open),
+ leave: () => hoverSubmenus?.cancelOpen()
+ })
+
return (
<DropdownMenuPrimitive.SubTrigger
className={cn(
+ searchHoverClass,
"flex cursor-pointer items-center gap-2 rounded-md px-2 py-1 text-xs outline-hidden select-none focus:bg-(--ui-control-active-background) focus:text-foreground data-[inset]:pl-7 data-[state=open]:bg-(--ui-control-active-background) data-[state=open]:text-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3.5 [&_svg:not([class*='text-'])]:text-(--ui-text-tertiary)",
className
)}
data-inset={inset}
data-slot="dropdown-menu-sub-trigger"
{...props}
+ {...hoverProps}
>
{children}
{!hideChevron && <Codicon className="ml-auto text-(--ui-text-tertiary)" name="chevron-right" size="1rem" />}
@@ -267,8 +468,11 @@ function DropdownMenuSubTrigger({
function DropdownMenuSubContent({
className,
collisionPadding = 8,
+ onPointerMove,
...props
}: React.ComponentProps<typeof DropdownMenuPrimitive.SubContent>) {
+ const hoverSubmenus = React.useContext(HoverSubmenusContext)
+
return (
// Portal the submenu out of the parent Content so it escapes that Content's
// `overflow` clip. Without this, a submenu opening from a scrollable menu
@@ -290,6 +494,11 @@ function DropdownMenuSubContent({
// the submenu never gets clipped.
collisionPadding={collisionPadding}
data-slot="dropdown-menu-sub-content"
+ onPointerMove={event => {
+ // The pointer made it into the submenu: cancel a pending hover close.
+ hoverSubmenus?.keep()
+ onPointerMove?.(event)
+ }}
{...props}
/>
</DropdownMenuPrimitive.Portal>