fix(ai): preserve composer focus after replies

This commit is contained in:
lucaronin
2026-05-06 11:35:30 +02:00
parent dee1c8b1e8
commit 0294bb6fc0
2 changed files with 24 additions and 0 deletions

View File

@@ -349,6 +349,28 @@ describe('AiPanel', () => {
vi.useRealTimers()
})
it('does not steal composer focus after a response when the send button becomes enabled', async () => {
vi.useFakeTimers()
mockMessages = [{
userMessage: 'First question',
actions: [],
response: 'First answer.',
id: 'msg-3',
}]
render(<AiPanel onClose={vi.fn()} vaultPath="/tmp/vault" />)
const input = screen.getByTestId('agent-input')
input.focus()
input.textContent = 'f'
fireEvent.input(input)
await act(() => { vi.advanceTimersByTime(1) })
expect(screen.getByTestId('agent-send')).toBeEnabled()
expect(document.activeElement).toBe(screen.getByTestId('agent-input'))
vi.useRealTimers()
})
it('calls onClose when Escape is pressed while panel has focus', async () => {
vi.useFakeTimers()
const onClose = vi.fn()

View File

@@ -13,6 +13,8 @@ function focusPreferredElement(
inputRef: React.RefObject<HTMLDivElement | null>,
shouldFocusPanel: boolean,
) {
if (panelRef.current?.contains(document.activeElement)) return
if (shouldFocusPanel) {
panelRef.current?.focus()
return