悬停或键盘聚焦时的一句说明。Button / IconButton 直接给 tooltip(与 shortcut);其他元素用 textTooltip(文字, 位置, 快捷键) 包成提示气泡挂到 tooltip 上;字段口径用 HelpTip ⓘ。长文字自己折行,快捷键排在右侧。
状态:stable · 组件:HelpTip、TooltipLabel
引入
定位到「引入」import { HelpTip, TooltipLabel } from 'nacre'用法
定位到「用法」IconButton 不给 tooltip 时用 label 当提示;Button 的 tooltip 与 shortcut 分开写,快捷键排在文案右侧、淡一档。
import { View } from 'nsh'import { Button, IconButton } from 'nacre'
export function TooltipUsage() { return ( <View class="flex-row flex-wrap items-center gap-2"> <IconButton icon="copy" label="复制" /> <Button variant="outline" icon="export" tooltip="导出当前筛选结果" shortcut="⇧⌘E"> 导出 </Button> </View> )}示例
定位到「示例」一个按钮的多个快捷键
定位到「一个按钮的多个快捷键」tooltip 可接收标签和快捷键的数组,每个动作独占一行;按钮本身的读屏名保留。
import { ToolbarButton } from 'nacre'
export function TooltipShortcuts() { return ( <ToolbarButton testID="demo-tip-shortcuts" icon="sparkles" label="写作助手" tooltip={[ { label: '翻译', shortcut: '⌥T' }, { label: '润色', shortcut: '⌥P' }, ]} /> )}截断的用户数据
定位到「截断的用户数据」客户名、公司名这类会截断的用户数据,给全文提示。
import { Text, View } from 'nsh'import { textTooltip, TEXT_SIZE } from 'nacre'
export function TooltipTruncated() { const company = '长桥证券(香港)有限公司机构客户服务部大中华区高净值客户专属服务通道二级子账户' return ( <View class="w-[240px] max-w-full flex-row items-center gap-2"> <Text class={`shrink-0 ${TEXT_SIZE.small} text-muted-foreground`}>客户</Text> <Text class={`flex-1 min-w-0 truncate ${TEXT_SIZE.small} text-foreground`} tooltip={textTooltip(company)}> {company} </Text> </View> )}字段说明 HelpTip
定位到「字段说明 HelpTip」指标、字段的口径:ⓘ 悬停 500ms、键盘聚焦或按下都弹出,同一时刻只开一个。
import { Text, View } from 'nsh'import { HelpTip, TEXT_SIZE } from 'nacre'
export function TooltipHelpTip() { return ( <View class="flex-row items-center gap-1"> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>首次响应时长</Text> <HelpTip text="从客户第一条消息到坐席首次回复,不含排队" /> </View> )}位置与快捷键
定位到「位置与快捷键」独立文字用 TooltipLabel,键盘焦点与提示落在带内边距的外框。普通档左右 8pt、上下 4pt;紧凑时间标签左右 6pt、上下 2pt。
import { View, cleanup, keymap } from 'nsh'import type { ViewRef } from 'nsh'import { TooltipLabel } from 'nacre'
export function TooltipPlacement() { const synced = '最后同步:今天 14:05' const labels = [ { label: '已同步', tooltip: synced }, { label: '已同步(提示在上方)', tooltip: synced, placement: 'top' as const }, { label: '下一个会话', tooltip: '切到下一个会话', shortcut: '⌘]' }, ] const targets: ViewRef[] = [] let unbind: (() => void) | undefined const release = () => { unbind?.(); unbind = undefined } cleanup(release) return ( <View class="flex-row flex-wrap items-center gap-4"> {labels.map((item, index) => ( <View onFocusOut={release} onFocusIn={() => { release() unbind = keymap.bind({ left: () => targets[index - 1]?.focus(), right: () => targets[index + 1]?.focus() }) }}> <TooltipLabel testID={`demo-tooltip-label-${index + 1}`} viewRef={(node) => (targets[index] = node)} label={item.label} tooltip={item.tooltip} placement={item.placement} shortcut={item.shortcut} /> </View> ))} </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Tab | 聚焦到带提示的控件时出现提示 |
| ← → | 在「位置与快捷键」示例中,按 Tab 的顺序前后查看三个文字提示 |
| Esc | 收起 HelpTip 的提示 |
无障碍
定位到「无障碍」- TooltipLabel 读出可见文字,随后读出额外说明和快捷键。
- HelpTip 的读屏名就是提示文字,role 是 button。
- IconButton 的读屏名是 label,提示默认也用它;只靠提示说明含义的元素必须另给 accessibilityLabel。
该用 / 不该用
定位到「该用 / 不该用」该用
- 截断的用户数据给全文提示
- 快捷键用 shortcut 参数,不拼进文案
- 提示只写一句说明
不该用
- 放操作或链接(提示里点不到;放进页面或 Dialog)
- 重复按钮上已经写着的文字
- 给 tooltip 直接传字符串
API
定位到「API」HelpTip
定位到「HelpTip」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
text * |
string |
— | |
testID |
string |
— |
* 必填
TooltipLabel
定位到「TooltipLabel」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
label * |
string |
— | |
tooltip * |
string |
— | |
placement |
'top' | 'bottom' |
— | |
size |
'sm' | 'md' |
— | sm 适配 20pt 的紧凑元信息行。 |
shortcut |
string |
— | |
viewRef |
(node: ViewRef) => void |
— | 把键盘焦点交给这段文字时,聚焦提示本身。 |
testID |
string |
— |
* 必填