Back to Ant Design

Menu

components/menu/index.en-US.md

6.3.78.7 KB
Original Source

When To Use

Navigation is an important part of any website, as a good navigation setup allows users to move around the site quickly and efficiently. Ant Design offers two navigation options: top and side. Top navigation provides all the categories and functions of the website. Side navigation provides the multi-level structure of the website.

More layouts with navigation: Layout.

Notes for developers

  • Menu is rendered as a ul element, so it only supports li and script-supporting elements as children nodes. Your customized node should be wrapped by Menu.Item.
  • Menu needs to collect its node structure, so its children should be Menu.* or encapsulated HOCs.

Examples

<!-- prettier-ignore -->

<code src="./demo/horizontal.tsx">Top Navigation</code> <code src="./demo/horizontal-dark.tsx" debug>Top Navigation (dark)</code> <code src="./demo/inline.tsx">Inline menu</code> <code src="./demo/inline-collapsed.tsx">Collapsed inline menu</code> <code src="./demo/tooltip.tsx">Menu tooltip</code> <code src="./demo/sider-current.tsx">Open current submenu only</code> <code src="./demo/vertical.tsx">Vertical menu</code> <code src="./demo/theme.tsx">Menu Themes</code> <code src="./demo/submenu-theme.tsx">Sub-menu theme</code> <code src="./demo/switch-mode.tsx">Switch the menu type</code> <code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code> <code src="./demo/style-debug.tsx" debug>Style debug</code> <code src="./demo/menu-v4.tsx" debug>Menu v4</code> <code src="./demo/component-token.tsx" debug>Component Token</code> <code src="./demo/extra-style.tsx" debug>Extra Style debug</code> <code src="./demo/custom-popup-render.tsx">Custom Submenu Render</code> <code src="./demo/collapsed-icon-debug.tsx" debug>Collapsed menu icon alignment</code>

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersion
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-
defaultOpenKeysArray with the keys of default opened sub menusstring[]-
defaultSelectedKeysArray with the keys of default selected menu itemsstring[]-
expandIconcustom expand icon of submenuReactNode | (props: SubMenuProps & { isSubMenu: boolean }) => ReactNode-4.9.0
forceSubMenuRenderRender submenu into DOM before it becomes visiblebooleanfalse
inlineCollapsedSpecifies the collapsed status when menu is inline modeboolean-
inlineIndentIndent (in pixels) of inline menu items on each levelnumber24
itemsMenu item contentItemType[]-4.20.0
modeType of menuvertical | horizontal | inlinevertical
multipleAllows selection of multiple itemsbooleanfalse
openKeysArray with the keys of currently opened sub-menusstring[]-
overflowedIndicatorCustomized the ellipsis icon when menu is collapsed horizontallyReactNode<EllipsisOutlined />
selectableAllows selecting menu itemsbooleantrue
selectedKeysArray with the keys of currently selected menu itemsstring[]-
styleStyle of the root nodeCSSProperties-
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-
subMenuCloseDelayDelay time to hide submenu when mouse leaves (in seconds)number0.1
subMenuOpenDelayDelay time to show submenu when mouse enters, (in seconds)number0
tooltipConfig tooltip props for menu items in inline collapsed mode. Set to false to disable.false | TooltipProps-6.3.0
themeColor theme of the menulight | darklight
triggerSubMenuActionWhich action can trigger submenu open/closehover | clickhover
onClickCalled when a menu item is clickedfunction({ key, keyPath, domEvent })-
onDeselectCalled when a menu item is deselected (multiple mode only)function({ key, keyPath, selectedKeys, domEvent })-
onOpenChangeCalled when sub-menus are opened or closedfunction(openKeys: string[])-
onSelectCalled when a menu item is selectedfunction({ key, keyPath, selectedKeys, domEvent })-
popupRenderCustom popup renderer for submenu(node: ReactElement, props: { item: SubMenuProps; keys: string[] }) => ReactElement-

More options in @rc-component/menu

ItemType

type ItemType = MenuItemType | SubMenuType | MenuItemGroupType | MenuDividerType;

MenuItemType

PropertyDescriptionTypeDefaultVersion
dangerDisplay the danger stylebooleanfalse
disabledWhether menu item is disabledbooleanfalse
extraThe extra of the menu itemReactNode-5.21.0
iconThe icon of the menu itemReactNode-
keyUnique ID of the menu itemstring-
labelMenu labelReactNode-
titleSet display title for collapsed itemstring-

SubMenuType

<!-- prettier-ignore -->
PropertyDescriptionTypeDefaultVersion
childrenSub-menus or sub-menu itemsItemType[]-
disabledWhether sub-menu is disabledbooleanfalse
iconIcon of sub menuReactNode-
keyUnique ID of the sub-menustring-
labelMenu labelReactNode-
popupClassNameSub-menu class name, not working when mode="inline"string-
popupOffsetSub-menu offset, not working when mode="inline"[number, number]-
themeColor theme of the SubMenu (inherits from Menu by default)light | dark-
onTitleClickCallback executed when the sub-menu title is clickedfunction({ key, domEvent })-
popupRenderCustom popup renderer for current sub-menu(node: ReactElement, props: { item: SubMenuProps; keys: string[] }) => ReactElement-

MenuItemGroupType

Define type as group to make as group:

ts
const groupItem = {
  type: 'group', // Must have
  label: 'My Group',
  children: [],
};
PropertyDescriptionTypeDefaultVersion
childrenSub-menu itemsMenuItemType[]-
labelThe title of the groupReactNode-

MenuDividerType

Divider line in between menu items, only used in vertical popup Menu or Dropdown Menu. Need define the type as divider

ts
const dividerItem = {
  type: 'divider', // Must have
};
PropertyDescriptionTypeDefaultVersion
dashedWhether line is dashedbooleanfalse

FAQ

Why will Menu's children be rendered twice? {#faq-render-twice}

Menu collects structure info with twice-render to support HOC usage. Merging into one render may cause the logic to become much more complex. Contributions to help improve the collection logic are welcomed.

Why Menu do not responsive collapse in Flex layout? {#faq-flex-layout}

Menu will render fully item in flex layout and then collapse it. You need tell flex not consider Menu width to enable responsive (online demo):

jsx
<div style={{ flex }}>
  <div style={{ ... }}>Some Content</div>
  <Menu style={{ minWidth: 0, flex: "auto" }} />
</div>

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Menu"></ComponentTokenTable>