From 28de970cc17f3a64e58db5dd9a238bb2c8513069 Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Fri, 20 Nov 2026 14:01:50 +0100 Subject: [PATCH] ui : focus the chat form when typing with nothing focused (#32570) A printable key pressed on the chat screen while no field, editor, dialog or menu has focus now moves focus to the chat form, and the character lands there. Assisted-by: Claude (Opus 5.5 Medium) --- .../app/chat/ChatScreen/ChatScreenForm.svelte | 12 +++- ui/apps/chat/src/lib/utils/index.ts | 1 + ui/apps/chat/src/lib/utils/type-to-focus.ts | 28 ++++++++ .../tests/client/type-to-focus.svelte.test.ts | 72 +++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 ui/apps/chat/src/lib/utils/type-to-focus.ts create mode 100644 ui/apps/chat/tests/client/type-to-focus.svelte.test.ts diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte index 309cb7222..21bd62201 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte @@ -3,7 +3,7 @@ import { SETTINGS_KEYS } from '#lib/constants/index.ts'; import { useDraftMessages } from '#lib/hooks/use-draft-messages.svelte.ts'; import { deviceStore, settingsStore } from '#lib/stores/index.ts'; - import { hapticTap } from '#lib/utils/index.ts'; + import { hapticTap, shouldTypeToFocus } from '#lib/utils/index.ts'; import { afterNavigate } from '$app/navigation'; import { page } from '$app/state'; import { onMount, tick } from 'svelte'; @@ -127,6 +127,14 @@ chatFormRef?.focus(); } + // Typing while nothing takes text starts the message: focused during keydown, the + // character the key types lands in the form + function handleWindowKeydown(event: KeyboardEvent) { + if (disabled || deviceStore.isMobile || !shouldTypeToFocus(event)) return; + + chatFormRef?.focus(); + } + onMount(() => { if (!deviceStore.isMobile) { setTimeout(focusFormUnlessCaptured, 100); @@ -148,6 +156,8 @@ }); + +
{ + afterEach(() => { + (document.activeElement as HTMLElement | null)?.blur(); + document.body.innerHTML = ''; + }); + + it('takes a printable key when nothing has focus', () => { + expect(shouldTypeToFocus(key('a'))).toBe(true); + expect(shouldTypeToFocus(key('A', { shiftKey: true }))).toBe(true); + expect(shouldTypeToFocus(key('?'))).toBe(true); + }); + + it('takes a printable key while a button has focus', () => { + mount('').querySelector('button')!.focus(); + + expect(shouldTypeToFocus(key('h'))).toBe(true); + }); + + it('leaves named keys, whitespace and shortcuts alone', () => { + for (const name of ['Enter', 'Escape', 'Tab', 'ArrowDown', 'F5', ' ']) { + expect(shouldTypeToFocus(key(name))).toBe(false); + } + + expect(shouldTypeToFocus(key('k', { metaKey: true }))).toBe(false); + expect(shouldTypeToFocus(key('k', { ctrlKey: true }))).toBe(false); + expect(shouldTypeToFocus(key('k', { altKey: true }))).toBe(false); + }); + + it('leaves keys another handler already took', () => { + const event = key('a'); + + event.preventDefault(); + + expect(shouldTypeToFocus(event)).toBe(false); + }); + + it('leaves typing in a field or editor where it is', () => { + const host = mount('
x
'); + + for (const el of host.querySelectorAll('input, textarea, [contenteditable]')) { + el.focus(); + expect(shouldTypeToFocus(key('a'))).toBe(false); + } + }); + + it('leaves keys inside dialogs and menus', () => { + const host = mount( + '
' + ); + + for (const button of host.querySelectorAll('button')) { + button.focus(); + expect(shouldTypeToFocus(key('r'))).toBe(false); + } + }); +});