Skip to content

Commit 8eaa1db

Browse files
committed
Add chat with markdown example
1 parent 636afa4 commit 8eaa1db

File tree

4 files changed

+721
-3
lines changed

4 files changed

+721
-3
lines changed
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, { useState } from 'react';
2+
import { ChakraProvider, Box, Input, Button, VStack, HStack, Avatar, Text } from '@chakra-ui/react';
3+
import ReactMarkdown from 'react-markdown';
4+
5+
function ChatAppWithMarkdown() {
6+
const [messages, setMessages] = useState([]);
7+
const [input, setInput] = useState("");
8+
9+
const sendMessage = () => {
10+
if (input.trim() !== "") {
11+
const newMessage = {
12+
id: messages.length + 1,
13+
text: input,
14+
sender: 'You',
15+
timestamp: new Date().toLocaleTimeString(),
16+
};
17+
setMessages([...messages, newMessage]);
18+
setInput("");
19+
}
20+
};
21+
22+
return (
23+
<ChakraProvider>
24+
<Box bg="gray.100" h="100vh" p={4} display="flex" flexDirection="column">
25+
{/* Chat Messages Display */}
26+
<VStack
27+
flex="1"
28+
bg="white"
29+
w="100%"
30+
overflowY="auto"
31+
p={4}
32+
spacing={4}
33+
borderRadius="md"
34+
boxShadow="md"
35+
>
36+
{messages.map((message) => (
37+
<HStack key={message.id} align="start" w="100%">
38+
<Avatar name={message.sender} size="sm" />
39+
<Box bg="blue.50" p={3} borderRadius="lg" maxW="80%">
40+
<Text fontSize="sm" fontWeight="bold">
41+
{message.sender}
42+
</Text>
43+
{/* Rendering the message text with markdown support */}
44+
<ReactMarkdown children={message.text} />
45+
<Text fontSize="xs" color="gray.500">
46+
{message.timestamp}
47+
</Text>
48+
</Box>
49+
</HStack>
50+
))}
51+
</VStack>
52+
53+
{/* Input Section */}
54+
<HStack mt={4} spacing={2}>
55+
<Input
56+
value={input}
57+
onChange={(e) => setInput(e.target.value)}
58+
placeholder="Type a message with Markdown..."
59+
bg="white"
60+
flex="1"
61+
/>
62+
<Button colorScheme="blue" onClick={sendMessage}>
63+
Send
64+
</Button>
65+
</HStack>
66+
</Box>
67+
</ChakraProvider>
68+
);
69+
}
70+
71+
export default ChatAppWithMarkdown;

example/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"next": "14.2.15",
1818
"react": "18.3.1",
1919
"react-deep-tree": "linksplatform/react-deep-tree#master",
20-
"react-dom": "18.3.1"
20+
"react-dom": "18.3.1",
21+
"react-markdown": "^9.0.1"
2122
},
2223
"devDependencies": {
2324
"@types/node": "^22.7.5",

example/pages/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ import { motion } from "framer-motion";
209209
import ScrollSync from "../components/scroll-sync";
210210
import ChakraBoxes from "../components/chakra-boxes";
211211
import ChatApp from "../components/chat";
212+
import ChatAppWithMarkdown from "../components/chat-with-markdown";
212213

213214
const MotionBox = motion(Box as any);
214215

@@ -241,6 +242,7 @@ export default function Index() {
241242
{/* <HorizontalScrollWithFramer></HorizontalScrollWithFramer> */}
242243
{/* <ScrollSync></ScrollSync> */}
243244
{/* <ChakraBoxes></ChakraBoxes> */}
244-
<ChatApp></ChatApp>
245+
{/* <ChatApp></ChatApp> */}
246+
<ChatAppWithMarkdown></ChatAppWithMarkdown>
245247
</ChakraProvider>;
246248
}

0 commit comments

Comments
 (0)