Menu
A list of actions in a dropdown, enhanced with keyboard navigation.
Examples#
Open on hover#
To create a menu that opens on hover, add the openOnHover prop to Menu. You can additionally configure how quickly the menu opens on hover using the delay prop.
Checkbox items#
Use the MenuCheckboxItem part to create a menu item that can toggle a setting on or off. Here, the server keeps the state: each item has checked and an onClick handler. With defaultChecked and no handler, the browser keeps it.
Radio items#
Use the MenuRadioGroup and MenuRadioItem parts to create menu items that work like radio buttons.
Close on click#
Use the closeOnClick prop to change whether the menu closes when an item is clicked. Here, the checkbox item closes the menu, and the first item keeps it open.
Group labels#
Use the MenuGroupLabel part to add a label to a MenuGroup or MenuRadioGroup.
#
To create a submenu, nest a MenuSubmenu inside the popup of the parent menu. Use the MenuSubmenuTrigger part for the menu item that opens the nested menu.
#
Use the MenuLinkItem part to create a link. Turbo follows it as a link of the page.
Open a dialog#
In order to open a dialog using a menu, make the menu item a detached trigger of the dialog: give it data-phem-dialog-for with the handle of the dialog. The click closes the menu and opens the dialog.
Detached triggers#
A menu can be opened by a trigger that lives either inside or outside the Menu. Keep the trigger inside Menu for simple, tightly coupled layouts like the hero demo at the top of this page. When the trigger and menu content need to live in different parts of the tree, give the same handle to the trigger and to the root. The root gets the handle as its id, and the trigger gets data-phem-menu-for.
Note that only top-level menus can have detached triggers. Submenus must have their triggers defined within the MenuSubmenu part.
Multiple triggers#
One menu can be opened by several triggers. You can either render multiple MenuTrigger components inside the same Menu, or attach several detached triggers to the same handle.
Menus can render different content depending on which trigger opened them. Give each trigger a payload, and put a MenuPayload with the same name in the MenuViewport: the server renders each content in a template, and the viewport shows a copy of the content of the active trigger.
Controlled mode#
You can control the menu's open state with the defaultOpen and triggerId props of Menu: a render with new values opens the menu next to the trigger with that id. The controller keeps the values of the browser in data-phem-menu-open-value and data-phem-menu-trigger-id-value.
Here, the button is in the root, and opens the menu without a request with the phem-menu#open action and a triggerId param.
Arrow#
Use the MenuArrow part inside the popup to visually connect the menu to its trigger.
#
When one menu is opened by multiple detached triggers, you can animate the menu as it moves between triggers. This includes animating position, size, and content.
To animate the menu's position, apply CSS transitions to the top and left properties of the MenuPopup. To animate its size, transition its width and height to var(--popup-width) and var(--popup-height).
To enable content animations, wrap the menu content in the MenuViewport part. It gets a data-activation-direction attribute with up to two space-separated tokens: a horizontal (left or right) and a vertical (up or down) value. Inside the viewport, content is wrapped in divs with data-current (the currently visible or the incoming content) and data-previous (the outgoing content).
Filtering#
Add MenuInput to the popup, and put the items inside MenuList. Label the input with aria-label, aria-labelledby, or a visible <label>. Type to narrow the actions, then use the arrow keys to move through matching items.
Items match their text by default, ignoring case, accents, and punctuation. Use label to override it. Groups with no matching items get the hidden attribute and stay in the page, and MenuEmpty appears when no items match. MenuClear empties the query, and the query resets when the menu closes.
Use autoHighlight to highlight the first match while filtering, or autoHighlight="always" to highlight it even when the query is empty. For server filtering, pass filter={{ false }} and render the results of an onInput handler on the input.
Opening the menu with a click or the keyboard focuses the input. Opening it on hover or with a touch does not, so the on-screen keyboard stays hidden. The arrow keys move the highlight while the input keeps focus. Tab closes the menu, and Shift+Tab returns focus to the trigger. The popup is a dialog that holds the searchbox input and the menu list. Give each submenu its own MenuInput: submenus without one remain unfiltered.
API reference#
Import the components of the family:
use Phem\Ui\Menu;
use Phem\Ui\MenuSubmenu;
use Phem\Ui\MenuRadioGroup;
use function Phem\Ui\{MenuTrigger, MenuSubmenuTrigger, MenuPopup, MenuArrow, MenuItem, MenuLinkItem, MenuSeparator, MenuGroup, MenuGroupLabel, MenuCheckboxItem, MenuItemIndicator, MenuItemLabel, MenuRadioItem, MenuViewport, MenuPayload, MenuInput, MenuClear, MenuEmpty, MenuList};Menu#
A list of actions in a popup: Base UI Menu.Root, on the phem-menu controller.
The props of the Base UI Positioner are props of the root. The popup goes in the top layer (popover="manual"), so it needs no Portal. The parts read the root, so the same parts work in a ContextMenu and in a MenuSubmenu.
As in Base UI, the menu is modal by default: the page does not scroll while it is open. A menu with openOnHover and a menu in a Menubar are not modal. With disabled, the trigger does not open it.
A handle links the root to detached triggers: a MenuTrigger with the same handle, anywhere in the page, opens this menu. The MenuViewport shows the MenuPayload of the payload of the trigger that opened it, and triggerId is the id of this trigger.
A MenuInput in the popup filters the items of the MenuList, as the FilterProvider of Base UI. autoHighlight highlights the first match while the query is not empty ('always': also when it is empty). With filter={{ false }}, the browser hides no item: an onInput handler on the MenuInput renders the items of the query.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
disabled | bool | false |
autoHighlight | bool|string | false |
filter | bool | true |
modal | bool | true |
openOnHover | bool | false |
delay | int | 100 |
loopFocus | bool | true |
highlightItemOnHover | bool | true |
orientation | 'horizontal' | 'vertical' | Orientation::Vertical |
side | 'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end' | Side::Bottom |
align | 'start' | 'center' | 'end' | Align::Start |
sideOffset | int | 0 |
alignOffset | int | 0 |
collisionPadding | int | 5 |
children | ?Html | null |
...attrs | array | [] |
MenuTrigger#
The button that opens the menu. It has a caret, unless caret is false. In a Menubar, it has the look of the bar and no caret. In a Toolbar, it is an item of the toolbar, with the look of a ToolbarButton.
With a handle, it is a detached trigger of the Menu with the same handle: it can be anywhere in the page. The payload selects the MenuPayload that the MenuViewport shows while this trigger opened the menu.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
payload | ?string | null |
disabled | bool | false |
caret | bool | true |
children | ?Html | null |
...rest | mixed | — |
MenuSubmenuTrigger#
The item of a menu that opens a MenuSubmenu. It has a caret on the side of the submenu.
| Prop | Type | Default |
|---|---|---|
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
MenuPopup#
The box of the menu, with its items. The controller places it next to the trigger.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuArrow#
The arrow of the popup, at the side of the trigger.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
MenuItem#
An action of the menu. A click runs its handler (onClick) and closes the menu. With closeOnClick={{ false }}, the menu stays open. The label replaces the text of the item for the filter of a MenuInput.
| Prop | Type | Default |
|---|---|---|
disabled | bool | false |
closeOnClick | bool | true |
label | ?string | null |
children | ?Html | null |
...rest | mixed | — |
MenuLinkItem#
A link of the menu: Base UI Menu.LinkItem.
| Prop | Type | Default |
|---|---|---|
href (required) | string | — |
children | ?Html | null |
...rest | mixed | — |
MenuSeparator#
A line between two items or two groups of the menu.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
MenuGroup#
A group of items of the menu, with a MenuGroupLabel.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuGroupLabel#
The label of a MenuGroup.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuCheckboxItem#
An item that the user checks and unchecks: Base UI Menu.CheckboxItem. The menu stays open.
With defaultChecked, the browser keeps the state: the item needs no handler. The item sends the phem-menu:change browser event after a change, with checked in its detail. With checked and an onClick handler, the server keeps the state: the handler changes it. A checked prop wins over defaultChecked.
| Prop | Type | Default |
|---|---|---|
checked | ?bool | null |
defaultChecked | bool | false |
disabled | bool | false |
closeOnClick | bool | false |
children | ?Html | null |
...rest | mixed | — |
MenuRadioGroup#
A group of radio items: Base UI Menu.RadioGroup. The item with the value of the group is checked.
With defaultValue, the browser keeps the state: the items need no handler. An item sends the phem-menu:change browser event after a change, with its value in the detail. With value and an onClick handler on each item, the server keeps the state. A value prop wins over defaultValue.
| Prop | Type | Default |
|---|---|---|
value | ?string | null |
defaultValue | ?string | null |
children | ?Html | null |
...attrs | array | [] |
MenuRadioItem#
An item of a radio group: Base UI Menu.RadioItem. The menu stays open.
In a MenuRadioGroup with a value, the item is checked when its value is the value of the group. With no group, or in a group with no value, the checked prop gives the state. A group of a menu around a submenu is not the group of an item of the submenu.
| Prop | Type | Default |
|---|---|---|
value | ?string | null |
checked | bool | false |
disabled | bool | false |
closeOnClick | bool | false |
children | ?Html | null |
...rest | mixed | — |
MenuViewport#
The content of the popup that changes with the trigger, as the Viewport of Base UI. It shows the MenuPayload of the payload of the active trigger, and animates the change: the content is in a div with data-current, the old content in a div with data-previous, and the viewport gets data-activation-direction.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuInput#
The input of the filter: its text hides the items of the MenuList that do not match it.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
MenuClear#
A button that empties the MenuInput.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuEmpty#
The text that shows when no item matches the query of the MenuInput.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuList#
The items that the MenuInput filters: the menu of a popup with a filter.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuSubmenu#
A menu in the popup of another menu: Base UI Menu.SubmenuRoot. Its trigger is a MenuSubmenuTrigger, an item of the menu around it. It opens on hover after delay.
| Prop | Type | Default |
|---|---|---|
openOnHover | bool | true |
side | 'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end' | Side::InlineEnd |
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
disabled | bool | false |
autoHighlight | bool|string | false |
filter | bool | true |
modal | bool | true |
delay | int | 100 |
loopFocus | bool | true |
highlightItemOnHover | bool | true |
orientation | 'horizontal' | 'vertical' | Orientation::Vertical |
align | 'start' | 'center' | 'end' | Align::Start |
sideOffset | int | 0 |
alignOffset | int | 0 |
collisionPadding | int | 5 |
children | ?Html | null |
...attrs | array | [] |
MenuItemIndicator#
The check of a checkbox item or a radio item. It shows only when the item is checked.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuItemLabel#
The label of a checkbox item or a radio item, in the column after the indicator.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
MenuPayload#
The content that the MenuViewport shows for the triggers with this payload.
| Prop | Type | Default |
|---|---|---|
name (required) | string | — |
children | ?Html | null |
The source of the examples: examples/ui/Menu.phem.