apps/www/content/docs/components/menu.mdx
import { Menu } from "@chakra-ui/react"
<Menu.Root>
<Menu.Trigger />
<Menu.Positioner>
<Menu.Content>
<Menu.Item />
<Menu.ItemGroup>
<Menu.Item />
</Menu.ItemGroup>
<Menu.Separator />
<Menu.Arrow />
<Menu.CheckboxItem>
<Menu.ItemIndicator />
</Menu.CheckboxItem>
<Menu.RadioItemGroup>
<Menu.RadioItem>
<Menu.ItemIndicator />
</Menu.RadioItem>
</Menu.RadioItemGroup>
</Menu.Content>
</Menu.Positioner>
</Menu.Root>
Use the open and onOpenChange prop to control the visibility of the menu.
An alternative way to control the menu is to use the RootProvider component
and the useMenu store hook.
This way you can access the menu state and methods from outside the menu.
<ExampleTabs name="menu-with-store" />Use the Menu.ItemCommand component to display a command in the menu.
Use the Menu.ContextTrigger component to create a context menu.
Use the Menu.ItemGroup component to group related menu items.
Here's an example of how to style a menu item that is used to delete an item.
<ExampleTabs name="menu-with-danger-item" />Here's an example of how to create a submenu.
<ExampleTabs name="menu-with-submenu" />Pass the asChild prop to the Menu.Item component to render a link.
When using custom router links, you need to set the navigate prop on the
Menu.Root component.
"use client"
import { Menu } from "@chakra-ui/react"
import { useNavigate } from "react-router-dom"
const Demo = () => {
const navigate = useNavigate()
return (
<Menu.Root navigate={({ value, node }) => navigate(`/${value}`)}>
</Menu.Root>
)
}
Here's an example of how to create a menu with radio items.
<ExampleTabs name="menu-with-radio-items" />Here's an example of how to create a menu with checkbox items.
<ExampleTabs name="menu-with-checkbox-items" />Compose the menu to include icons and commands.
<ExampleTabs name="menu-with-icon-and-command" />Use the positioning.placement prop to control the placement of the menu.
Here's an example that composes the Menu with the Avatar component to
display a menu underneath an avatar.
Use the positioning.anchorPoint prop to control the anchor point of the menu.
You can derive it from the getBoundingClientRect of a DOM element, or use
something like DOMRect.fromRect({ x: 0, y: 0, width: 1, height: 1 }) to create
a new rect.
Here's an example of how to create a mixed layout of menu items. In this layout, the top horizontal menu includes common menu items.
<ExampleTabs name="menu-with-mixed-layout" />When you have a long list of menu items, you can set a maxH prop on the
Menu.Content to create a scrollable menu.
:::note
The content has a default maxHeight: "var(--available-height)", which is the
maximum available height for the content relative to the viewport.
:::
<ExampleTabs name="menu-with-overflow" />When the menu is rendered in an scrolling container, set the
positioning.hideWhenDetached to true to hide the menu when the trigger is
scrolled out of view.
To use the Menu within a Dialog, you need to avoid portalling the
Menu.Positioner to the document's body.
-<Portal>
<Menu.Positioner>
<Menu.Content>
</Menu.Content>
</Menu.Positioner>
-</Portal>
If you have set scrollBehavior="inside" on the Dialog, you need to:
fixed to avoid the menu from being clipped by
the dialog.hideWhenDetached to true to hide the menu when the trigger is scrolled
out of view.<Menu.Root positioning={{ strategy: "fixed", hideWhenDetached: true }}>
</Menu.Root>
When a Menu item opens a Dialog that contains another Menu, use
createOverlay to avoid unexpected close behavior caused by event bubbling.
Use the _highlighted prop to style menu items when they are hovered or focused
with keyboard navigation.
<Menu.Item _highlighted={{ bg: "blue.500", color: "white" }}>
Custom highlighted item
</Menu.Item>
Use the _open prop to style the menu trigger when the menu is open.
<Menu.Trigger asChild>
<Button _open={{ bg: "blue.500", color: "white" }}>Menu</Button>
</Menu.Trigger>
Explore the Menu component parts interactively. Click on parts in the sidebar
to highlight them in the preview.