消息 ChatMessage
在浏览器里运行真实组件。
消息流里的一行:ChatMessage 管左右排布、头像列、作者、组间距与行状态,ChatBubble 是气泡表面,ChatDeliveryIndicator 是我方消息的送达状态。只管外观,谁在哪一侧、是否成组、能不能重发由应用决定(参照 coss Chat UI)。
状态:experimental · 组件:ChatMessage、ChatBubble、ChatDeliveryIndicator、ChatCollapsibleMessage
引入
定位到「引入」import { ChatMessage, ChatBubble, ChatDeliveryIndicator, ChatCollapsibleMessage } from 'nacre/chat'用法
定位到「用法」对方在左、留一列头像宽;我方在右。一组的第一条(first)带头像或作者、上方 24 的组间距,组内只留 8。送达状态放在 leading,发送中显示进度标记,pending 向读屏报告忙碌。
import { Text, View } from 'nsh'import { Avatar, TEXT_SIZE } from 'nacre'import { ChatBubble, ChatDeliveryIndicator, ChatMessage, chatTextClass } from 'nacre/chat'
const Time = (props: { text: string }) => <Text class={`h-6 px-1 ${TEXT_SIZE.caption} text-muted-foreground tabular-nums leading-6`}>{props.text}</Text>
export function ChatMessageUsage() { const customer = () => '陈智源' return ( <View testID="demo-chat" class="w-full max-w-[480px] pb-3 rounded-lg border border-border bg-background"> <ChatMessage testID="demo-chat-in-1" side="incoming" first avatar={<Avatar name={customer()} person size={32} />} footer={<Time text="10:42" />}> <ChatBubble side="incoming" first> <Text class={`${TEXT_SIZE.body} ${chatTextClass('incoming')}`}>那中签率大概多少?</Text> </ChatBubble> </ChatMessage> <ChatMessage testID="demo-chat-in-2" side="incoming" footer={<Time text="10:42" />}> <ChatBubble side="incoming"> <Text class={`${TEXT_SIZE.body} ${chatTextClass('incoming')}`}>结果什么时候公布?</Text> </ChatBubble> </ChatMessage> <ChatMessage testID="demo-chat-out-1" side="outgoing" first author="林嘉怡" leading={<ChatDeliveryIndicator status="read" label="已读" />} footer={<Time text="10:43" />}> <ChatBubble side="outgoing" first> <Text class={`${TEXT_SIZE.body} ${chatTextClass('outgoing')}`}>中签结果一般在上市前一个交易日晚上公布</Text> </ChatBubble> </ChatMessage> <ChatMessage testID="demo-chat-out-2" side="outgoing" pending leading={<ChatDeliveryIndicator status="sending" label="发送中" />} footer={<Time text="10:43" />}> <ChatBubble side="outgoing"> <Text class={`${TEXT_SIZE.body} ${chatTextClass('outgoing')}`}>稍后我把链接发给您</Text> </ChatBubble> </ChatMessage> </View> )}示例
定位到「示例」可跳转的当前句
定位到「可跳转的当前句」compact 保持 1pt 描边;按下、Return 或 Space 可选择当前句。播放位置由调用方控制。
手机:compact 保持 1pt 描边;点按可选择当前句。
import { Text, signal } from 'nsh'import { TEXT_SIZE } from 'nacre'import { ChatBubble, chatTextClass } from 'nacre/chat'
export function ChatCurrentBubble() { const current = signal(false) const label = () => '我帮您查看记录' return ( <ChatBubble testID="demo-current-bubble" side="outgoing" variant="compact" current={current()} accessibilityLabel={label()} onPress={() => current.update((value) => !value)}> <Text class={`${TEXT_SIZE.body} ${chatTextClass('outgoing', current())}`}>{label()}</Text> </ChatBubble> )}气泡
定位到「气泡」组首的气泡在发送方一侧的上角收小(尖角);我方实底带浅品牌描边。borderClass 替换描边颜色(质检命中这类业务描边由应用给),media 变体给单张图片用。
import { Text, View } from 'nsh'import { TEXT_SIZE } from 'nacre'import { ChatBubble, chatTextClass } from 'nacre/chat'
export function ChatMessageBubbles() { const text = (side: 'incoming' | 'outgoing', s: string) => <Text class={`${TEXT_SIZE.body} ${chatTextClass(side)}`}>{s}</Text> return ( <View class="flex-row flex-wrap items-start gap-3"> <ChatBubble side="incoming" first>{text('incoming', '对方 · 组首')}</ChatBubble> <ChatBubble side="incoming">{text('incoming', '对方 · 组内')}</ChatBubble> <ChatBubble side="outgoing" first>{text('outgoing', '我方 · 组首')}</ChatBubble> <ChatBubble side="outgoing">{text('outgoing', '我方 · 组内')}</ChatBubble> <ChatBubble testID="demo-bubble-border" side="outgoing" borderClass="border-destructive">{text('outgoing', '换描边(质检命中)')}</ChatBubble> </View> )}长消息折叠
定位到「长消息折叠」ChatCollapsibleMessage:正文排完超过 maxHeight(默认 240,约 12 行;示例 60)才收起并出「展开全文」,按渲染高度判断、不截断 Markdown 源。展开状态是受控的,应用按消息 key 记住(列表复用格子不丢)。
import { Text, View, signal } from 'nsh'import { TEXT_SIZE, TOUCH } from 'nacre'import { ChatBubble, ChatCollapsibleMessage, chatTextClass } from 'nacre/chat'
export function ChatMessageCollapsible() { const open = signal(false) return ( <View class="w-full max-w-[420px]"> <ChatBubble side="incoming" first> <ChatCollapsibleMessage testID="demo-collapsible" open={open()} onOpenChange={(next) => open.set(next)} maxHeight={TOUCH ? 72 : 60} showMoreLabel="展开全文" showLessLabel="收起" > <Text class={`${TEXT_SIZE.body} ${chatTextClass('incoming')}`}> 我上周三通过 FPS 入金 5 万港币,银行那边显示已经扣款,但 App 里一直没到账。客服说一般 1–3 个工作日,现在已经第 5 天了。期间我还提交过一次工单,编号 T0123,回复说在核实。想确认三件事:第一,资金现在在哪一步;第二,如果入金失败,会原路退回还是需要我再操作;第三,这几天里我挂的港股孖展订单会不会因为资金没到账被取消。另外我下周要出差,能不能先把进度发到我的邮箱。 </Text> </ChatCollapsibleMessage> </ChatBubble> </View> )}送达状态
定位到「送达状态」发送中转圈、已发送单勾、已送达灰双勾、已读成功色双勾、失败红色感叹号(可点重发)。不可重发时感叹号仍是实色,不跟着淡掉。
import { Text, View, signal } from 'nsh'import { Button, TEXT_SIZE } from 'nacre'import { ChatDeliveryIndicator } from 'nacre/chat'import type { ChatDeliveryStatus } from 'nacre/chat'
export function ChatMessageDelivery() { const retried = signal(0) const label = (status: ChatDeliveryStatus) => status === 'sending' ? '发送中' : status === 'sent' ? '已发送' : status === 'delivered' ? '已送达' : '已读' const states: ChatDeliveryStatus[] = ['sending', 'sent', 'delivered', 'read'] return ( <View class="flex-row flex-wrap items-center gap-4"> {states.map((status) => ( <View class="flex-row items-center gap-1.5"> <ChatDeliveryIndicator testID={`demo-delivery-${status}`} status={status} label={label(status)} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{label(status)}</Text> </View> ))} <View class="flex-row items-center gap-1.5"> <ChatDeliveryIndicator testID="demo-delivery-failed" status="failed" label="发送失败" retryLabel="重试" onRetry={() => retried.update((n) => n + 1)} /> <Text testID="demo-delivery-retried" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$重试 ${retried()}`}</Text> </View> <Button size="xs" variant="ghost" onPress={() => retried.set(0)}> 清零 </Button> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Space / Return | 在「展开全文」上:展开 / 收起长消息 |
| Tab | focusable 的消息行可聚焦,显示内描焦点环;应用在 onFocus 里绑 ⏎ 打开消息菜单 |
| Space / Return | 在失败的送达状态上:重发 |
无障碍
定位到「无障碍」- 整行可聚焦时给 accessibilityLabel(「陈智源 的消息」)。
- 送达状态读 label(「已读」);失败时读 retryLabel,悬停看失败原因。
- 新到消息才传 animate,历史与补页不播入场;系统「减少动态效果」时不播。
该用 / 不该用
定位到「该用 / 不该用」该用
- 分组由应用算(同一发送者连续消息),传 first
- 日期胶囊、未读线之后的第一条传 afterSeparator
- 状态从业务映射到五种之一,不凭空推「已送达」
不该用
- 把业务等级(质检)做成气泡的属性
- 给历史消息播入场
- 只靠悬停露出消息操作(键盘也要能打开)
API
定位到「API」ChatMessage
定位到「ChatMessage」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
side * |
ChatSide |
— | |
first |
boolean |
— | 一组的第一条:显示头像与作者,上方留组间距;组内后续消息只留小间距 |
afterSeparator |
boolean |
— | 紧跟在日期胶囊、未读线这类分隔之后:不再叠组间距 |
avatar |
JSX.Element |
— | 对方的头像(只在 first 时给);对方消息总留一列头像宽,组内消息左缘对齐 |
avatarSize |
number |
32 |
头像列宽,默认 32 |
author |
string |
— | 气泡上方的作者名(只在 first 时给) |
authorTestID |
string |
— | |
leading |
JSX.Element |
— | 气泡旁的小件(送达状态 ChatDeliveryIndicator):我方在气泡左侧 |
children * |
JSX.Element |
— | 气泡(ChatBubble)或撤回占位这类替代内容 |
below |
JSX.Element |
— | 气泡下方、页脚上方(译文、发送失败说明) |
footer |
JSX.Element |
— | 最下一行(时间、操作) |
pending |
boolean |
— | 发送中,在辅助功能树标为忙碌,送达状态由 leading 呈现 |
highlighted |
'flash' | 'hit' | null |
— | 定位闪烁(flash)或搜索命中(hit)的整行底色 |
animate |
boolean |
— | 新到的消息播一次入场(MOTION.rise);历史、补页、格子复用都传 false。只在挂载时读一次 |
inset |
number |
16 |
左右内边距,默认 16(与输入区对齐时由应用给) |
testID |
string |
— | |
accessibilityLabel |
string |
— | 整行可聚焦时的读屏名(如「林嘉怡 的消息」) |
focusable |
boolean |
— | |
viewRef |
(node: ViewRef) => void |
— | |
onHoverChange |
(hovered: boolean) => void |
— | |
onContextMenu |
(e: ContextMenuEvent) => void |
— | |
onLongPress |
() => void |
— | 触控端长按(消息菜单的操作表);桌面不给 |
onFocus |
() => void |
— | |
onBlur |
() => void |
— |
* 必填
ChatBubble
定位到「ChatBubble」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
side * |
ChatSide |
— | |
first |
boolean |
— | 一组的第一条:发送方那一侧的上角收小(气泡尖角) |
variant |
'filled' | 'compact' | 'media' |
— | filled 用在消息流;compact 缩小纵向内边距并保留 1pt 描边;media 用在单张图片 |
current |
boolean |
— | 当前句。传入后切换时保留相同的描边宽度 |
onPress |
() => void |
— | 提供时可用鼠标、Return 或 Space 激活 |
accessibilityLabel |
string |
— | |
tooltip |
string |
— | |
borderClass |
string |
— | 描边颜色类(如质检命中 border-destructive):给了就替换默认描边; 默认我方 filled 是 border-brand-subtle-border,对方与 media 无描边 |
testID |
string |
— | |
children * |
JSX.Element |
— |
* 必填
ChatDeliveryIndicator
定位到「ChatDeliveryIndicator」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
status * |
ChatDeliveryStatus |
— | |
label * |
string |
— | 状态说明(读屏名与悬停提示):「发送中」「已发送」「已读」;失败时是失败原因 |
onRetry |
() => void |
— | 失败时点了重发;不给或 retryDisabled 时不可点,但红色标识仍是实色(不跟着淡到 40%) |
retryDisabled |
boolean |
— | |
retryLabel |
string |
— | 失败钮的读屏名,如「重试」 |
testID |
string |
— |
* 必填
ChatCollapsibleMessage
定位到「ChatCollapsibleMessage」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
open * |
boolean |
— | 是否展开(受控:应用按消息 key 记住,列表复用格子时不丢) |
onOpenChange * |
(open: boolean) => void |
— | |
maxHeight |
number |
240 |
收起时最多露出的高度(pt),默认 240(约 12 行正文);内容不超过它就不出「展开」 |
showMoreLabel * |
string |
— | 「展开全文」 |
showLessLabel * |
string |
— | 「收起」 |
testID |
string |
— | 根节点;切换钮是 ${testID}-toggle |
children * |
JSX.Element |
— | 正文(纯文本或 Markdown 视图);不放图片与附件 |
* 必填