Skip to content

Commit

Permalink
a different kind of autofocus?
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow committed Dec 10, 2024
1 parent f186878 commit 410f28f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Chat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import stream from "./stream";
export let groqKey: string;
let textarea: HTMLTextAreaElement;
const getModel = async () => {
if (localStorage.model) return localStorage.model;
Expand Down Expand Up @@ -46,6 +47,13 @@
}
};
const maybeFocus = (e: KeyboardEvent) => {
const isAlphanumeric = /^[a-zA-Z0-9]$/.test(e.key);
if (e.target == document.body && isAlphanumeric && !e.ctrlKey) {
textarea.focus();
}
};
let conversation: { role: string; content: string }[] = [];
const searchParams = new URLSearchParams(window.location.search);
const q = searchParams.get("q");
Expand All @@ -54,6 +62,7 @@
}
</script>

<svelte:window on:keydown={maybeFocus} />
<button on:click={() => (conversation = [])}>
<Icon icon={iconTrash} width="1.5rem" height="1.5rem" />
</button>
Expand All @@ -75,10 +84,9 @@
textarea.value = "";
}}
>
<!-- svelte-ignore a11y_autofocus -->
<textarea
name="query"
autofocus
bind:this={textarea}
on:keydown={(e) => {
if (e.key == "Enter" && !e.shiftKey) {
e.currentTarget.form.requestSubmit();
Expand Down

0 comments on commit 410f28f

Please sign in to comment.