xonecas.io
53129
Pa

PAT / commit 033c109

feat: refactor input box

xonecas2025-12-17 033c10933d0e19f19b34407ce838208a809e8f86 parent 84b4ad0

2 files changed

src/components/MessageInput.tsx+55−15
@@ -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 }
src/components/MessageList.tsx+43−2
@@ -1,6 +1,6 @@
11 "use client";
22
3-import { useRef, useEffect } from "react";
3+import { useRef, useEffect, useState } from "react";
44 import ReactMarkdown from "react-markdown";
55 import remarkGfm from "remark-gfm";
66 import rehypeHighlight from "rehype-highlight";
@@ -88,7 +88,7 @@ export default function MessageList({ messages }: MessageListProps) {
8888 {msg.content}
8989 </ReactMarkdown>
9090 ) : (
91- <div>{msg.content}</div>
91+ <UserMessageContent content={msg.content} />
9292 )}
9393 <div className="mt-1 text-xs text-neutral-600 dark:text-neutral-400">
9494 sent on {formatTimestamp(msg.createdAt)}
@@ -108,3 +108,44 @@ export default function MessageList({ messages }: MessageListProps) {
108108 );
109109 }
110110
111+function UserMessageContent({ content }: { content: string }) {
112+ const contentRef = useRef<HTMLDivElement>(null);
113+ const [showGradient, setShowGradient] = useState(false);
114+
115+ useEffect(() => {
116+ const checkOverflow = () => {
117+ if (contentRef.current) {
118+ const isScrollable =
119+ contentRef.current.scrollHeight > contentRef.current.clientHeight;
120+ setShowGradient(isScrollable);
121+ }
122+ };
123+
124+ checkOverflow();
125+ // Check on resize
126+ const resizeObserver = new ResizeObserver(checkOverflow);
127+ if (contentRef.current) {
128+ resizeObserver.observe(contentRef.current);
129+ }
130+
131+ return () => {
132+ resizeObserver.disconnect();
133+ };
134+ }, [content]);
135+
136+ return (
137+ <div className="relative">
138+ <div
139+ ref={contentRef}
140+ className="max-h-48 overflow-y-auto pr-2"
141+ style={{ scrollbarWidth: "thin" }}
142+ >
143+ {content}
144+ </div>
145+ {showGradient && (
146+ <div className="pointer-events-none absolute bottom-0 left-0 right-0 h-8 bg-gradient-to-t from-neutral-300 to-transparent dark:from-neutral-800 dark:to-transparent" />
147+ )}
148+ </div>
149+ );
150+}
151+