diff --git a/src/renderer/src/components/native-chat/native-chat-composer-reveal-focus.test.tsx b/src/renderer/src/components/native-chat/native-chat-composer-reveal-focus.test.tsx index daab2c0bf50..f8685a1bde8 100644 --- a/src/renderer/src/components/native-chat/native-chat-composer-reveal-focus.test.tsx +++ b/src/renderer/src/components/native-chat/native-chat-composer-reveal-focus.test.tsx @@ -385,6 +385,21 @@ describe('useNativeChatComposerRevealFocus', () => { outside.remove() }) + it('claims focus from the composer of the chat tab hidden by the same switch', () => { + const hiddenPane = document.createElement('div') + const previousComposer = document.createElement('textarea') + hiddenPane.appendChild(previousComposer) + document.body.appendChild(hiddenPane) + previousComposer.focus() + render({ isVisible: false, isFocusedGroup: true }) + // Chromium leaves focus on the hidden field until a later task, after the reveal claim runs. + hiddenPane.setAttribute('inert', '') + render({ isVisible: true, isFocusedGroup: true }) + drainFrames() + expect(container.querySelector('textarea')).toBe(document.activeElement) + hiddenPane.remove() + }) + it('still claims focus from the covered xterm helper textarea', () => { const helper = document.createElement('textarea') helper.className = 'xterm-helper-textarea' diff --git a/src/renderer/src/components/terminal-pane/pane-helpers.ts b/src/renderer/src/components/terminal-pane/pane-helpers.ts index 318e78c4efa..0de7278022c 100644 --- a/src/renderer/src/components/terminal-pane/pane-helpers.ts +++ b/src/renderer/src/components/terminal-pane/pane-helpers.ts @@ -52,6 +52,10 @@ export function shouldPreserveEditableFocus(element: Element | null): boolean { if (element.classList.contains('xterm-helper-textarea') || element.closest('.xterm')) { return false } + // Why: a field in a just-hidden pane keeps focus until Chromium drops it to body a task later. + if (element.closest('[hidden], [inert]')) { + return false + } // Why: deferred fit/focus work can run after inline rename or settings // fields take focus. Layout maintenance must not blur user edits closed. return (