Popover
Use Popover for a form or calendar with a trigger and HoverPreview for a hover preview. Use FloatingLayer for selection actions or a panel docked to the window. Its caller controls dismissal.
Phone: Use Popover for a form or calendar opened by a trigger; on phones the hover preview opens on tap.
Status: experimental · Components: Popover, HoverPreview, FloatingLayer
Import
Section titled “Import”import { Popover, HoverPreview, FloatingLayer } from 'nacre'Controlled: open + onOpenChange(open, reason). Write the trigger as a function: pass open() to expanded / pressed and ref to viewRef so focus can return to the trigger on close. The default card surface uses the content-card motion and a 12pt inset.
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="Customer tier notes" width={280} trigger={({ open, ref }) => ( <IconButton testID="demo-popover-trigger" icon="info" label="Customer tier notes" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} viewRef={ref} /> )} > <View class="gap-1"> <Text class={`${TEXT_SIZE.body} font-medium text-popover-foreground`}>Customer tier</Text> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}> Rated by assets and trading volume over the last 90 days; updated on the 1st of each month. </Text> </View> </Popover> <Text testID="demo-popover-reason" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$Last close reason: ${reason() || '—'}`}</Text> </View> )}Examples
Section titled “Examples”Independent anchor or window dock
Section titled “Independent anchor or window dock”Like Floating UI virtual elements (https://floating-ui.com/docs/virtual-elements), NSH can anchor to selection bounds, a point or a view, or dock by left/bottom. Presence owns the exit. Focus stays in place; Escape and outside presses are opt-in.
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 = 'Show details' 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`}>This note follows its anchor.</Text> </FloatingLayer> )} </Presence> </View> )}Read-only hover preview
Section titled “Read-only hover preview”Like Radix Hover Card (https://www.radix-ui.com/primitives/docs/components/hover-card), this delays opening and stays visible during pointer travel into the card. NSH positions the surface. Hover for 300ms or use the separate keyboard and touch button. Previewing does not run the row action. Use Popover for forms.
Phone: Phones have no hover: the preview opens on tap and closes when you tap outside.
import { Text } from 'nsh'import { Button, HoverPreview, TEXT_SIZE } from 'nacre'
export function HoverPreviewUsage() { const label = 'Preview' 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`}>Attachment preview</Text> </HoverPreview> )}Covered by a child menu
Section titled “Covered by a child menu”obscured hides the surface while preserving content and position, avoiding stacked translucent surfaces.
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}>Open panel</Button>}> <TextField testID="obscured-input" accessibilityLabel="Note" /> </Popover> <Button testID="obscured-toggle" onPress={() => obscured.set(!obscured())}>Toggle obscured</Button> </View>}surface=“menu”: a list of choosable rows, with the menu motion (ADR 0020: appears at once, fades out). The Combobox inside has its own inset, hence 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: 'Online' }, { value: 'busy', label: 'Busy' }, { value: 'away', label: 'Away' }, ] 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="Status" value={label()} expanded={open()} viewRef={ref} /> )} > <Combobox testID="demo-popover-menu-list" accessibilityLabel="Status" options={options} value={[value()]} minWidth={160} onChange={(values) => { if (values[0]) value.set(values[0]) }} onDone={() => open.set(false)} /> </Popover> )}Content card
Section titled “Content card”A panel with a header and full-width dividers: flush lets the content lay out its own padding; width fixes the width; the close button in the header sets open to 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="Downloads" trigger={({ open, ref }) => ( <IconButton icon="download" label="Downloads" 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`}>Downloads</Text> <IconButton icon="close" size="xs" label="Close" onPress={() => open.set(false)} /> </View> <View class="p-3 gap-1"> <Text class={`${TEXT_SIZE.body} text-popover-foreground`}>chats_1125.xlsx</Text> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>Done · 128 KB</Text> </View> </Popover> )}Controlled close
Section titled “Controlled close”Decide by reason: a half-written note does not close on a press outside (pressOutside is ignored); Escape, the trigger, Cancel and Save close it, and focus returns to the Note button.
Phone: Decide by reason whether to close: a half-written note stays open on an outside tap and closes only on cancel, save or the trigger.
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}> Note </Button> )} > <View class="gap-3"> <TextField testID="demo-popover-note-field" size="sm" accessibilityLabel="Note" placeholder="A note for teammates" value={note()} onChange={(v) => note.set(v)} /> <View class="flex-row justify-end gap-2"> <Button size="sm" variant="ghost" onPress={() => open.set(false)}>Cancel</Button> <Button testID="demo-popover-note-save" size="sm" variant="primary" onPress={() => { saved.set(note()) open.set(false) }} > Save </Button> </View> </View> </Popover> <Text testID="demo-popover-note-saved" class={`${TEXT_SIZE.small} text-muted-foreground`}>{saved() || '—'}</Text> </View> )}Moving focus inside on open
Section titled “Moving focus inside on open”onOpen runs once the content is rendered; focus the first item there. Opened with the pointer, focus otherwise stays where it was (an editor next to the trigger, say), and Return and the arrow keys go to it. Picking closes it and focus returns to the trigger.
Phone: onOpen fires after the content renders; move focus to the first item there (useful with an external keyboard or a screen reader).
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="Insert" trigger={({ open, ref }) => ( <Button testID="demo-popover-focus-trigger" variant="outline" icon="plus" expanded={open()} pressed={open()} viewRef={ref}> Insert </Button> )} > <View class="flex-row gap-2"> <Button testID="demo-popover-focus-first" size="sm" viewRef={(node) => (first = node)} onPress={() => pick('Link')}> Link </Button> <Button size="sm" onPress={() => pick('Image')}> Image </Button> </View> </Popover> <Text testID="demo-popover-focus-picked" class={`${TEXT_SIZE.small} text-muted-foreground`}>{picked() || '—'}</Text> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Space / Return | On the trigger: open or close |
| Esc | Close (reason cancel) and return focus to the trigger |
| Tab | Move between the controls inside the popover |
Accessibility
Section titled “Accessibility”- The trigger passes expanded, read as expanded / collapsed; an icon-only trigger is an IconButton whose label names the popover.
- accessibilityLabel names the popover itself (“Customer tier notes”, “Transfer to…”); required when its content has no title.
- Focus returns to the trigger on close (returnFocus, on by default); not after a press outside, where focus follows the pointer.
Do / Don’t
Section titled “Do / Don’t”Do
- Use surface=“menu” for a list of choosable rows, card for forms, calendars and panels
- Add flush when the content has a header or its own inset
- Add solid for a large calendar over dense text
- Write the trigger as a function and pass ref to viewRef
- Use onOpen to move focus inside when an editor next to the trigger takes Return and the arrow keys
Don’t
- A list of actions (use DropdownMenu)
- Picking one value (use SelectMenu / Combobox)
- Styling nsh/base’s Popover with FLOAT_CLASS and popupMotion by hand
- A popover inside a popover
- A confirmation that interrupts the task (use Dialog)
Property descriptions follow the Chinese source comments.
Popover
Section titled “Popover”| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— | 内容,每次打开时创建 |
* required
HoverPreview
Section titled “HoverPreview”| Prop | Type | Default | Description |
|---|---|---|---|
trigger * |
(state: HoverPreviewTrigger) => JSX.Element |
— | 悬停区域;把 ref / toggle 交给独立预览按钮的 viewRef / onPress。按钮主动打开后,移开指针不关闭;预览里的动作跳走时调 close |
placement |
PopoverPlacement |
'bottom' |
|
accessibilityLabel * |
string |
— | |
testID |
string |
— | |
children |
JSX.Element |
— |
* required
FloatingLayer
Section titled “FloatingLayer”| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— |
* required