Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Example.svelte

60 lines1.7 KB

1<script lang="ts">
2 import ChatMessage from './ChatMessage.svelte'
3​
4 let questions = $state<Array<{ id: string; question: string }>>([])
5 let currentQuestion = $state('')
6​
7 const submitMessage = () => {
8 questions = [
9 ...questions,
10 { id: crypto.randomUUID(), question: currentQuestion },
11 ]
12 currentQuestion = ''
13 }
14</script>
15​
16<div class="flex flex-col h-screen max-w-3xl mx-auto p-4">
17 <h1 class="text-3xl font-bold text-gray-800">TanStack Chat Example</h1>
18 <div class="overflow-y-auto mb-4 space-y-4">
19 {#each questions as { id, question } (id)}
20 <ChatMessage {id} {question} />
21 {/each}
22 </div>
23​
24 <form
25 class="flex items-center space-x-2"
26 onsubmit={(e) => {
27 e.preventDefault()
28 submitMessage()
29 }}
30 >
31 <input
32 class="flex-1 p-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-100"
33 bind:value={currentQuestion}
34 placeholder="Type your message..."
35 />
36 <button
37 type="submit"
38 disabled={!currentQuestion.trim()}
39 class="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold px-4 py-2 rounded-2xl shadow-md transition"
40 >
41 <span>Send</span>
42 <svg
43 xmlns="http://www.w3.org/2000/svg"
44 width="24"
45 height="24"
46 viewBox="0 0 24 24"
47 fill="none"
48 stroke="currentColor"
49 stroke-width="2"
50 stroke-linecap="round"
51 stroke-linejoin="round"
52 >
53 <path
54 d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"
55 />
56 <path d="m21.854 2.147-10.94 10.939" />
57 </svg>
58 </button>
59 </form>
60</div>