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' ;
3
3
import ReactMarkdown from 'react-markdown' ;
4
4
5
- function ChatAppWithMarkdown ( ) {
5
+ function ChatApp ( ) {
6
6
const [ messages , setMessages ] = useState ( [ ] ) ;
7
7
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
8
21
9
22
const sendMessage = ( ) => {
10
23
if ( input . trim ( ) !== "" ) {
@@ -51,12 +64,16 @@ function ChatAppWithMarkdown() {
51
64
</ VStack >
52
65
53
66
{ /* Input Section */ }
54
- < HStack mt = { 4 } spacing = { 2 } >
55
- < Input
67
+ < HStack mt = { 4 } spacing = { 2 } align = "end" >
68
+ < Textarea
69
+ ref = { textareaRef }
56
70
value = { input }
57
71
onChange = { ( e ) => setInput ( e . target . value ) }
58
72
placeholder = "Type a message with Markdown..."
59
73
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
60
77
flex = "1"
61
78
/>
62
79
< Button colorScheme = "blue" onClick = { sendMessage } >
@@ -68,4 +85,4 @@ function ChatAppWithMarkdown() {
68
85
) ;
69
86
}
70
87
71
- export default ChatAppWithMarkdown ;
88
+ export default ChatApp ;
0 commit comments