Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ export type { FileAttachmentForApi } from '@/app/workspace/[workspaceId]/home/ty

const logger = createLogger('UserInput')

/**
* Whether the element is somewhere the user could be typing. Focusing the composer on mount
* must not steal focus from another field, but may take it from a link or button — opening a
* chat leaves the sidebar link focused, and the composer should win.
*/
function isTextEntry(element: HTMLElement): boolean {
const tag = element.tagName
return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || element.isContentEditable
}

interface UserInputProps {
defaultValue?: string
draftScopeKey?: string
Expand Down Expand Up @@ -453,12 +463,10 @@ const UserInputImpl = forwardRef<UserInputHandle, UserInputProps>(function UserI

useEffect(() => {
const raf = window.requestAnimationFrame(() => {
if (!document.hasFocus()) return
const active = document.activeElement
const pageHasFocus = document.hasFocus()
const hasNeutralFocus = active === document.body || active === document.documentElement
if (pageHasFocus && hasNeutralFocus) {
textareaRef.current?.focus()
}
if (active instanceof HTMLElement && isTextEntry(active)) return
textareaRef.current?.focus()
})
return () => window.cancelAnimationFrame(raf)
}, [textareaRef])
Expand Down
Loading