跳转到内容
Nacre
组件配色
EN

时间线 ChatDatePill

experimentalChatDatePill · ChatSystemPill · ChatUnreadDivider · ChatJumpToLatestButton在画廊运行
ChatDatePill
在浏览器里运行真实组件。

消息流里的分隔与导航:日期胶囊、可换行的系统提示、未读线(打开会话时的未读分界)、回到最新胶囊(不在底部或有更新的消息时)。日期怎么划分、未读从哪条开始、点了要不要先加载最新一页,都由应用决定(参照 coss ChatDatePill / ChatUnreadDivider / ChatJumpToLatestButton)。

状态:experimental · 组件:ChatDatePill、ChatSystemPill、ChatUnreadDivider、ChatJumpToLatestButton

import { ChatDatePill, ChatSystemPill, ChatUnreadDivider, ChatJumpToLatestButton } from 'nacre/chat'

ChatSystemPill 放可换行的系统记录,换天处用日期胶囊;未读线画在第一条未读消息之上,分界按打开会话时的快照,读完也不消失。分隔之后的第一条消息新起一组、不叠组间距(afterSeparator)。

import { Text, View } from 'nsh'
import { TEXT_SIZE } from 'nacre'
import { ChatBubble, ChatDatePill, ChatMessage, ChatSystemPill, ChatUnreadDivider, chatTextClass } from 'nacre/chat'
export function ChatTimelineUsage() {
const line = (side: 'incoming' | 'outgoing', s: string, first = false, afterSeparator = false) => (
<ChatMessage side={side} first={first} afterSeparator={afterSeparator}>
<ChatBubble side={side} first={first}>
<Text class={`${TEXT_SIZE.body} ${chatTextClass(side)}`}>{s}</Text>
</ChatBubble>
</ChatMessage>
)
return (
<View testID="demo-timeline" class="w-full max-w-[480px] pb-3 rounded-lg border border-border bg-background">
<ChatDatePill testID="demo-date" label="昨天" />
{line('incoming', '资金什么时候退回?', true, true)}
{line('outgoing', '一般 1–3 个工作日', true)}
<ChatDatePill label="今天" />
{line('incoming', '还没到账', true, true)}
<ChatSystemPill testID="demo-system-pill" label="已为客户创建工单" />
<ChatUnreadDivider testID="demo-unread" label="以下为新消息" />
{line('incoming', '能帮我查一下吗?', true, true)}
{line('incoming', '流水号 T0123')}
</View>
)
}

客服创建工单等系统记录可以换行,卡片宽度随时间线调整。

import { View } from 'nsh'
import { ChatSystemPill } from 'nacre/chat'
export function ChatTimelineNotice() {
return (
<View class="w-[180px] gap-2">
<ChatSystemPill testID="demo-notice-short" label="已创建工单" />
<ChatSystemPill testID="demo-notice-long" label="客服已创建跟进工单,将在下一个工作日与客户联系。" />
</View>
)
}

读者往上翻看历史时出现,叠在消息流底部居中;有新消息时带计数。出没只淡入淡出,新消息到达时不自动把读者拉回底部。

import { Text, View, signal } from 'nsh'
import { Button, TEXT_SIZE } from 'nacre'
import { ChatJumpToLatestButton } from 'nacre/chat'
export function ChatTimelineJump() {
const away = signal(true)
const fresh = signal(3)
return (
<View class="gap-2">
<View class="relative w-full max-w-[480px] h-[120px] rounded-lg border border-border bg-background items-center justify-center">
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>(正在看历史消息)</Text>
<ChatJumpToLatestButton
testID="demo-jump"
labelTestID="demo-jump-label"
visible={away()}
label="回到底部"
text={fresh() > 0 ? `${fresh()} $条新消息` : undefined}
onPress={() => away.set(false)}
/>
</View>
<View class="flex-row gap-2">
<Button size="xs" testID="demo-jump-away" onPress={() => away.set(true)}>
往上翻
</Button>
<Button size="xs" variant="ghost" onPress={() => fresh.update((n) => n + 1)}>
来一条新消息
</Button>
</View>
</View>
)
}
键 行为
Space / Return 在回到最新上:回到最新消息
  • 日期与未读线是文字,读屏照读。
  • 回到最新读 label(「回到底部」),计数文字跟在后面。

该用

  • 日期文字由应用格式化(今天、昨天、11-23)
  • 未读分界在打开时拍快照
  • 读者在看历史时新消息只更新计数

不该用

  • 每条消息都画日期
  • 未读读完就让分界线消失(内容会跳)
  • 读者在看历史时自动滚到底
属性 类型 默认 说明
label * string —
testID string —

* 必填

属性 类型 默认 说明
label * string —
class string —
testID string —

* 必填

属性 类型 默认 说明
label * string —
testID string —
inset number 16

* 必填

属性 类型 默认 说明
visible * boolean —
label * string — 读屏名与悬停提示,如「回到底部」
text string — 胶囊文字,如「3 条新消息」;不给时只有箭头
onPress * () => void —
testID string —
labelTestID string — 胶囊文字的 testID

* 必填