Dropdown下拉菜单
下拉菜单是带面板菜单的按钮(或任意元素),可通过悬停、点击或右键打开。菜单就是本库的 Menu,可用对象或标记书写,因此菜单项、子菜单、分组、选择与键盘行为与它一致。
何时使用
- 当某处有多个操作而放不下全部时使用下拉菜单:表格行的按钮、页头的用户菜单、右键时某个对象的操作。
- 在表单中选择值请用
Select;始终显示在页面上的列表(如导航)请用Menu。 - 请用按钮或链接作触发元素:键盘与读屏软件能到达它们。悬停菜单也能用
Enter、Space、方向键与触摸打开。
代码演示
menu as an object: hover the button, or press Enter, Space or an arrow on it. Archive is switched off with disabled: true Nothing chosen yet
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
let chosen = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">menu as an object: hover the button, or press Enter, Space or an arrow on it. Archive is switched off with disabled: true</span>
<Dropdown
menu={{
items: [
{ key: 'rename', label: 'Rename' },
{ key: 'duplicate', label: 'Duplicate' },
// disabled: true switches an item off: it is grey and cannot be chosen.
{ key: 'archive', label: 'Archive', disabled: true }
],
onclick: ({ key }) => (chosen = key)
}}
>
<Button>Hover me</Button>
</Dropdown>
<span role="status">{chosen ? `Chosen: ${chosen}` : 'Nothing chosen yet'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
icon, extra, disabled, a divider, a group and danger: the items of the Menu Nothing chosen yet
<script lang="ts">
import { Button, Dropdown, type MenuItems } from 'svelte-ant-design';
const items: MenuItems = [
{ key: 'open', label: 'Open', icon: '▤', extra: '⌘O' },
{ key: 'save', label: 'Save', icon: '▥', extra: '⌘S' },
{ key: 'print', label: 'Print (disabled)', icon: '▦', disabled: true },
{ type: 'divider' },
{
type: 'group',
label: 'Danger zone',
children: [
{ key: 'move', label: 'Move to trash' },
{ key: 'delete', label: 'Delete forever', danger: true }
]
}
];
let chosen = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">icon, extra, disabled, a divider, a group and danger: the items of the Menu</span>
<Dropdown menu={{ items, onclick: ({ key }) => (chosen = key) }} trigger="click">
<Button>File</Button>
</Dropdown>
<span role="status">{chosen ? `Chosen: ${chosen}` : 'Nothing chosen yet'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
trigger="hover" (the default)
trigger="click": a second click closes it
trigger={['hover', 'click']}: either one opens it
mouseEnterDelay={0.5} and mouseLeaveDelay={0.4}, in seconds
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
const menu = { items: [{ key: 'one', label: 'First action' }, { key: 'two', label: 'Second action' }] };
</script>
<div class="stack">
<div class="field">
<span class="caption">trigger="hover" (the default)</span>
<Dropdown {menu}>
<Button>Hover</Button>
</Dropdown>
</div>
<div class="field">
<span class="caption">trigger="click": a second click closes it</span>
<Dropdown {menu} trigger="click">
<Button>Click</Button>
</Dropdown>
</div>
<div class="field">
<span class="caption">trigger={['hover', 'click']}: either one opens it</span>
<Dropdown {menu} trigger={['hover', 'click']}>
<Button>Hover or click</Button>
</Dropdown>
</div>
<div class="field">
<span class="caption">mouseEnterDelay={0.5} and mouseLeaveDelay={0.4}, in seconds</span>
<Dropdown {menu} mouseEnterDelay={0.5} mouseLeaveDelay={0.4}>
<Button>Slow</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
placement: the side and the alignment; at the edge of the window the panel flips or moves in
autoAdjustOverflow={false}: the panel stays where placement says, also off the screen
<script lang="ts">
import { Button, Dropdown, type DropdownPlacement } from 'svelte-ant-design';
const rows: DropdownPlacement[][] = [
['topLeft', 'top', 'topRight'],
['bottomLeft', 'bottom', 'bottomRight'],
['leftTop', 'left', 'leftBottom'],
['rightTop', 'right', 'rightBottom']
];
const menu = { items: [{ key: 'a', label: 'A longer first entry' }, { key: 'b', label: 'Second' }] };
</script>
<div class="stack">
<div class="field">
<span class="caption">placement: the side and the alignment; at the edge of the window the panel flips or moves in</span>
{#each rows as row (row[0])}
<div class="row">
{#each row as placement (placement)}
<Dropdown {menu} {placement} trigger="click">
<Button class="at">{placement}</Button>
</Dropdown>
{/each}
</div>
{/each}
</div>
<div class="field">
<span class="caption">autoAdjustOverflow={false}: the panel stays where placement says, also off the screen</span>
<Dropdown {menu} placement="top" autoAdjustOverflow={false} trigger="click">
<Button>Stays above</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.row :global(.at) {
min-width: 108px;
}
</style>
arrow
arrow={{ pointAtCenter: true }}: at the middle of the trigger
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
const menu = { items: [{ key: 'a', label: 'First' }, { key: 'b', label: 'Second' }] };
</script>
<div class="stack">
<div class="field">
<span class="caption">arrow</span>
<Dropdown {menu} arrow trigger="click">
<Button>With an arrow</Button>
</Dropdown>
</div>
<div class="field">
<span class="caption">arrow={{ pointAtCenter: true }}: at the middle of the trigger</span>
<Dropdown {menu} arrow={{ pointAtCenter: true }} placement="bottomRight" trigger="click">
<Button>A wider button, pointed at its center</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
trigger="contextMenu": right click in the area, or focus it and press Shift+F10
Right click here
Nothing chosen yet<script lang="ts">
import { Dropdown } from 'svelte-ant-design';
let chosen = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">trigger="contextMenu": right click in the area, or focus it and press Shift+F10</span>
<Dropdown
trigger="contextMenu"
menu={{
'aria-label': 'Photo actions',
items: [
{ key: 'copy', label: 'Copy' },
{ key: 'share', label: 'Share' },
{ key: 'remove', label: 'Remove', danger: true }
],
onclick: ({ key }) => (chosen = key)
}}
>
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div class="area" tabindex="0">Right click here</div>
</Dropdown>
<span role="status">{chosen ? `Chosen: ${chosen}` : 'Nothing chosen yet'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.area {
display: grid;
place-items: center;
width: 100%;
height: 160px;
border: 1px dashed var(--ads-color-border);
border-radius: 8px;
color: var(--ads-color-text-secondary);
}
</style>
menu.onclick gets the item; onopenchange says whether a choice or the trigger changed the panel
Open the menu to see the calls
<script lang="ts">
import { Button, Dropdown, type DropdownOpenChangeInfo } from 'svelte-ant-design';
let log = $state<string[]>([]);
const write = (line: string) => (log = [...log.slice(-3), line]);
const changed = (open: boolean, { source }: DropdownOpenChangeInfo) =>
write(`onopenchange(${open}, { source: '${source}' })`);
</script>
<div class="stack">
<div class="field">
<span class="caption">menu.onclick gets the item; onopenchange says whether a choice or the trigger changed the panel</span>
<Dropdown
trigger="click"
onopenchange={changed}
menu={{
items: [{ key: 'yes', label: 'Yes' }, { key: 'no', label: 'No' }],
onclick: ({ key }) => write(`menu.onclick: ${key}`)
}}
>
<Button>Answer</Button>
</Dropdown>
<div class="log" role="status">
{#each log as line, index (index)}
<div>{line}</div>
{:else}
<div>Open the menu to see the calls</div>
{/each}
</div>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.log {
font-family: ui-monospace, monospace;
font-size: 12px;
}
</style>
menu.selectable: one choice, shown in the menu; the panel closes selected: en
selectable and multiple: the panel stays open while you choose selected: en, zh
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
const languages = [
{ key: 'en', label: 'English' },
{ key: 'ru', label: 'Русский' },
{ key: 'zh', label: '中文' }
];
let language = $state(['en']);
let shown = $state(['en', 'zh']);
</script>
<div class="stack">
<div class="field">
<span class="caption">menu.selectable: one choice, shown in the menu; the panel closes</span>
<Dropdown
trigger="click"
menu={{ items: languages, selectable: true, selectedKeys: language, onselect: ({ selectedKeys }) => (language = selectedKeys) }}
>
<Button>Language</Button>
</Dropdown>
<span role="status">selected: {language.join(', ')}</span>
</div>
<div class="field">
<span class="caption">selectable and multiple: the panel stays open while you choose</span>
<Dropdown
trigger="click"
menu={{
items: languages,
selectable: true,
multiple: true,
selectedKeys: shown,
onselect: ({ selectedKeys }) => (shown = selectedKeys),
ondeselect: ({ selectedKeys }) => (shown = selectedKeys)
}}
>
<Button>Show columns</Button>
</Dropdown>
<span role="status">selected: {shown.join(', ') || '(none)'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
bind:open with trigger={[]}: another button opens and closes it; a choice and Escape close it too open: false
Keep it open after a choice: with bind:open, set it back when source is 'menu' chosen 0 times
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
let controlled = $state(false);
let staying = $state(false);
let picked = $state(0);
</script>
<div class="stack">
<div class="field">
<span class="caption">bind:open with trigger={[]}: another button opens and closes it; a choice and Escape close it too</span>
<div class="row">
<Dropdown bind:open={controlled} trigger={[]} menu={{ items: [{ key: 'a', label: 'Action' }] }}>
<Button>Controlled</Button>
</Dropdown>
<Button type="primary" onclick={() => (controlled = !controlled)}>{controlled ? 'Close' : 'Open'} it</Button>
</div>
<span role="status">open: {controlled}</span>
</div>
<div class="field">
<span class="caption">Keep it open after a choice: with bind:open, set it back when source is 'menu'</span>
<Dropdown
trigger="click"
bind:open={staying}
onopenchange={(next, { source }) => {
if (source === 'menu') staying = true;
}}
menu={{ items: [{ key: 'more', label: 'Add one more' }], onclick: () => picked++ }}
>
<Button>Stays open</Button>
</Dropdown>
<span role="status">chosen {picked} {picked === 1 ? 'time' : 'times'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
popupRender: content around the menu; Tab walks from the menu into it Nothing yet
popupRender without a menu
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
let note = $state('');
</script>
{#snippet withFooter({ menu }: { menu: import('svelte').Snippet })}
{@render menu()}
<div class="footer">
<Button type="primary" size="small" onclick={() => (note = 'Saved the view')}>Save the view</Button>
</div>
{/snippet}
{#snippet card()}
<div class="card">Content of your own, without a menu</div>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">popupRender: content around the menu; Tab walks from the menu into it</span>
<Dropdown
trigger="click"
popupRender={withFooter}
menu={{ items: [{ key: 'list', label: 'List' }, { key: 'grid', label: 'Grid' }], onclick: ({ key }) => (note = `Chose ${key}`) }}
>
<Button>View</Button>
</Dropdown>
<span role="status">{note || 'Nothing yet'}</span>
</div>
<div class="field">
<span class="caption">popupRender without a menu</span>
<Dropdown trigger="click" popupRender={card}>
<Button>About</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.footer {
display: flex;
justify-content: flex-end;
padding: 8px;
border-top: 1px solid var(--ads-color-border-secondary);
}
.card {
padding: 12px 16px;
}
</style>
disabled: nothing opens; the button itself is yours to disable
A menu with no items opens nothing
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
const menu = { items: [{ key: 'a', label: 'Action' }] };
let off = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">disabled: nothing opens; the button itself is yours to disable</span>
<div class="row">
<Dropdown {menu} disabled={off} trigger="click">
<Button disabled={off}>Actions</Button>
</Dropdown>
<label class="check"><input type="checkbox" bind:checked={off} /> disabled</label>
</div>
</div>
<div class="field">
<span class="caption">A menu with no items opens nothing</span>
<Dropdown menu={{ items: [] }} trigger="click">
<Button>Empty</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.check {
display: inline-flex;
align-items: center;
gap: 6px;
}
</style>
container: the panel is moved into this box instead of the page body
destroyOnHidden: the panel is removed from the page after closing
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
const menu = { items: [{ key: 'a', label: 'First' }, { key: 'b', label: 'Second' }] };
let box = $state<HTMLElement>();
</script>
<div class="stack">
<div class="field">
<span class="caption">container: the panel is moved into this box instead of the page body</span>
<div class="box" bind:this={box}>
<Dropdown {menu} trigger="click" container={() => box!}>
<Button>In the box</Button>
</Dropdown>
</div>
</div>
<div class="field">
<span class="caption">destroyOnHidden: the panel is removed from the page after closing</span>
<Dropdown {menu} trigger="click" destroyOnHidden>
<Button>Removed on close</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
position: relative;
padding: 12px;
border: 1px dashed var(--ads-color-border);
border-radius: 8px;
}
</style>
classNames and styles: root, menu, item, itemTitle, itemContent, itemIcon
<script lang="ts">
import { Button, Dropdown, type MenuItems } from 'svelte-ant-design';
const items: MenuItems = [
{ key: 'star', label: 'Star', icon: '★' },
{ key: 'sub', label: 'Share', children: [{ key: 'mail', label: 'Mail' }] },
{ type: 'group', label: 'Group', children: [{ key: 'in', label: 'Inside' }] }
];
</script>
<div class="stack">
<div class="field">
<span class="caption">classNames and styles: root, menu, item, itemTitle, itemContent, itemIcon</span>
<Dropdown
trigger="click"
menu={{ items }}
classNames={{ root: 'demo-dropdown', menu: 'demo-menu' }}
styles={{
menu: { 'min-width': '200px' },
item: { 'font-weight': 500 },
itemTitle: { 'text-transform': 'uppercase' },
itemContent: { 'letter-spacing': '0.02em' },
itemIcon: { color: 'var(--ads-color-primary-text)' }
}}
>
<Button>Styled</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
A button with a menu beside it: two buttons side by side, the second one opens the menu Nothing yet
<script lang="ts">
import { Button, Dropdown } from 'svelte-ant-design';
let last = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">A button with a menu beside it: two buttons side by side, the second one opens the menu</span>
<div class="split-button">
<Button onclick={() => (last = 'Published')}>Publish</Button>
<Dropdown
placement="bottomRight"
trigger="click"
menu={{
items: [{ key: 'draft', label: 'Save a draft' }, { key: 'later', label: 'Publish later' }],
onclick: ({ key }) => (last = key === 'draft' ? 'Saved a draft' : 'Planned')
}}
>
<Button aria-label="Other ways to publish">▾</Button>
</Dropdown>
</div>
<span role="status">{last || 'Nothing yet'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.split-button {
display: inline-flex;
}
.split-button :global(.ads-btn:first-of-type) {
border-start-end-radius: 0;
border-end-end-radius: 0;
}
.split-button :global(.ads-dropdown-trigger > .ads-btn) {
margin-inline-start: -1px;
border-start-start-radius: 0;
border-end-start-radius: 0;
}
</style>
menu as a snippet: write the Menu yourself; it behaves as the menu of the panel Nothing chosen yet
<script lang="ts">
import { Button, Dropdown, Menu, MenuDivider, MenuItem, MenuSubMenu } from 'svelte-ant-design';
let chosen = $state('');
let beta = $state(false);
</script>
<div class="stack">
<div class="field">
<span class="caption">menu as a snippet: write the Menu yourself; it behaves as the menu of the panel</span>
<div class="row">
<Dropdown>
<Button>Account</Button>
{#snippet menu()}
<Menu onclick={({ key }) => (chosen = key)}>
<MenuItem key="profile">Profile</MenuItem>
<MenuSubMenu key="theme" label="Theme">
<MenuItem key="light">Light</MenuItem>
<MenuItem key="dark">Dark</MenuItem>
</MenuSubMenu>
{#if beta}
<MenuItem key="labs">Labs</MenuItem>
{/if}
<MenuDivider />
<MenuItem key="logout" danger>Log out</MenuItem>
</Menu>
{/snippet}
</Dropdown>
<label class="check"><input type="checkbox" bind:checked={beta} /> Labs</label>
</div>
<span role="status">{chosen ? `Chosen: ${chosen}` : 'Nothing chosen yet'}</span>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.check {
display: inline-flex;
align-items: center;
gap: 6px;
}
</style>
href: an item that is a link; a middle click or Ctrl+click opens it in a new tab
<script lang="ts">
import { Button, Dropdown, type MenuItems } from 'svelte-ant-design';
const items: MenuItems = [
{ key: 'examples', label: 'Examples', href: '#examples' },
{ key: 'api', label: 'API table', href: '#api' },
{ key: 'repo', label: 'Source (new tab)', href: 'https://github.com/prostreyd/ant-design-svelte', target: '_blank', rel: 'noreferrer' }
];
</script>
<div class="stack">
<div class="field">
<span class="caption">href: an item that is a link; a middle click or Ctrl+click opens it in a new tab</span>
<Dropdown menu={{ items }}>
<Button type="link">Go to…</Button>
</Dropdown>
</div>
</div>
<style>
.stack {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 16px;
width: 100%;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| menu | 面板中的菜单:包含 Menu 属性的对象(不含对垂直菜单无效的 mode、inlineCollapsed、inlineIndent、overflowedIndicator、overflowLabel、tooltip),或写有 Menu 的 snippet。两者行为一致:垂直、未设 selectable 时不选中、选择后关闭面板。 | DropdownMenuProps | Snippet | — |
| children | 触发元素:单个元素。菜单按钮属性加在其中可聚焦的元素上。 | Snippet | — |
| open | 面板是否显示;bind:open 跟随用户。普通值为初始值。 | boolean | false |
| onopenchange | 用户引起的变化:选择菜单项(source: 'menu'),或触发元素、Escape、Tab、外部点击('trigger')。 | (open: boolean, info: { source: 'trigger' | 'menu' }) => void | — |
| trigger | 指针触发方式:hover、click、contextMenu 或多个。空数组表示仅由 open 控制,外部点击不会关闭面板(Escape 与选择菜单项会关闭)。 | 'click' | 'hover' | 'contextMenu' | Array | ['hover'] |
| placement | 方向与对齐方式;从右到左时镜像。 | 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight' | 'left' | 'leftTop' | 'leftBottom' | 'right' | 'rightTop' | 'rightBottom' | 'bottomLeft' |
| arrow | 指向触发元素的箭头;pointAtCenter 指向中心。 | boolean | { pointAtCenter: boolean } | false |
| disabled | 不打开任何内容,并关闭已打开的面板。 | boolean | false |
| autoAdjustOverflow | 靠近窗口边缘时面板翻转、换对齐方式或移入窗口。 | boolean | true |
| mouseEnterDelay | 悬停:打开前的延迟(秒)。 | number | 0.15 |
| mouseLeaveDelay | 悬停:指针离开后关闭前的延迟(秒)。 | number | 0.1 |
| container | 面板挂载的位置;默认为 body。 | HTMLElement | ((anchor) => HTMLElement) | — |
| destroyOnHidden | 关闭后从页面移除面板。对象形式菜单的选择会保留。 | boolean | false |
| popupRender | 绘制面板内容:menu 渲染菜单。 | Snippet<[{ menu: Snippet }]> | — |
| classNames | root、menu、item、itemTitle(子菜单与分组标题)、itemContent、itemIcon 的附加类名。 | object | — |
| styles | 相同部分的内联样式。 | object | — |
与 Ant Design 的差异:无论触发方式键盘都能打开菜单并移入焦点;
menu 为对象或含 Menu 的 snippet;菜单项支持 href;触发元素为单个元素并带 ARIA 属性;没有 Dropdown.Button(那里已弃用)与 autoFocus;使用 bind:open 与小写回调名;classNames 与 styles 为对象。参数
| 属性 | 说明 | 类型 |
|---|---|---|
| source | onopenchange 的第二个参数:'menu' 或 'trigger'。 | DropdownOpenSource |
| menu | popupRender 的参数:绘制菜单(任一形式)的 snippet。 | Snippet |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-dropdown-z-index | 面板层级(Select 同层)。 | 1050 |
| --ads-dropdown-radius | 面板圆角。 | 8px |
| --ads-dropdown-padding | 菜单项周围的内边距。 | 4px |
| --ads-dropdown-item-height | 菜单项高度。 | 32px |
| --ads-dropdown-item-padding-inline | 菜单项水平内边距。 | 12px |
| --ads-dropdown-item-radius | 菜单项圆角。 | 4px |
| --ads-dropdown-item-hover-bg | 悬停时菜单项背景。页面深色主题下为 rgba(255, 255, 255, 0.08)。 | rgba(0, 0, 0, 0.04) |
| --ads-dropdown-item-icon-size | 图标大小。 | 12px |
| --ads-dropdown-item-icon-gap | 图标与文字间距。 | 8px |
| --ads-dropdown-item-extra-gap | extra 前的间距。 | 16px |
| --ads-dropdown-item-extra-font-size | extra 的字号。 | 12px |
| --ads-dropdown-group-item-indent | 分组内菜单项文字的起始位置。 | 20px |
无障碍
- 触发元素中可聚焦的元素会带上
aria-haspopup="menu"、aria-expanded,打开时带aria-controls;菜单以它命名(菜单自带的aria-label优先)。Ant Design 的示例没有这些属性。 - 无论
trigger如何,Enter、Space与ArrowDown打开并聚焦第一项,ArrowUp聚焦最后一项(链接上Enter打开链接)。点击打开也会把焦点移入菜单,悬停打开则不会。Escape返回触发元素,Tab关闭并继续页面焦点顺序;会先经过popupRender添加的控件。 - 选择菜单项后,焦点在调用
menu.onclick之前回到触发元素,因此在其中打开的对话框关闭时会还给它。角色、禁用项、选中状态与链接项与Menu一致。 - 在聚焦区域上按
Shift+F10或菜单键可打开右键菜单;请给菜单aria-label。Shift+F10已在 Chromium 以及(配合读屏软件)Firefox 中测试;未在 Safari 中测试。iPad 与 iPhone 长按不发送事件,因此无法打开。 - 强制颜色模式下面板带系统色边框,选中项有竖条;
tokens-accessible.css与深色主题达到 WCAG AA(同 Menu)。prefers-reduced-motion下无动画。 - 已用真实读屏软件(Orca 搭配 Firefox)验证:它能读出处于折叠状态、会打开菜单的按钮,以按钮命名的菜单,折叠或展开的子菜单,右键菜单与选中项,以及按
Escape或选择后再次读出按钮。未验证:NVDA、JAWS、VoiceOver、手机与真实触屏设备(已在三个浏览器引擎中测试模拟触摸)。