状态与标记的方角小标签:dot 表状态、text 表分类标记、soft 整块着色,outline / dashed 是标签行里的小操作;可点、可选、可删。人与分组用圆胶囊 Pill。
状态:stable · 组件:Tag
引入
定位到「引入」import { Tag } from 'nacre'用法
定位到「用法」import { View } from 'nsh'import { Tag } from 'nacre'
export function TagUsage() { return ( <View class="flex-row items-center gap-2"> <Tag variant="dot" tone="success" label="已解决" /> <Tag tone="warning" label="重大" /> <Tag variant="soft" tone="info" label="系统通知" /> </View> )}示例
定位到「示例」外观
定位到「外观」dot 浅灰底 + 色点,彩色只落在点上,列表与详情里同一个状态长得一样;text 浅灰底 + 状态色字;soft 状态色淡底;outline 是「+N / 收起」,也用在灰底、选中底上的静态标记(text 的浅灰底在同色底上看不出框);dashed 是「+ 添加标签」,或表示「没有」(知识库文章缺的语种)。sm 18(默认)用在行内,md 20 用在表格与详情。
手机:手机上只读标签 sm 20 / md 24、圆角 5;可点、可删的标签是 32 高的胶囊,点击区补到 44。dot、text、soft、outline、dashed 的用法同桌面。
import { View } from 'nsh'import { Tag } from 'nacre'
export function TagVariants() { return ( <View class="gap-3"> <View class="flex-row flex-wrap items-center gap-2"> <Tag variant="dot" tone="warning" label="处理中" /> <Tag variant="text" label="未登录" /> <Tag variant="soft" tone="destructive" label="已超时" /> <Tag variant="outline" label="+3" /> <Tag variant="dashed" icon="plus" label="添加标签" /> </View> <View class="flex-row flex-wrap items-center gap-2"> <Tag variant="dot" tone="warning" size="md" label="处理中" /> <Tag testID="demo-dot-uncolored" variant="dot-plain" size="md" label="未设置" /> <Tag testID="demo-dot-plain" variant="dot-plain" tone="warning" size="md" label="待授权" /> <Tag variant="text" size="md" label="未登录" /> <Tag variant="soft" tone="destructive" size="md" label="已超时" /> </View> </View> )}色调
定位到「色调」neutral / success / warning / destructive / info / brand 六种色调,quiet 是「默认值」(常规客诉这类),不占底色。icon 放在文字前。
import { View } from 'nsh'import { Tag } from 'nacre'
export function TagTones() { return ( <View class="gap-3"> <View class="flex-row flex-wrap items-center gap-2"> <Tag variant="dot" tone="neutral" label="未分配" /> <Tag variant="dot" tone="success" label="已完成" /> <Tag variant="dot" tone="warning" label="处理中" /> <Tag variant="dot" tone="destructive" label="已超时" /> <Tag variant="dot" tone="info" label="已转接" /> <Tag variant="dot" tone="brand" label="AI" /> </View> <View class="flex-row flex-wrap items-center gap-2"> <Tag tone="quiet" label="常规" /> <Tag tone="warning" label="重大" /> <Tag tone="destructive" label="严重" /> <Tag tone="info" label="咨询" /> </View> <View class="flex-row flex-wrap items-center gap-2"> <Tag variant="soft" tone="success" label="配置正常" /> <Tag variant="soft" tone="warning" icon="warning" label="缺少回调地址" /> <Tag variant="soft" tone="info" label="系统通知" /> <Tag variant="soft" tone="brand" label="AI 建议" /> </View> </View> )}可点与展开
定位到「可点与展开」给 onPress 就可点(质检标签定位到条目),悬停加深一档;「+N / 收起」这类控制展开的标签传 expanded。
手机:给 onPress 就可点(质检标签定位到条目),按下加深一档;「+N / 收起」这类控制展开的标签传 expanded。
import { Text, View, signal } from 'nsh'import { Tag, TEXT_SIZE } from 'nacre'
export function TagPressable() { const located = signal(0) const more = signal(false) return ( <View class="flex-row flex-wrap items-center gap-2"> <Tag testID="tag-press" tone="destructive" label="AI 差错 · 未致歉" onPress={() => located.update((n) => n + 1)} /> <Tag variant="soft" tone="warning" label="实时命中 2" onPress={() => located.update((n) => n + 1)} /> <Tag testID="tag-more" variant="outline" label={more() ? '收起' : '+3'} expanded={more()} onPress={() => more.update((v) => !v)} /> <Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{`$已定位 ${located()}$ 次`}</Text> </View> )}可选、可删、添加
定位到「可选、可删、添加」备注标签:selected 选中换底并在行尾打勾,未选中的给 onRemove 放删除钮,勾与删除钮共用一个槽,切换时宽度不变;末尾用 dashed 标签添加。删掉一个后用 removeRef 把焦点交给相邻标签的删除钮,键盘可以连删。
import { Text, View, signal } from 'nsh'import type { ViewRef } from 'nsh'import { Tag, TEXT_SIZE } from 'nacre'
export function TagSelectable() { const words = signal(['需回电', '已转二线', '资料待补', 'VIP', '投诉倾向']) const picked = signal(['需回电']) const toggle = (word: string) => picked.update((list) => (list.includes(word) ? list.filter((w) => w !== word) : [...list, word])) const removeButtons = new Map<string, ViewRef>() /** 删掉后焦点交给后面(没有就前面)最近一个可删标签的删除钮,键盘连删不丢焦点 */ const remove = (word: string) => { const index = words().indexOf(word) const rest = words().filter((w) => w !== word) const removable = (w: string) => !picked().includes(w) const next = rest.slice(index).find(removable) ?? rest.slice(0, index).reverse().find(removable) removeButtons.delete(word) words.set(rest) if (next !== undefined) removeButtons.get(next)?.focus() } const add = () => { const word = '新标签' words.update((list) => (list.includes(word) ? list : [...list, word])) } return ( <View class="w-[360px] max-w-full gap-2"> <View class="flex-row flex-wrap items-center gap-1"> <Each of={words()} key={(word) => word}> {(word) => ( <Tag testID={`tag-pick-${word()}`} label={word()} selected={picked().includes(word())} onPress={() => toggle(word())} onRemove={picked().includes(word()) ? undefined : () => remove(word())} removeRef={(node) => removeButtons.set(word(), node)} /> )} </Each> <Tag testID="tag-add" variant="dashed" icon="plus" label="添加标签" onPress={add} /> </View> <Text class={`${TEXT_SIZE.caption} text-muted-foreground`}>{`$已选:${picked().join('、') || '无'} · $共 ${words().length}$ 个`}</Text> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Tab | 聚焦到可点的标签;可删的标签,删除钮单独一站 |
| Space / Return | 按下(切换选中、展开);在删除钮上删掉标签,焦点交给 removeRef 指定的位置 |
无障碍
定位到「无障碍」- dot-plain 不画底色,不传 tone 则只显示文字。用于表格枚举和授权状态;不是坐席在线标识。
- 只有可点的标签是 button,读屏名是 label;静态标签只是文字。
- 可选 / 可删的标签整枚不是按钮:文字那段是按钮,删除钮是旁边并排的另一个按钮,读屏名默认「删除」(removeLabel 可改)。
- selected 报选中态,expanded 报展开态;选中时打勾,不只靠颜色区分。
该用 / 不该用
定位到「该用 / 不该用」该用
- 状态用 dot,标记用 text,强调用 soft
- 可选标签选中时打勾
- 同一列的状态用同一种长相和尺寸
不该用
- 用颜色作为唯一区分
- 用 Tag 表示人、技能、分组(用 Pill)
- 用 Tag 做普通按钮
API
定位到「API」Tag
定位到「Tag」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
label * |
string |
— | |
tone |
TagTone |
'neutral' |
|
variant |
TagVariant |
'text' |
|
size |
'sm' | 'md' |
— | sm 18 高 11pt(行内标记,默认);md 20 高 12pt(表格与详情里的状态) |
icon |
IconName |
— | 文字前的小图标(11pt) |
truncate |
boolean |
— | 放不下时截断,悬停看全文;默认保持原宽 |
tooltip |
string |
— | 悬停提示;truncate 时默认是 label |
onPress |
() => void |
— | 可点(质检标签定位到条目):悬停加深一档 |
selected |
boolean |
— | 可选的标签(备注标签):选中换 selected-tint 底、正文色字,行尾 16 宽的槽里放勾(非颜色指示,不改字重); 和 onRemove 共用这个槽,未选中可删、选中打勾时宽度不变 |
expanded |
boolean |
— | 控制展开的标签(「+N / 收起」)报给读屏的展开态 |
onRemove |
() => void |
— | 给了就在行尾放 16 的删除钮(2xs IconButton);testID 落在标签本体上,删除钮是 ${testID}-remove |
removeLabel |
string |
— | 删除钮的无障碍名与提示,默认「删除」 |
removeRef |
(node: ViewRef) => void |
— | 删除钮的节点:删掉一个后把焦点交给相邻标签的删除钮(或输入框),键盘连删不丢焦点 |
testID |
string |
— | |
class |
string |
— | 根节点的布局类 |
* 必填