ChatMessage
A row in a conversation: ChatMessage lays out the side, avatar column, author, group spacing and row state; ChatBubble is the bubble surface; ChatDeliveryIndicator is the delivery state of your own messages. They only draw: which side a message is on, whether it groups and whether it can be resent are the app’s call (after coss Chat UI).
Status: experimental · Components: ChatMessage, ChatBubble, ChatDeliveryIndicator, ChatCollapsibleMessage
Import
Section titled “Import”import { ChatMessage, ChatBubble, ChatDeliveryIndicator, ChatCollapsibleMessage } from 'nacre/chat'Incoming messages sit on the left with a column for the avatar; outgoing ones on the right. The first of a group (first) carries the avatar or author and a 24pt group gap above; grouped ones keep 8. Show the delivery indicator in leading. While sending, pending marks the row as busy for assistive technology.
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 = () => 'Chen Zhiyuan' 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')}`}>What’s the allotment rate, roughly?</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')}`}>When are the results out?</Text> </ChatBubble> </ChatMessage> <ChatMessage testID="demo-chat-out-1" side="outgoing" first author="Lin Jiayi" leading={<ChatDeliveryIndicator status="read" label="Read" />} footer={<Time text="10:43" />}> <ChatBubble side="outgoing" first> <Text class={`${TEXT_SIZE.body} ${chatTextClass('outgoing')}`}>Results usually come out the evening before listing</Text> </ChatBubble> </ChatMessage> <ChatMessage testID="demo-chat-out-2" side="outgoing" pending leading={<ChatDeliveryIndicator status="sending" label="Sending" />} footer={<Time text="10:43" />}> <ChatBubble side="outgoing"> <Text class={`${TEXT_SIZE.body} ${chatTextClass('outgoing')}`}>I’ll send you the link shortly</Text> </ChatBubble> </ChatMessage> </View> )}Examples
Section titled “Examples”Seekable current line
Section titled “Seekable current line”Compact bubbles reserve their border when the active line changes. Activate by pointer, Return or Space; the caller owns playback position.
Phone: compact keeps the 1 pt outline; tap to select the current sentence.
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 = () => 'I will check the record' 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> )}Bubbles
Section titled “Bubbles”A group’s first bubble tightens the top corner on the sender’s side (the tail); outgoing bubbles are filled with a light brand border. borderClass replaces the border color (business borders such as QC hits come from the app); the media variant is for a single image.
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', 'Incoming · first')}</ChatBubble> <ChatBubble side="incoming">{text('incoming', 'Incoming · grouped')}</ChatBubble> <ChatBubble side="outgoing" first>{text('outgoing', 'Outgoing · first')}</ChatBubble> <ChatBubble side="outgoing">{text('outgoing', 'Outgoing · grouped')}</ChatBubble> <ChatBubble testID="demo-bubble-border" side="outgoing" borderClass="border-destructive">{text('outgoing', 'Custom border (QC hit)')}</ChatBubble> </View> )}Long messages
Section titled “Long messages”ChatCollapsibleMessage: only text that lays out taller than maxHeight (240 by default, about 12 lines; 60 here) collapses and gets “Show more”, judged by rendered height without cutting the Markdown source. Open state is controlled: the app remembers it by message key (so recycled list cells keep it).
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="Show more" showLessLabel="Show less" > <Text class={`${TEXT_SIZE.body} ${chatTextClass('incoming')}`}> Last Wednesday I deposited HKD 50,000 by FPS. My bank shows it was debited, but it has not arrived in the app. Support said it usually takes 1–3 business days, and this is day 5. I also filed ticket T0123 in the meantime, and the reply said it was being checked. I’d like to confirm three things: first, which step the funds are at; second, if the deposit fails, whether it is refunded the same way or I need to do something; third, whether my HK margin orders placed these days will be cancelled because the funds have not arrived. I’m also traveling next week, so could you email me the progress first. </Text> </ChatCollapsibleMessage> </ChatBubble> </View> )}Delivery states
Section titled “Delivery states”Sending spins, sent is one check, delivered is a gray double check, read is a success-colored double check, failed is a red exclamation mark (press to resend). When it cannot be resent, the mark stays solid instead of fading.
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' ? 'Sending' : status === 'sent' ? 'Sent' : status === 'delivered' ? 'Delivered' : 'Read' 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="Not sent" retryLabel="Retry" onRetry={() => retried.update((n) => n + 1)} /> <Text testID="demo-delivery-retried" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$Retry ${retried()}`}</Text> </View> <Button size="xs" variant="ghost" onPress={() => retried.set(0)}> Reset </Button> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Space / Return | On “Show more”: expand / collapse a long message |
| Tab | A focusable message row takes focus with an inset ring; the app binds ⏎ in onFocus to open the message menu |
| Space / Return | On a failed delivery state: resend |
Accessibility
Section titled “Accessibility”- Give a focusable row an accessibilityLabel (“Message from Chen Zhiyuan”).
- The delivery state reads its label (“Read”); when failed it reads retryLabel, and hovering shows the reason.
- Pass animate only for newly arrived messages, never for history or older pages; nothing plays with Reduce Motion.
Do / Don’t
Section titled “Do / Don’t”Do
- Let the app compute groups (consecutive messages from one sender) and pass first
- Pass afterSeparator to the first message after a date pill or unread divider
- Map business state to one of the five; don’t invent “delivered”
Don’t
- Make business levels (QC) a bubble property
- Play the entrance for history
- Reveal message actions on hover only (the keyboard must reach them too)
Property descriptions follow the Chinese source comments.
ChatMessage
Section titled “ChatMessage”| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— |
* required
ChatBubble
Section titled “ChatBubble”| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— |
* required
ChatDeliveryIndicator
Section titled “ChatDeliveryIndicator”| Prop | Type | Default | Description |
|---|---|---|---|
status * |
ChatDeliveryStatus |
— | |
label * |
string |
— | 状态说明(读屏名与悬停提示):「发送中」「已发送」「已读」;失败时是失败原因 |
onRetry |
() => void |
— | 失败时点了重发;不给或 retryDisabled 时不可点,但红色标识仍是实色(不跟着淡到 40%) |
retryDisabled |
boolean |
— | |
retryLabel |
string |
— | 失败钮的读屏名,如「重试」 |
testID |
string |
— |
* required
ChatCollapsibleMessage
Section titled “ChatCollapsibleMessage”| Prop | Type | Default | Description |
|---|---|---|---|
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 视图);不放图片与附件 |
* required