跳到主要内容

Message 消息

普通文本、Markdown 与自定义卡片共用的消息外壳。

交互示例​

交互示例加载中…

用法​

<Message name="智能助手" text="你好,今天想聊些什么?" />
<Message placement="end" text="帮我整理一份提纲。" />
<Message
format="markdown"
text="**整理思路**:先确认主题,再补充资料。"
onLinkPress={(url) => openLink(url)}
/>

行为与接入​

正文使用 text 或 children,两种形式互斥。format="markdown" 使用真实 Markdown 渲染器;链接点击由 onLinkPress 交出。自定义卡片可以包含按钮等交互内容。

Markdown 的顶层块放在同一个正文区域中,段落、列表等内部留白沿用渲染器;消息头部、正文和附加内容之间的间距不会叠加到各个 Markdown 块之间。正文容器使用普通布局,不增加纵向滚动容器。

有序列表按各项编号分别分配宽度。流式追加第 10、100 等项时,已有正文的缩进与换行保持不变;新增项按完整编号所需的宽度缩进,保留自定义起始编号和嵌套列表。

Markdown 图片使用原始资源地址,位图保持比例,SVG 沿 react-native-marked 的矢量图片能力加载。当前渲染器按完整地址的 .svg 后缀识别 SVG,带查询参数或片段的 SVG 地址尚不自动识别。图片链接也只通过 onLinkPress 交付,未提供回调时保持静态。

默认 placement="start"、surface="bubble"、status="idle",fullWidth 默认关闭。身份、消息状态和附加区域只展示调用方提供的信息。

actions 表示消息操作。正文中的链接、按钮等交互区域独立交付事件;消息外壳支持 onPress 和 onLongPress。

默认气泡使用 radius['2xl'],水平 space[5]、垂直 space[4] 留白,最大占消息行宽的 78%。外发文字采用品牌色底与 onPrimary 文字;卡片使用 fullWidth 和 surface="plain",附件自行提供中性表面。普通正文和 Markdown 正文采用 type.sm,字体系数只应用一次。 Markdown 段落不叠加渲染器默认的上下内边距,段后保留 6 个设计单位;连续段落、流式追加和普通消息使用相同间距。

有 icon 的消息操作显示独立图标按钮,label 保留为中文可访问名称;没有图标的动作显示文字按钮。

空的 pending/streaming 用三点显示等待,statusText 可提供真实阶段文案;正文、header、footer 或自定义内容到达后撤下等待。三点遵守减少动态效果,卸载停止动画;它不生成身份、推理内容或业务进度。

已有过程头部而正文尚未到达时,在 children 中显式组合包根导出的 MessageWaiting。它沿用同一三点动画,可独立使用;可选 label 与三点同行,省略时只有三点,读屏使用“正在等待回复”并标记忙碌。调用方按真实等待状态挂载,正文到达、取消或中断时撤下;组件不解析请求状态、不改变 Message 的自动等待规则,也不添加外壳、头像或固定宽度。

import { Message, MessageWaiting } from '@unif/react-native-chat';

<Message
fullWidth
status="streaming"
header={processHeader}
{...(replyText
? { text: replyText, format: 'markdown' as const }
: { children: waitingForReply ? <MessageWaiting /> : null })}
/>;

MessageWaiting 是明确的正文内容;该组合不会再插入第二份自动等待,也不会采用空等待气泡的宽度。text 与 children 继续互斥;等待结束由调用方切换正文,不嵌套第二个 Message。

failed 保留消息,自有纯文字、Markdown 正文和状态说明以 0.7 透明度表达失败,不额外绘制红边。气泡及调用方提供的 header/footer/children 不整体淡化,其中的附件、确认按钮等玻璃控件保持正常材质层级。调用方提供 failureAction 时,气泡旁显示错误图标按钮(外发消息在左);它沿用 ChatAction 的中文 label、disabled、loading 和 onPress。只有实际失败时才呈现该操作,未提供不生成重试。

内收气泡和 outlined 消息使用共享 GlassLayer,外发气泡保留品牌主色。正文下图标操作使用 glass;plain 模式由内部内容组件自行提供表面。失败只淡化正文,不隐藏材质。

API​

查看完整 TypeScript 类型

以下内容由包根公开类型生成,与当前源码保持一致。

import type { ReactNode } from 'react';

import type { StyleProp, ViewStyle } from 'react-native';

import type { ChatAction } from '@unif/react-native-chat';

export type MessagePlacement = 'start' | 'end';

export type MessageStatus = 'idle' | 'pending' | 'streaming' | 'failed';

export interface MessageBaseProps {
placement?: MessagePlacement;
avatar?: ReactNode;
name?: string;
surface?: 'bubble' | 'outlined' | 'plain';
fullWidth?: boolean;
status?: MessageStatus;
statusText?: string;
header?: ReactNode;
footer?: ReactNode;
actions?: readonly ChatAction[];
/** 仅 failed 时在气泡旁显示的错误操作;不自动生成重试。 */
failureAction?: ChatAction;
onPress?(): void;
onLongPress?(): void;
style?: StyleProp<ViewStyle>;
testID?: string;
}

export interface MessagePlainContent {
text: string;
format?: 'text';
numberOfLines?: number;
children?: never;
onLinkPress?: never;
}

export interface MessageMarkdownContent {
text: string;
format: 'markdown';
onLinkPress?(url: string): void;
numberOfLines?: never;
children?: never;
}

export interface MessageCustomContent {
children?: ReactNode;
text?: never;
format?: never;
numberOfLines?: never;
onLinkPress?: never;
}

export type MessageProps = MessageBaseProps &
(MessagePlainContent | MessageMarkdownContent | MessageCustomContent);

export interface MessageWaitingProps {
/** 可选等待说明;省略时只显示三点,读屏使用“正在等待回复”。 */
label?: string;
}