Message
One message in a conversation: the avatar, the alignment, a header above the bubble and a footer below it.
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Bubble, BubbleContent } from "@/components/ui/bubble";
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message";
export default function MessageDemo() {
return (
<div className="flex w-full max-w-lg flex-col gap-5">
<Message>
<MessageAvatar>
<Avatar seed="msg-bot">
<AvatarFallback>BP</AvatarFallback>
</Avatar>
</MessageAvatar>
<MessageContent>
<MessageHeader>Ballpoint</MessageHeader>
<Bubble variant="secondary">
<BubbleContent seed="msg-a">How can I help you today?</BubbleContent>
</Bubble>
<MessageFooter>10:02</MessageFooter>
</MessageContent>
</Message>
<Message align="end">
<MessageAvatar>
<Avatar seed="msg-me">
<AvatarFallback>ME</AvatarFallback>
</Avatar>
</MessageAvatar>
<MessageContent>
<Bubble align="end">
<BubbleContent seed="msg-b">Sketch me a margin note.</BubbleContent>
</Bubble>
<MessageFooter>Delivered</MessageFooter>
</MessageContent>
</Message>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ messageimport type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
// One message in a conversation: an avatar, and a column with a header, the
// bubble and a footer. Where it sits (start or end) is the sender's.
function Message({ className, align = "start", ...props }: ComponentProps<"div"> & { align?: "start" | "end" }) {
return <div data-slot="message" data-align={align} className={cn("group/message flex w-full min-w-0 items-start gap-3 data-[align=end]:flex-row-reverse", className)} {...props} />;
}
/** Messages in a row from the same sender, set close together. */
function MessageGroup({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="message-group" className={cn("flex w-full min-w-0 flex-col gap-1", className)} {...props} />;
}
/** The avatar, kept at the foot of the message. */
function MessageAvatar({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="message-avatar" className={cn("flex shrink-0 self-end", className)} {...props} />;
}
function MessageContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="message-content" className={cn("flex min-w-0 flex-1 flex-col items-start gap-1 group-data-[align=end]/message:items-end", className)} {...props} />;
}
/** Above the bubble: who said it. */
function MessageHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="message-header" className={cn("flex items-center gap-2 px-1 text-sm text-ink-3", className)} {...props} />;
}
/** Below the bubble: when, how it went, what to do about it. */
function MessageFooter({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="message-footer" className={cn("flex items-center gap-2 px-1 text-sm text-ink-3", className)} {...props} />;
}
export { Message, MessageGroup, MessageAvatar, MessageContent, MessageHeader, MessageFooter };Usage
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import { Message, MessageAvatar, MessageContent } from "@/components/ui/message"
<Message>
<MessageAvatar>
<Avatar><AvatarFallback>BP</AvatarFallback></Avatar>
</MessageAvatar>
<MessageContent>
<Bubble variant="secondary">
<BubbleContent>How can I help you today?</BubbleContent>
</Bubble>
</MessageContent>
</Message>Examples
Group
Messages in a row from one sender, with only the last showing the avatar.
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Bubble, BubbleContent } from "@/components/ui/bubble";
import { Message, MessageAvatar, MessageContent, MessageGroup } from "@/components/ui/message";
export default function MessageGroupExample() {
return (
<MessageGroup className="max-w-lg">
{["I had a look at the page.", "The hatching reads well.", "Only the corner needs another pass."].map((text, i, all) => (
<Message key={text}>
<MessageAvatar className={i < all.length - 1 ? "invisible" : undefined}>
<Avatar seed="mg-bot">
<AvatarFallback>BP</AvatarFallback>
</Avatar>
</MessageAvatar>
<MessageContent>
<Bubble variant="secondary">
<BubbleContent seed={`mg-${i}`}>{text}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
))}
</MessageGroup>
);
}Header and footer
A sender and a time above; buttons below.
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Bubble, BubbleContent } from "@/components/ui/bubble";
import { Button } from "@/components/ui/button";
import { InkIcon } from "@/components/ui/ink-icons";
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message";
export default function MessageActions() {
return (
<Message className="max-w-lg">
<MessageAvatar>
<Avatar seed="ma-bot">
<AvatarFallback>BP</AvatarFallback>
</Avatar>
</MessageAvatar>
<MessageContent>
<MessageHeader>
Ballpoint <span>· just now</span>
</MessageHeader>
<Bubble variant="secondary">
<BubbleContent seed="ma-a">Blue ink for the keywords, red pen for the names, pencil for the comments.</BubbleContent>
</Bubble>
<MessageFooter>
<Button variant="ghost" size="icon-xs" aria-label="Copy message" seed="ma-copy">
<InkIcon name="copy" />
</Button>
<Button variant="ghost" size="icon-xs" aria-label="Good answer" seed="ma-up">
<InkIcon name="heart" />
</Button>
<Button variant="ghost" size="icon-xs" aria-label="Try again" seed="ma-redo">
<InkIcon name="refresh" />
</Button>
</MessageFooter>
</MessageContent>
</Message>
);
}API reference
- Message aligndefault
"start" "start" | "end"Which edge of the conversation it sits against; the avatar and footer follow.
- MessageGroup
divStacks messages from the same sender.
- MessageHeader / MessageFooter
divAbove the bubble (a name) and below it (a time, a status, actions). Give icon-only buttons an aria-label.