Dropdown

A dropdown is a button (or any element) with a menu in a panel next to it, opened by hover, click or a right click. The menu is the library’s Menu, given as an object or written as markup, so items, submenus, groups, selection and keyboard work as there.

Import
import { Dropdown } from 'svelte-ant-design';
Links
src/lib/Dropdown Report an issue Component README

When to use

  • Use a dropdown when a place offers several actions and there is no room to show them all: a button of a table row, a user menu in a header, the actions of an object on a right click.
  • To choose a value of a form use Select; for a list that is always on the page, such as navigation, use Menu.
  • Give the trigger a button or a link: it is what a keyboard and a screen reader reach. A hover menu also opens on Enter, Space, the arrows and a tap.

Examples

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

Basic

A button and a menu in a panel. The panel opens on hover by default; menu.onclick gets the chosen item. Any item can be switched off with disabled: true (here Archive): it is grey, the arrow keys skip it and a click on it does nothing.

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

Items

The items of the Menu: icon, extra, disabled, a divider, a group and 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

What opens it

trigger is hover, click or several of them; the delays are in seconds. The keyboard opens a hover or a click Dropdown either way.

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

Placement

Twelve placements. At the edge of the window the panel flips to the other side, takes the other alignment or moves in, unless autoAdjustOverflow is false.

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

Arrow

arrow draws an arrow to the trigger; pointAtCenter points it at the middle.

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

Context menu

With trigger="contextMenu" the menu opens at the pointer instead of the menu of the browser. Give it an aria-label and make the area focusable, so that Shift+F10 and the menu key work. A long press on iPad and iPhone does not open it.

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

Submenus

Submenus of any depth, as in the Menu. A disabled submenu does not open.

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

Callbacks

menu.onclick for the chosen item; onopenchange with source: 'menu' for a choice, 'trigger' for the trigger, Escape and a press outside.

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

Selectable

The menu of a Dropdown selects nothing unless menu.selectable is set; with multiple the panel stays open. The selection is kept while the Dropdown lives.

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

Controlled

bind:open hands the state to the page. With trigger={[]} a press outside does not close the panel, so one button can open and close it. To keep the panel open after a choice, set it back when source is 'menu'.

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

Custom content

popupRender gets menu, a snippet that draws the menu: put content around it, or leave the menu out.

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

Disabled and empty

disabled opens nothing and closes an open panel. A menu with no items opens nothing either.

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

The panel

container chooses where the panel lives in the page (a subtree with its own tokens, a scrolling box); destroyOnHidden removes it after closing.

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

Semantic classes and styles

classNames and styles reach six parts: root, menu, item, itemTitle, itemContent and itemIcon.

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

Button with a menu

An action and its other ways: two buttons side by side, the second one opens a Dropdown. Ant Design deprecated Dropdown.Button in favor of this.

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

The menu as markup

Instead of an object, menu may be a snippet with a Menu of your own: markup items, {#if} and bind: work.

API

PropertyDescriptionTypeDefault
menuThe menu of the panel: an object with the props of Menu (without mode, inlineCollapsed, inlineIndent, overflowedIndicator, overflowLabel and tooltip, which have no effect in a vertical menu), or a snippet that writes a Menu. Both behave alike: vertical, not selectable unless selectable, a choice closes the panel.DropdownMenuProps | Snippet—
childrenThe trigger: one element. The attributes of a menu button go to what takes the focus in it.Snippet—
openWhether the panel is shown; bind:open follows the user. A plain value is the initial one.booleanfalse
onopenchangeA change made by the user: a choice (source: 'menu'), or the trigger, Escape, Tab and a press outside ('trigger').(open: boolean, info: { source: 'trigger' | 'menu' }) => void—
triggerWhat opens it with a pointer: hover, click, contextMenu or several. An empty array: only open, and a press outside does not close it (Escape and a choice do).'click' | 'hover' | 'contextMenu' | Array['hover']
placementSide and alignment; mirrored in right-to-left.'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight' | 'left' | 'leftTop' | 'leftBottom' | 'right' | 'rightTop' | 'rightBottom''bottomLeft'
arrowAn arrow to the trigger; pointAtCenter points it at the middle.boolean | { pointAtCenter: boolean }false
disabledOpens nothing, and an open panel closes.booleanfalse
autoAdjustOverflowAt the edge of the window the panel flips, takes the other alignment or moves in.booleantrue
mouseEnterDelayHover: seconds before the panel opens.number0.15
mouseLeaveDelayHover: seconds before it closes after the pointer left.number0.1
containerWhere the panel is moved; the page body by default.HTMLElement | ((anchor) => HTMLElement)—
destroyOnHiddenRemoves the panel from the page after closing. The selection of an object menu is kept.booleanfalse
popupRenderDraws the content of the panel: menu renders the menu.Snippet<[{ menu: Snippet }]>—
classNamesExtra classes of root, menu, item, itemTitle (titles of submenus and groups), itemContent and itemIcon.object—
stylesInline styles of the same parts.object—
Differences from Ant Design: the keyboard opens the menu whatever the trigger and the focus moves into it; menu is an object or a snippet with a Menu; an item takes href; the trigger is one element and gets the ARIA attributes; there is no Dropdown.Button (deprecated there) and no autoFocus; bind:open and callbacks in lower case; classNames and styles are objects.

Arguments

PropertyDescriptionType
sourceSecond argument of onopenchange: 'menu' or 'trigger'.DropdownOpenSource
menuArgument of popupRender: a snippet that draws the menu, given in either form.Snippet

Design tokens

TokenDescriptionDefault
--ads-dropdown-z-indexLayer of the panel (also of Select).1050
--ads-dropdown-radiusCorner radius of the panel.8px
--ads-dropdown-paddingPadding around the items.4px
--ads-dropdown-item-heightHeight of an item.32px
--ads-dropdown-item-padding-inlineHorizontal padding of an item.12px
--ads-dropdown-item-radiusCorner radius of an item.4px
--ads-dropdown-item-hover-bgBackground of an item on hover. In the dark theme of the page rgba(255, 255, 255, 0.08).rgba(0, 0, 0, 0.04)
--ads-dropdown-item-icon-sizeSize of an icon.12px
--ads-dropdown-item-icon-gapSpace between an icon and the label.8px
--ads-dropdown-item-extra-gapSpace before extra.16px
--ads-dropdown-item-extra-font-sizeFont size of extra.12px
--ads-dropdown-group-item-indentWhere the text of an item of a group starts.20px

Accessibility

  • The element of the trigger that takes focus gets aria-haspopup="menu", aria-expanded and, while open, aria-controls; the menu is named by it (an aria-label of the menu wins). Ant Design sets none of these on its demos.
  • Enter, Space and ArrowDown open on the first item, ArrowUp on the last, whatever trigger says (on a link Enter follows the link). A click opening moves the focus into the menu too, a hover opening leaves it. Escape returns to the trigger, Tab closes and goes on in the page; controls added by popupRender are walked first.
  • A chosen item puts the focus back on the trigger before menu.onclick runs, so a dialog opened there returns it to the trigger. Roles, disabled items, the selected state and link items are those of the Menu.
  • A context menu opens with Shift+F10 and the menu key on the focused area; give the menu an aria-label. Shift+F10 is tested in Chromium and, with a screen reader, in Firefox; not in Safari. iPad and iPhone send no event for a long press, so it does not open there.
  • In forced-colors modes the panel has a border and the selected item a bar in system colors; tokens-accessible.css and the dark theme reach WCAG AA, as for the Menu. Nothing moves under prefers-reduced-motion.
  • Checked with a real screen reader (Orca with Firefox): it reads the button as collapsed and opening a menu, the menu with the name of the button, submenus as collapsed or expanded, the context menu and the selected item, and the button again after Escape or a choice. Not checked: NVDA, JAWS, VoiceOver, a phone, and a real touch device (emulated touch is tested in three browser engines).