有触发按钮的表单或日历用 Popover,悬停预览用 HoverPreview。选区旁的操作和停靠在窗口边缘的面板用 FloatingLayer,关闭时机由调用方控制。
手机:有触发按钮的表单或日历用 Popover;手机上悬停预览改为点按打开。
状态:experimental · 组件:Popover、HoverPreview、FloatingLayer
引入
定位到「引入」import { Popover, HoverPreview, FloatingLayer } from 'nacre'用法
定位到「用法」受控:open + onOpenChange(open, reason)。触发器写成函数,open() 交给 expanded / pressed,ref 交给 viewRef,关闭后焦点才回得到触发器。默认 card 面:内容卡动效、内缩 12。
import { Text, View, signal } from 'nsh'import { IconButton, Popover, TEXT_SIZE } from 'nacre'
export function PopoverUsage() { const open = signal(false) const reason = signal('') return ( <View class="flex-row items-center gap-3"> <Popover open={open()} onOpenChange={(next, why) => { open.set(next) if (!next) reason.set(why) }} testID="demo-popover" accessibilityLabel="客户等级说明" width={280} trigger={({ open, ref }) => ( <IconButton testID="demo-popover-trigger" icon="info" label="客户等级说明" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} viewRef={ref} /> )} > <View class="gap-1"> <Text class={`${TEXT_SIZE.body} font-medium text-popover-foreground`}>客户等级</Text> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}> 按近 90 天资产与交易量评定,每月 1 日更新。 </Text> </View> </Popover> <Text testID="demo-popover-reason" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次关闭原因:${reason() || '—'}`}</Text> </View> )}示例
定位到「示例」独立锚点与停靠
定位到「独立锚点与停靠」参照 Floating UI 虚拟元素(https://floating-ui.com/docs/virtual-elements):原生 Overlay 直接接选区矩形、点或视图,也可传窗口 left/bottom。与 Presence 配合退场;不移动焦点,只有显式提供回调才响应 Esc 和点外。
import { Text, View, signal } from 'nsh'import type { ViewRef } from 'nsh'import { Button, FloatingLayer, Presence, TEXT_SIZE } from 'nacre'
export function FloatingAnchor() { const visible = signal(false) let anchor: ViewRef | undefined const cancel = () => { visible.set(false) anchor?.focus() } const label = '查看说明' return ( <View class="flex-row gap-2"> <Button testID="demo-float-show" viewRef={(node) => (anchor = node)} expanded={visible()} onPress={() => visible.set(!visible())}>{label}</Button> <Presence when={visible() && anchor}> {(target, leave) => ( <FloatingLayer testID="demo-float" position={{ anchor: target(), placement: 'top', offset: 8, margin: 8 }} leave={leave} class="p-3" onCancel={cancel} onPressOutside={() => visible.set(false)}> <Text class={`${TEXT_SIZE.small} text-popover-foreground`}>此说明跟随锚点。</Text> </FloatingLayer> )} </Presence> </View> )}只读悬停预览
定位到「只读悬停预览」参照 Radix Hover Card 的延迟显示和移入卡片后继续阅读:https://www.radix-ui.com/primitives/docs/components/hover-card 。NSH 原生浮层在桌面悬停 300ms 后打开;另外提供键盘和触屏可用的独立按钮,预览不执行行的主动作。含表单时用 Popover。
手机:手机没有悬停:预览改为点按打开,再点外面收起。
import { Text } from 'nsh'import { Button, HoverPreview, TEXT_SIZE } from 'nacre'
export function HoverPreviewUsage() { const label = '查看预览' return ( <HoverPreview accessibilityLabel={label} testID="demo-hover-preview" trigger={({ open, toggle, ref }) => <Button testID="demo-preview-trigger" viewRef={ref} expanded={open()} onPress={toggle}>{label}</Button>} > <Text class={`p-3 ${TEXT_SIZE.body} text-popover-foreground`}>附件预览</Text> </HoverPreview> )}子菜单接管
定位到「子菜单接管」obscured 暂时隐藏整张面,保留内容与定位;子菜单关闭后恢复,避免玻璃叠影。
import { View, signal } from 'nsh'import { Button, Popover, TextField } from 'nacre'
export function PopoverObscured() { const open = signal(false) const obscured = signal(false) return <View class="flex-row gap-3"> <Popover testID="obscured-panel" open={open()} onOpenChange={(next, reason) => { if (reason !== 'pressOutside') open.set(next) }} obscured={obscured()} trigger={(state) => <Button testID="obscured-trigger" expanded={state.open()} viewRef={state.ref}>打开面板</Button>}> <TextField testID="obscured-input" accessibilityLabel="备注" /> </Popover> <Button testID="obscured-toggle" onPress={() => obscured.set(!obscured())}>切换遮挡</Button> </View>}菜单型
定位到「菜单型」surface=“menu”:一列可选的行,菜单档动效(ADR 0020:直接出现、退场淡出)。里面的 Combobox 自带内缩,所以加 flush。
import { signal } from 'nsh'import { Combobox, Popover, SelectTrigger } from 'nacre'
export function PopoverMenu() { const open = signal(false) const value = signal('online') const options = [ { value: 'online', label: '在线' }, { value: 'busy', label: '忙碌' }, { value: 'away', label: '离开' }, ] const label = () => options.find((o) => o.value === value())?.label ?? '' return ( <Popover surface="menu" flush open={open()} onOpenChange={(next) => open.set(next)} testID="demo-popover-menu" trigger={({ open, ref }) => ( <SelectTrigger testID="demo-popover-menu-trigger" accessibilityLabel="接待状态" value={label()} expanded={open()} viewRef={ref} /> )} > <Combobox testID="demo-popover-menu-list" accessibilityLabel="接待状态" options={options} value={[value()]} minWidth={160} onChange={(values) => { if (values[0]) value.set(values[0]) }} onDone={() => open.set(false)} /> </Popover> )}内容卡型
定位到「内容卡型」带标题栏、分隔线通栏的面板:flush 让内容自己排内边距;width 定宽;标题栏的关闭钮直接把 open 设成 false。
import { Text, View, signal } from 'nsh'import { IconButton, Popover, TEXT_SIZE } from 'nacre'
export function PopoverCard() { const open = signal(false) return ( <Popover flush open={open()} onOpenChange={(next) => open.set(next)} align="end" width={300} testID="demo-popover-card" accessibilityLabel="下载中心" trigger={({ open, ref }) => ( <IconButton icon="download" label="下载中心" pressed={open()} expanded={open()} viewRef={ref} /> )} > {/* 带标题栏的面板:flush 后自己排内边距,分隔线通栏 */} <View class="flex-row items-center h-10 px-3 border-b border-border"> <Text class={`flex-1 ${TEXT_SIZE.body} font-medium text-popover-foreground`}>下载中心</Text> <IconButton icon="close" size="xs" label="关闭" onPress={() => open.set(false)} /> </View> <View class="p-3 gap-1"> <Text class={`${TEXT_SIZE.body} text-popover-foreground`}>会话记录_1125.xlsx</Text> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>已完成 · 128 KB</Text> </View> </Popover> )}受控关闭
定位到「受控关闭」按 reason 决定关不关:写了一半的备注点外面不关(忽略 pressOutside),Esc、触发器、取消、保存才关;关闭后焦点回到「备注」按钮。
手机:按 reason 决定关不关:写了一半的备注点外面不关,点取消、保存或触发器才关。
import { Text, View, signal } from 'nsh'import { Button, Popover, TextField, TEXT_SIZE } from 'nacre'
export function PopoverControlled() { const open = signal(false) const note = signal('') const saved = signal('') return ( <View class="flex-row items-center gap-3"> <Popover open={open()} // 只认 Esc 与触发器:点外面不关,免得写了一半的备注丢掉 onOpenChange={(next, why) => { if (why !== 'pressOutside') open.set(next) }} width={280} testID="demo-popover-note" trigger={({ open, ref }) => ( <Button testID="demo-popover-note-trigger" variant="outline" icon="pencil" expanded={open()} pressed={open()} viewRef={ref}> 备注 </Button> )} > <View class="gap-3"> <TextField testID="demo-popover-note-field" size="sm" accessibilityLabel="备注" placeholder="写给同事看的备注" value={note()} onChange={(v) => note.set(v)} /> <View class="flex-row justify-end gap-2"> <Button size="sm" variant="ghost" onPress={() => open.set(false)}>取消</Button> <Button testID="demo-popover-note-save" size="sm" variant="primary" onPress={() => { saved.set(note()) open.set(false) }} > 保存 </Button> </View> </View> </Popover> <Text testID="demo-popover-note-saved" class={`${TEXT_SIZE.small} text-muted-foreground`}>{saved() || '—'}</Text> </View> )}打开时焦点移进浮层
定位到「打开时焦点移进浮层」onOpen 在内容渲染后回调,在这里 focus 第一项:指针点开时焦点本来留在原处(比如旁边的编辑器),⏎、方向键会落到它上面。选完关闭,焦点回到触发器。
手机:onOpen 在内容渲染后回调,在这里把焦点交给第一项(外接键盘与读屏时有用)。
import { Text, View, signal } from 'nsh'import type { ViewRef } from 'nsh'import { Button, Popover, TEXT_SIZE } from 'nacre'
export function PopoverFocusInside() { const open = signal(false) const picked = signal('') let first: ViewRef | undefined const pick = (value: string) => { picked.set(value) open.set(false) } return ( <View class="flex-row items-center gap-3"> <Popover open={open()} onOpenChange={(next) => open.set(next)} // 打开后焦点移到第一项:旁边有吃 ⏎ 的编辑器时,⏎ 落在浮层里 onOpen={() => first?.focus()} testID="demo-popover-focus" accessibilityLabel="插入" trigger={({ open, ref }) => ( <Button testID="demo-popover-focus-trigger" variant="outline" icon="plus" expanded={open()} pressed={open()} viewRef={ref}> 插入 </Button> )} > <View class="flex-row gap-2"> <Button testID="demo-popover-focus-first" size="sm" viewRef={(node) => (first = node)} onPress={() => pick('链接')}> 链接 </Button> <Button size="sm" onPress={() => pick('图片')}> 图片 </Button> </View> </Popover> <Text testID="demo-popover-focus-picked" class={`${TEXT_SIZE.small} text-muted-foreground`}>{picked() || '—'}</Text> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Space / Return | 在触发器上:开合 |
| Esc | 关闭(reason 为 cancel),焦点回到触发器 |
| Tab | 在浮层内容里的控件之间移动 |
无障碍
定位到「无障碍」- 触发器传 expanded,读屏会说「已展开 / 已收起」;只有图标的触发器用 IconButton,label 写这个浮层是什么。
- accessibilityLabel 给浮层本身起名(「客户等级说明」「转接给…」),内容里没有标题时必填。
- 关闭后焦点回到触发器(returnFocus,默认开);点外面关闭的不回,焦点跟着指针走。
该用 / 不该用
定位到「该用 / 不该用」该用
- 一列可选的行用 surface=“menu”,表单、日历、面板用 card
- 带标题栏或自带内缩的内容加 flush
- 大块日历浮在密集文字上时加 solid
- 触发器写成函数,把 ref 交给 viewRef
- 触发器旁边有吃 ⏎ / 方向键的编辑器时,用 onOpen 把焦点移进浮层
不该用
- 一列动作(用 DropdownMenu)
- 选一个值(用 SelectMenu / Combobox)
- 直接用 nsh/base 的 Popover 拼 FLOAT_CLASS、popupMotion
- 浮层里再开浮层
- 要打断当前操作的确认(用 Dialog)
API
定位到「API」Popover
定位到「Popover」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
open * |
boolean |
— | 开着吗(受控):在 onOpenChange 里改它 |
onOpenChange * |
(open: boolean, reason: OpenChangeReason) => void |
— | 要开 / 要关,带原因(nsh/base 的 OpenChangeReason):triggerPress 点触发器、pressOutside 点外面、cancel Esc。 内容里自己关(选完、点「取消」)直接把 open 设成 false,不经这里 |
trigger * |
JSX.Element | ((state: PopoverTriggerState) => JSX.Element) |
— | 触发器,按下开合。写成函数时拿到 { open, ref }:open() 交给 expanded(读屏「已展开」)与 pressed; ref 交给触发器的 viewRef(Button / IconButton / SelectTrigger 都有),关闭后焦点才回得去。只调用一次 |
placement |
PopoverPlacement |
'bottom' |
落在触发器哪一侧,默认下;放不下时换到对面 |
align |
'start' | 'center' | 'end' |
'start' |
沿那一侧怎么对齐,默认 start |
offset |
number |
4 |
与触发器的间距,默认 4 |
surface |
'menu' | 'card' |
'card' |
面的种类,决定进退场(popupMotion)与内缩: menu 一列可选的行(选值、筛选、选项列表):菜单档动效,内缩 6(外圆角 14 − 6 = 行圆角 8); card 内容卡(表单、日历、说明、面板):内容卡档动效,内缩 12。默认 card |
flush |
boolean |
— | 内容自己排内边距(带标题栏、分隔线通栏的面板;自带内缩的列表):面不再内缩 |
solid |
boolean |
— | 不透明面:大块内容(日历)浮在密集文字上时用,玻璃会把底下的字透成重影 |
obscured |
boolean |
— | 子菜单接管显示时暂时隐藏整张面,保留挂载与定位(避免半透明面叠影) |
width |
number |
— | 面的宽度(pt);不给时随内容 |
returnFocus |
boolean |
true |
关闭后焦点回到触发器,默认 true;点外面关的不回(焦点跟着指针去了别处)。要函数式触发器把 ref 交给 viewRef |
onPosition |
(position: PopoverPosition) => void |
— | 每次定位后回调(打开、窗口缩放):内容按 available.height 限高、超出内部滚动时用 |
onOpen |
() => void |
— | 打开且内容已渲染后回调:要把焦点移进浮层时在这里 focus() 第一项(拿内容里的 viewRef)。 触发器旁边有吃 ⏎ / 方向键的编辑器时用,免得浮层开着按键还落到编辑器上 |
testID |
string |
— | 面的 testID |
accessibilityLabel |
string |
— | 读屏对浮层的称呼(「转接给…」「自定义时间」) |
children |
JSX.Element |
— | 内容,每次打开时创建 |
* 必填
HoverPreview
定位到「HoverPreview」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
trigger * |
(state: HoverPreviewTrigger) => JSX.Element |
— | 悬停区域;把 ref / toggle 交给独立预览按钮的 viewRef / onPress。按钮主动打开后,移开指针不关闭;预览里的动作跳走时调 close |
placement |
PopoverPlacement |
'bottom' |
|
accessibilityLabel * |
string |
— | |
testID |
string |
— | |
children |
JSX.Element |
— |
* 必填
FloatingLayer
定位到「FloatingLayer」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
position * |
FloatingPosition |
— | 原生选区、点或视图锚点(自动避边),或距窗口左、下缘的停靠位置 |
surface |
'glass' | 'panel' | 'compact' |
— | glass 通知卡;panel 不透明的操作面板;compact 单个选区操作 |
leave * |
Leave |
— | 与 Presence 共用退场生命周期 |
onCancel |
() => void |
— | 只在传入时监听;来电卡不接管 Esc 或点外事件 |
onPressOutside |
(event: { pagePosition: Point }) => void |
— | |
accessibilityLabel |
string |
— | |
class |
string |
— | |
testID |
string |
— | |
children * |
JSX.Element |
— |
* 必填