Skip to content

The agent that grows with you

fix(desktop): keep the message reaction picker open while the pointer moves toward it (#114130)

teknium1a2ed82c88d94
diff --git a/apps/desktop/src/app/chat/composer/floating-target-floating-layer.test.ts b/apps/desktop/src/app/chat/composer/floating-target-floating-layer.test.ts
new file mode 100644
index 00000000000..e2433bebcc0
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/floating-target-floating-layer.test.ts
@@ -0,0 +1,66 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { registerFloatingComposer } from './floating-target'
+
+/** The owner's composer host, a chat surface with a plain control, and a
+ * body-portaled Radix popper layer holding a control — the shapes the
+ * window-level focus-follow has to tell apart. */
+function mount() {
+ const host = document.createElement('div')
+ host.dataset.composerOwner = 'surface-1'
+ const editor = document.createElement('div')
+ editor.dataset.slot = 'composer-rich-input'
+ editor.tabIndex = -1
+ host.appendChild(editor)
+ document.body.appendChild(host)
+
+ const surface = document.createElement('div')
+ surface.dataset.chatSurface = ''
+ surface.dataset.composerSurfaceId = 'surface-1'
+ const plainButton = document.createElement('button')
+ surface.appendChild(plainButton)
+ document.body.appendChild(surface)
+
+ const layer = document.createElement('div')
+ layer.setAttribute('data-radix-popper-content-wrapper', '')
+ const layerButton = document.createElement('button')
+ layer.appendChild(layerButton)
+ document.body.appendChild(layer)
+
+ return { editor, layerButton, plainButton, surface }
+}
+
+/** Button-up movement over the chat surface — every hover of the transcript. */
+function movePointerOver(target: Element, x: number) {
+ target.dispatchEvent(new PointerEvent('pointermove', { bubbles: true, buttons: 0, clientX: x, clientY: 40 }))
+}
+
+/** Radix moves focus into a popover/menu when it opens and dismisses it as soon
+ * as focus leaves. The focus-follow used to pull that focus back into the pane
+ * composer on the next pointermove, so the message reaction picker closed
+ * before the pointer could reach it. */
+describe('floating composer focus-follow vs an open floating layer', () => {
+ let unregister: (() => void) | undefined
+
+ afterEach(() => {
+ unregister?.()
+ unregister = undefined
+ document.body.innerHTML = ''
+ })
+
+ it('leaves focus inside a floating layer, but still follows the pointer otherwise', () => {
+ const { editor, layerButton, plainButton, surface } = mount()
+ unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })
+
+ layerButton.focus()
+ movePointerOver(surface, 10)
+ expect(document.activeElement).toBe(layerButton)
+
+ // Control: focus on an ordinary control in the surface is not "owned" —
+ // hovering the transcript hands the caret to the composer as before.
+ plainButton.focus()
+ movePointerOver(surface, 20)
+ expect(document.activeElement).toBe(editor)
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/floating-target.ts b/apps/desktop/src/app/chat/composer/floating-target.ts
index 6bf5f2451af..4fe29bf1d94 100644
--- a/apps/desktop/src/app/chat/composer/floating-target.ts
+++ b/apps/desktop/src/app/chat/composer/floating-target.ts
@@ -1,7 +1,7 @@
import { flushSync } from 'react-dom'
import { $activeTreeGroup, $hoveredTreeGroup, noteActiveTreeGroup } from '@/components/pane-shell/tree/store'
-import { isEditableTarget } from '@/lib/keybinds/combo'
+import { isEditableTarget, OVERLAY_SURFACE } from '@/lib/keybinds/combo'
import { $composerPopout } from '@/store/composer-popout'
import { $floatingComposerOwner, type FloatingComposerOwner } from './floating-state'
@@ -32,6 +32,13 @@ const inInlineEdit = (el: Element | null) => Boolean(el?.closest(EDIT_COMPOSER_R
const keepsOwnFocus = (el: Element | null) =>
inInlineEdit(el) || (isEditableTarget(el) && !el?.closest('[data-slot="composer-rich-input"]'))
+/** Focus inside an open floating layer — a popover, menu, listbox or dialog
+ * portaled over the transcript — is the user's own as well. Radix moves focus
+ * into such a layer when it opens and dismisses it the moment focus leaves, so
+ * pulling the caret back to the pane composer on the very next pointermove
+ * closed the message reaction picker before the pointer could reach it. */
+const inFloatingLayer = (el: Element | null) => Boolean(el?.closest(OVERLAY_SURFACE))
+
function rememberCaret(editor: EventTarget | null) {
const selection = window.getSelection()
@@ -66,11 +73,12 @@ function selectionOutsideComposer(): boolean {
}
/** Every focus-follow branch (pointermove and focusin) funnels here, so the
- * selection guard lives at this chokepoint rather than at one call site. */
+ * selection and floating-layer guards live at this chokepoint rather than at
+ * one call site. */
function focusSelectedComposer() {
const owner = $floatingComposerOwner.get()
- if (!owner || selectionOutsideComposer()) {
+ if (!owner || selectionOutsideComposer() || inFloatingLayer(document.activeElement)) {
return
}
diff --git a/apps/desktop/src/components/assistant-ui/thread/reaction-picker-focus-follow.test.tsx b/apps/desktop/src/components/assistant-ui/thread/reaction-picker-focus-follow.test.tsx
new file mode 100644
index 00000000000..dc3f1c804c7
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/reaction-picker-focus-follow.test.tsx
@@ -0,0 +1,65 @@
+import { AssistantRuntimeProvider, type ThreadMessage, useExternalStoreRuntime } from '@assistant-ui/react'
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+
+import { registerFloatingComposer } from '@/app/chat/composer/floating-target'
+import { $reactionsEnabled } from '@/store/reactions-enabled'
+
+import { assistantMessage, stubThreadEnvironment } from '../test-utils'
+
+import { Thread } from '.'
+
+stubThreadEnvironment()
+
+/** The transcript inside a chat surface that owns a floating composer — the
+ * shape of every chat pane, and the one the window-level focus-follow acts on. */
+function Harness() {
+ const runtime = useExternalStoreRuntime<ThreadMessage>({
+ messages: [assistantMessage()],
+ isRunning: false,
+ onNew: async () => {}
+ })
+
+ return (
+ <AssistantRuntimeProvider runtime={runtime}>
+ <div data-chat-surface="" data-composer-surface-id="surface-1">
+ <Thread />
+ <div data-composer-owner="surface-1">
+ <div contentEditable data-slot="composer-rich-input" suppressContentEditableWarning tabIndex={-1} />
+ </div>
+ </div>
+ </AssistantRuntimeProvider>
+ )
+}
+
+let unregister: (() => void) | undefined
+
+beforeEach(() => {
+ $reactionsEnabled.set(true)
+ unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })
+})
+
+afterEach(() => {
+ unregister?.()
+ unregister = undefined
+ cleanup()
+ $reactionsEnabled.set(false)
+})
+
+describe('assistant reaction picker', () => {
+ it('stays open while the pointer moves over the message toward it', async () => {
+ render(<Harness />)
+
+ const message = (await screen.findByText('done')).closest<HTMLElement>('[data-slot="aui_assistant-message-root"]')
+ const trigger = message?.querySelector<HTMLButtonElement>('[data-slot="aui_msg-reactions"]')
+
+ expect(trigger).toBeTruthy()
+ fireEvent.click(trigger!)
+ expect(await screen.findByRole('button', { name: '👍' })).toBeTruthy()
+
+ fireEvent.pointerMove(message!, { buttons: 0, clientX: 40, clientY: 40 })
+
+ expect(screen.queryByRole('button', { name: '👍' })).not.toBeNull()
+ expect(trigger?.getAttribute('data-state')).toBe('open')
+ })
+})
diff --git a/apps/desktop/src/lib/keybinds/combo.ts b/apps/desktop/src/lib/keybinds/combo.ts
index d5cedeeb90a..4222816e420 100644
--- a/apps/desktop/src/lib/keybinds/combo.ts
+++ b/apps/desktop/src/lib/keybinds/combo.ts
@@ -235,6 +235,13 @@ export function isFocusWithin(selector: string): boolean {
return document.activeElement?.closest(selector) != null
}
+// Overlays that cover the whole window (portaled to the body, or the overlay
+// shell itself): dialogs, menus, listboxes, every Radix popper layer. One
+// anywhere means the composer is behind it — its keys, and any focus the
+// user has inside it, are the overlay's own.
+export const OVERLAY_SURFACE =
+ '[role="dialog"],[role="alertdialog"],[role="menu"],[role="listbox"],[data-radix-popper-content-wrapper],[data-overlay-surface]'
+
// True when focus is in a text-entry surface, so bare-key shortcuts don't fire
// while the user is typing.
export function isEditableTarget(target: EventTarget | null): boolean {
diff --git a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
index 8cc7d7bdcd5..b56ad899fd5 100644
--- a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
+++ b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
@@ -11,7 +11,7 @@ import { queryAllVisible } from '@/components/pane-shell/pane-visibility'
import { $activeTreeGroup, $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import { switcherActive } from '@/store/session-switcher'
-import { isEditableTarget, isFocusWithin } from './combo'
+import { isEditableTarget, isFocusWithin, OVERLAY_SURFACE } from './combo'
/** `composer.focus` defaults that need the surface/target gate. */
export const isComposerFocusSoftCombo = (combo: string) => combo === '/' || combo === 'enter'
@@ -41,8 +41,7 @@ const ENTER_ACTIVATES = [
// Overlays that cover the whole window (portaled to the body, or the overlay
// shell itself) — one anywhere means the composer is behind it.
-const BLOCKING_OVERLAY =
- '[role="dialog"],[role="alertdialog"],[role="menu"],[role="listbox"],[data-radix-popper-content-wrapper],[data-overlay-surface]'
+const BLOCKING_OVERLAY = OVERLAY_SURFACE
// Blockers that live INSIDE a chat surface. Inactive tabs stay mounted, so this
// one has to be visible-scoped: a clarify card waiting in a background thread