svelte-chatbot-test / src /components /ChatInput.svelte
ferrywuai's picture
Create chat UI
24e4478
<script lang="ts">
export let sending: boolean = false;
export let onSend: (text: string) => void;
let input = "";
function send() {
const text = input.trim();
if (!text) return;
onSend(text);
input = "";
}
function onKeydown(event: KeyboardEvent) {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
send();
}
}
</script>
<form class="composer" on:submit|preventDefault={send}>
<textarea
class="input"
bind:value={input}
placeholder="Type a message..."
on:keydown={onKeydown}
rows="1"
aria-label="Message input"
></textarea>
<button
type="submit"
class="send"
disabled={sending || input.trim() === ""}
aria-label="Send message"
>
{#if sending}
Sending...
{:else}
Send
{/if}
</button>
</form>
<style>
.composer {
display: flex;
gap: 8px;
padding: 12px;
border-top: 1px solid #eef2ff;
background: #ffffff;
}
.input {
flex: 1 1 auto;
min-height: 40px;
max-height: 160px;
resize: none;
padding: 10px 12px;
border-radius: 10px;
border: 1px solid #e6eef8;
outline: none;
font-size: 0.95rem;
background: #fbfdff;
}
.input:focus {
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.08);
border-color: rgba(59, 130, 246, 0.6);
}
.send {
flex: 0 0 auto;
padding: 0 14px;
border-radius: 10px;
border: none;
background: linear-gradient(180deg, #2563eb, #1d4ed8);
color: #fff;
font-weight: 600;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 72px;
}
.send:disabled {
opacity: 0.5;
cursor: not-allowed;
}
@media (max-width: 640px) {
.composer {
padding: 10px;
}
.send {
min-width: 56px;
padding: 0 10px;
}
}
</style>