Skip to content

Commit a12a464

Browse files
committed
Replace input with growable textarea
1 parent 8eaa1db commit a12a464

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

example/components/chat-with-markdown.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import React, { useState } from 'react';
2-
import { ChakraProvider, Box, Input, Button, VStack, HStack, Avatar, Text } from '@chakra-ui/react';
1+
import React, { useState, useRef, useEffect } from 'react';
2+
import { ChakraProvider, Box, Textarea, Button, VStack, HStack, Avatar, Text } from '@chakra-ui/react';
33
import ReactMarkdown from 'react-markdown';
44

5-
function ChatAppWithMarkdown() {
5+
function ChatApp() {
66
const [messages, setMessages] = useState([]);
77
const [input, setInput] = useState("");
8+
const textareaRef = useRef(null);
9+
10+
// Maximum height of the textarea should be 30% of the viewport height
11+
const maxHeight = window.innerHeight * 0.3;
12+
13+
// Adjust textarea height based on content
14+
useEffect(() => {
15+
if (textareaRef.current) {
16+
textareaRef.current.style.height = 'auto'; // Reset the height
17+
const newHeight = Math.min(textareaRef.current.scrollHeight, maxHeight); // Set to scroll height or maxHeight
18+
textareaRef.current.style.height = `${newHeight}px`;
19+
}
20+
}, [input]); // Recalculate height when input changes
821

922
const sendMessage = () => {
1023
if (input.trim() !== "") {
@@ -51,12 +64,16 @@ function ChatAppWithMarkdown() {
5164
</VStack>
5265

5366
{/* Input Section */}
54-
<HStack mt={4} spacing={2}>
55-
<Input
67+
<HStack mt={4} spacing={2} align="end">
68+
<Textarea
69+
ref={textareaRef}
5670
value={input}
5771
onChange={(e) => setInput(e.target.value)}
5872
placeholder="Type a message with Markdown..."
5973
bg="white"
74+
resize="none" // Disable manual resize
75+
maxH={`${maxHeight}px`} // Set maximum height
76+
overflow="hidden" // Hide overflow when max height is reached
6077
flex="1"
6178
/>
6279
<Button colorScheme="blue" onClick={sendMessage}>
@@ -68,4 +85,4 @@ function ChatAppWithMarkdown() {
6885
);
6986
}
7087

71-
export default ChatAppWithMarkdown;
88+
export default ChatApp;

0 commit comments

Comments
 (0)