Menu
通过子菜单、选择、折叠、主题、键盘和本机链接进行导航。
何时使用
菜单组织应用程序的各个部分,显示所选项目并扩展嵌套导航。下拉菜单适合按钮的简短操作列表;菜单 - 用于稳定的部分结构。页面转换必须保持真实的链接。
菜单 CSS 前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。所有模式、项目、组和子菜单都使用本地前缀。主题和链接设置独立;保留选择和披露。
导航方式及结构
在 Svelte 中,树定义了 items。常规项目具有稳定的 key 和 label、子菜单 - children 菜单、组 - type:group、分隔符 - type:divider。事件键被转换为字符串。label 和 icon 接受片段;将链接放在 label 片段内,因为 MenuItem 没有单独的 href。
| 脚本 | 设置 | 示例 |
|---|---|---|
| 标题面板 | mode="horizontal" |
横向列表 |
| 侧边栏内的树 | mode="inline" |
嵌套部分 |
| 侧面的子菜单 | mode="vertical" |
垂直列表 |
| 窄边栏 | inlineCollapsed、LayoutSider |
Sider 中的菜单 |
| 多个选定项目 | multiple |
选择和命令 |
内嵌模式在列表中展开子菜单,vertical 和 horizontal 模式在独立面板中打开。指向折叠的菜单项可查看标签提示。
选择、开启及申请状态
defaultSelectedKeys 和 defaultOpenKeys 设置初始本地状态。 selectedKeys 和 openKeys - 受控道具:处理 onSelect/onDeselect 和 onOpenChange,然后将更新后的数组传回。不为这些道具提供 bind。
onClick 报告 key、keyPath、domEvent 和 itemData。 Selectable:false 允许命令处于不可选状态。 multiple 允许选择多个键。选择一个项目本身不应该取代应用程序路由:相应的 selectedKeys 通常是从当前地址派生的。
对于“一次一个分支”模式,应用程序存储 openKeys 并保存新子菜单的祖先。常规内联允许多个扩展分支。 手风琴 在不更改菜单数据的情况下显示此逻辑。
弹出窗口、键盘和响应能力
triggerSubMenuAction 选择悬停或 click;打开和关闭延迟以秒为单位。缺少的 getPopupContainer 继承了ConfigProvider,而自己的 undefined 则禁用了继承。这与 Dropdown 不同,在 Dropdown 中,undefined 保存后备。 forceSubMenuRender 提前创建隐藏分支; SSR 不会调用容器的 getter。
箭头、Home/End 和 Enter 移动焦点并打开分支;水平导航和 RTL 会考虑方向。 Escape 关闭弹出窗口。空格不能作为 li 子句的通用激活。通过 bind:this focus(options?) 和 findItem({key});搜索 DOM 节点不会揭示隐藏分支。
快速打开第二层弹出子菜单时,焦点有时会停留在分支标题上。这时按 Enter 会触发标题,子项仍未选中。
缩小水平菜单可将末尾的菜单项收进“更多”,增宽时菜单项会回到原位。
在样式示例中,ConfigProvider.menu 提供通用类名,列表通过 styles 设置内边距。设计令牌控制主题颜色和尺寸。
示例
报告部分。选定的键:
横向导航
空间不足时,末尾的菜单项会移入“更多”子菜单。
<script lang="ts">
import { Menu } from '@woodger/antd';
let selected = $state<string[]>(['overview']);
</script>
{#snippet reports()}
<a href={('#menu-horizontal-report')}>{("报告")}</a>
{/snippet}
<nav aria-label={("项目部分")}>
<Menu
mode="horizontal"
style="
min-width: 0;
flex: 1;
"
aria-label={("水平项目菜单")}
overflowedIndicator={("更多的")}
selectedKeys={selected}
onSelect={info => selected = info.selectedKeys}
items={([
{ key: 'overview', label: "概览" },
{ key: 'archive', label: "档案", disabled: true },
{
key: 'settings', label: "设置", children: [
{ key: 'team', label: "团队" }, { key: 'access', label: "使用权" }
]
},
{ key: 'reports', label: reports }
])}
/>
</nav>
<p id="menu-horizontal-report">
{("报告部分。选定的键: ")}
<output aria-label={("选择水平菜单")}>{selected.join(', ')}</output>
</p>
<style>
nav {
display: flex;
min-width: 0;
}
p {
margin: 16px 0 0;
scroll-margin-top: 160px;
}
</style>
-
- 开始
- 联系
- 主题
- 帮助
嵌套导航
在内联模式下,子菜单在列表内展开。 defaultOpenKeys/defaultSelectedKeys 设置初始状态;嵌套组组合相关项目。
<script lang="ts">
import { Menu } from '@woodger/antd';
let chosen = $state('install');
</script>
<style>
.preview {
max-width: 280px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div class="preview">
<Menu mode="inline" aria-label={("嵌套导航")} defaultOpenKeys={['guides']}
defaultSelectedKeys={['install']} onSelect={info => chosen = info.key}
items={([
{ key: 'guides', label: "指南", children: [
{ type: 'group', key: 'start', label: "开始", children: [
{ key: 'install', label: "联系" }, { key: 'theme', label: "主题" }
] },
{ key: 'examples', label: "示例", children: [
{ key: 'forms', label: "表格" }, { key: 'tables', label: "表格" }
] }
] },
{ key: 'help', label: "帮助" }
])} />
</div>
<output aria-label={("选择子菜单")}>{chosen}</output>
- 概览
-
- 文件
- 文件
- 档案
折叠菜单
inlineCollapsed 保留图标并以弹出窗口形式打开子菜单。展开后,本地菜单将恢复之前展开的内联部分。
<script lang="ts">
import { Button, Menu } from '@woodger/antd';
let collapsed = $state(false);
</script>
<style>
.controls {
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
</style>
{#snippet icon()}
<svg
width="1em"
height="1em"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
aria-hidden="true"
>
<rect x="2" y="2" width="12" height="12" rx="2" />
<path d="M5 6h6M5 10h6" />
</svg>
{/snippet}
<div class="controls"><Button onclick={() => collapsed = !collapsed}>{(collapsed ? "展开菜单" : "折叠菜单")}</Button></div>
<div class="preview">
<Menu mode="inline" inlineCollapsed={collapsed} aria-label={("可折叠菜单")}
defaultSelectedKeys={['docs']} defaultOpenKeys={['project']}
items={([
{ key: 'home', label: "概览", icon },
{
key: 'project', label: "项目", icon, children: [
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
]
},
{ key: 'archive', label: "档案", icon, disabled: true }
])} />
</div>
- 任务
- 日历
<script lang="ts">
import { Checkbox, Menu } from '@woodger/antd';
let custom = $state(true);
</script>
<style>.controls { margin-bottom:16px; }</style>
{#snippet icon()}
<svg
width="1em"
height="1em"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
aria-hidden="true"
>
<rect x="2" y="2" width="12" height="12" rx="2" />
<path d="M5 6h6M5 10h6" />
</svg>
{/snippet}
<div
class="controls"
><Checkbox
bind:checked={custom}
>{("自己的物品签名")}</Checkbox></div>
<Menu
mode="inline"
inlineCollapsed
tooltip={{ placement: 'right' }}
aria-label={("带提示的菜单")}
items={([
{ key: 'tasks', label: "任务", icon, title: custom ? "打开任务列表" : undefined },
{ key: 'calendar', label: "日历", icon }
])}
/>
-
-
- 输入字段
-
一次一个分支
应用程序管理 openKeys 并仅留下新子菜单的路径。打开相邻分支会关闭前一个分支及其子部分。
<script lang="ts">
import { Menu } from '@woodger/antd';
let opened = $state<string[]>(['guides', 'forms']);
const parents: Record<string, string | undefined> = {
guides: undefined, forms: 'guides', data: 'guides', project: undefined
};
function changeOpen(keys: string[]) {
const added = keys.find(key => !opened.includes(key));
if (added) {
const path: string[] = [];
for (let key: string | undefined = added; key; key = parents[key]) path.unshift(key);
opened = path;
return;
}
opened = keys.filter(key => !parents[key] || keys.includes(parents[key]!));
}
</script>
<style>
.preview {
max-width: 280px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div
class="preview"
><Menu
mode="inline"
aria-label={("单店菜单")}
openKeys={opened}
onOpenChange={changeOpen}
items={([
{
key: 'guides', label: "指南", children: [
{ key: 'forms', label: "表格", children: [{ key: 'inputs', label: "输入字段" }] },
{ key: 'data', label: "数据", children: [{ key: 'table', label: "桌子" }] }
]
},
{ key: 'project', label: "项目", children: [{ key: 'team', label: "团队" }] }
])}
/></div>
<output aria-label={("扩展菜单分支")}>{(opened.join(' → ')
|| ("没有开放的分支机构"))}</output>
- 家
垂直弹出菜单
与内联不同,子列表显示在项目旁边。 triggerSubMenuAction 选择悬停或 click;键盘打开和关闭嵌套级别。
<script lang="ts">
import { Checkbox, Menu } from '@woodger/antd';
let click = $state(true);
let chosen = $state('');
</script>
<style>.controls {
margin-bottom:16px;
} .preview {
max-width:280px;
} output {
display:block;
margin-top:16px;
}</style>
<div
class="controls"
><Checkbox
bind:checked={click}
>{("按打开")}</Checkbox></div>
<div class="preview"><Menu mode="vertical" aria-label={("垂直截面")}
triggerSubMenuAction={click ? 'click' : 'hover'} onSelect={info => chosen = info.key}
items={([
{ key: 'home', label: "家" },
{
key: 'workspace', label: "工作区", children: [
{ key: 'tasks', label: "任务" },
{ key: 'settings', label: "选项", children: [{ key: 'access', label: "使用权" }] }
]
}
])} /></div>
<output aria-label={("垂直菜单选择")}>{(chosen
|| ("未选择任何内容"))}</output>
- 收件箱8
- 发送
- 篮子
浅色和深色主题
theme 切换菜单的明暗调色板。选择存储在应用程序中,并在更改主题时保留。
<script lang="ts">
import { Checkbox, Menu } from '@woodger/antd';
let dark = $state(true);
let selected = $state<string[]>(['inbox']);
</script>
<style>
.controls {
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
</style>
<div class="controls"><Checkbox bind:checked={dark}>{("深色菜单")}</Checkbox></div>
<div class="preview"><Menu mode="inline" theme={dark ? 'dark' : 'light'}
aria-label={("带有主题更改的菜单")} selectedKeys={selected}
onSelect={info => selected = info.selectedKeys} items={([
{ key: 'inbox', label: "收件箱", extra: '8' },
{ key: 'sent', label: "发送" }, { key: 'trash', label: "篮子", danger: true }
])} /></div>
- 家
子菜单主题
subMenu.theme 细化特定分支的调色板。这里根保持黑暗,但弹出窗口可以变亮。
<script lang="ts">
import { Checkbox, Menu } from '@woodger/antd';
let light = $state(true);
</script>
<style>
.controls {
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
</style>
<div
class="controls"
><Checkbox
bind:checked={light}
>{("浅色子菜单")}</Checkbox></div>
<div class="preview"><Menu mode="vertical" theme="dark" triggerSubMenuAction="click"
aria-label={("具有单独线程主题的菜单")} items={([
{ key: 'home', label: "家" },
{
key: 'project', label: "项目部分", theme: light ? 'light' : 'dark', children: [
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
]
}
])} /></div>
- 家
-
- 文件
- 文件
切换方式
选定的键保持应用程序的状态。在内联和弹出模式之间切换时,打开的子菜单可能会被关闭;当前的 openKeys 单独显示。
<script lang="ts">
import { Menu, type MenuMode } from '@woodger/antd';
let mode = $state<MenuMode>('inline');
let selected = $state<string[]>(['docs']);
let opened = $state<string[]>(['project']);
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
.scroll {
max-width: 100%;
overflow-x: auto;
}
output {
display: block;
margin-top: 16px;
}
select {
max-width: 100%;
font: inherit;
}
</style>
<label>{("菜单模式 ")}<select bind:value={mode}>
<option value="inline">inline</option><option value="vertical">vertical</option>
<option value="horizontal">horizontal</option>
</select></label>
<div class="scroll">
<Menu {mode} triggerSubMenuAction="click" aria-label={("带模式更改的菜单")}
selectedKeys={selected} openKeys={opened} onSelect={info => selected = info.selectedKeys}
onOpenChange={keys => opened = keys} items={([
{ key: 'home', label: "家" },
{
key: 'project', label: "项目", children: [
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
]
}
])} />
</div>
<output aria-label={("菜单模式状态")}>{mode}: {selected.join(', ')}{(";打开: ")}{(opened
.join(', ')
|| ("不"))}</output>
- 工作
- 任务2
- 档案
- 删除项目
项目结构
items 组合了组、分隔符、图标、extra 和项目状态。label 和 extra 接受文本、数字或片段;disabled 将从选择中排除该操作。
<script lang="ts">
import { Menu } from '@woodger/antd';
</script>
<style>.preview { max-width:280px; }</style>
{#snippet icon()}
<svg
width="1em"
height="1em"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
aria-hidden="true"
>
<rect x="2" y="2" width="12" height="12" rx="2" />
<path d="M5 6h6M5 10h6" />
</svg>
{/snippet}
{#snippet selectedIcon({ isSelected }: { isSelected: boolean })}
{#if isSelected}<span aria-hidden="true">✓</span>{/if}
{/snippet}
{#snippet extra()}<span aria-label={("两个新条目")}>2</span>{/snippet}
<div class="preview">
<Menu
itemIcon={selectedIcon}
aria-label={("菜单组和状态")}
items={([
{
type: 'group', key: 'work', label: "工作", children: [
{ key: 'tasks', label: "任务", icon, extra },
{ key: 'archive', label: "档案", disabled: true }
]
},
{ type: 'divider', key: 'separator', dashed: true },
{ key: 'delete', label: "删除项目", danger: true }
])} />
</div>
- 文件
- 文件
- 笔记
多项选择和命令
multiple 控制所选项目的集合。 selectable=false 离开 onClick 但停止更改选择;已选择的按键将保留直至重置。
<script lang="ts">
import { Button, Checkbox, Menu } from '@woodger/antd';
let selectable = $state(true);
let selected = $state<string[]>(['docs']);
let clicked = $state('');
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
output {
display: block;
margin-top: 12px;
}
</style>
<div
class="controls"
><Checkbox
bind:checked={selectable}
>{("允许选择")}</Checkbox>
<Button onclick={() => selected = []}>{("重置选择")}</Button></div>
<div class="preview"><Menu mode="inline" multiple {selectable}
aria-label={("多选菜单")} selectedKeys={selected}
onSelect={info => selected = info.selectedKeys} onDeselect={info => selected = info.selectedKeys}
onClick={info => clicked = info.keyPath.join(' / ')} items={([
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" },
{ key: 'notes', label: "笔记" }
])} /></div>
<output aria-label={("多项选择菜单")}>{(selected.join(', ')
|| ("没有选择任何项目"))}</output>
<output
aria-label={("按菜单")}
>{(clicked || ("无点击次数"))}</output>
子菜单容器
通用 getter 指定 ConfigProvider。自定义 getter 会替换它,并且显式传递的 undefined 会禁用继承 - 这与后备 Dropdown 不同。
<script lang="ts">
import type { Snippet } from 'svelte';
import { ConfigProvider, Menu, type MenuProps } from '@woodger/antd';
let target = $state<'provider' | 'local' | 'native'>('provider');
let providerTarget = $state<HTMLDivElement>();
let localTarget = $state<HTMLDivElement>();
let popupProps = $derived<Pick<MenuProps, 'getPopupContainer'>>(
target === 'provider'
? {}
: target === 'local'
? { getPopupContainer: () => localTarget! }
: { getPopupContainer: undefined }
);
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
.targets {
display: grid;
gap: 12px;
margin-top: 16px;
}
.branch-popup { background: var(--ant-color-bg-elevated); }
.branch-popup strong {
display: block;
padding: 8px 16px;
}
.target {
min-height:44px;
padding:12px;
border:1px dashed var(--ant-color-border);
border-radius:6px;
}
select {
max-width: 100%;
font: inherit;
}
</style>
{#snippet popupRender(origin: Snippet<[children?: Snippet]>)}
<div class="branch-popup">
<strong>{("项目")}</strong>
{@render origin()}
</div>
{/snippet}
<label>{("菜单容器 ")}<select bind:value={target}>
<option
value="provider"
>ConfigProvider</option><option
value="local"
>{("当地的")}</option>
<option value="native">{("无吸气剂")}</option>
</select></label>
<ConfigProvider getPopupContainer={() => providerTarget!}>
<div class="preview"><Menu {...popupProps} {popupRender} mode="vertical" triggerSubMenuAction="click"
aria-label={("带有容器选择的菜单")} items={([
{
key: 'project', label: "项目菜单", popupOffset: [4, 0], children: [
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
]
}
])} /></div>
<div class="targets">
<div class="target provider-target" bind:this={providerTarget}>
{("ConfigProvider容器")}
</div>
<div
class="target local-target"
bind:this={localTarget}
>{("本地容器")}</div>
</div>
</ConfigProvider>
- 文件
- 文件
组件代币
本地主题指定所选项目的高度、舍入和颜色。 colorSuccess 值响应整体主题;相邻的菜单保留其设置。
<script lang="ts">
import { ConfigProvider, Menu, theme } from '@woodger/antd';
const tokens = theme.useToken();
let height = $state(44);
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
.preview { max-width:280px; }
</style>
<label>{("菜单项高度 ")}<input
type="range"
min="32"
max="56"
bind:value={height}
/>
<output>{height}px</output></label>
<ConfigProvider theme={{
cssVar: {}, components: {
Menu: {
itemHeight: height, itemBorderRadius: 12,
itemSelectedColor: tokens.token.colorSuccess, itemSelectedBg: tokens.token.colorSuccessBg
}
}
}}>
<div class="preview"><Menu mode="inline" aria-label={("有自己的主题的菜单")}
defaultSelectedKeys={['docs']} items={([
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
])} /></div>
</ConfigProvider>
- 文件
- 文件
设置根和段落的格式
ConfigProvider.menu 设置通用根类名,局部 styles 设置列表内边距。
<script lang="ts">
import { Checkbox, ConfigProvider, Menu } from '@woodger/antd';
let accent = $state(true);
</script>
<style>
.controls {
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
.preview :global(.example-menu-root) {
border:1px solid var(--ant-color-border);
border-radius:8px;
}
.preview :global(.example-menu-accent) { font-weight:600; }
</style>
<div
class="controls"
><Checkbox
bind:checked={accent}
>{("选择项目")}</Checkbox></div>
<div class="preview">
<ConfigProvider menu={{ classNames: { root: 'example-menu-root' } }}>
<Menu
aria-label={("设计菜单")}
styles={() => ({ root: 'padding:8px' })}
items={([
{ key: 'docs', label: "文件", class: accent ? 'example-menu-accent' : undefined },
{ key: 'files', label: "文件", style: 'font-style:italic' }
])}
/>
</ConfigProvider>
</div>
- 概览
- 档案
- 使用权
原生元素和焦点
bind:nativeElement 在挂载后返回 ul。 Public focus 和 findItem 允许您将焦点定向到可访问的项目,而无需搜索私有 CSS 类。
<script lang="ts">
import { Button, Menu } from '@woodger/antd';
let menu = $state<ReturnType<typeof Menu>>();
let element = $state<HTMLUListElement>();
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.preview {
max-width: 280px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div class="controls">
<Button onclick={() => menu?.focus()}>{("专注于菜单")}</Button>
<Button onclick={() => menu?.findItem({ key: 'access' })?.focus()}>
{("找到“访问”")}
</Button>
</div>
<div class="preview"><Menu bind:this={menu} bind:nativeElement={element}
aria-label={("带有本机参考的菜单")} items={([
{ key: 'home', label: "概览" }, { key: 'archive', label: "档案", disabled: true },
{ key: 'access', label: "使用权" }
])} /></div>
<output aria-label={("本机菜单元素")}>{(element?.tagName || "等待挂载")}</output>
- 家
方向和键盘
direction 继承自 ConfigProvider。在垂直弹出菜单中,RTL 会更改开口方向:ArrowLeft 进入子菜单,ArrowRight 返回父菜单。
<script lang="ts">
import { Checkbox, ConfigProvider, Menu } from '@woodger/antd';
let rtl = $state(true);
let chosen = $state('');
</script>
<style>.controls {
margin-bottom:16px;
} .preview {
max-width:280px;
} output {
display:block;
margin-top:16px;
}</style>
<div class="controls"><Checkbox bind:checked={rtl}>RTL Menu</Checkbox></div>
<ConfigProvider direction={rtl ? 'rtl' : 'ltr'}>
<div class="preview"><Menu
mode="vertical"
triggerSubMenuAction="click"
aria-label={("带方向的菜单")}
onSelect={info => chosen = info.key}
items={([
{ key: 'home', label: "家" },
{
key: 'sections', label: "部分", children: [
{ key: 'docs', label: "文件" }, { key: 'files', label: "文件" }
]
}
])}
/></div>
</ConfigProvider>
<output
aria-label={("选择 RTL 菜单")}
>{(chosen || ("未选择任何内容"))}</output>
Sider 内的菜单
Menu继承了LayoutSider的折叠。一般 collapsedWidth=80 为图标留有空间;该示例被隔离在其自己的框架内。
<script lang="ts">
import { Layout, LayoutSider, LayoutContent, Menu } from '@woodger/antd';
let collapsed = $state(false);
</script>
<style>
.preview {
min-width:0;
overflow:hidden;
border:1px solid var(--ant-color-border);
border-radius:8px;
isolation:isolate;
transform:translate(0);
}
.preview :global(.ant-layout-content) {
min-width: 0;
padding: 16px;
overflow-wrap: anywhere;
}
</style>
{#snippet icon()}
<svg
width="1em"
height="1em"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
aria-hidden="true"
>
<rect x="2" y="2" width="12" height="12" rx="2" />
<path d="M5 6h6M5 10h6" />
</svg>
{/snippet}
<div class="preview">
<Layout hasSider style="min-height:240px">
<LayoutSider width={180} collapsedWidth={80} collapsible bind:collapsed theme="light"
expandLabel={("展开侧边菜单")} collapseLabel={("折叠侧面菜单")}>
<Menu
mode="inline"
aria-label={("侧边栏内的菜单")}
defaultSelectedKeys={['docs']}
items={([{ key: 'docs', label: "文件", icon }, {
key: 'files',
label: "文件",
icon
}])}
/>
</LayoutSider>
<LayoutContent><output aria-label={("折叠侧边菜单")}>{(collapsed ? "面板倒塌" : "面板打开")}</output></LayoutContent>
</Layout>
</div>
- 库概述
-
- 开始
- 联系
- 表格
- 档案
文档部分导航
菜单会保留选中的菜单项和展开的分支,直到你重置菜单。
<script lang="ts">
import { Menu, Button, type MenuItemType } from '@woodger/antd';
let selected = $state<string[]>(['overview']);
let opened = $state<string[]>(['guides']);
let result = $state('');
const items: MenuItemType[] = [
{ key: 'overview', label: overview },
{
key: 'guides',
label: "指南",
children: [
{
type: 'group',
key: 'start',
label: "开始",
children: [{ key: 'setup', label: "联系" }, { key: 'forms', label: "表格" }]
},
{
key: 'advanced',
label: "此外",
children: [{ key: 'theme', label: "主题" }, { key: 'locale', label: "本土化" }]
}
]
},
{ type: 'divider', key: 'divider' },
{ key: 'archive', label: "档案", disabled: true }
];
</script>
<style>
output {
display: block;
margin-top: 16px;
}
</style>
{#snippet overview()}
<a href={("/ant-design#overview")}>
{("库概述")}
</a>
{/snippet}
<div style="max-width:400px">
<Menu
aria-label={("导航示例")}
items={(items)}
mode="inline"
selectedKeys={selected}
openKeys={opened}
onOpenChange={keys => { opened = keys; }}
onSelect={info => {
selected = info.selectedKeys;
result = info.keyPath.join(' / ');
}}
onDeselect={info => {
selected = info.selectedKeys;
result = info.keyPath.join(' / ');
}}
/>
<Button
style="margin-top:16px"
onclick={() => {
selected = ['overview'];
opened = ['guides'];
result = '';
}}
>{("重置菜单")}</Button>
<output aria-label={("选定的菜单项")}>{result}</output>
</div>
-
- 文档
- 示例
所有模式、项目、组和子菜单都使用本地前缀。主题和链接设置独立;保留选择和披露。
菜单 CSS 前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import { Checkbox, ConfigProvider, Menu } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-menu' : '');
let selectedKeys = $state(['docs']);
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("通用前缀 菜单套件")}</Checkbox>
<Checkbox bind:checked={local}>{("本地菜单前缀")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'menu-theme' } }}>
<div class="prefix-stage">
<Menu prefixCls={localPrefix} mode="inline" {selectedKeys} onSelect={info => selectedKeys = [
...info.selectedKeys
]} defaultOpenKeys={['project']}
aria-label={("带前缀的菜单")} items={([
{
key: 'project', label: "项目", children: [
{ key: 'docs', label: "文档" }, { key: 'examples', label: "示例" }
]
}
])} />
<output
aria-label={("选择带前缀的菜单")}
>{selectedKeys.join(', ')}</output>
</div>
</ConfigProvider>
<output aria-label={("菜单 CSS 前缀")}>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
{("所有模式、项目、组和子菜单都使用本地前缀。主题和链接设置独立;保留选择和披露。")}
</p>
</div>
API
这些示例是根据当前的 ant.design 页面和已安装包的公共 API 进行检查的。 Props 的完整定义位于 @woodger/antd 中。
| 主题 | 示例或限制 |
|---|---|
| Top Navigation | 空间不足时,末尾的菜单项会移入“更多”子菜单。 |
| Inline menu | 嵌套的部分和组,初始 open/选定的键。 |
| Collapsed inline menu | 使用 LayoutSider 折叠内联和单独的组合。 |
| Menu tooltip | 在折叠菜单中,提示框显示菜单项的标签。使用 title 可自定义文字。 |
| Open current submenu only | 一个开放分支的受控路径;相邻的分支取代了旧的分支。 |
| Vertical menu | 带有 hover/click、附件和键盘的弹出子菜单。 |
| Menu Themes | 浅色和深色调色板可供选择。 |
| Sub-menu theme | 深色根上的树枝的单独调色板。 |
| Switch the menu type | 使用受控按键切换所有三种模式。 |
| Custom semantic dom styling | ConfigProvider.menu 设置通用根类名,局部 styles 设置列表内边距。 |
| Custom Submenu Render | popupRender 在原有子菜单列表上方添加标题。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
overflowedIndicator | 用于容纳一行中放不下的菜单项的子菜单标签。 | MenuContent | false | null | … |
popupRender | 包裹或替换子菜单内容。分支也可以提供自己的片段。 | MenuPopupRender | — |
builtinPlacements | 设置子菜单和溢出菜单的位置。单个分支可使用 popupOffset 调整偏移。 | Record<string, MenuPopupAlign> | — |
tooltip | 折叠菜单项的提示配置。设为 false 可关闭提示。 | MenuTooltip | — |
motion | 配置内嵌列表和弹出子菜单的过渡效果。 | MenuMotion | — |
itemIcon | 显示在标签和 extra 后的图标。片段会收到菜单项的选中状态。 | MenuItemIcon | — |
prefixCls | 组件及其部件的完整 CSS 前缀。非空本地 prop 优先;空字符串和 undefined 继承ConfigProvider。主题的命名空间是单独设置的。 | string | ConfigProvider.prefixCls + "-menu" |
items | 项目、子菜单、组、分隔符和 null。 label/icon/extra 使用 Snippet 而不是 JSX。 | readonly MenuItemType[] | [] |
mode | 将菜单项排列为水平、垂直或内嵌树形列表。 | 'vertical' | 'horizontal' | 'inline' | vertical |
theme | 调色板菜单; subMenu.theme 针对分支进行了细化。 | 'light' | 'dark' | light |
selectedKeys | 引导选择;从 onSelect/onDeselect 更新数组。不支持 bind。 | readonly string[] | 当地州 |
defaultSelectedKeys | 初步选择;对 prop 的后续更改不会重置状态。 | readonly string[] | [] |
openKeys | 管理子菜单列表;从 onOpenChange 更新。 | readonly string[] | 当地州 |
defaultOpenKeys | 最初打开的部分。 | readonly string[] | [] |
onOpenChange | 要求披露;单个分支规则属于应用程序。 | (keys: string[]) => void | — |
multiple | 允许多项选择。 | boolean | false |
selectable | 控制选择更改; onClick 仍然可用。 | boolean | true |
disabled | 整个菜单不可用。显式传递 prop; Menu 不读取 ConfigProvider.componentDisabled。 | boolean | false |
inlineCollapsed | 显式的 prop 指定继承的折叠。不适用于 horizontal 方向。 | boolean | 来自 LayoutSider |
inlineIndent | 水平缩进内嵌,考虑 direction。 | number | 24 |
triggerSubMenuAction | 对于弹出子菜单。按下即可内联打开。 | 'hover' | 'click' | hover |
subMenuOpenDelay | 悬停打开延迟(以秒为单位)。 | number | 0.1 |
subMenuCloseDelay | 悬停关闭延迟(以秒为单位)。 | number | 0.1 |
forceSubMenuRender | 在首次打开之前创建隐藏内容。 | boolean | false |
onClick | key、keyPath、domEvent 和 itemData。 keyPath 从项目开始。 | (info: MenuClickInfo) => void | — |
onSelect | 将 selectedKeys 与单击项目的数据一起传递。 | (info: MenuSelectInfo) => void | — |
onDeselect | multiple 示例更新回调中选定的键。 | (info: MenuSelectInfo) => void | — |
getPopupContainer | 本机 undefined 会禁用 getter 继承。当强制 render 零元数时,吸气剂可能首先是 undefined 的。 | (trigger: HTMLElement) => HTMLElement | ShadowRoot | 来自配置提供者 |
nativeElement | bind:nativeElement 返回根 ul 并在卸载时清除。 | HTMLUListElement | undefined | 挂载后 |
rootClassName | 类旁边的附加根列表 class。 | string | — |
style | CSS 根 ul。本机 HTML 和 aria/data 属性被传递到根。 | string | — |
项目和子菜单结构
菜单项支持 label、icon、extra 和末尾的 itemIcon。分支可以设置 children、popupOffset 和 expandIcon。行和分支标题均支持悬停回调,行还接受 data 属性。
Semantic DOM
根 - ul role=menu。这些项目创建 li role=menuitem,子菜单标题是带有 aria-expanded/aria-controls 的本机 button。将链接作为内部 label 片段传递。组和分隔符仍然是单独的结构单元。
内联使用 ArrowUp/ArrowDown、Home/End 和 Enter 进行扩展和选择。在 horizontal 方向中,ArrowLeft/ArrowRight 在顶部项目之间移动焦点,ArrowDown 打开子菜单。在垂直弹出窗口中,左/右箭头根据 RTL 进入和退出分支; Escape 关闭弹出窗口。空格键并非选择 li 的通用按键。
通过 bind:this 可调用 focus 和 findItem。按键查找会返回已挂载的 DOM 节点或 null。
注册令牌
Menu 连接到 ConfigProvider theme.components.Menu。该卡检查项目的高度和四舍五入、选择的颜色和背景;整体颜色和版式遵循该部分的主题。下面列出了主要的现代代币名称,完整的集合位于包类型中。
| 代币 | 目的 |
|---|---|
itemHeight | 点高。 |
itemBorderRadius | 点舍入。 |
itemSelectedColor | 所选项目的颜色。 |
itemSelectedBg | 所选项目的背景。 |
itemColor | 正常文本颜色。 |
itemHoverColor | 悬停颜色。 |
itemHoverBg | 悬停时的背景。 |
itemDisabledColor | 不可用物品的颜色。 |
itemPaddingInline | 水平压痕。 |
itemMarginInline | 外部水平边距。 |
itemMarginBlock | 外部垂直边距。 |
collapsedWidth | 折叠菜单宽度。 |
dropdownWidth | 弹出子菜单宽度。 |
activeBarHeight | 活动指示器的 horizontal 高度。 |
darkItemBg | 深色菜单背景。 |
darkItemSelectedBg | 深色菜单选择背景。 |
zIndexPopup | CSS z-index popup. |
| 代币 | 当前值 |
|---|---|
colorBgContainer | #ffffff |
colorBgElevated | #ffffff |
colorText | rgba(0,0,0,0.88) |
colorPrimary | #2f54eb |
colorTextDisabled | rgba(0,0,0,0.25) |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
borderRadiusLG | 8 |
FAQ
如何自定义溢出菜单按钮?
通过 overflowedIndicator 设置按钮标签。在弹性容器内,使用 min-width: 0 允许菜单收缩。若要保留整行菜单项,可传入 disabledOverflow。
为什么 selectedKeys 不改变自身?
这是一个受控道具。在回调中更新数组;仅初始本地选择需要 defaultSelectedKeys。
如何只扩展一个分支?
管理应用程序中的 openKeys 并保存新分支的祖先。
如何更改折叠菜单项的提示?
在菜单项的 title 中填写文本。菜单的 tooltip 用于设置通用 Tooltip 选项,传入 false 可关闭提示。
为什么容器 undefined 时不被继承?
对于 Menu,其自身 undefined 会禁用通用 getPopupContainer。要继承 getter,不要传递 prop。
Menu.Item 和 subMenu 是否需要位于标记中?
不会。在这个 Svelte API 中,结构由 items 指定,复杂内容由片段指定。