1
- import React , { useState , useRef , useEffect } from 'react' ;
2
- import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text } from '@chakra-ui/react' ;
1
+ import React , { useState } from 'react' ;
2
+ import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading } from '@chakra-ui/react' ;
3
3
import ReactMarkdown from 'react-markdown' ;
4
4
5
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
21
8
22
9
const sendMessage = ( ) => {
23
10
if ( input . trim ( ) !== "" ) {
@@ -32,6 +19,21 @@ function ChatApp() {
32
19
}
33
20
} ;
34
21
22
+ // Custom renderers for Markdown components using Chakra UI
23
+ const renderers = {
24
+ // Render headers using Chakra's Heading component
25
+ h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
26
+ h2 : ( { children } ) => < Heading as = "h2" size = "lg" mb = { 2 } > { children } </ Heading > ,
27
+ h3 : ( { children } ) => < Heading as = "h3" size = "md" mb = { 2 } > { children } </ Heading > ,
28
+
29
+ // Render links using Chakra's Link component
30
+ a : ( { href, children } ) => (
31
+ < ChakraLink href = { href } color = "blue.500" isExternal >
32
+ { children }
33
+ </ ChakraLink >
34
+ ) ,
35
+ } ;
36
+
35
37
return (
36
38
< ChakraProvider >
37
39
< Box bg = "gray.100" h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
@@ -54,7 +56,7 @@ function ChatApp() {
54
56
{ message . sender }
55
57
</ Text >
56
58
{ /* Rendering the message text with markdown support */ }
57
- < ReactMarkdown children = { message . text } />
59
+ < ReactMarkdown components = { renderers } children = { message . text } />
58
60
< Text fontSize = "xs" color = "gray.500" >
59
61
{ message . timestamp }
60
62
</ Text >
@@ -66,13 +68,11 @@ function ChatApp() {
66
68
{ /* Input Section */ }
67
69
< HStack mt = { 4 } spacing = { 2 } align = "end" >
68
70
< Textarea
69
- ref = { textareaRef }
70
71
value = { input }
71
72
onChange = { ( e ) => setInput ( e . target . value ) }
72
73
placeholder = "Type a message with Markdown..."
73
74
bg = "white"
74
75
resize = "none" // Disable manual resize
75
- maxH = { `${ maxHeight } px` } // Set maximum height
76
76
overflow = "hidden" // Hide overflow when max height is reached
77
77
flex = "1"
78
78
/>
0 commit comments