From 855e9ee9df5a2a782dbe27e9685b6b596b24a321 Mon Sep 17 00:00:00 2001 From: 0xSage Date: Fri, 13 Oct 2023 14:55:00 +0800 Subject: [PATCH] style: textarea resizes --- .../_components/BasicPromptInput/index.tsx | 39 ++++++++++++++----- web/app/_components/InputToolbar/index.tsx | 2 +- .../_components/SimpleTextMessage/index.tsx | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/web/app/_components/BasicPromptInput/index.tsx b/web/app/_components/BasicPromptInput/index.tsx index 2dfa4e71a..7490aa993 100644 --- a/web/app/_components/BasicPromptInput/index.tsx +++ b/web/app/_components/BasicPromptInput/index.tsx @@ -7,7 +7,7 @@ import useCreateConversation from "@/_hooks/useCreateConversation"; import useInitModel from "@/_hooks/useInitModel"; import useSendChatMessage from "@/_hooks/useSendChatMessage"; import { useAtom, useAtomValue } from "jotai"; -import { ChangeEvent } from "react"; +import { ChangeEvent, useEffect, useRef } from "react"; const BasicPromptInput: React.FC = () => { const activeConversationId = useAtomValue(getActiveConvoIdAtom); @@ -18,10 +18,6 @@ const BasicPromptInput: React.FC = () => { const { initModel } = useInitModel(); - const handleMessageChange = (event: ChangeEvent) => { - setCurrentPrompt(event.target.value); - }; - const handleKeyDown = async ( event: React.KeyboardEvent ) => { @@ -44,20 +40,45 @@ const BasicPromptInput: React.FC = () => { } }; + // Auto adjust textarea height based on content + const textareaRef = useRef(null); + const MAX_ROWS = 10; + const adjustTextareaHeight = () => { + if (textareaRef.current) { + textareaRef.current.style.height = "auto"; + const scrollHeight = textareaRef.current.scrollHeight; + const maxScrollHeight = + parseInt(window.getComputedStyle(textareaRef.current).lineHeight, 10) * + MAX_ROWS; + textareaRef.current.style.height = `${Math.min( + scrollHeight, + maxScrollHeight + )}px`; + } + }; + + useEffect(() => { + adjustTextareaHeight(); + }, []); + + const handleMessageChange = (event: ChangeEvent) => { + setCurrentPrompt(event.target.value); + adjustTextareaHeight(); + }; + return (
-