@@ -1,6 +1,6 @@
11 "use client";
22
3-import { useState, useRef, useImperativeHandle, forwardRef } from "react";
3+import { useState, useRef, useImperativeHandle, forwardRef, useEffect } from "react";
44 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
55 import PrimaryButton from "@/src/components/buttons/PrimaryButton";
66 import type { Message } from "@/src/lib/db/schema";
@@ -19,29 +19,67 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
1919 ({ isLoading, messages, onSubmit }, ref) => {
2020 const [input, setInput] = useState("");
2121 const [historyIndex, setHistoryIndex] = useState<number | null>(null);
22- const inputRef = useRef<HTMLInputElement>(null);
22+ const textareaRef = useRef<HTMLTextAreaElement>(null);
2323
2424 useImperativeHandle(ref, () => ({
2525 focus: () => {
26- inputRef.current?.focus();
26+ textareaRef.current?.focus();
2727 },
2828 }));
2929
30- const handleSubmit = (e: React.FormEvent) => {
31- e.preventDefault();
30+ // Auto-resize textarea to keep an empty line at the bottom
31+ useEffect(() => {
32+ const textarea = textareaRef.current;
33+ if (!textarea) return;
34+
35+ // Reset height to auto to get the correct scrollHeight
36+ textarea.style.height = "auto";
37+
38+ // Calculate line height from computed styles
39+ const computedStyle = getComputedStyle(textarea);
40+ const lineHeight = parseFloat(computedStyle.lineHeight) || 24;
41+ const paddingTop = parseFloat(computedStyle.paddingTop) || 8;
42+ const paddingBottom = parseFloat(computedStyle.paddingBottom) || 8;
43+
44+ // Minimum height for 3 lines
45+ const minHeight = lineHeight * 3 + paddingTop + paddingBottom;
46+
47+ // Maximum height for 10 lines
48+ const maxHeight = lineHeight * 10 + paddingTop + paddingBottom;
49+
50+ // Set height to scrollHeight or minHeight, whichever is larger
51+ // Add one extra line height to keep an empty line at the bottom
52+ // Cap at maxHeight to enable scrolling after 10 lines
53+ const calculatedHeight = textarea.scrollHeight + lineHeight;
54+ const newHeight = Math.min(maxHeight, Math.max(minHeight, calculatedHeight));
55+ textarea.style.height = `${newHeight}px`;
56+ }, [input]);
57+
58+ const handleSubmit = (e?: React.FormEvent) => {
59+ if (e) {
60+ e.preventDefault();
61+ }
3262 if (!input.trim() || isLoading) return;
3363
3464 const inputValue = input.trim();
3565 onSubmit(inputValue);
3666 setInput("");
3767 setHistoryIndex(null);
68+
69+ // Reset textarea height after submit
70+ if (textareaRef.current) {
71+ textareaRef.current.style.height = "auto";
72+ }
3873 };
3974
40- const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
75+ const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
4176 // Only cycle through user messages, not assistant messages
4277 const userMessages = messages.filter((m) => m.role === "user");
4378
44- if (e.key === "ArrowUp") {
79+ if (e.key === "Enter" && e.shiftKey) {
80+ e.preventDefault();
81+ handleSubmit();
82+ } else if (e.key === "ArrowUp") {
4583 e.preventDefault();
4684 const idx = historyIndex ?? userMessages.length;
4785 if (idx > 0) {
@@ -67,21 +105,23 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
67105 return (
68106 <form
69107 onSubmit={handleSubmit}
70- className="flex shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1),0_-2px_4px_-1px_rgba(0,0,0,0.06)] dark:shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.3),0_-2px_4px_-1px_rgba(0,0,0,0.2)]"
108+ className="relative shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1),0_-2px_4px_-1px_rgba(0,0,0,0.06)] dark:shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.3),0_-2px_4px_-1px_rgba(0,0,0,0.2)]"
71109 >
72- <input
73- ref={inputRef}
74- type="text"
110+ <textarea
111+ ref={textareaRef}
75112 value={input}
76113 onChange={(e) => setInput(e.target.value)}
77114 onKeyDown={handleKeyDown}
78115 placeholder={isLoading ? "Loading answer..." : "Type a message..."}
79116 disabled={isLoading}
80- className="flex-1 bg-neutral-200 px-2 py-2 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:bg-neutral-900 dark:placeholder:text-neutral-400"
117+ rows={3}
118+ className="w-full bg-neutral-200 px-2 py-2 pr-12 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:bg-neutral-900 dark:placeholder:text-neutral-400 resize-none overflow-y-auto"
81119 />
82- <PrimaryButton type="submit" disabled={isLoading}>
83- <PaperPlaneIcon className={`h-5 w-5 ${isLoading ? "animate-spin" : ""}`} />
84- </PrimaryButton>
120+ <div className="absolute bottom-2 right-2">
121+ <PrimaryButton type="submit" disabled={isLoading}>
122+ <PaperPlaneIcon className={`h-8 w-5 ${isLoading ? "animate-spin" : ""}`} />
123+ </PrimaryButton>
124+ </div>
85125 </form>
86126 );
87127 }