Improve focus handling for ChatInput textarea
Browse filesReplaces direct focus state assignment with handleFocus and handleBlur functions. Adds a blur timeout to better support virtual keyboards, ensuring the focus state is managed correctly in different environments.
src/lib/components/chat/ChatInput.svelte
CHANGED
|
@@ -47,6 +47,7 @@
|
|
| 47 |
|
| 48 |
let textareaElement: HTMLTextAreaElement | undefined = $state();
|
| 49 |
let isCompositionOn = $state(false);
|
|
|
|
| 50 |
|
| 51 |
const waitForAnimationFrame = () =>
|
| 52 |
typeof requestAnimationFrame === "function"
|
|
@@ -120,6 +121,30 @@
|
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
// Tools removed; only show file upload when applicable
|
| 124 |
let showFileUpload = $derived(modelIsMultimodal && mimeTypes.length > 0);
|
| 125 |
let showNoTools = $derived(!showFileUpload);
|
|
@@ -146,8 +171,8 @@
|
|
| 146 |
}}
|
| 147 |
{placeholder}
|
| 148 |
{disabled}
|
| 149 |
-
onfocus={
|
| 150 |
-
onblur={
|
| 151 |
></textarea>
|
| 152 |
|
| 153 |
{#if !showNoTools}
|
|
|
|
| 47 |
|
| 48 |
let textareaElement: HTMLTextAreaElement | undefined = $state();
|
| 49 |
let isCompositionOn = $state(false);
|
| 50 |
+
let blurTimeout: ReturnType<typeof setTimeout> | null = $state(null);
|
| 51 |
|
| 52 |
const waitForAnimationFrame = () =>
|
| 53 |
typeof requestAnimationFrame === "function"
|
|
|
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
+
function handleFocus() {
|
| 125 |
+
if (blurTimeout) {
|
| 126 |
+
clearTimeout(blurTimeout);
|
| 127 |
+
blurTimeout = null;
|
| 128 |
+
}
|
| 129 |
+
focused = true;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
function handleBlur() {
|
| 133 |
+
if (!isVirtualKeyboard()) {
|
| 134 |
+
focused = false;
|
| 135 |
+
return;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
if (blurTimeout) {
|
| 139 |
+
clearTimeout(blurTimeout);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
blurTimeout = setTimeout(() => {
|
| 143 |
+
blurTimeout = null;
|
| 144 |
+
focused = false;
|
| 145 |
+
});
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
// Tools removed; only show file upload when applicable
|
| 149 |
let showFileUpload = $derived(modelIsMultimodal && mimeTypes.length > 0);
|
| 150 |
let showNoTools = $derived(!showFileUpload);
|
|
|
|
| 171 |
}}
|
| 172 |
{placeholder}
|
| 173 |
{disabled}
|
| 174 |
+
onfocus={handleFocus}
|
| 175 |
+
onblur={handleBlur}
|
| 176 |
></textarea>
|
| 177 |
|
| 178 |
{#if !showNoTools}
|