Message(消息)
在会话中显示消息,支持可选头像、顶部与底部区域以及对齐方式。
Message 组件负责布局会话中的单条消息,处理消息主体周围的头像、对齐、顶部和底部区域。
在 AI 应用中,可以使用 Message 组件渲染推理步骤、工具调用和助手消息。
安装
npx shadcn@latest add message1. 将以下代码复制并粘贴到项目中。
import * as React from "react"
import { cn } from "cn"
function MessageGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="message-group"
className={cn("flex min-w-0 flex-col gap-2", className)}
{...props}
/>
)
}
function Message({
className,
align = "start",
...props
}: React.ComponentProps<"div"> & { align?: "start" | "end" }) {
return (
<div
data-slot="message"
data-align={align}
className={cn(
"group/message relative flex w-full min-w-0 gap-2 text-sm data-[align=end]:flex-row-reverse",
className
)}
{...props}
/>
)
}
function MessageAvatar({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="message-avatar"
className={cn(
"flex w-fit min-w-8 shrink-0 items-center justify-center self-end overflow-hidden rounded-full bg-muted group-has-data-[slot=message-footer]/message:-translate-y-8",
className
)}
{...props}
/>
)
}
function MessageContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="message-content"
className={cn(
"flex w-full min-w-0 flex-col gap-2.5 wrap-break-word group-data-[align=end]/message:*:data-slot:self-end",
className
)}
{...props}
/>
)
}
function MessageHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="message-header"
className={cn(
"flex max-w-full min-w-0 items-center px-3.5 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0",
className
)}
{...props}
/>
)
}
function MessageFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="message-footer"
className={cn(
"flex max-w-full min-w-0 items-center px-3.5 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0 group-data-[align=end]/message:justify-end",
className
)}
{...props}
/>
)
}
export {
MessageGroup,
Message,
MessageAvatar,
MessageContent,
MessageFooter,
MessageHeader,
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
2. 根据项目配置调整导入路径。
用法
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import { Message, MessageAvatar, MessageContent } from "@/components/ui/message"2
3
<Message>
<MessageAvatar>
<Avatar>
<AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" />
<AvatarFallback>CN</AvatarFallback>
</Avatar>
</MessageAvatar>
<MessageContent>
<Bubble>
<BubbleContent>How can I help you today?</BubbleContent>
</Bubble>
</MessageContent>
</Message>2
3
4
5
6
7
8
9
10
11
12
13
注意: Message 负责消息行的布局,包括头像、对齐、顶部和底部区域。 使用 Bubble 在其中渲染可见的消息主体。会话外层的滚动容器 使用 MessageScroller。
组件结构
使用以下结构构建消息:
Message
├── MessageAvatar
└── MessageContent
├── MessageHeader
├── Bubble
└── MessageFooter2
3
4
5
6
使用 MessageGroup 堆叠同一发送者连续发出的消息:
MessageGroup
├── Message
└── Message2
3
特性
- 通过
align属性将发送方与接收方的消息行分别对齐到起始侧和末尾侧 - 头像插槽与消息底部对齐,避开底部区域
- 顶部和底部插槽可显示发送者姓名、状态和消息操作
- 底部区域跟随消息所在的一侧,操作按钮在
align="end"的行中也保持对齐 - 分组容器堆叠同一发送者连续发出的消息
- 每个部分都可通过
className属性自定义样式
头像
使用 MessageAvatar 在消息旁渲染头像。在消息上设置 align="end" 可让头像与消息的末尾侧对齐。
| align | 说明 |
|---|---|
start | 将消息对齐到会话的起始侧。 |
end | 将消息对齐到会话的末尾侧。 |
分组
使用 MessageGroup 堆叠同一发送者连续发出的消息。在前面的消息中渲染空的 MessageAvatar,使它们与最后一条消息的头像对齐。
顶部和底部区域
使用 MessageHeader 显示发送者姓名,使用 MessageFooter 显示送达或已读状态等元数据。
操作
将复制、重试或反馈按钮等消息操作放在 MessageFooter 中。
附件
无障碍
Message 是用于呈现的布局容器。无障碍功能由其中放置的内容决定。
为纯图标操作添加标签
MessageFooter 中的操作按钮通常只有图标,因此请为每个按钮添加 aria-label。
<MessageFooter>
<Button variant="ghost" size="icon" aria-label="Copy">
<CopyIcon />
</Button>
</MessageFooter>2
3
4
5
状态更新
对于进行中的消息,请使用带 role="status" 的 Marker,使辅助技术在更新出现时播报。
<Message>
<Marker role="status">
<MarkerIcon>
<Spinner />
</MarkerIcon>
<MarkerContent>Checking the logs...</MarkerContent>
</Marker>
</Message>2
3
4
5
6
7
8
API 参考
Message
消息行容器。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
align | "start" | "end" | "start" | 消息在会话中的对齐方式。 |
className | string | - | 应用于消息行的附加类。 |
MessageGroup
将同一发送者连续发出的消息分组。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于分组根元素的附加类。 |
MessageAvatar
头像插槽,与消息底部对齐。当消息包含 MessageFooter 时,头像会上移,与消息主体底部对齐。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于头像插槽的附加类。 |
MessageContent
包裹顶部区域、消息主体和底部区域。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于内容插槽的附加类。 |
MessageHeader
在消息上方显示发送者姓名等内容。无论 align 的值为何,始终在起始侧对齐。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于顶部区域的附加类。 |
MessageFooter
在消息下方显示状态或操作等内容。与消息所在的一侧对齐。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于底部区域的附加类。 |