import { useEffect, useRef, useState } from "react"; import { Send, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "../lib/utils"; interface ChatInputProps { onSend: (content: string) => void; isSubmitting?: boolean; disabled?: boolean; } export function ChatInput({ onSend, isSubmitting = false, disabled = false }: ChatInputProps) { const [value, setValue] = useState(""); const textareaRef = useRef(null); // Auto-resize fallback for browsers without field-sizing: content support useEffect(() => { const el = textareaRef.current; if (!el) return; el.style.height = "auto"; el.style.height = `${el.scrollHeight}px`; }, [value]); function submit() { const trimmed = value.trim(); if (!trimmed || isSubmitting || disabled) return; onSend(trimmed); setValue(""); if (textareaRef.current) { textareaRef.current.style.height = "auto"; } } function handleKeyDown(e: React.KeyboardEvent) { if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); submit(); } else if (e.key === "Escape") { e.preventDefault(); setValue(""); if (textareaRef.current) { textareaRef.current.style.height = "auto"; } } // Shift+Enter falls through to default behavior (inserts newline) } const isEmpty = value.trim().length === 0; return (
{ e.preventDefault(); submit(); }} className="flex items-end gap-2" >