style: textarea resizes

This commit is contained in:
0xSage 2023-10-13 14:55:00 +08:00
parent ba90045cbf
commit 855e9ee9df
3 changed files with 32 additions and 11 deletions

View File

@ -7,7 +7,7 @@ import useCreateConversation from "@/_hooks/useCreateConversation";
import useInitModel from "@/_hooks/useInitModel"; import useInitModel from "@/_hooks/useInitModel";
import useSendChatMessage from "@/_hooks/useSendChatMessage"; import useSendChatMessage from "@/_hooks/useSendChatMessage";
import { useAtom, useAtomValue } from "jotai"; import { useAtom, useAtomValue } from "jotai";
import { ChangeEvent } from "react"; import { ChangeEvent, useEffect, useRef } from "react";
const BasicPromptInput: React.FC = () => { const BasicPromptInput: React.FC = () => {
const activeConversationId = useAtomValue(getActiveConvoIdAtom); const activeConversationId = useAtomValue(getActiveConvoIdAtom);
@ -18,10 +18,6 @@ const BasicPromptInput: React.FC = () => {
const { initModel } = useInitModel(); const { initModel } = useInitModel();
const handleMessageChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
setCurrentPrompt(event.target.value);
};
const handleKeyDown = async ( const handleKeyDown = async (
event: React.KeyboardEvent<HTMLTextAreaElement> event: React.KeyboardEvent<HTMLTextAreaElement>
) => { ) => {
@ -44,20 +40,45 @@ const BasicPromptInput: React.FC = () => {
} }
}; };
// Auto adjust textarea height based on content
const textareaRef = useRef<HTMLTextAreaElement>(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<HTMLTextAreaElement>) => {
setCurrentPrompt(event.target.value);
adjustTextareaHeight();
};
return ( return (
<div className="overflow-hidden rounded-lg shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-indigo-600"> <div className="overflow-hidden rounded-lg shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-indigo-600">
<label htmlFor="comment" className="sr-only">
Message ...
</label>
<textarea <textarea
ref={textareaRef}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
value={currentPrompt} value={currentPrompt}
onChange={handleMessageChange} onChange={handleMessageChange}
rows={2}
name="comment" name="comment"
id="comment" id="comment"
className="block w-full resize-none border-0 bg-transparent py-1.5 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6" className="block w-full resize-none border-0 bg-transparent py-1.5 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
placeholder="Message ..." placeholder="Message ..."
rows={1}
style={{ overflow: "auto" }}
/> />
{/* Spacer element to match the height of the toolbar */} {/* Spacer element to match the height of the toolbar */}
<div className="py-2" aria-hidden="true"> <div className="py-2" aria-hidden="true">

View File

@ -59,7 +59,7 @@ const InputToolbar: React.FC = () => {
/> />
</div> </div>
{/* My text input */} {/* My text input */}
<div className="flex items-start space-x-4 mx-12 md:mx-32 2xl:mx-80 mb-3"> <div className="flex items-start space-x-4 mx-12 md:mx-32 2xl:mx-64 mb-3">
<div className="min-w-0 flex-1 relative"> <div className="min-w-0 flex-1 relative">
<BasicPromptInput /> <BasicPromptInput />
<BasicPromptAccessories /> <BasicPromptAccessories />

View File

@ -25,7 +25,7 @@ const SimpleTextMessage: React.FC<Props> = ({
return ( return (
<div <div
className={`flex items-start gap-2 px-12 md:px-32 2xl:px-80 ${backgroundColor} py-5`} className={`flex items-start gap-2 px-12 md:px-32 2xl:px-64 ${backgroundColor} py-5`}
> >
<Image <Image
className="rounded-full" className="rounded-full"