Bubble(消息气泡)
在消息气泡中展示对话内容,支持变体、对齐、分组、回应和可折叠内容。
Bubble 组件以气泡的形式显示对话内容。可用它展示聊天文字、简短的结构化输出、引用回复、建议和回应。
要构建功能完整的聊天界面,请使用 Message 组件。Bubble 只负责气泡本身。将头像、名称、时间戳、元数据和消息级操作放在 Message 中。
安装
npx shadcn@latest add bubble1. 将以下代码复制到你的项目中。
import * as React from "react"
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
function BubbleGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="bubble-group"
className={cn("flex min-w-0 flex-col gap-2", className)}
{...props}
/>
)
}
const bubbleVariants = cva(
"group/bubble relative flex w-fit max-w-[80%] min-w-0 flex-col gap-1 group-data-[align=end]/message:self-end data-[align=end]:self-end data-[variant=ghost]:max-w-full",
{
variants: {
variant: {
default:
"*:data-[slot=bubble-content]:bg-primary *:data-[slot=bubble-content]:text-primary-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-primary/80",
secondary:
"*:data-[slot=bubble-content]:bg-secondary *:data-[slot=bubble-content]:text-secondary-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-accent [&>[data-slot=bubble-content]:is(button,a):hover]:text-accent-foreground",
muted:
"*:data-[slot=bubble-content]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:bg-accent [&>[data-slot=bubble-content]:is(button,a):hover]:text-accent-foreground",
tinted:
"*:data-[slot=bubble-content]:bg-accent *:data-[slot=bubble-content]:text-accent-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-accent/80",
outline:
"*:data-[slot=bubble-content]:border-border *:data-[slot=bubble-content]:bg-card *:data-[slot=bubble-content]:text-card-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:text-foreground dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-input/30",
ghost:
"border-none *:data-[slot=bubble-content]:rounded-none *:data-[slot=bubble-content]:bg-transparent *:data-[slot=bubble-content]:p-0 [&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:text-foreground dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted/50",
destructive:
"*:data-[slot=bubble-content]:bg-destructive/10 *:data-[slot=bubble-content]:text-destructive dark:*:data-[slot=bubble-content]:bg-destructive/20 [&>[data-slot=bubble-content]:is(button,a):hover]:bg-destructive/20 dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-destructive/30",
},
},
defaultVariants: {
variant: "default",
},
}
)
function Bubble({
variant = "default",
align = "start",
className,
...props
}: React.ComponentProps<"div"> &
VariantProps<typeof bubbleVariants> & {
align?: "start" | "end"
}) {
return (
<div
data-slot="bubble"
data-variant={variant}
data-align={align}
className={cn(bubbleVariants({ variant }), className)}
{...props}
/>
)
}
function BubbleContent({
className,
render,
...props
}: useRender.ComponentProps<"div">) {
return useRender({
defaultTagName: "div",
props: mergeProps<"div">(
{
className: cn(
"w-fit max-w-full min-w-0 overflow-hidden rounded-3xl border border-transparent px-3.5 py-2.5 type-body wrap-break-word group-data-[align=end]/bubble:self-end [button]:text-start [button,a]:transition-colors [button,a]:outline-none [button,a]:focus-visible:border-ring [button,a]:focus-visible:ring-[3px] [button,a]:focus-visible:ring-ring/50",
className
),
},
props
),
render,
state: {
slot: "bubble-content",
},
})
}
const bubbleReactionsVariants = cva(
"absolute z-10 flex w-fit shrink-0 items-center justify-center gap-1 rounded-full bg-muted px-1.5 py-0.5 text-sm ring-3 ring-card has-[button]:p-0",
{
variants: {
side: {
top: "top-0 -translate-y-3/4",
bottom: "bottom-0 translate-y-3/4",
},
align: {
start: "start-3",
end: "end-3",
},
},
defaultVariants: {
side: "bottom",
align: "end",
},
}
)
function BubbleReactions({
side = "bottom",
align = "end",
className,
...props
}: React.ComponentProps<"div"> & {
align?: "start" | "end"
side?: "top" | "bottom"
}) {
return (
<div
data-slot="bubble-reactions"
data-align={align}
data-side={side}
className={cn(bubbleReactionsVariants({ side, align }), className)}
{...props}
/>
)
}
export { BubbleGroup, Bubble, BubbleContent, BubbleReactions }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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
2. 根据你的项目配置调整导入路径。
用法
import { Bubble, BubbleContent, BubbleReactions } from "@/components/ui/bubble"<Bubble>
<BubbleContent>
I checked the registry output and removed the stale route.
</BubbleContent>
<BubbleReactions>
<span>👍</span>
</BubbleReactions>
</Bubble>2
3
4
5
6
7
8
结构
按以下结构组合 bubble:
Bubble
├── BubbleContent
└── BubbleReactions2
3
使用 BubbleGroup 将同一发送者的连续气泡分组:
BubbleGroup
├── Bubble
│ └── BubbleContent
└── Bubble
└── BubbleContent2
3
4
5
功能
- 七种视觉变体,从醒目的主色气泡到没有外框的 ghost 内容
- 支持起始侧和结束侧对齐,可分别用于发送方和接收方的气泡
- 回应固定在气泡边缘,支持配置位置和对齐方式
- 气泡随内容调整尺寸,最大宽度为容器的 80%
- 通过
render将气泡渲染为链接或按钮 - 每个部分均可通过
className属性自定义样式
变体
使用 variant 改变气泡的视觉样式。
| 变体 | 说明 |
|---|---|
default | 醒目的主色气泡,通常用于当前用户。 |
secondary | 用于对话内容的标准中性气泡。 |
muted | 降低强调程度的气泡,用于辅助内容。 |
tinted | 带淡主色的气泡。 |
outline | 带边框的气泡,用于次要内容或富内容。 |
ghost | 没有外框的内容,用于助手文字或富内容。 |
destructive | 用于错误或失败操作的气泡。 |
气泡随内容调整尺寸,最大宽度为容器的 80%。ghost 变体移除最大宽度限制,让助手文字和富内容可以占满整行。
对齐
在 Bubble 上使用 align 将气泡对齐到对话的起始侧或结束侧。
| align | 说明 |
|---|---|
start | 将气泡对齐到对话的起始侧。 |
end | 将气泡对齐到对话的结束侧。 |
注意: 构建聊天界面时,通常应在 Message 组件上设置对齐方式。可以通过 Message 组件的 role 属性自动将气泡对齐到对话的起始侧或结束侧。
气泡组
使用 BubbleGroup 将同一发送者的连续气泡分组。请将 align 属性设置在 Bubble 组件本身。
BubbleGroup
├── Bubble
│ └── BubbleContent
└── Bubble
└── BubbleContent2
3
4
5
链接和按钮
可以通过 BubbleContent 上的 render 属性将气泡渲染为链接或按钮。
import { Bubble, BubbleContent } from "@/components/ui/bubble"
export function BubbleLinkDemo() {
return (
<Bubble variant="muted">
<BubbleContent render={<button />}>Click here</BubbleContent>
</Bubble>
)
}2
3
4
5
6
7
8
9
回应
使用 BubbleReactions 显示气泡的回应,也可用它展示快捷操作按钮。通过 side 和 align 设置整行的位置,side="top" 会将它固定在上边缘。回应与气泡边缘重叠,因此应留出行间距,以下示例为此使用了更大的 gap。
显示更多 / 折叠
长篇气泡内容可以和 Collapsible 组合,让用户展开更多内容或收起内容。使用 CollapsibleTrigger 组件触发折叠内容。
工具提示
用 Tooltip 包裹气泡,即可在悬停时显示元数据,例如消息阅读时间。
弹出框
将气泡与 Popover 搭配,可按需显示更多信息,例如操作失败时的完整错误消息。
无障碍
Bubble 负责呈现消息本身。请在外部容器保留对话层面的语义,并遵循以下指引。
为回应添加标签
回应渲染为一行表情符号。屏幕阅读器会逐个读出符号,缺乏上下文,而且会将 +8 等计数读成「加八」。使用描述性的 aria-label 将整行作为一张图像,使它只被播报一次。role="img" 也会向辅助技术隐藏单个表情符号,因此无需设置 aria-hidden。
<BubbleReactions role="img" aria-label="Reactions: thumbs up, fire, and 8 more">
<span>👍</span>
<span>🔥</span>
<span>+8</span>
</BubbleReactions>2
3
4
5
当回应可交互时,请将它们渲染为按钮,并为纯图标按钮设置 aria-label。
<BubbleReactions>
<Button aria-label="Thumbs up" variant="secondary" size="icon-xs">
<ThumbsUpIcon />
</Button>
</BubbleReactions>2
3
4
5
交互式气泡
气泡可点击时,请用 render 属性将其渲染为原生 <button> 或 <a>,使其可聚焦并向辅助技术提供正确的角色。BubbleContent 为交互元素提供可见的焦点环,无障碍名称来自气泡文字,无需额外标签。
<Bubble variant="muted" align="end">
<BubbleContent render={<button type="button" onClick={onReply} />}>
I forgot my password
</BubbleContent>
</Bubble>2
3
4
5
用文字等方式补充颜色信息
气泡变体通过颜色传达角色和语气。搭配文字、对齐方式或图标,让颜色之外也有信息提示。对于 destructive 气泡,请在消息文字中保留错误背景。
API 参考
Bubble
气泡的根包裹元素。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
variant | "default" | "secondary" | "muted" | "tinted" | "outline" | "ghost" | "destructive" | "default" | 气泡的视觉样式。 |
align | "start" | "end" | "start" | 气泡在行内方向上的对齐方式。 |
className | string | - | 应用于根元素的附加类。 |
BubbleContent
包裹气泡内容。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
render | ReactElement | function | - | 将内容渲染为其他元素,例如链接。 |
className | string | - | 应用于内容元素的附加类。 |
BubbleReactions
显示与气泡边缘重叠的回应。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
side | "top" | "bottom" | "bottom" | 回应固定在气泡的哪一侧。 |
align | "start" | "end" | "end" | 回应在行内方向上的对齐方式。 |
className | string | - | 应用于回应行的附加类。 |
BubbleGroup
将同一发送者的连续气泡分组。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
className | string | - | 应用于组根元素的附加类。 |