Web · Components
Menu
Action menu opened from a button (edit, copy, delete…).
9 exports
Live example
Import
Action menu opened from a button (edit, copy, delete…). Items are given with the items array; icons, shortcuts, groups and separators are drawn automatically.
- Keyboard: focuses the first item on open; ↑/↓ cycle through items, Home/End go to the first/last item, pressing a letter jumps to the item starting with it, Enter/Space selects, Esc closes.
- Shortcuts (
shortcut: "Mod+E") really work and show as⌘Eon macOS andCtrl+Eon Windows/Linux. SeeMenuItem.shortcut. - Any side and alignment can be chosen with
side("top-right","bottom","left-top"…); the default"auto"picks the place that fits the screen best. - For a menu opened by right-click use
ContextMenu. - Uses the native Popover API and CSS anchor positioning (see
Popover). - Mobile: On small screens the menu opens from the bottom like iOS/ Android action sheets; items are tall enough for comfortable thumb taps, and it closes by pulling down and with the back button.
Examples
Menu that takes over the right-click: right-clicking an area (long press on touch) opens your own items instead of the browser's menu. Items are given with the same items array as Menu.
- Mouse: The menu opens at the cursor; if it doesn't fit on screen it flips left/up. It closes on an outside click, with Esc, on scroll or when leaving the window. Right-clicking elsewhere while open moves the menu there.
- Keyboard: While the area (or an element inside) is focused, Shift+F10 or the menu key opens the menu below the focused element; ↑/↓, Home/End, type-ahead, Enter and Esc work like
Menu. Focus returns on close. - Shortcuts: Items'
shortcutvalues work; by default only while focus is inside the area (seeContextMenuShortcutScope). - Mobile: Opens on long press, and becomes a bottom action sheet on small screens (closed by pulling down and with the back button). Scrolling and normal taps aren't broken; the click following the press that opened the menu is swallowed. iOS's link preview bubble is disabled in this area.
- Performance: The menu panel is added to the DOM on the first right-click, long press or focus; wrapping every table row is cheap.
- In nested areas the innermost menu opens. If you call
event.preventDefault()in your ownonContextMenuhandler, the menu doesn't open.
Examples
When ContextMenu item shortcuts work.
"area": While focus is inside the area (e.g. a table row is selected) or the menu is open. This is the right one if the same menu appears many times on the page (in every row)."global": Anywhere on the page."open": Only while the menu is open."none": Shortcuts are only displayed.
ContextMenuProps
interfaceProps of the ContextMenu component.
| Name | Type | Default | Description |
|---|---|---|---|
children | ReactElement | Area to right-click: a single element (card, table row, file icon…). Props are passed to this element; no extra wrapper is added. | |
items | readonly MenuEntry[] | Items, groups and separators; same as Menu. | |
disabled? | boolean | false | If true, the browser's own context menu opens. |
longPressDelay? | number | 500 | Long press duration to open on touch screens (ms). |
shortcutScope? | ContextMenuShortcutScope | "area" | When item shortcuts work. See ContextMenuShortcutScope. |
onOpenChange? | (open: boolean) => void | Called with the new state when it opens or closes. | |
mobileSheet? | boolean | true | Open from the bottom like an action sheet on small screens (below 640 px)? |
label? | string | Screen reader name of the menu. | |
className? | string | Classes added to the menu panel (e.g. w-56). | |
effect? | EffectProp | Effect or effects of the items (e.g. "ripple", "shine"). If omitted, UIProvider appearance.defaults.menu.effect. See BuiltinEffect. |
MenuItem
interfaceAn action inside a Menu.
| Name | Type | Description |
|---|---|---|
label | ReactNode | Text of the item. |
icon? | IconProp | Icon at the start of the text: a name or an element. |
description? | ReactNode | Small description under the text. |
shortcut? | string | Keyboard shortcut; both shown on the right and working. Parts are separated with +: "Mod+E", "Mod+Shift+K", "Alt+Enter", "Delete", "F2". - Mod is ⌘ on macOS/iOS and Ctrl on Windows/Linux; the display changes accordingly (⇧⌘K / Ctrl+Shift+K). For a system-independent key write Ctrl, Meta, Alt, Shift. - Pressing the shortcut is the same as clicking the item (onSelect or href). A disabled item's shortcut doesn't work. - When it works is set by the menu's shortcutScope prop. - In a text field, shortcuts without modifiers and Mod+A/C/V/X/Y/Z, and Mod+C/X while text is selected on the page, don't fire; the browser's own behavior isn't broken. - Mobile: The shortcut text is hidden on touch screens; shortcuts still work with a keyboard attached to a tablet. |
tone? | "neutral" | "danger" | If "danger", it appears red (e.g. Delete). |
disabled? | boolean | Cannot be selected. |
tooltip? | TooltipProp | Hint shown when the item is reached (mouse or arrow keys). It shows even while the item is disabled; good for explaining why it can't be selected. See TooltipProp. |
onSelect? | (event: MouseEvent<HTMLElement>) => void | Called when selected (click, Enter, Space). Call event.preventDefault() if you don't want the menu to close. |
href? | string | If given, the item becomes a link; rendered with the link component given to UIProvider (e.g. Next.js Link). |
MenuGroup
interfaceA titled group of items.
| Name | Type | Description |
|---|---|---|
label | ReactNode | Title of the group. |
items | readonly MenuItem[] | Items in the group. |
MenuEntry
typeAn element of the Menu items array: an item, a group or a "separator" line.
When menu item shortcuts work.
"global": Always while the menu is on the page (even while closed). If the same shortcut is in several menus, the first rendered one wins."open": Only while the menu is open."none": Shortcuts are only displayed and don't work.
MenuProps
interfaceProps of the Menu component.
| Name | Type | Default | Description |
|---|---|---|---|
trigger | ReactElement | Element that opens the menu, usually a Button or IconButton. | |
items | readonly MenuEntry[] | Items, groups and separators. | |
side? | Placement | "auto" | Position relative to the trigger: side and alignment (e.g. "top-right", "bottom-left", "right"). See Placement. |
open? | boolean | Whether it is open when controlled. | |
defaultOpen? | boolean | false | Is it initially open when uncontrolled? |
onOpenChange? | (open: boolean) => void | Called with the new state when it opens or closes. | |
mobileSheet? | boolean | true | Open from the bottom like an action sheet on small screens (below 640 px)? |
shortcutScope? | MenuShortcutScope | "global" | When item shortcuts work. If the same menu appears many times on the page, like a menu in every table row, pass "open". See MenuShortcutScope. |
label? | string | Screen reader name of the menu. | |
className? | string | Classes added to the menu panel (e.g. w-56). | |
effect? | EffectProp | Effect or effects of the items (e.g. "ripple", "shine"). If omitted, UIProvider appearance.defaults.menu.effect. See BuiltinEffect. |