summaryrefslogtreecommitdiffhomepage
path: root/packages/frontend/src/lib/components/ChatInput.svelte
blob: dac7a3adc57364db6ee1f2d6baa62d68e7ff9bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script lang="ts">
import { tabStore } from "../tabs.svelte.js";

let inputEl: HTMLInputElement | undefined;
let inputValue = $state("");

$effect(() => {
	inputEl?.focus();
});

function handleKeydown(e: KeyboardEvent) {
	if (e.key === "Enter" && !e.shiftKey) {
		e.preventDefault();
		submit();
	}
}

function submit() {
	const text = inputValue.trim();
	if (!text) return;
	inputValue = "";
	tabStore.sendMessage(text);
}
</script>

<div class="flex items-center gap-2 p-3">
	<input
		bind:this={inputEl}
		bind:value={inputValue}
		type="text"
		placeholder="Type a message..."
		class="input input-ghost flex-1"
		onkeydown={handleKeydown}
	/>
	<button
		type="button"
		class="btn btn-primary"
		disabled={!inputValue.trim()}
		onclick={submit}
	>
		Send
	</button>
</div>