Skip to content

The agent that grows with you

fix(desktop): keep the caret in a focused clarify/text field when the pointer moves (#114245)

PRATHAMESH75e2bc5e8a0c61
diff --git a/apps/desktop/src/app/chat/composer/floating-target-editable-guard.test.ts b/apps/desktop/src/app/chat/composer/floating-target-editable-guard.test.ts
new file mode 100644
index 00000000000..a724699d36b
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/floating-target-editable-guard.test.ts
@@ -0,0 +1,86 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { registerFloatingComposer } from './floating-target'
+
+/** The owner's composer host plus a chat surface that holds a text-entry field
+ * outside every composer — the clarify card's "Other" answer box lives here, in
+ * the transcript, inside the pane that carries `data-chat-surface`. */
+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 answer = document.createElement('textarea')
+ surface.appendChild(answer)
+ document.body.appendChild(surface)
+
+ return { answer, editor, surface }
+}
+
+/** Button-up movement over the surface: the gesture the focus-follow reacts to. */
+function movePointerOver(target: Element) {
+ target.dispatchEvent(new PointerEvent('pointermove', { bubbles: true, buttons: 0, clientX: 43, clientY: 44 }))
+}
+
+/** A caret in a clarify answer box (a transcript textarea, not a composer
+ * editor) used to be stolen the instant the pointer moved, and a programmatic
+ * focus of that field was swallowed before a character could land — the
+ * focus-follow only exempted composer editors and the inline edit (#114245). */
+describe('floating composer focus-follow vs a focused transcript text field', () => {
+ let unregister: (() => void) | undefined
+
+ afterEach(() => {
+ unregister?.()
+ unregister = undefined
+ document.body.innerHTML = ''
+ })
+
+ it('keeps the caret in a clarify answer box when the pointer moves', () => {
+ const { answer, editor } = mount()
+ unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })
+
+ answer.focus()
+ expect(document.activeElement).toBe(answer)
+
+ movePointerOver(answer)
+
+ expect(document.activeElement).toBe(answer)
+ expect(document.activeElement).not.toBe(editor)
+ })
+
+ it('does not swallow a programmatically focused answer box, leaving its focusin visible to React', () => {
+ const { answer, editor } = mount()
+ unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })
+
+ const focusin = vi.fn()
+ document.addEventListener('focusin', focusin)
+ answer.focus()
+ document.removeEventListener('focusin', focusin)
+
+ expect(document.activeElement).toBe(answer)
+ expect(document.activeElement).not.toBe(editor)
+ expect(focusin).toHaveBeenCalledTimes(1)
+ })
+
+ it('still focuses the composer on pointermove when nothing editable outside it is focused', () => {
+ const { editor, surface } = mount()
+ unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })
+
+ // Focus a non-editable control in the surface so the guard must not fire.
+ const button = document.createElement('button')
+ surface.appendChild(button)
+ button.focus()
+
+ movePointerOver(surface)
+
+ 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 43869924587..6bf5f2451af 100644
--- a/apps/desktop/src/app/chat/composer/floating-target.ts
+++ b/apps/desktop/src/app/chat/composer/floating-target.ts
@@ -1,6 +1,7 @@
import { flushSync } from 'react-dom'
import { $activeTreeGroup, $hoveredTreeGroup, noteActiveTreeGroup } from '@/components/pane-shell/tree/store'
+import { isEditableTarget } from '@/lib/keybinds/combo'
import { $composerPopout } from '@/store/composer-popout'
import { $floatingComposerOwner, type FloatingComposerOwner } from './floating-state'
@@ -22,6 +23,15 @@ let reconcileQueued = false
* the edit ~80ms after it opened (#112935). */
const inInlineEdit = (el: Element | null) => Boolean(el?.closest(EDIT_COMPOSER_ROOT))
+/** A caret the user placed in a live text-entry surface outside every composer
+ * — the clarify answer box, the transcript find bar, a dialog field — is their
+ * own focus, exactly like the inline edit: moving the pointer must not hand the
+ * caret back to the pane composer, and a refused redirect must leave that
+ * element's focusin visible to React. Composer editors are excluded, so
+ * hover-switching between panes keeps behaving as before (#114245). */
+const keepsOwnFocus = (el: Element | null) =>
+ inInlineEdit(el) || (isEditableTarget(el) && !el?.closest('[data-slot="composer-rich-input"]'))
+
function rememberCaret(editor: EventTarget | null) {
const selection = window.getSelection()
@@ -160,7 +170,7 @@ function trackPointer(event: PointerEvent) {
active.dataset.slot === 'composer-rich-input' &&
active.closest<HTMLElement>('[data-composer-owner]')?.dataset.composerOwner === id
- if (event.type === 'pointermove' && !alreadyTyping && !inInlineEdit(active)) {
+ if (event.type === 'pointermove' && !alreadyTyping && !keepsOwnFocus(active)) {
focusSelectedComposer()
}
}
@@ -191,7 +201,7 @@ function trackFocus(event: FocusEvent) {
// A delayed focus/restore isn't a navigation gesture. A clicked control,
// keyboard Tab, or the inline edit opened by a bubble click still keeps its
// normal focus, without redirecting to the input.
- if (keyboardNavigation || inInlineEdit(target) || (pointerDownTarget && target.contains(pointerDownTarget))) {
+ if (keyboardNavigation || keepsOwnFocus(target) || (pointerDownTarget && target.contains(pointerDownTarget))) {
flushSync(() => selectSurface(id))
} else {
// A refused redirect leaves focus where it landed: that element's focusin