1
1
import React , { useState } from 'react' ;
2
- import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading } from '@chakra-ui/react' ;
2
+ import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading , useColorMode , useColorModeValue } from '@chakra-ui/react' ;
3
3
import ReactMarkdown from 'react-markdown' ;
4
4
import MonacoEditor from '@monaco-editor/react' ; // Monaco Editor
5
5
6
6
function ChatApp ( ) {
7
7
const [ messages , setMessages ] = useState ( [ ] ) ;
8
8
const [ input , setInput ] = useState ( "" ) ;
9
+ const { colorMode, toggleColorMode } = useColorMode ( ) ; // Chakra color mode
9
10
10
11
const sendMessage = ( ) => {
11
12
if ( input . trim ( ) !== "" ) {
@@ -20,32 +21,30 @@ function ChatApp() {
20
21
}
21
22
} ;
22
23
24
+ // Dynamic theme for Monaco Editor based on Chakra UI color mode
25
+ const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light' ;
26
+
23
27
// Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
24
28
const renderers = {
25
- // Render headers using Chakra's Heading component
26
29
h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
27
30
h2 : ( { children } ) => < Heading as = "h2" size = "lg" mb = { 2 } > { children } </ Heading > ,
28
31
h3 : ( { children } ) => < Heading as = "h3" size = "md" mb = { 2 } > { children } </ Heading > ,
29
-
30
- // Render links using Chakra's Link component
31
32
a : ( { href, children } ) => (
32
33
< ChakraLink href = { href } color = "blue.500" isExternal >
33
34
{ children }
34
35
</ ChakraLink >
35
36
) ,
36
-
37
- // Use Monaco Editor for code blocks
38
37
code : ( { inline, children, className } ) => {
39
38
const language = className ? className . replace ( 'language-' , '' ) : 'plaintext' ;
40
39
if ( inline ) {
41
- return < Text as = "code" bg = "gray.100" p = "1" borderRadius = "md" > { children } </ Text > ;
40
+ return < Text as = "code" bg = { useColorModeValue ( "gray.100" , "gray.700" ) } p = "1" borderRadius = "md" > { children } </ Text > ;
42
41
}
43
42
return (
44
- < Box border = "1px solid" borderColor = "gray.200" borderRadius = "md" overflow = "hidden" my = { 2 } >
43
+ < Box border = "1px solid" borderColor = { useColorModeValue ( "gray.200" , "gray.600" ) } borderRadius = "md" overflow = "hidden" my = { 2 } >
45
44
< MonacoEditor
46
45
height = "200px"
47
46
language = { language }
48
- theme = "vs-dark"
47
+ theme = { monacoTheme }
49
48
value = { String ( children ) . trim ( ) }
50
49
options = { {
51
50
readOnly : true ,
@@ -61,11 +60,11 @@ function ChatApp() {
61
60
62
61
return (
63
62
< ChakraProvider >
64
- < Box bg = "gray.100" h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
63
+ < Box bg = { useColorModeValue ( "gray.100" , "gray.900" ) } h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
65
64
{ /* Chat Messages Display */ }
66
65
< VStack
67
66
flex = "1"
68
- bg = "white"
67
+ bg = { useColorModeValue ( "white" , "gray.800" ) }
69
68
w = "100%"
70
69
overflowY = "auto"
71
70
p = { 4 }
@@ -76,11 +75,10 @@ function ChatApp() {
76
75
{ messages . map ( ( message ) => (
77
76
< HStack key = { message . id } align = "start" w = "100%" >
78
77
< Avatar name = { message . sender } size = "sm" />
79
- < Box bg = "blue.50" p = { 3 } borderRadius = "lg" width = "80%" >
78
+ < Box bg = { useColorModeValue ( "blue.50" , "blue.900" ) } p = { 3 } borderRadius = "lg" width = "80%" >
80
79
< Text fontSize = "sm" fontWeight = "bold" >
81
80
{ message . sender }
82
81
</ Text >
83
- { /* Rendering the message text with markdown support */ }
84
82
< ReactMarkdown components = { renderers } children = { message . text } />
85
83
< Text fontSize = "xs" color = "gray.500" >
86
84
{ message . timestamp }
@@ -96,7 +94,7 @@ function ChatApp() {
96
94
value = { input }
97
95
onChange = { ( e ) => setInput ( e . target . value ) }
98
96
placeholder = "Type a message with Markdown..."
99
- bg = "white"
97
+ bg = { useColorModeValue ( "white" , "gray.800" ) }
100
98
resize = "none"
101
99
overflow = "hidden"
102
100
flex = "1"
0 commit comments