Menu
A temporary list of choices anchored to a trigger, with groups, selection, submenus and context menus, or a bottom sheet on phones.
import { Menu, MenuItem, MenuGroup, MenuTrigger, SubmenuTrigger, ContextMenuTrigger } from '@windmorph/react';Props
Menu 31 props
MenuItems,MenuGroups andSubmenuTriggers.With
selectionMode: the last selected item can be deselected, leaving none.falsekeeps at least one item selected.The keys of items that can't be chosen. They stay focusable, as M3 asks ("Disabled menu items can receive focus but aren't selectable").
standard(surface colours) orvibrant(tertiary container). A submenu shown in place (in a sheet, or withsubmenuPresentation="replace") takes the root menu's: a menu's pages share one variant, so a page never looks different from the one that opened it, and the same in a popover and in a sheet.How groups are set apart.
divider: one surface with dividers between groups, which M3 recommends on the web and for menus that scroll.gap: each group is its own surface, 2px apart; agapmenu that has to scroll switches to dividers, since M3 doesn't use gaps in scrolling menus. A submenu beside its item takes its parent's layout and variant unless it sets its own; shown in place, it takes thedividerlayout and the root's variant.Web density (M3: "On web only … Increasing density decreases the top and bottom padding"): each step down takes 4px off the items' height (44px at 0, 32px at -3) and 2px off the space around dividers. Left out, the menu takes the density of the nearest
ThemeProviderorThemeScope; a submenu takes its parent menu's. Keep 0 for touch.placementtype"top" | "bottom" | "start" | "end" | "top-start" | "top-end" | "bottom-start" | "bottom-end" | "start-top" | "start-bottom" | "end-top" | "end-bottom"default"bottom-start"Placement relative to the trigger: a side, then optionally the edge to align with (
start/endfollow the text direction). A submenu opens beside its item, or in its place (submenuPresentation).Gap between the trigger and the menu, in px (a split button's menu sits 4px away).
The least space kept between the menu and the window's edges (or the
portalContainer's), in px. A split button passes 0, so its menu lines up with the trailing button even at the edge of the screen.Where the menu opens.
popover: anchored to its trigger.sheet: in a modal bottom sheet.auto: a sheet on compact windows (below 600px wide) and a popover on larger ones (M3: "Consider adapting menus into bottom sheets on small screens"). Set it on the root menu.On a submenu's
Menu(inside aSubmenuTrigger): what follows a choice on it, by press or Enter, in a single-select submenu or a single-choiceMenuGroupof it.close: the whole menu closes, as in any single-select menu.back: only this submenu closes, so the page that opened it shows again (shown in place) or stays (beside), showing the new choice, with focus on the item that opened it; for a settings page whose choice is all it's for. Space keeps the submenu open either way, a toggle in a multi-select submenu never closes it, and an action (a plain item) still closes the whole menu.The element the menu's popover opens in, instead of
body: a video player, so the menu opens over the picture, moves with the player and shows with it in full screen, opened before or after. The menu is placed against its trigger as before, then kept inside the element:containerPaddingfrom its edges, flipping, shifting and capping its height (the list scrolls) within it, and placed again when the element resizes. It keeps the theme,ThemeScope, density and direction of where it's declared. Give the elementposition: relative, keep the trigger inside it or along its edge, and pass the element from a callback ref kept in state. It must draw its children (not a<video>,<canvas>or<img>). Submenus beside the menu are kept within it as far as they can shift: in an element too narrow for one on either side of the menu, a submenu runs past its edge, so usesubmenuPresentation="replace"for a small element. Popovers only: a bottom sheet opens over the window (and inside the element in full screen, as every overlay does). The page'sbodyor<html>counts as no element: the menu opens inbody, kept in the window. Set it on the root menu.backIcontypeReactElement<unknown, string | JSXElementConstructor<any>>default Material Symbolsarrow_back, mirrored in right-to-leftThe icon of the back button above a submenu shown in place of its menu. Set it on the root menu.
Open and close with no motion (M3: "In dense products, such as on desktop, menus can open instantly to reduce motion"), and change pages shown in place with none. Submenus follow their root menu. Popovers only: a menu shown as a bottom sheet (
presentation="sheet", orautoon a compact window) keeps the sheet's slide, as M3's and Compose's modal bottom sheets always animate; under reduced motion it fades instead.Ref to the menu's list, the
role="menu"element, in a popover or a sheet.
Text
Words the menu shows above a submenu shown in place (in a sheet, or with
submenuPresentation="replace"). Set them on the root menu. ALabelsProvidersets them for the whole app.
Goes with
styleon the menu's outermost element: the box React Aria places against the trigger, or the bottom sheet's outermost element in a sheet. The surface inside keeps its size and its place against the trigger whatever layout class lands here (the box keeps the menu's own width); style it withclassNames.panel. On a submenu shown in place (in a sheet, or withsubmenuPresentation="replace"), its page: the back row and its list.Classes for each part, by slot.
The menu's surface (the
menuelement): its colour, corners, size and the list that scrolls. In a sheet it spans the sheet, without a surface of its own. WithsubmenuPresentation="replace", the surface that holds the pages, around the lists.The bottom sheet the menu opens in (
presentation="sheet", or"auto"on compact windows): the sheet's surface, as itsclassNames.panel.Each group of items (its own surface in the
gaplayout).Each
MenuGroup's heading.Each item.
Each item's leading icon, or the icon marking a selected item.
Each item's
descriptionline.Each item's keyboard
shortcut.Each item's trailing icon, including a submenu's arrow.
The dividers between groups in the
dividerlayout.The row above a submenu shown in place of its menu: its back button and its title.
The back button in that row, which goes back to the menu that opened the submenu.
The submenu's title in that row: the label of the item that opened it.
Goes on the menu's outermost element, with
className(a submenu's page, shown in place).
Handler that is called when an item is selected.
The type of selection that is allowed in the collection.
The currently selected keys in the collection (controlled).
The initial selected keys in the collection (uncontrolled).
Handler that is called when the selection changes.
The element's unique identifier. See MDN.
Defines a string value that labels the current element.
Identifies the element (or elements) that labels the current element.
Identifies the element (or elements) that describes the object.
Identifies the element (or elements) that provide a detailed, extended description for the object.
Also accepts the root element's HTML attributes (id, data-*, event handlers).
MenuItem 14 props
The label.
Plain-text label for typeahead when
childrenis not a string.Icon before the label.
Icon shown while selected (defaults to a check in selectable menus).
Second line of text (supporting text), read as part of the item's name after the label.
A badge before the trailing text or icon (M3 anatomy), such as a
Badgewith a count. It's read after the name, as the item's description.Keyboard shortcut shown at the end, e.g. "⌘C", read as the item's description.
Icon at the end (an item with a submenu shows an arrow by default).
Makes the item a link: a real
<a>with the item's look, role and keys, so the browser's link menu (open in a new tab, copy or save the link) and modified clicks work. A press, Enter or Space follows it, through aRouterProvider(such asNextRouterProvider) on a plain click, and closes the menu. A disabled item, or one that opens a submenu, isn't a link.A link item's target;
_blankaddsrel="noopener noreferrer"unlessrelis given.A link item's
rel, e.g."nofollow".Downloads the link item's file instead of opening it (optionally with a file name).
MenuGroup 8 props
childrentypeReactElement<unknown, string | JSXElementConstructor<any>> | ReactElement<unknown, string | JSXElementConstructor<any>>[]RequiredMenuItems andSubmenuTriggers.Group label shown above its items.
A group of choices with a selection of its own, beside the menu's other items: its items are radio items (
single) or checkbox items (multiple), whatever the menu's ownselectionMode, and choosing one never changes another group's. Closing follows the group's mode, as a whole menu's would: a choice closes the menu (Space keeps it open), a toggle keeps it open.nonemakes the group's items plain actions that close the menu, in a selectable menu too.The group's selected keys (controlled), with
selectionMode;'all'is all its items.The group's keys selected at first (uncontrolled), with
selectionMode.With
selectionMode: the group's last selected item can be deselected, leaving none.falsekeeps one of its items selected.
Events
Called with the group's selected keys, its own only, when they change.
MenuTrigger 4 props
The trigger (a Button, IconButton, …) followed by the
Menu.Whether the menu is open (controlled).
Whether the menu starts open, when uncontrolled.
SubmenuTrigger 2 props
ContextMenuTrigger 5 props
The content the menu belongs to, followed by the
Menu.Whether the menu is open (controlled).
Whether the menu starts open, when uncontrolled.
Ignore secondary clicks and long presses.
Tokens
M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-menus-container-elevation | var(--md-sys-elevation-level-2) | The menu's shadow (each group's, in the gap layout). | MenuTokens.ContainerElevation |
--md-comp-menus-container-shape | var(--md-sys-shape-corner-large) | The menu's outer corners at rest. | MenuTokens.ContainerShape |
| Standard (variant) | |||
--md-comp-menus-standard-container-color | var(--md-sys-color-surface-container-low) | The menu (each group, in the gap layout). | StandardMenuTokens.ContainerColor |
--md-comp-menus-standard-menu-item-label-text-color | var(--md-sys-color-on-surface) | An item’s label. | StandardMenuTokens.ItemLabelTextColor |
--md-comp-menus-standard-menu-item-leading-icon-color | var(--md-sys-color-on-surface-variant) | An item’s leading icon. | StandardMenuTokens.ItemLeadingIconColor |
--md-comp-menus-standard-menu-item-trailing-icon-color | var(--md-sys-color-on-surface-variant) | An item’s trailing icon, keyboard shortcut or trailing text. | StandardMenuTokens.ItemTrailingIconColor |
--md-comp-menus-standard-menu-item-selected-container-color | var(--md-sys-color-tertiary-container) | A selected item. | StandardMenuTokens.ItemSelectedContainerColor |
--md-comp-menus-standard-menu-item-selected-label-text-color | var(--md-sys-color-on-tertiary-container) | A selected item’s label. | StandardMenuTokens.ItemSelectedLabelTextColor |
Spec status
Built to the M3 Expressive menu spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.
| Item | Status | Details |
|---|---|---|
| Vertical menu with divider | Built | Variant · Layouts, Basic |
| Vertical menu with gap | Built | Variant · Layouts |
| Color: standard | Built | Configuration · Basic, Layouts |
| Color: vibrant | Built | Configuration · Selection |
| Layout: standard | Built | Configuration · Selection |
| Layout: grouped | Built | Configuration · Basic, Layouts |
| Selection states | Built | Use case · Selection |
| Submenus | Built | Use case · Submenu |
| Context menu | Built | Use case · Context |
| Bottom sheet on small screens | Built | Use case · Sheet |
| Density (web) | Built | Configuration · Density |
| Gaps on the web | Extension | The spec says gaps aren't available on the web yet; layout="gap" offers them, and a gap menu that has to scroll switches to dividers, as the spec asks. |
| Offset | Extension | offset sets a gap between the trigger and the menu (the split button uses 4px). |
| Submenus in place | Extension | The spec opens a submenu next to its item, without covering it. submenuPresentation="replace" shows it in place of the menu instead, on the same surface below a back button, for a menu confined to a small surface such as a video player; submenuTransition fades or slides the pages, and afterSelect="back" returns to the page that opened a submenu after a choice. |
| Opening inside an element | Extension | portalContainer opens the menu inside an element (a player) instead of the page, kept within it and shown with it in full screen. |
| Baseline menu | Not built | The spec keeps the square-cornered baseline menu available, but the M3 Expressive vertical menu replaces it here. |
Vertical menu with gap
BuiltVariant · Layouts
Color: vibrant
BuiltConfiguration · Selection
Layout: standard
BuiltConfiguration · Selection
Selection states
BuiltUse case · Selection
Submenus
BuiltUse case · Submenu
Context menu
BuiltUse case · Context
Bottom sheet on small screens
BuiltUse case · Sheet
Density (web)
BuiltConfiguration · Density
Gaps on the web
ExtensionThe spec says gaps aren't available on the web yet;
layout="gap"offers them, and a gap menu that has to scroll switches to dividers, as the spec asks.Offset
Extensionoffsetsets a gap between the trigger and the menu (the split button uses 4px).Submenus in place
ExtensionThe spec opens a submenu next to its item, without covering it.
submenuPresentation="replace"shows it in place of the menu instead, on the same surface below a back button, for a menu confined to a small surface such as a video player;submenuTransitionfades or slides the pages, andafterSelect="back"returns to the page that opened a submenu after a choice.Opening inside an element
ExtensionportalContaineropens the menu inside an element (a player) instead of the page, kept within it and shown with it in full screen.Baseline menu
Not builtThe spec keeps the square-cornered baseline menu available, but the M3 Expressive vertical menu replaces it here.
Bundle size
Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.
Menu,MenuItem,MenuGroup,SubmenuTrigger: 38.6 kBMenuTrigger,ContextMenuTrigger: 2.1 kB
M3 Expressive specs
- Elevation
level2
Differences from Compose
- The Expressive grouped menu (Compose's
Menu.kt) is thegaplayout: each group is its own surface at elevation 2, 2px apart; loose items form implicit groups. The defaultdividerlayout follows the M3 spec for the web. - Items follow the M3 Menu token files rather than Compose's code: labels are
body-large, with 16px of padding at each side, a 12px gap after the leading icon andlabel-smalltrailing text (the shortcut). Compose drawslabel-largelabels with 12px of padding and an 8px gap on touch. Select's and Autocomplete's option lists share these items. Group headings start in line with the items' content, where Compose starts them 12px from the group's edge. - Submenus aren't in Compose. While one is open, the focused menu takes 24px corners and the other 8px (the SegmentedMenu active and inactive shapes), as the M3 spec describes. Shown in place (
submenuPresentation), only one page shows, so the surface keeps its resting corner. - The menu is revealed along one axis from its anchored edge as it fades in, instead of Compose's uniform 80% scale, which the M3 motion spec avoids.
standardusessurface-container-lowand selects withtertiary-container;vibrantistertiary-containerwith icons turningtertiaryon interaction and selects withtertiary.- In a selectable menu an item with no leading icon grows a check in (grid
0fr → 1fr) when selected, as in Compose. placementdefaults tobottom-startwith no offset; because React Aria resolves start / end from its locale, the menu converts them to physical sides from the trigger's computed direction.- A focus fix cancels the trigger's follow-up
mousedownso focus stays inside the menu after a mouse open.