- Gallery
- Social & Communication
- ChatBubble
New
ChatBubble
Chat message component with avatar, message bubble, timestamp, and read receipts. Supports sent/received variants with different alignment and colors.
Social & Communicationchatmessagebubbleavatarcommunication
Dependencies
Other dependencies:
@/components/ui/avatar
How to use this component
Copy the code below into your project. Make sure you have the required shadcn/ui dependencies installed. Then import and use the component in your pages or layouts.
Code
1"use client";23import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";4import { CheckCheck } from "lucide-react";56interface ChatBubbleProps {7 variant: "sent" | "received";8 message: string;9 timestamp: string;10 avatarSrc?: string;11 senderName: string;12 isRead?: boolean;13}1415export default function ChatBubble({ variant, message, timestamp, avatarSrc, senderName, isRead }: ChatBubbleProps) {16 const isSent = variant === "sent";17 18 return (19 <div className={`flex gap-3 mb-4 ${isSent ? "flex-row-reverse" : ""}`}>20 <Avatar className="w-8 h-8">21 <AvatarImage src={avatarSrc} />22 <AvatarFallback>{senderName[0]}</AvatarFallback>23 </Avatar>24 <div className={`flex flex-col ${isSent ? "items-end" : "items-start"} max-w-[70%]`}>25 <div className={`px-4 py-2 rounded-2xl ${isSent ? "bg-primary text-primary-foreground rounded-br-md" : "bg-muted rounded-bl-md"}`}>26 <p className="text-sm">{message}</p>27 </div>28 <div className="flex items-center gap-1 mt-1">29 <span className="text-xs text-muted-foreground">{timestamp}</span>30 {isSent && isRead && <CheckCheck className="w-3 h-3 text-primary" />}31 </div>32 </div>33 </div>34 );35}3637export function ChatDemo() {38 return (39 <div className="p-4 space-y-4 max-w-md mx-auto">40 <ChatBubble41 variant="received"42 message="Hey! How are you doing today?"43 timestamp="10:30 AM"44 senderName="Alice"45 avatarSrc="https://i.pravatar.cc/150?img=1"46 />47 <ChatBubble48 variant="sent"49 message="I'm doing great! Just finished the project. 🎉"50 timestamp="10:32 AM"51 senderName="You"52 isRead={true}53 />54 <ChatBubble55 variant="received"56 message="That's awesome! Can you share the details?"57 timestamp="10:33 AM"58 senderName="Alice"59 avatarSrc="https://i.pravatar.cc/150?img=1"60 />61 <ChatBubble62 variant="sent"63 message="Sure, I'll send it over in a bit."64 timestamp="10:35 AM"65 senderName="You"66 isRead={false}67 />68 </div>69 );70}