Skip to content

Repository files navigation

@rc-component/menu

Ant Design Part of the Ant Design ecosystem.

🧭 Accessible React menu primitives for navigation, command surfaces, and nested item trees.

NPM versionnpm downloadsbuild statusCodecovbundle sizedumi

English | 简体中文

Highlights

  • Horizontal, vertical, and inline menu modes.
  • Controlled and uncontrolled selection, open keys, and active key state.
  • items configuration API with legacy children support.
  • Sub menus, item groups, dividers, icons, overflow, popup rendering, and keyboard focus helpers.
  • TypeScript definitions and semantic classNames / styles slots.
  • Used by Ant Design as the shared menu foundation.

Install

npm install @rc-component/menu

Usage

importMenu,{typeMenuProps}from'@rc-component/menu';constitems: MenuProps['items']=[{key: 'home',label: 'Home'},{key: 'docs',label: 'Docs',children: [{key: 'quick-start',label: 'Quick start'},{key: 'api',label: 'API'},],},];exportdefault()=>(<Menumode="inline"items={items}defaultSelectedKeys={['home']}defaultOpenKeys={['docs']}/>);
importMenu,{MenuItem,SubMenu}from'@rc-component/menu';exportdefault()=>(<Menu><MenuItemkey="1">Item 1</MenuItem><SubMenukey="2"title="More"><MenuItemkey="2-1">Item 2-1</MenuItem></SubMenu></Menu>);

Examples

Run the local dumi site:

npm install
npm start

Then open http://localhost:8000.

API

Menu

PropertyTypeDefaultDescription
activeKeystring-Controlled active item key.
builtinPlacementsRecord<string, any>-Popup alignment placements for sub menus.
classNamestring-Class name for the root menu.
classNamesPartial<Record<'list' | 'listTitle', string>>-Semantic class names for menu slots.
defaultActiveFirstbooleanfalseFocus the first enabled item when active key is absent.
defaultOpenKeysstring[][]Initial open sub menu keys.
defaultSelectedKeysstring[][]Initial selected item keys.
defaultMotionsPartial<Record<MenuMode | 'other', CSSMotionProps>>-Motion config by menu mode.
direction'ltr' | 'rtl'-Layout direction.
disabledbooleanfalseDisable all menu interactions.
disabledOverflowbooleanfalseDisable overflow measurement.
expandIconReactNode | (props: RenderIconInfo) => ReactNode-Custom sub menu expand icon.
forceSubMenuRenderbooleanfalseRender popup sub menus before they are opened.
getPopupContainer(node: HTMLElement) => HTMLElement-Container for popup sub menus.
inlineCollapsedboolean-Collapse inline menu layout.
inlineIndentnumber24Indent width for inline mode.
itemIconReactNode | (props: RenderIconInfo) => ReactNode-Custom item icon.
itemsItemType[]-Menu item configuration.
mode'horizontal' | 'vertical' | 'inline'verticalMenu display mode.
motionCSSMotionProps-Motion config for menu transitions.
multiplebooleanfalseAllow multiple selected items.
openKeysstring[]-Controlled open sub menu keys.
overflowedIndicatorReactNode"..."Indicator rendered for overflowed items.
popupRender(node, info) => ReactNode-Customize popup menu rendering.
prefixClsstringrc-menuClass name prefix.
rootClassNamestring-Class name for the root wrapper.
selectablebooleantrueAllow item selection.
selectedKeysstring[]-Controlled selected item keys.
stylesPartial<Record<'list' | 'listTitle', CSSProperties>>-Semantic styles for menu slots.
subMenuCloseDelaynumber0.1Delay in seconds before closing popup sub menus.
subMenuOpenDelaynumber0.1Delay in seconds before opening popup sub menus.
triggerSubMenuAction'click' | 'hover'hoverInteraction that opens sub menus.
onClick(info: MenuInfo) => void-Triggered when an item is clicked.
onDeselect(info: SelectInfo) => void-Triggered when an item is deselected.
onOpenChange(openKeys: string[]) => void-Triggered when open keys change.
onSelect(info: SelectInfo) => void-Triggered when an item is selected.

ItemType

typeItemType=|{type?: 'item';// Item keys accept React.Key and are normalized to strings in event info.key: React.Key;label?: React.ReactNode;disabled?: boolean;itemIcon?: RenderIconType;extra?: React.ReactNode;}|{type?: 'submenu';// Sub menu keys are strings to match openKeys/defaultOpenKeys.key: string;label?: React.ReactNode;children: ItemType[];disabled?: boolean;}|{type: 'group';label?: React.ReactNode;children?: ItemType[];}|{type: 'divider';};

Ref

MethodTypeDescription
focus(options?: FocusOptions) => voidFocus the active item or first enabled item.
findItem({ key: string }) => HTMLElement | nullFind the DOM element for an item key.
listHTMLUListElementRoot menu list element.

Development

npm install
npm start
npm test
npm run tsc
npm run compile
npm run build

The dumi site runs at http://localhost:8000 by default.

Release

npm run prepublishOnly

The release flow is handled by @rc-component/np through the rc-np command after the package build.

License

@rc-component/menu is released under the MIT license.

About

🧭 Accessible React menu primitives for navigation, command surfaces, and nested item trees.

Topics

Resources

Stars

700 stars

Watchers

30 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages