Menu
View as MarkdownAn accessible dropdown and context menu that displays a list of actions or options.
Features
Permalink to heading "Features"- Support for items, labels, and groups of items
- Support for checkbox and radio menu items
- Support for nested/submenu patterns
- Support for context menus (triggered by right-click) and multiple independent triggers sharing one menu
- Focus is fully managed using the
aria-activedescendantpattern - Typeahead to allow focusing items by typing text
- Full keyboard navigation support, including arrow keys, home/end, and submenu navigation
<primitives:menu.root>
<primitives:menu.trigger>
<primitives:menu.indicator />
</primitives:menu.trigger>
<primitives:menu.contextTrigger />
<primitives:menu.positioner>
<primitives:menu.arrow />
<primitives:menu.content>
<primitives:menu.item>
<primitives:menu.itemText />
</primitives:menu.item>
<primitives:menu.checkboxItem>
<primitives:menu.itemIndicator />
<primitives:menu.itemText />
</primitives:menu.checkboxItem>
<primitives:menu.radioItem>
<primitives:menu.itemIndicator />
<primitives:menu.itemText />
</primitives:menu.radioItem>
<primitives:menu.separator />
<primitives:menu.itemGroup>
<primitives:menu.itemGroupLabel />
</primitives:menu.itemGroup>
<f:comment><!-- Opens the submenu below, matched by rootId/childId --></f:comment>
<primitives:menu.triggerItem childId="submenu" />
</primitives:menu.content>
</primitives:menu.positioner>
</primitives:menu.root>
<f:comment><!-- A submenu: linked to its parent by id, not by nesting --></f:comment>
<primitives:menu.root rootId="submenu" parentId="parent-rootId">
<primitives:menu.positioner>
<primitives:menu.content>
<primitives:menu.item />
</primitives:menu.content>
</primitives:menu.positioner>
</primitives:menu.root>
Installation
Permalink to heading "Installation"typo3 ui:add menu
Please copy the files manually from GitHub into your project.
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Grouping
Permalink to heading "Grouping"Organize items into logical groups with their own labels.
With Links
Permalink to heading "With Links"Pass asChild="{true}" on menu.item to render the item's attributes and behavior directly onto a
single child element, like a plain <a href>, so it behaves like real navigation, including
keyboard activation.
With Checkboxes
Permalink to heading "With Checkboxes"Use menu.checkboxItem for items that toggle independently of each other.
With Radios
Permalink to heading "With Radios"Use menu.radioItem with a shared name for mutually exclusive options.
Nested Menu
Permalink to heading "Nested Menu"A submenu is just another menu.root, linked to its parent by two explicit ids: give the submenu's
menu.root a rootId and point its parentId back at the parent menu's own rootId, then render a
menu.triggerItem inside the parent's own content with a childId matching the submenu's rootId.
The submenu itself doesn't need to live anywhere near the parent's markup - it's linked entirely by id.
Context Menu
Permalink to heading "Context Menu"Use menu.contextTrigger to open the menu on right-click instead of (or in addition to) a regular trigger.
Multiple Triggers
Permalink to heading "Multiple Triggers"Give several menu.trigger elements different values to share a single menu instance between them.
Inside a Dialog
Permalink to heading "Inside a Dialog"By default menu.content portals to the end of the document body, same as Select. Pass
portalled="{false}" when nesting a menu inside another portalled/focus-trapped element, like a
dialog, so it stays within that element's DOM subtree instead.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
menu.root
Permalink to heading "menu.root"Provides shared menu state to the rest of the parts. Renders no element of its own.
| Name | Description | Required | Default |
|---|---|---|---|
closeOnSelect | booleanWhether to close the menu when an option is selected. | No | true |
composite | booleanWhether the menu is composed with other composite widgets like a combobox or tabs. | No | true |
typeahead | booleanWhether pressing printable characters should trigger typeahead navigation. | No | true |
loopFocus | booleanWhether to loop the keyboard navigation. | No | false |
positioning | arrayThe options used to dynamically position the menu. | No | {
"placement": "bottom-start",
"gutter": 8
} |
defaultOpen | booleanThe initial open state of the menu when rendered. Use when you don't need to control the open state of the menu. | No | false |
defaultHighlightedValue | stringThe initial highlighted value of the menu item when rendered. | No | - |
defaultTriggerValue | stringThe initial trigger value when rendered, for a menu with multiple triggers. | No | - |
ariaLabel | stringThe accessibility label for the menu. Forwarded to the machine as `aria-label`. | No | - |
parentId | stringThe `rootId` of the parent menu this menu is a submenu of. Must match a `menu.triggerItem`'s `childId` on that parent, pointing back at this menu's own `rootId` - see the Nested Menu docs example. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
menu.trigger
Permalink to heading "menu.trigger"Opens and closes the menu. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value that identifies this specific trigger, for a menu with multiple triggers. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-menu-trigger | <uid> |
data-disabled | Present when disabled |
data-placement | The placement of the trigger |
data-side | The side of the trigger that the trigger is positioned on |
data-value | The value of the item |
data-current | Present when current |
data-controls | |
data-state | "open" | "closed" |
menu.contextTrigger
Permalink to heading "menu.contextTrigger"Opens the menu at the cursor position on right-click. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value that identifies this specific context trigger, for a menu with multiple context triggers. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-menu-context-trigger | <uid> |
data-value | The value of the item |
data-current | Present when current |
data-state | "open" | "closed" |
menu.content
Permalink to heading "menu.content"Contains the menu items. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-menu-content | <uid> |
data-state | "open" | "closed" |
data-instant | |
data-placement | The placement of the content |
data-side | The side of the trigger that the content is positioned on |
data-nested | menu |
data-has-nested | menu |
menu.item
Permalink to heading "menu.item"A selectable menu action. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe unique value of the menu item. | Yes | - |
disabled | booleanWhether the menu item is disabled. | No | - |
valueText | stringThe textual value of the item, used for typeahead navigation. Falls back to the item's text content when omitted. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-menu-item | <uid> |
data-disabled | Present when disabled |
data-highlighted | Present when highlighted |
data-value | The value of the item |
data-valuetext | The human-readable value |
menu.checkboxItem
Permalink to heading "menu.checkboxItem"A menu item that toggles independently of other items. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe unique value of the checkbox item. | Yes | - |
checked | booleanWhether the checkbox item is checked when rendered. | No | false |
disabled | booleanWhether the checkbox item is disabled. | No | - |
valueText | stringThe textual value of the item, used for typeahead navigation. Falls back to the item's text content when omitted. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
menu.radioItem
Permalink to heading "menu.radioItem"A menu item that's mutually exclusive with other radio items sharing the same name. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe unique value of the radio item. | Yes | - |
checked | booleanWhether the radio item is checked when rendered. | No | false |
disabled | booleanWhether the radio item is disabled. | No | - |
valueText | stringThe textual value of the item, used for typeahead navigation. Falls back to the item's text content when omitted. | No | - |
name | stringGroups radio items so selecting one unchecks the others sharing the same `name`. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
menu.itemGroup
Permalink to heading "menu.itemGroup"Groups related items together. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value identifier of the item group. Picked up automatically by a `menu.itemGroupLabel` nested inside it. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
menu.itemGroupLabel
Permalink to heading "menu.itemGroupLabel"Labels an itemGroup. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
menu.separator
Permalink to heading "menu.separator"A visual divider between items. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
menu.triggerItem
Permalink to heading "menu.triggerItem"Opens a nested submenu from within another menu's content. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
childId | stringThe `rootId` of the submenu (its `menu.root`'s `rootId`) that this item opens. That submenu's own `parentId` must point back at this menu's `rootId`. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
open | boolean | Whether the menu is open |
setOpen | (open: boolean) => void | Function to open or close the menu |
triggerValue | string | null | The trigger value |
setTriggerValue | (value: string | null) => void | Function to set the trigger value |
highlightedValue | string | null | The id of the currently highlighted menuitem |
setHighlightedValue | (value: string) => void | Function to set the highlighted menuitem |
setParent | (parent: MenuService) => void | Function to register a parent menu. This is used for submenus |
setChild | (child: MenuService) => void | Function to register a child menu. This is used for submenus |
reposition | (options?: Partial<PositioningOptions>) => void | Function to reposition the popover |
getOptionItemState | (props: OptionItemProps) => OptionItemState | Returns the state of the option item |
getItemState | (props: ItemProps) => ItemState | Returns the state of the menu item |
addItemListener | (props: ItemListenerProps) => VoidFunction | undefined | Setup the custom event listener for item selection event |
getTriggerState | (props?: TriggerProps) => TriggerState | Returns the state of a specific trigger, including whether it's the currently active one |
getPositionerState | () => PositionerState | Returns the state of the positioner |
getContentState | () => ContentState | Returns the state of the content |
Accessibility
Permalink to heading "Accessibility"| Key | Description |
|---|---|
Space | Activates/Selects the highlighted item |
Enter | Activates/Selects the highlighted item |
ArrowDown | Highlights the next item in the menu |
ArrowUp | Highlights the previous item in the menu |
ArrowRightArrowLeft | When focus is on trigger, opens or closes the submenu depending on reading direction. |
Esc | Closes the menu and moves focus to the trigger |