logoAnt Design X

设计研发组件X MarkdownX SDK演示
  • 介绍
  • 总览
  • 通用
    • Bubble对话气泡
    • Conversations管理对话
    • Notification系统通知
  • 唤醒
    • Welcome欢迎
    • Prompts提示集
  • 表达
    • Attachments输入附件
    • Sender输入框
    • Suggestion快捷指令
  • 确认
    • Sources来源引用
    • Think思考过程
    • ThoughtChain思维链
  • 反馈
    • Actions操作列表
    • FileCard文件卡片
  • 其他
    • XProvider全局化配置

Bubble
对话气泡

用于聊天的气泡组件。
使用import { Bubble } from "@ant-design/x";
源码components/bubble
文档
编辑此页...
更新日志
loading

何时使用

常用于聊天的时候。

代码演示

列表演示

API

通用属性参考:通用属性

Bubble

属性说明类型默认值版本
placement气泡位置start | endstart-
loading加载状态boolean--
loadingRender自定义加载内容渲染() => React.ReactNode--
content气泡内容ContentType--
contentRender自定义内容渲染(content: ContentType, info: InfoType ) => React.ReactNode--
editable是否可编辑boolean | EditableBubbleOptionfalse-
typing打字动画效果boolean | BubbleAnimationOption | ((content: ContentType, info: InfoType) => boolean | BubbleAnimationOption)false-
streaming是否为流式传输booleanfalse-
variant气泡样式变体filled | outlined | shadow | borderlessfilled-
shape气泡形状default | round | cornerdefault-
footerPlacement底部插槽位置outer-start | outer-end | inner-start | inner-endouter-start-
components扩展槽位配置{ header?: BubbleSlot; footer?: BubbleSlot; avatar?: BubbleSlot; extra?: BubbleSlot; }--
onTyping动画执行回调(rendererContent: string, currentContent: string) => void--
onTypingComplete动画结束回调(content: string) => void--
onEditing编辑态下内容变化时回调(content: string) => void--

ContentType

默认类型

typescript
type ContentType = React.ReactNode | AnyObject | string | number;

自定义类型使用

tsx
type CustomContentType {
...
}
<Bubble<CustomContentType> {...props} />

BubbleSlot

typescript
type BubbleSlot<ContentType> =
| React.ReactNode
| ((content: ContentType, info: InfoType) => React.ReactNode);

EditableBubbleOption

typescript
interface EditableBubbleOption {
/**
* @description 是否可编辑
*/
editing?: boolean;
/**
* @description 确认按钮
*/
okText?: React.ReactNode;
/**
* @description 取消按钮
*/
cancelText?: React.ReactNode;
}

BubbleAnimationOption

typescript
interface BubbleAnimationOption {
/**
* @description 动画效果类型,打字机,渐入
* @default 'fade-in'
*/
effect: 'typing' | 'fade-in';
/**
* @description 内容步进单位,数组格式为随机区间
* @default 6
*/
step?: number | [number, number];
/**
* @description 动画触发间隔
* @default 100
*/
interval?: number;
/**
* @description 重新开始一段动画时是否保留文本的公共前缀
* @default true
*/
keepPrefix?: boolean;
}

streaming

streaming 用于通知 Bubble 当前的 content 是否属于流式输入的当处于流式传输模。当处于流式传输模式,无论是否启用 Bubble 输入动画,在 streaming 变为 false 之前,Bubble 不会因为把当前 content 全部输出完毕就触发 onTypingComplete 回调,只有当 streaming 变为 false,且 content 全部输出完毕后,Bubble 才会触发 onTypingComplete 回调。这样可以避免由于流式传输不稳定而导致多次触发 onTypingComplete 回调的问题,保证一次流式传输过程仅触发一次 onTypingComplete。

在这个例子中,你可以尝试强制关闭流式标志,同时

  • 若你启用了输入动画,进行 慢速加载 时,会因为流式传输的速度跟不上动画速度而导致多次触发 onTypingComplete。
  • 若你关闭了输入动画,每一次的流式输入都会触发 onTypingComplete。

Bubble.List

属性说明类型默认值版本
items气泡数据列表,key,role 必填 ,当结合X SDK useXChat 使用时可传入status 帮助 Bubble 对配置进行管理((BubbleProps & DividerBubbleProps) & { key: string | number, role: string , status: MessageStatus, extra?: AnyObject})[]--
autoScroll是否自动滚动booleantrue-
role气泡角色默认配置RoleType--

MessageStatus

typescript
type MessageStatus = 'local' | 'loading' | 'updating' | 'success' | 'error' | 'abort';

InfoType

配合 useXChat 使用 ,key 可做为 MessageId,extra 可作为自定义参数。

typescript
type InfoType = {
status?: MessageStatus;
key?: string | number;
extra?: AnyObject;
};

RoleType

typescript
export type RoleProps = Pick<
BubbleProps<any>,
| 'typing'
| 'variant'
| 'shape'
| 'placement'
| 'rootClassName'
| 'classNames'
| 'className'
| 'styles'
| 'style'
| 'loading'
| 'loadingRender'
| 'contentRender'
| 'footerPlacement'
| 'components'
| 'editable'
| 'onTyping'
| 'onTypingComplete'
| 'onEditConfirm'
| 'onEditCancel'
>;
export type FuncRoleProps = (data: BubbleItemType) => RoleProps;
export type DividerRoleProps = Partial<DividerBubbleProps>;
export type FuncDividerRoleProps = (data: BubbleItemType) => DividerRoleProps;
export type RoleType = Partial<
'ai' | 'system' | 'user', RoleProps | FuncRoleProps>
> & { divider: DividerRoleProps | FuncDividerRoleProps } & Record<
string,
RoleProps | FuncRoleProps
>;

Bubble.List autoScroll

Bubble.List 滚动托管需要设置 height,否则无法滚动。

tsx
<Bubble.List items={items} style={{ height: 500 }} autoScroll />

Bubble.List role 与自定义 Bubble

Bubble.List 的 role 和 items 两个属性都可以配置气泡,其中 role 的配置作为默认配置使用,可缺省。item.role 用于指明该条数据的气泡角色,会与 Bubble.List.role 进行匹配。items 本身也可配置气泡属性,优先级高于 role 的配置,最终的气泡配置为:{ ...role[item.role], ...item }。

特别说明,我们为 role 提供了四个默认字段,ai、user、system、divider。其中,system、divider 是保留字段,如果 item.role 赋值为它们俩之一,Bubble.List 会把这条气泡数据渲染为 Bubble.System (role = 'system') 或 Bubble.Divider (role = 'divider')。

因此,若你想自定义渲染系统消息或分割线时,应该使用其他的命名。

自定义渲染消息,可以参考这个例子中 reference 的渲染方式。

Bubble.System

通用属性参考:通用属性

属性说明类型默认值版本
content气泡内容ContentType--
variant气泡样式变体filled | outlined | shadow | borderlessshadow-
shape气泡形状default | round | cornerdefault-

Bubble.Divider

通用属性参考:通用属性

属性说明类型默认值版本
content气泡内容,等效 Divider.childrenContentType--
dividerPropsDivider 组件属性Divider--

Semantic DOM

Bubble

Bubble.System

Bubble.Divider

Bubble.List

主题变量(Design Token)

组件 Token如何定制?
Token 名称描述类型默认值
typingAnimationDuration打字动画持续时间string0.8s
typingAnimationName打字动画名称stringcursorBlink
typingContent打字动画内容string|
全局 Token如何定制?
Ant Design X
基本

基础用法。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
filled - default
filled - round
filled - corner left
filled - corner right
outlined - default
outlined - round
outlined - corner left
outlined - corner right
shadow - default
shadow - round
shadow - corner left
shadow - corner right
borderless bubble
变体与形状

变体与形状,无边框样式 borderless 的 Bubble 适用于渲染自定义样式的内容。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
align left
align right
边栏与位置

气泡边栏与位置,位置 placement 会改变主次侧栏的位置。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Hello, this is a system message
Hello, this is a system message
ok
Hello, this is a system message
cancel
系统信息气泡

系统信息 Bubble。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
message 1
Solid
message 2
Dashed
message 3
Dotted
message 4
Plain Text
message 5
分割线气泡

分割线 Bubble。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
header
align left
header
align right
气泡头

气泡头部,位置 placement 会改变头部的对齐方式。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
footer
outer footer
footer
inner footer
footer
outer footer and align right
footer
inner footer and align left
气泡尾

气泡尾部,不受 placement 影响,需要通过 footerPlacement 来控制尾部的渲染位置。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Loading state:
加载中

通过 loading 属性控制加载状态。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
非流式数据 / Non-streaming data:
动画效果 / Animation effects:
保留公共前缀 / Preserve common prefix:
动画

动画效果,仅支持 content 是字符串或 contentRender 渲染字符串的情况下生效。非字符串场景需要自定义渲染动画。生效时,如果 content 不变,而其他配置发生变化,动画不会重新执行。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
流式数据 / steaming data:
强制关闭流式标志 / Force close the streaming flag:
启用动画 / Enable animation:
onTypingComplete 触发次数 / trigger times: 0
流式传输

流式传输。可以传递 streaming 来通知 Bubble 当前的 content 是否属于流式输入的。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Ant Design X
自定义渲染内容

自定义渲染内容。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
渲染markdown内容

配合 x-markdown 实现自定义渲染内容。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
使用 GPT-Vis 渲染图表

配合 @antv/GPT-Vis 实现大模型输出的图表渲染,支持模型流式输出。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
editable bubble 1
editable bubble 2
可编辑气泡

可编辑气泡,支持 string 类型 content 的简单编辑,使用时应该在 onEditing 处进行 XSS 防护。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
气泡列表

预设样式的气泡列表,支持自动滚动,支持使用 role 定义不同类别的气泡并设置属性。Bubble.List 是一个受控组件,内部对 Bubble 做了 memo 处理,因此推荐使用 setState 的回调形式来修改 items 属性,尽可能保证非必要渲染数据项的配置稳定,以此来保证 Bubble.List 的高性能渲染。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
滚动条控制

可以使用 ref 控制列表滚动条。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Custom loading...
Mock user content.
语义化自定义

示例通过语义化以及加载定制,来调整气泡效果。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Custom loading...
Mock user content.
Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content.
Mock user content.
Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content.
Mock user content.
Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content.
Mock user content.
Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content. Mock welcome content.
列表扩展参数

配合扩展参数实现自定义扩展能力。

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Ant Design X
Feel free to use Ant Design !
  • root
    气泡根节点
  • body
    主体容器
  • avatar
    头像的外层容器
  • header
    头部的容器
  • content
    聊天内容的容器
  • footer
    底部的容器
  • extra
    气泡尾边栏容器
Feel free to use Ant Design !
  • root
    气泡根节点
  • body
    主体容器
  • content
    内容的容器
Feel free to use Ant Design !
  • root
    气泡根节点
  • body
    主体容器
  • content
    内容的容器
AI
show me the code of Bubble.List demo with semantic styles
AI
divider
hello, Ant Design X
Welcome to Ant Design X
  • root
    对话列表根节点
  • scroll
    对话列表滚动容器
  • bubble
    对话气泡容器
  • body
    对话气泡的主体容器
  • avatar
    对话气泡的头像的外层容器
  • header
    对话气泡的头部的容器
  • content
    对话气泡的聊天内容的容器
  • footer
    对话气泡的底部的容器
  • extra
    对话气泡的尾边栏容器
  • system
    系统气泡容器
  • divider
    分割线气泡容器