Skip to content

Commit 636afa4

Browse files
committed
Add chat example
1 parent 025a274 commit 636afa4

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

example/components/chat.tsx

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

example/pages/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const HorizontalScrollAnimation = () => {
208208
import { motion } from "framer-motion";
209209
import ScrollSync from "../components/scroll-sync";
210210
import ChakraBoxes from "../components/chakra-boxes";
211+
import ChatApp from "../components/chat";
211212

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

@@ -239,6 +240,7 @@ export default function Index() {
239240
{/* <HorizontalScrollAnimation></HorizontalScrollAnimation> */}
240241
{/* <HorizontalScrollWithFramer></HorizontalScrollWithFramer> */}
241242
{/* <ScrollSync></ScrollSync> */}
242-
<ChakraBoxes></ChakraBoxes>
243+
{/* <ChakraBoxes></ChakraBoxes> */}
244+
<ChatApp></ChatApp>
243245
</ChakraProvider>;
244246
}

0 commit comments

Comments
 (0)