Menu

A menu is a list of choices for navigation: a top bar, a side list whose submenus open in popups, or a side list that expands in place and can collapse to icons. Items are written as data (items) or as markup. It is keyboard-ready and built on the popup layer of the library.

Import
import { Menu, MenuItem, MenuSubMenu, MenuItemGroup, MenuDivider } from 'svelte-ant-design';
Links
src/lib/Menu Report an issue Component README

When to use

  • Use a menu for the navigation of a site or an application: a top bar with the main sections, or a side list with several levels.
  • Pick the type by the room and the depth: horizontal for a few sections, vertical for a side list whose submenus open beside it, inline for a deep tree that expands in place, and inlineCollapsed to give the room back.
  • A menu of actions (nothing is selected) uses selectable={false}. A menu that opens from a button or by a right click is the Dropdown, which draws this one.

Examples

mode="horizontal", with a submenu and a disabled item
selectedKeys: mail
A narrow container: what does not fit waits in the "…" item (overflowLabel)

Top navigation

mode="horizontal": a bar with a submenu and a disabled item. In a narrow container the items that do not fit move into the “…” item (overflowLabel names it for assistive technology). A bar follows the width of its container: in a flex row or a grid give the element that holds it min-width: 0.

mode="inline": submenus expand in place
selectedKeys: 11; openKeys: sub1
inlineIndent={12}

Inline menu

mode="inline": submenus expand in place; bind:openKeys and bind:selectedKeys follow the user. inlineIndent sets the indent of each level (24px by default).

inlineCollapsed: icons only, a tip with the label (or title) on hover and focus, submenus in popups
tooltip={false} turns the tips off; tooltip={{ placement: 'bottom' }} sets them

Collapsed inline menu

inlineCollapsed narrows the menu to icons. An item shows a tip with its label (or title) on hover and focus, and a submenu opens in a popup. tooltip sets the tips or, as false, turns them off.

mode="vertical": submenus open in popups; popupOffset moves one of them by [4, 0]
Point at a submenu
triggerSubMenuAction="click", subMenuOpenDelay={0.4} and subMenuCloseDelay={0.5} (seconds)
Click a title

Vertical menu

mode="vertical": a submenu opens in a popup beside its title. popupOffset moves it, ontitleclick hears the click on the title. triggerSubMenuAction="click" opens it on a click; subMenuOpenDelay and subMenuCloseDelay are in seconds. The keyboard and touch open a popup in either case. A popup is flipped to the side with more room but not moved inside the window: on a screen narrower than the menu and its popup it can be cut off. Use mode="inline" there, or a narrower menu (the examples on this page do that on a phone).

theme="light" (the default); the submenu has theme="dark" for its popup
theme="dark"
theme="dark", horizontal

Themes

theme="light" follows the page, theme="dark" is the dark blue menu of Ant Design on any page. A submenu may take its own theme for its popup.

Single selection: a click replaces the choice; onselect runs when it changes
selectedKeys: a; no change yet
multiple: a click toggles the item; onselect and ondeselect tell which
selectedKeys: a, c; no change yet
selectable={false}: nothing is selected, onclick still runs (an action menu)
Choose an item

Selection

A click selects an item; multiple makes a click toggle it, with onselect and ondeselect. selectable={false} selects nothing and onclick reports the choice, with the key path. A selected item is spoken as checked.

Open only the current submenu: onopenchange keeps the last one that was opened
openKeys: sub1; no change yet

Controlled open keys

openKeys and onopenchange let the parent decide: here only the submenu opened last stays open. onopenchange is called for changes the user makes, not for those the parent makes.

extra, icon, title, disabled, danger, a link, a group, a divider and a dashed one

Kinds of items

extra at the end of the row, icon, title (the tip of a collapsed item), disabled, danger, a link (href, target, rel), a group with a title and a divider, solid or dashed.

label and icon as snippets; expandIcon replaces the arrow (it gets open, disabled, key)
overflowedIndicator replaces the dots of a bar that does not fit; overflowLabel names it

Icons, labels and indicators

label and icon take text or a snippet (the library has no icon set; draw your own). expandIcon replaces the arrow of a submenu and gets its state. overflowedIndicator replaces the dots of a bar that does not fit.

mode: inline, theme: light
The same items in every mode; the state of the menu is kept while the type changes

Switch the type

mode and theme change while the page is open; the selection and the open submenus are kept.

By default a popup is built when it is first shown
Items in the page, before any popup was opened: 0
forceSubMenuRender builds them at once, hidden (useful for a test, or to measure)
Items in the page, before any popup was opened: 0

Render submenus in advance

A popup submenu is built when it is first shown. forceSubMenuRender builds it at once, hidden. An inline submenu is always in the page, closed.

classNames and styles address root, item, itemIcon, itemContent, popup and subMenu.*
popupRender wraps the content of a popup: it gets the submenu, its keys and the content to put inside

Classes, styles and popupRender

classNames and styles address ten parts: root, item, itemIcon, itemContent, popup and the five of subMenu. popupRender wraps the content of a popup and gets the submenu, its keys and the content.

Tab enters the menu once, on the selected item. Left and Right move, Down opens a submenu, Escape closes it
selectedKeys: view
Up, Down, Home, End move; Right opens a submenu; typing a letter finds an item; Enter or Space chooses
selectedKeys: view

Keyboard

One Tab stop for the whole menu, on an item. The arrows, Home, End, Enter, Space, Escape and typing a letter work as in the menu and menubar patterns of the WAI-ARIA Authoring Practices.

MenuItem, MenuSubMenu, MenuItemGroup and MenuDivider written as children: the same tree as items
selectedKeys: home
Components appear in the order of the markup, also when {#if} and {#each} add and remove them

Items written as markup

MenuItem, MenuSubMenu, MenuItemGroup and MenuDivider make the same tree as items, in the order of the markup, also when {#if} and {#each} add and remove them. A non-empty items wins.

API

PropertyDescriptionTypeDefault
itemsThe content of the menu: items, submenus (an item with children), groups and dividers. The markup components below write the same tree.MenuItems[]
modeThe type of the menu. In vertical and horizontal submenus open in popups; in inline they expand in place. A popup that does not fit is flipped to the other side, not moved inside the window: on a very narrow screen use inline.'vertical' | 'horizontal' | 'inline''vertical'
themeColor theme. The light menu follows the theme of the page; the dark one is the dark blue menu of Ant Design.'light' | 'dark''light'
selectedKeysThe keys of the selected items. Use bind:selectedKeys; a plain value is the initial one. Keys that match no item are ignored and kept.string[][]
openKeysThe keys of the open submenus: expanded ones in inline, shown popups otherwise. Use bind:openKeys; a plain value is the initial one.string[][]
multipleA click toggles an item, so several can be selected. The items are checkboxes for assistive technology.booleanfalse
selectableWhen false nothing is selected and selectedKeys is ignored: a menu of actions. The items are plain menu items. By default true, and false in the menu of a Dropdown.boolean—
inlineCollapsedOnly for mode="inline": the menu narrows to the collapsed width and shows icons only. The expanded submenus are remembered and expand again with the menu.boolean—
inlineIndentThe indent of each level in inline, in pixels.number24
forceSubMenuRenderBuild the popups of submenus before they are first shown (hidden).booleanfalse
overflowedIndicatorThe content of the “…” item of a horizontal menu that does not fit; the default is three dots.string | Snippet—
overflowLabelThe accessible name of the “…” item. Write it in the language of the page; when the indicator shows text, the name must contain that text.string'More'
subMenuOpenDelayDelay before a popup shows when the pointer enters its title, in seconds.number0
subMenuCloseDelayDelay before a popup closes when the pointer has left the title and the popup, in seconds. The pointer may cross the gap between them.number0.1
triggerSubMenuActionWhat opens a popup submenu with the pointer. The keyboard and touch always work.'hover' | 'click''hover'
expandIconReplaces the arrow of a submenu. It gets { isSubMenu, open, disabled, key }.Snippet<[MenuExpandIconInfo]>—
tooltipThe tips of the items of a collapsed menu: the Tooltip props placement, arrow, mouseEnterDelay, mouseLeaveDelay, autoAdjustOverflow, zIndex, classNames and styles. false turns them off.false | MenuTooltipProps—
classNamesExtra classes for the parts root, item, itemIcon, itemContent, popup and subMenu (itemTitle, list, item, itemIcon, itemContent).MenuClassNames—
stylesInline styles for the same parts, as objects with kebab-case property names or custom properties; numbers are written as they are.MenuStyles—
popupRenderWraps the content of a popup submenu. It gets { item, keys, children }: render children where the content goes. A submenu may have its own.Snippet<[MenuPopupInfo]>—
onclickAn item was chosen by a click, Enter or Space: { key, keyPath, domEvent, itemData }. Not called for a disabled item.(info: MenuClickInfo) => void—
onselectAn item was added to the selection by the user: the info of onclick plus selectedKeys. Not called when the item was already the selected one.(info: MenuSelectInfo) => void—
ondeselectOnly with multiple: the user removed an item from the selection.(info: MenuSelectInfo) => void—
onopenchangeThe user opened or closed submenus. Not called for a change made by the parent.(openKeys: string[]) => void—
childrenMarkup components (MenuItem, MenuSubMenu, MenuItemGroup, MenuDivider). Ignored when items is not empty.Snippet—
classAn additional class of the root list.string—
styleAn inline style of the root list. Other attributes (aria-label, id, …) go to it too: give the menu a name.string—
Differences from Ant Design: items can also be written as markup (MenuItem, MenuSubMenu, MenuItemGroup, MenuDivider); the Tab stop is on an item, not on the list, and the arrows also work between the top items and their popups; the selected item is exposed to assistive technology; selectedKeys and openKeys are bound with bind: (there is no defaultSelectedKeys or defaultOpenKeys); callbacks are in lower case; keys are strings only.

Items (MenuItemType)

PropertyDescriptionType
typeOptional 'item'.'item'
keyUnique among all items and submenus of the menu.string
labelThe label: text, a number or a snippet.string | number | Snippet
iconAn icon: a snippet, or text such as an emoji. Hidden from assistive technology.string | Snippet
titleThe text of the tip of a collapsed item. Default: the text of the label.string
extraContent at the end of the row.string | number | Snippet
disabledCannot be chosen, is skipped by the arrow keys and says so.boolean
dangerThe danger color.boolean
hrefThe item is a link, drawn as an <a>: a click, Enter and Space follow it, and the selected one is announced as the current page. A disabled item has no address.string
targetThe target of the link, only with href.string
relThe relationship of the link, only with href.string
classAn extra class of the row.string

Submenus (MenuSubMenuType)

PropertyDescriptionType
typeOptional 'submenu'; an item with children is a submenu.'submenu'
keyUnique among all items and submenus of the menu.string
labelThe label: text, a number or a snippet.string | number | Snippet
iconAn icon: a snippet, or text such as an emoji. Hidden from assistive technology.string | Snippet
childrenThe items, groups, dividers and submenus inside.MenuItems
disabledCannot be chosen, is skipped by the arrow keys and says so.boolean
popupClassNameAn extra class of the popup. Not used in inline.string
popupOffsetMoves the popup by [x, y] pixels. Not used in inline.[number, number]
themeThe color theme of the popup of this submenu; by default the theme of the menu.'light' | 'dark'
ontitleclickCalled when the title is clicked or activated by Enter or Space: { key, domEvent }.(info: MenuTitleClickInfo) => void
popupRenderReplaces the content of the popup of this submenu; the menu-level one is the default.Snippet<[MenuPopupInfo]>
classAn extra class of the row.string

Groups (MenuGroupType)

PropertyDescriptionType
typeMust be 'group'.'group'
keyOptional; a group is not selectable or openable.string
labelThe title of the group. It names the group for assistive technology.string | number | Snippet
childrenThe items and dividers of the group.(MenuItemType | MenuDividerType)[]
classAn extra class of the row.string

Dividers (MenuDividerType)

PropertyDescriptionType
typeMust be 'divider'.'divider'
keyOptional.string
dashedA dashed line instead of a solid one.boolean

Arguments of the callbacks

PropertyDescriptionType
keyMenuClickInfo, MenuSelectInfo, MenuTitleClickInfo: the key of the item or the submenu.string
keyPathMenuClickInfo: the keys from the item up to the outermost submenu.string[]
domEventMenuClickInfo, MenuTitleClickInfo: the native event (a click, or the key press).MouseEvent | KeyboardEvent
itemDataMenuClickInfo: the item object, or the one made from the props of a MenuItem.MenuItemType
selectedKeysMenuSelectInfo: the selection after the change.string[]
isSubMenuMenuExpandIconInfo: always true (only submenus have an expand icon).boolean
openMenuExpandIconInfo: the submenu is open.boolean
disabledMenuExpandIconInfo: the submenu is disabled.boolean
itemMenuPopupInfo: the submenu, or null for the popup of the “…” item.MenuSubMenuType | null
keysMenuPopupInfo: the keys from the outermost submenu to this one.string[]
childrenMenuPopupInfo: the content of the popup, to render.Snippet

Markup components

PropertyDescriptionType
MenuItemkey, label (or the content), icon, title, extra, disabled, danger, href, target, rel, class.component
MenuSubMenukey, label, icon, disabled, popupClassName, popupOffset, theme, ontitleclick, popupRender, class; the content is its items.component
MenuItemGrouplabel, class; the content is its items.component
MenuDividerdashed.component

Methods

PropertyDescriptionType
focus(options)Moves the focus into the menu: item is 'first' or 'last' (without it the item that holds the tab stop), and preventScroll keeps the page where it is. Disabled items are skipped. Call it on the component: bind:this.(options?: { item?: 'first' | 'last'; preventScroll?: boolean }) => void

Design tokens

TokenDescriptionDefault
--ads-menu-item-heightHeight of an item.40px
--ads-menu-item-margin-blockVertical margin of an item; two items that follow each other share one margin.4px
--ads-menu-item-margin-inlineHorizontal margin of an item.4px
--ads-menu-item-padding-inlineHorizontal padding of an item.16px
--ads-menu-group-item-indentWhere the text of an item of a group starts, from the edge of a vertical list or a popup; the title of the group starts at the item padding.32px
--ads-menu-item-radiusCorner radius of an item and of a popup.8px
--ads-menu-submenu-item-radiusCorner radius of an item inside a submenu.4px
--ads-menu-icon-sizeSize of an icon.14px
--ads-menu-icon-gapSpace between an icon and the label.10px
--ads-menu-collapsed-widthWidth of a collapsed menu.80px
--ads-menu-collapsed-icon-sizeSize of an icon in a collapsed menu.16px
--ads-menu-horizontal-line-heightHeight of an item of a bar.46px
--ads-menu-horizontal-radiusCorner radius of an item of a bar.0
--ads-menu-active-bar-heightHeight of the bar under the selected item of a bar.2px
--ads-menu-group-title-font-sizeFont size of the title of a group.14px
--ads-menu-group-title-line-heightLine height of the title of a group.1.5714285714285714
--ads-menu-popup-min-widthMinimum width of a popup.160px
--ads-menu-bgBackground of the menu. In the dark theme of the page #141414.#ffffff
--ads-menu-popup-bgBackground of a popup.#ffffff
--ads-menu-submenu-bgBackground of an expanded inline submenu.rgba(0, 0, 0, 0.02)
--ads-menu-splitColor of the border of the menu and of the dividers.rgba(5, 5, 5, 0.06)
--ads-menu-popup-shadowShadow of a popup.0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-menu-item-colorText color of an item.rgba(0, 0, 0, 0.88)
--ads-menu-item-hover-colorText color of an item on hover.rgba(0, 0, 0, 0.88)
--ads-menu-item-hover-bgBackground of an item on hover.rgba(0, 0, 0, 0.06)
--ads-menu-item-active-bgBackground of an item while it is pressed.#e6f4ff
--ads-menu-item-selected-colorText color of the selected item. tokens-accessible.css uses #1668dc (4.63:1 on its background).#1677ff
--ads-menu-item-selected-bgBackground of the selected item.#e6f4ff
--ads-menu-submenu-selected-colorText color of the title of a submenu that holds the selected item.#1677ff
--ads-menu-item-disabled-colorText color of a disabled item.rgba(0, 0, 0, 0.25)
--ads-menu-group-title-colorText color of the title of a group and of extra. tokens-accessible.css uses 65% black (6.98:1).rgba(0, 0, 0, 0.45)
--ads-menu-danger-colorText color of a danger item. tokens-accessible.css uses #cf1322.#ff4d4f
--ads-menu-danger-hover-colorText color of a danger item on hover.#ff4d4f
--ads-menu-danger-selected-colorText color of the selected danger item.#ff4d4f
--ads-menu-danger-selected-bgBackground of the selected danger item.#fff2f0
--ads-menu-danger-active-bgBackground of a danger item while it is pressed.#fff2f0
--ads-menu-horizontal-hover-colorText color of an item of a bar on hover.#1677ff
--ads-menu-horizontal-selected-colorText color of the selected item of a bar, and the color of its bar.#1677ff
--ads-menu-horizontal-hover-bgBackground of an item of a bar on hover.transparent
--ads-menu-horizontal-selected-bgBackground of the selected item of a bar.transparent
--ads-menu-dark-bgBackground of the dark menu, on a light and on a dark page.#001529
--ads-menu-dark-popup-bgBackground of a popup of the dark menu.#001529
--ads-menu-dark-submenu-bgBackground of an expanded inline submenu of the dark menu.#000c17
--ads-menu-dark-item-colorText color of an item of the dark menu.rgba(255, 255, 255, 0.65)
--ads-menu-dark-item-hover-colorText color of an item of the dark menu on hover.#ffffff
--ads-menu-dark-item-hover-bgBackground of an item of the dark menu on hover.transparent
--ads-menu-dark-item-selected-colorText color of the selected item of the dark menu.#ffffff
--ads-menu-dark-item-selected-bgBackground of the selected item of the dark menu. tokens-accessible.css and the dark page use #1668dc (5.19:1 with white text).#1677ff
--ads-menu-dark-item-disabled-colorText color of a disabled item of the dark menu.rgba(255, 255, 255, 0.25)
--ads-menu-dark-group-title-colorText color of the title of a group of the dark menu.rgba(255, 255, 255, 0.65)
--ads-menu-dark-danger-colorText color of a danger item of the dark menu.#ff4d4f
--ads-menu-dark-danger-hover-colorText color of a danger item of the dark menu on hover.#ff7875
--ads-menu-dark-danger-selected-colorText color of the selected danger item of the dark menu.#ffffff
--ads-menu-dark-danger-selected-bgBackground of the selected danger item of the dark menu. tokens-accessible.css and the dark page use #d9363e (4.62:1 with white text).#ff4d4f
--ads-menu-dark-danger-active-bgBackground of a danger item of the dark menu while it is pressed.#ff4d4f

Accessibility

  • The root is role="menu" (menubar for horizontal) and the items are menuitem. A selectable item is a menuitemradio (a menuitemcheckbox with multiple) with aria-checked, so the selected item is spoken; Ant Design does not expose it. Name the menu with aria-label or aria-labelledby.
  • The title of a submenu has aria-haspopup="menu", aria-expanded and aria-controls, and its list is named by the title. The ids are unique per menu, so two menus on one page never clash (Ant Design had this bug).
  • One Tab stop for the whole menu, on an item (the selected one, else the first). The arrows, Home, End, typing a letter, Enter, Space and Escape work as in the WAI-ARIA menu and menubar patterns; disabled items are skipped, and Escape closes the topmost popup and returns focus to its title.
  • The keyboard and a tap open a popup whatever triggerSubMenuAction says: a menu that opens only by hover cannot be used without a mouse.
  • Icons are hidden from assistive technology, so they never join the name of an item (Ant Design reads “mail Navigation One”). The “…” item of a bar that does not fit is named by overflowLabel; write it in the language of the page. In a collapsed menu the labels stay in the page and the tips are visual only, so a label is not read twice.
  • Contrast: three defaults of the Ant Design token table are below 4.5:1 (the selected item 3.66:1, the group title 3.35:1, the danger item 3.27:1), and so are the selected fills of the dark menu (4.10:1 and 3.27:1 with white text). tokens-accessible.css darkens them; the dark theme of the page needs no file. Every state is tested.
  • In forced-colors modes such as Windows High Contrast the selected item gets a bar and a popup a border in system colors. The animations of submenus and of collapsing stop under prefers-reduced-motion.
  • Checked with a real screen reader (Orca with Firefox): it reads the name of the menu, the items as radio menu items that are selected or not, the submenus as collapsed or expanded, the title of a group with its first item, the popup and the inline list of a submenu, and skips a disabled item and a divider. Not checked: NVDA, JAWS and VoiceOver, whether a screen reader says "menu bar" for the horizontal mode (Orca did not say it where the focus came into the bar), and a real touch device (emulated touch is tested in three browser engines).