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)
This commit is contained in:
Aleksander Grygier authored and GitHub committed 2026-11-20 14:01:50 +01:00
1 parent 4b69c79cca
commit 28de970cc1
4 files changed
+112 -1

No files matched your search

@@ -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 @@
});
</script>
<svelte:window onkeydown={handleWindowKeydown} />
<div bind:this={formWrapperEl} class="chat-screen-form-wrapper">
<ChatForm
bind:this={chatFormRef}
+1
View File
@@ -108,6 +108,7 @@ export {
// IME utilities
export { isIMEComposing } from './is-ime-composing';
export { shouldTypeToFocus } from './type-to-focus';
// LaTeX utilities
export { maskInlineLaTeX, preprocessLaTeX } from './latex-protection';
@@ -0,0 +1,28 @@
import { isIMEComposing } from './is-ime-composing';
const EDITABLE_SELECTOR =
'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
// widgets that take typed letters themselves (typeahead) or trap focus
const KEY_CAPTURING_SELECTOR =
'[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]';
/**
* Whether a keypress should move focus to the chat form so the character lands there:
* a printable key typed while nothing that takes text has focus.
*/
export function shouldTypeToFocus(event: KeyboardEvent): boolean {
if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return false;
if (isIMEComposing(event)) return false;
// a single character, not a named key; whitespace keeps scrolling or pressing the focused button
if (event.key.length !== 1 || event.key.trim() === '') return false;
const active = document.activeElement;
if (active instanceof HTMLElement && active !== document.body) {
if (active.closest(EDITABLE_SELECTOR) || active.closest(KEY_CAPTURING_SELECTOR)) return false;
}
return true;
}
@@ -0,0 +1,72 @@
import { shouldTypeToFocus } from '#lib/utils/type-to-focus.ts';
import { afterEach, describe, expect, it } from 'vitest';
function key(key: string, init: KeyboardEventInit = {}) {
return new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key, ...init });
}
function mount(html: string) {
const host = document.createElement('div');
host.innerHTML = html;
document.body.appendChild(host);
return host;
}
describe('shouldTypeToFocus', () => {
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('<button>Copy</button>').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('<input /><textarea></textarea><div contenteditable="true">x</div>');
for (const el of host.querySelectorAll<HTMLElement>('input, textarea, [contenteditable]')) {
el.focus();
expect(shouldTypeToFocus(key('a'))).toBe(false);
}
});
it('leaves keys inside dialogs and menus', () => {
const host = mount(
'<div role="dialog"><button>OK</button></div><div role="menu"><button role="menuitem">Rename</button></div>'
);
for (const button of host.querySelectorAll('button')) {
button.focus();
expect(shouldTypeToFocus(key('r'))).toBe(false);
}
});
});