Skip to content

Commit 4ff339e

Browse files
authored
fix(chat): focus the composer when opening a new or existing chat (#6683)
1 parent ab8a64f commit 4ff339e

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/home/components/user-input

apps/sim/app/workspace/[workspaceId]/home/components/user-input/user-input.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ export type { FileAttachmentForApi } from '@/app/workspace/[workspaceId]/home/ty
4646

4747
const logger = createLogger('UserInput')
4848

49+
/**
50+
* Whether the element is somewhere the user could be typing. Focusing the composer on mount
51+
* must not steal focus from another field, but may take it from a link or button — opening a
52+
* chat leaves the sidebar link focused, and the composer should win.
53+
*/
54+
function isTextEntry(element: HTMLElement): boolean {
55+
const tag = element.tagName
56+
return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || element.isContentEditable
57+
}
58+
4959
interface UserInputProps {
5060
defaultValue?: string
5161
draftScopeKey?: string
@@ -453,12 +463,10 @@ const UserInputImpl = forwardRef<UserInputHandle, UserInputProps>(function UserI
453463

454464
useEffect(() => {
455465
const raf = window.requestAnimationFrame(() => {
466+
if (!document.hasFocus()) return
456467
const active = document.activeElement
457-
const pageHasFocus = document.hasFocus()
458-
const hasNeutralFocus = active === document.body || active === document.documentElement
459-
if (pageHasFocus && hasNeutralFocus) {
460-
textareaRef.current?.focus()
461-
}
468+
if (active instanceof HTMLElement && isTextEntry(active)) return
469+
textareaRef.current?.focus()
462470
})
463471
return () => window.cancelAnimationFrame(raf)
464472
}, [textareaRef])

0 commit comments

Comments
 (0)