Menu Component
A wrapper component around WordPress's private Menu component from @wordpress/components.
Usage
import { Menu } from '@automattic/components';
import { useState } from 'react';
function MyComponent() {
const [ isChecked, setIsChecked ] = useState( false );
return (
<Menu trigger={ <Menu.TriggerButton>Open Menu</Menu.TriggerButton> }>
<Menu.Popover>
<Menu.Group>
<Menu.Item onClick={ () => console.log( 'Item 1 clicked' ) }>Menu Item 1</Menu.Item>
<Menu.Separator />
<Menu.CheckboxItem checked={ isChecked } onChange={ () => setIsChecked( ! isChecked ) }>
Checkbox Item
</Menu.CheckboxItem>
</Menu.Group>
</Menu.Popover>
</Menu>
);
}
Available Subcomponents
The Menu component includes all the original subcomponents from the Gutenberg Menu implementation:
Menu.Item- A basic menu itemMenu.RadioItem- A radio button menu itemMenu.CheckboxItem- A checkbox menu itemMenu.ItemLabel- A label for a menu itemMenu.ItemHelpText- Help text for a menu itemMenu.Group- A group of menu itemsMenu.GroupLabel- A label for a group of menu itemsMenu.Separator- A separator line between menu itemsMenu.Popover- The popover component that contains the menuMenu.TriggerButton- The button that triggers the menuMenu.SubmenuTriggerItem- A menu item that triggers a submenuMenu.Context- React context for the menu
Important Notes
Private API Usage
This component currently uses WordPress's private API through @wordpress/private-apis. The underlying Menu component is not yet part of the public API and may change or break in future versions of WordPress.
We will migrate to the public version of this component once it becomes available in @wordpress/components.
Why a Wrapper?
Centralized Private API Management: Without this wrapper, we would need to duplicate the private API acknowledgment code everywhere the Menu is used. This wrapper centralizes the code in one place and makes it easier to track its usage.
Future-Proofing: When the Menu component becomes public, we'll only need to update this one wrapper component instead of finding and updating every direct usage throughout the codebase. This makes the eventual migration to the public API much simpler.
Future Plans
Once the Menu component becomes publicly available in @wordpress/components, we should:
- Remove the private API usage
- Update this wrapper to use the public API
- Eventually consider deprecating this wrapper in favor of direct Menu usage