按钮触发的一组动作。菜单项(MenuEntry)用 type 区分 item / heading / separator,可带图标、快捷键、勾选、禁用、危险项;选完即收起。
状态:stable · 组件:DropdownMenu
引入
定位到「引入」import { DropdownMenu } from 'nacre'用法
定位到「用法」最常见的是表格行尾、卡片角上的「更多」:IconButton 触发,开着时收起它的悬停提示(tooltip={open() ? null : undefined})。
import { Text, View, signal } from 'nsh'import { DropdownMenu, IconButton, TEXT_SIZE } from 'nacre'
export function DropdownMenuUsage() { const last = signal('') return ( <View class="flex-row items-center gap-3"> <DropdownMenu trigger={(open) => <IconButton icon="more" label="行操作" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} />} items={() => [ { label: '查看', onSelect: () => last.set('查看') }, { label: '分配', onSelect: () => last.set('分配') }, ]} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次操作:${last() || '—'}`}</Text> </View> )}示例
定位到「示例」菜单项
定位到「菜单项」heading 组标题、icon 图标、shortcut 行尾快捷键、checked 勾选、disabled 禁用、separator 分隔线、destructive 危险项(放最后)。
手机:手机上菜单行 44 高、不显示 shortcut;图标、勾选、禁用、分隔线与危险项同桌面。
import { Text, View, signal } from 'nsh'import { Button, DropdownMenu, TEXT_SIZE } from 'nacre'
export function DropdownMenuEntries() { const last = signal('') const pinned = signal(true) return ( <View class="flex-row items-center gap-3"> <DropdownMenu align="start" trigger={(open) => ( <Button variant="outline" icon="more" trailingIcon="chevron-down" expanded={open()} pressed={open()} tooltip={open() ? null : undefined}> 操作 </Button> )} items={() => [ { type: 'heading', label: '会话' }, { label: '复制链接', icon: 'copy', shortcut: '⌘C', onSelect: () => last.set('复制链接') }, { label: '导出…', icon: 'export', shortcut: '⇧⌘E', onSelect: () => last.set('导出') }, { label: '置顶', checked: pinned(), onSelect: () => pinned.update((v) => !v) }, { label: '转接(无权限)', icon: 'reply', disabled: true }, { type: 'separator' }, { label: '删除会话', icon: 'trash', destructive: true, onSelect: () => last.set('删除') }, ]} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$选了:${last() || '—'}`}</Text> </View> )}条件项
定位到「条件项」items 每次打开时取值;按权限出现的项写成 cond && { … },假值自动跳过。
import { Text, View, signal } from 'nsh'import { Button, DropdownMenu, Switch, TEXT_SIZE } from 'nacre'
export function DropdownMenuConditional() { const canTransfer = signal(false) return ( <View class="flex-row flex-wrap items-center gap-3"> <DropdownMenu align="start" trigger={(open) => ( <Button variant="outline" trailingIcon="chevron-down" expanded={open()} pressed={open()}> 会话操作 </Button> )} items={() => [ { label: '标记为待跟进', icon: 'star', onSelect: () => {} }, canTransfer() && { label: '转接给其他坐席', icon: 'reply', onSelect: () => {} }, { type: 'separator' }, { label: '结束会话', icon: 'close', destructive: true, onSelect: () => {} }, ]} /> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>有转接权限</Text> <Switch checked={canTransfer()} onChange={(next) => canTransfer.set(next)} accessibilityLabel="有转接权限" /> </View> )}悬停展开
定位到「悬停展开」openOnHover:指针停在触发器上 150ms 也展开,用在表格行尾这类密集的「更多」。
手机:手机没有悬停:openOnHover 不起作用,点按触发器展开;菜单行 44 高,不显示快捷键。
import { DropdownMenu, IconButton } from 'nacre'
export function DropdownMenuHover() { return ( <DropdownMenu openOnHover trigger={(open) => <IconButton icon="more" label="更多操作" pressed={open()} expanded={open()} tooltip={open() ? null : undefined} />} items={() => [ { label: '查看详情', icon: 'eye', onSelect: () => {} }, { label: '复制工单号', icon: 'copy', onSelect: () => {} }, { label: '完结', icon: 'check', onSelect: () => {} }, ]} /> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Space / Return | 在触发器上:展开;在菜单项上:选中并收起 |
| ↑ ↓ | 在可选的项之间移动(跳过组标题、分隔线与禁用项) |
| Home / End | 到第一项 / 最后一项 |
| 字母键 | 跳到以该字母开头的项 |
| Esc | 收起,焦点回到触发器 |
无障碍
定位到「无障碍」- 触发器传 expanded,读屏会说「已展开 / 已收起」;只有图标的触发器用 IconButton,label 写动作(「行操作」)。
- 勾选项读作 checked;没有 onSelect 的普通项读作禁用。
- 组标题必须显式写 type: ‘heading’,读屏与键盘都跳过它。
该用 / 不该用
定位到「该用 / 不该用」该用
- 危险项标红(destructive)放最后,前面加分隔线
- 组标题显式写 heading
- 快捷键写在 shortcut,不拼进 label
不该用
- 用来选值(用 Select)
- 右键菜单用自绘(用系统菜单)
- 只有一两项时还收进菜单(直接放按钮)
API
定位到「API」DropdownMenu
定位到「DropdownMenu」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
trigger * |
JSX.Element | ((open: () => boolean) => JSX.Element) |
— | 触发器(一般是一枚 IconButton),按下开合。写成函数时拿到「菜单开着吗」:开着时把触发器的悬停提示收起 (NSH 不会替你收,提示会压在菜单第一行上,FINDINGS 49)。只调用一次,open 是读值函数,不会因开合重建触发器 |
items * |
() => readonly DropdownEntry[] |
— | 菜单项,每次打开时取值;{ type: 'separator' } 是分隔线,只有 label 没有 onSelect 的是组标题 |
placement |
'top' | 'bottom' | 'left' | 'right' |
'bottom' |
弹层落在触发器哪一侧,默认下 |
align |
'start' | 'center' | 'end' |
'end' |
与触发器对齐,默认 end:这些菜单都在窗口右侧,靠右不出界 |
offset |
number |
4 |
|
openOnHover |
boolean |
— | 指针停在触发器上 150ms 也展开(表格行尾「更多」);悬停开着时再点触发器不收起 |
* 必填