Dropdown下拉菜单

下拉菜单是带面板菜单的按钮(或任意元素),可通过悬停、点击或右键打开。菜单就是本库的 Menu,可用对象或标记书写,因此菜单项、子菜单、分组、选择与键盘行为与它一致。

引入
import { Dropdown } from 'svelte-ant-design';
链接
src/lib/Dropdown 问题反馈 组件 README

何时使用

  • 当某处有多个操作而放不下全部时使用下拉菜单:表格行的按钮、页头的用户菜单、右键时某个对象的操作。
  • 在表单中选择值请用 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

基本用法

按钮与面板中的菜单。默认悬停打开面板;menu.onclick 得到所选菜单项。任意菜单项都可以用 disabled: true 禁用(此处为 Archive):它显示为灰色,方向键会跳过它,点击它没有任何效果。

icon, extra, disabled, a divider, a group and danger: the items of the Menu Nothing chosen yet

菜单项

Menu 的菜单项:icon、extra、disabled、分隔线、分组与 danger。

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

触发方式

trigger 为 hover、click 或两者;延迟以秒为单位。无论哪种方式,键盘都能打开。

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

弹出位置

十二个位置。靠近窗口边缘时面板翻转、改用另一种对齐或移入窗口,除非 autoAdjustOverflow 为 false。

arrow
arrow={{ pointAtCenter: true }}: at the middle of the trigger

箭头

arrow 显示指向触发元素的箭头;pointAtCenter 使其指向中心。

trigger="contextMenu": right click in the area, or focus it and press Shift+F10
Right click here
Nothing chosen yet

右键菜单

trigger="contextMenu" 时菜单在指针处打开,代替浏览器菜单。请给它 aria-label 并让区域可聚焦,使 Shift+F10 与菜单键可用。iPad 与 iPhone 上长按不会打开它。

Submenus open beside their item; ArrowRight opens one from the keyboard Nothing chosen yet

子菜单

任意层级的子菜单,与 Menu 相同。禁用的子菜单不会打开。

menu.onclick gets the item; onopenchange says whether a choice or the trigger changed the panel
Open the menu to see the calls

回调

menu.onclick 对应所选项;onopenchange 带 source:选择菜单项为 'menu',触发元素、Escape 与外部点击为 'trigger'。

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

可选择

未设置 menu.selectable 时 Dropdown 的菜单不保留选中状态;multiple 时面板保持打开。选择在 Dropdown 存续期间保留。

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

受控

bind:open 把状态交给页面。使用 trigger={[]} 时外部点击不会关闭面板,因此同一个按钮即可打开和关闭它。想在选择后保持打开,请在 source 为 'menu' 时把它设回。

popupRender: content around the menu; Tab walks from the menu into it Nothing yet
popupRender without a menu

自定义内容

popupRender 收到绘制菜单的 snippet menu:可在周围放置内容,或不渲染菜单。

disabled: nothing opens; the button itself is yours to disable
A menu with no items opens nothing

禁用与空菜单

disabled 不打开任何内容并关闭已打开的面板。没有菜单项的菜单同样不会打开。

container: the panel is moved into this box instead of the page body
destroyOnHidden: the panel is removed from the page after closing

面板

container 指定面板在页面中的位置(拥有自定义令牌的子树、滚动容器);destroyOnHidden 关闭后移除面板。

classNames and styles: root, menu, item, itemTitle, itemContent, itemIcon

语义化类名与样式

classNames 与 styles 作用于六个部分:root、menu、item、itemTitle、itemContent 与 itemIcon。

A button with a menu beside it: two buttons side by side, the second one opens the menu
Nothing yet

带菜单的按钮

一个操作及其其他方式:两个并排按钮,第二个打开 Dropdown。Ant Design 已弃用 Dropdown.Button,改用这种写法。

menu as a snippet: write the Menu yourself; it behaves as the menu of the panel
Nothing chosen yet

用标记书写菜单

menu 也可以是包含你自己的 Menu 的 snippet:支持标记菜单项、{#if} 与 bind:。

API

属性说明类型默认值
menu面板中的菜单:包含 Menu 属性的对象(不含对垂直菜单无效的 mode、inlineCollapsed、inlineIndent、overflowedIndicator、overflowLabel、tooltip),或写有 Menu 的 snippet。两者行为一致:垂直、未设 selectable 时不选中、选择后关闭面板。DropdownMenuProps | Snippet—
children触发元素:单个元素。菜单按钮属性加在其中可聚焦的元素上。Snippet—
open面板是否显示;bind:open 跟随用户。普通值为初始值。booleanfalse
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不打开任何内容,并关闭已打开的面板。booleanfalse
autoAdjustOverflow靠近窗口边缘时面板翻转、换对齐方式或移入窗口。booleantrue
mouseEnterDelay悬停:打开前的延迟(秒)。number0.15
mouseLeaveDelay悬停:指针离开后关闭前的延迟(秒)。number0.1
container面板挂载的位置;默认为 body。HTMLElement | ((anchor) => HTMLElement)—
destroyOnHidden关闭后从页面移除面板。对象形式菜单的选择会保留。booleanfalse
popupRender绘制面板内容:menu 渲染菜单。Snippet<[{ menu: Snippet }]>—
classNamesroot、menu、item、itemTitle(子菜单与分组标题)、itemContent、itemIcon 的附加类名。object—
styles相同部分的内联样式。object—
与 Ant Design 的差异:无论触发方式键盘都能打开菜单并移入焦点;menu 为对象或含 Menu 的 snippet;菜单项支持 href;触发元素为单个元素并带 ARIA 属性;没有 Dropdown.Button(那里已弃用)与 autoFocus;使用 bind:open 与小写回调名;classNames 与 styles 为对象。

参数

属性说明类型
sourceonopenchange 的第二个参数:'menu' 或 'trigger'。DropdownOpenSource
menupopupRender 的参数:绘制菜单(任一形式)的 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-gapextra 前的间距。16px
--ads-dropdown-item-extra-font-sizeextra 的字号。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、手机与真实触屏设备(已在三个浏览器引擎中测试模拟触摸)。