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';Purpose
A menu shows a temporary list of choices anchored to the button that opened it. Use it for actions or options that don't need to be visible all the time; for a persistent list of items use a List, and for choosing one value in a form use a selection control.
A Menu must sit inside a MenuTrigger (trigger first, menu second), or a ContextMenuTrigger for a context menu. Items are MenuItems, optionally gathered into MenuGroups; both are identified by a React key. A SubmenuTrigger gives an item a submenu. The variant is standard or vibrant.
An item that opens a page or a file takes href (with target, rel and download) instead of running it from onAction: it becomes a real link with the item's look and keys, so people can open it in a new tab or copy and save its address, and following it closes the menu. Inside a RouterProvider (NextRouterProvider in Next.js) a plain click goes through the app's router. A disabled item, or one that opens a submenu, isn't a link.
Groups are set apart by dividers on one surface (layout="divider", the default, as M3 recommends on the web) or by 2px gaps between separate surfaces (layout="gap"). M3 doesn't use gaps in menus that scroll, so a gap menu that has to scroll switches to dividers. density (0 to −3) tightens items for dense desktop layouts; left out, a menu takes the density of its ThemeProvider or ThemeScope (see Theming).
presentation="auto" opens the menu in a bottom sheet on compact windows (below 600px wide) and anchored to its trigger on larger ones; "sheet" always uses the sheet, which opens at the menu's own height (up to the window). In a sheet, a submenu replaces the menu's content, below a back button (labels.back).
Submenus beside or in place
By default a submenu opens beside its item without covering it, as M3 shows (submenuPresentation="beside"). Use it for app menus, toolbars and context menus with room around them.
submenuPresentation="replace" shows a submenu in place of the menu instead, on the same surface, below a back button and its item's label, as a sheet does. Use it for a menu confined to a small surface: a video player's settings, a card, a narrow panel. The surface keeps its corner and its side of the trigger, and its height follows each page; hovering never opens a page. Escape, the back arrow and the back button go back one page, and Escape on the first page closes the menu. It extends M3, which shows submenus beside their item only.
Pages fade in by default; submenuTransition="slide" slides each one in 30px from the end edge going forward and from the start edge going back, M3's forward and backward pattern. On a submenu's own Menu, afterSelect="back" makes a choice return to the page that opened it, which shows the new choice, rather than close the menu: right for a settings page whose choice is all it's for. Space keeps the page open either way, and an action still closes the whole menu.
Opening inside an element
portalContainer opens the menu inside an element instead of body: a player, so the menu opens over the picture, moves with the player and shows in full screen with it, opened before or after. The menu is kept within the element, containerPadding from its edges, flipping and shifting inside it, and it's placed again when the element resizes. It keeps the theme, density and direction of where it's declared.
Pass the element from a callback ref kept in state, give it position: relative, and keep the trigger inside it or along its edge: a menu can't move past its trigger, so with the trigger outside it can only be kept within the element sideways. The element must draw its children, so pass the player around a <video>, never the video itself. A bottom sheet still opens over the window.
When not to use
For a persistent set of choices use a List or Tabs; for a single action use a Button.
See also
- Menu on m3.material.io: Material's own guidelines, with anatomy and specs.
- Split button: A primary action beside a trailing button that opens a menu of related actions.
- List: Rows with headline, overline, supporting text and leading or trailing content (icons, avatars, images, video thumbnails); items can be links, switches, checkboxes or radio buttons, and put in order by drag or a menu.
- Dialog: A modal surface for a focused task or a decision, with a title, content and actions, or full screen on compact windows.