入口原地弹出的菜单:在侧栏底部的账号行位置向上弹出一块面板,底行(GrowMenuFooter)与触发行同位同内容;直接出现、收起淡出,不变形。项可带状态点、图标、字块与子菜单。
状态:experimental · 组件:GrowMenu、GrowMenuFooter、MenuMark
引入
定位到「引入」import { GrowMenu, GrowMenuFooter, MenuMark } from 'nacre'用法
定位到「用法」left / bottom / width 定面板在窗口里的位置与宽,底边对齐触发行;footer 与触发行同一个内容。
import { signal } from 'nsh'import type { ViewRef } from 'nsh'import { Avatar, Button, GrowMenu, GrowMenuFooter } from 'nacre'
export function GrowMenuUsage() { const open = signal(false) /** 键盘(Enter / Space)打开时菜单焦点落到第一行 */ const byKeyboard = signal(false) let trigger: ViewRef | undefined return ( <> <Button viewRef={(node) => (trigger = node)} variant="outline" pressed={open()} expanded={open()} onPress={(e) => { byKeyboard.set(e.clickCount === 0) open.set(!open()) }}> 打开账号菜单 </Button> <GrowMenu testID="demo-grow" returnFocus={() => trigger} focusFirst={byKeyboard()} open={open()} onClose={() => open.set(false)} left={24} bottom={24} width={240} items={[ { type: 'heading', label: '在线 · 4 个会话' }, { label: '在线', presence: 'online', checked: true, onSelect: () => open.set(false) }, { label: '小休', presence: 'break', onSelect: () => open.set(false) }, { type: 'separator' }, { label: '外观', icon: 'sun-moon', submenu: [ { label: '跟随系统', checked: true, onSelect: () => {} }, { label: '浅色', onSelect: () => {} }, { label: '深色', onSelect: () => {} }, ], }, { label: '退出登录', icon: 'logout', destructive: true, onSelect: () => open.set(false) }, ]} footer={ <GrowMenuFooter leading={<Avatar name="智源" self size={28} />} title="智源" subtitle="在线 · 00:12:40" onPress={() => open.set(false)} /> } /> </> )}示例
定位到「示例」侧栏收起时
定位到「侧栏收起时」侧栏只剩图标时面板仍用菜单的宽(不跟着触发钮变窄);MenuMark 负责行首的图标、状态点、色块或文字,示例里用 mark 显示语言,footer 的副行用 subtitleTone=“warning” 提示连接中断。
import { signal } from 'nsh'import type { ViewRef } from 'nsh'import { Avatar, Button, GrowMenu, GrowMenuFooter } from 'nacre'
export function GrowMenuCollapsed() { const open = signal(false) /** 键盘(Enter / Space)打开时菜单焦点落到第一行 */ const byKeyboard = signal(false) const lang = signal('zh-CN') let trigger: ViewRef | undefined const pick = (value: string) => { lang.set(value) open.set(false) } return ( <> <Button viewRef={(node) => (trigger = node)} variant="outline" pressed={open()} expanded={open()} onPress={(e) => { byKeyboard.set(e.clickCount === 0) open.set(!open()) }}> 从收起的侧栏打开 </Button> <GrowMenu returnFocus={() => trigger} focusFirst={byKeyboard()} open={open()} onClose={() => open.set(false)} left={24} bottom={24} width={240} items={[ { label: '语言', icon: 'translate', submenu: [ { label: '简体中文', mark: '简', checked: lang() === 'zh-CN', onSelect: () => pick('zh-CN') }, { label: '繁體中文', mark: '繁', checked: lang() === 'zh-HK', onSelect: () => pick('zh-HK') }, { label: 'English', mark: 'EN', checked: lang() === 'en', onSelect: () => pick('en') }, ], }, { type: 'separator' }, { label: '退出登录', icon: 'logout', destructive: true, onSelect: () => open.set(false) }, ]} footer={ <GrowMenuFooter leading={<Avatar name="智源" self size={28} />} title="智源" subtitle="连接中断,正在重连…" subtitleTone="warning" onPress={() => open.set(false)} /> } /> </> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| ↑ ↓ | 依次经过可操作项和底部收起行,到头回绕。跳过禁用项和折叠的子项 |
| Home / End | 到首个可操作项 / 底部收起行 |
| Space / Return | 选中当前行;带子菜单的行原地展开或收起 |
| Esc | 收起,焦点回到入口(returnFocus) |
无障碍
定位到「无障碍」- MenuMark 绘制行首标记,菜单项提供读屏名称。
- 面板的 role 是 menu;勾选项读作 checked,带子菜单的行读作 expanded / collapsed。
- GrowMenuFooter 是按钮,读屏名默认是 title,可用 accessibilityLabel 换掉。
- 一定要给 returnFocus:收起后键盘焦点回到入口(WCAG 2.4.3)。
该用 / 不该用
定位到「该用 / 不该用」该用
- 关闭后焦点回到入口(returnFocus)
- 组标题显式写 heading;长说明(出错原因)也写成 heading,放不下会折行
- 同一时间只展开一个子菜单(组件已保证)
- 连着勾几项的开关项写 keepOpen,选了不收
不该用
- 用于普通下拉(用 DropdownMenu)
- 入口下方没有地方时用(面板只会向上长)
API
定位到「API」GrowMenu
定位到「GrowMenu」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
open * |
boolean |
— | |
onClose * |
() => void |
— | |
items * |
readonly GrowMenuEntry[] |
— | |
footer * |
JSX.Element |
— | 面板底部那一行:与触发行同一个位置、同一个内容,按下收起 |
left * |
number |
— | 面板在窗口里的位置(左、下)与宽度:底边对齐触发行,向上长 |
bottom * |
number |
— | |
width * |
number |
— | |
testID |
string |
— | |
returnFocus |
() => ViewRef | undefined |
— | 收起后把键盘焦点交还的触发元素(WCAG 2.4.3:焦点回到触发点,与 DropdownMenu 一致) |
focusFirst |
boolean |
— | 打开后把焦点放到第一个可用行(显示当前行):键盘(Enter / Space,PressEvent.clickCount === 0)打开时给 true, 同 DropdownMenu;指针打开时不给,macOS 菜单不预先高亮(2026-11-27 组件交互复评:键盘打开后看不到当前行) |
* 必填
GrowMenuFooter
定位到「GrowMenuFooter」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
leading * |
JSX.Element |
— | 行首:和触发行同一个头像 / 图标(28) |
title * |
string |
— | |
subtitle |
string |
— | |
subtitleTone |
'muted' | 'warning' |
— | 副行的语气:warning 是连接中断这类要注意的状态 |
onPress * |
() => void |
— | 按下收起菜单 |
accessibilityLabel |
string |
— | |
testID |
string |
— |
* 必填
MenuMark
定位到「MenuMark」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
item * |
MarkedItem |
— |
* 必填