Skip to Content
文档

菜单

用于创建可访问的下拉菜单

SourceStorybookRecipeArk

用法

¥Usage

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>

示例

¥Examples

命令

¥Command

使用 Menu.ItemCommand 组件在菜单中显示命令。

¥Use the Menu.ItemCommand component to display a command in the menu.

上下文菜单

¥Context menu

使用 Menu.ContextTrigger 组件创建上下文菜单。

¥Use the Menu.ContextTrigger component to create a context menu.

¥Group

使用 Menu.ItemGroup 组件将相关菜单项分组。

¥Use the Menu.ItemGroup component to group related menu items.

危险项目

¥Danger Item

以下是如何设置用于删除菜单项的菜单项样式的示例。

¥Here's an example of how to style a menu item that is used to delete an item.

子菜单

¥Submenu

以下是如何创建子菜单的示例。

¥Here's an example of how to create a submenu.

链接

¥Links

asChild 属性传递给 Menu.Item 组件,以渲染链接。

¥Pass the asChild prop to the Menu.Item component to render a link.

使用自定义路由链接时,需要在 Menu.Root 组件上设置 navigate 属性。

¥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>
  )
}

单选按钮项

¥Radio Items

以下是如何创建包含单选菜单的示例。

¥Here's an example of how to create a menu with radio items.

复选框项目

¥Checkbox Items

以下是如何创建包含复选框菜单的示例。

¥Here's an example of how to create a menu with checkbox items.

图标和命令

¥Icon and Command

编写菜单,使其包含图标和命令。

¥Compose the menu to include icons and commands.

放置位置

¥Placement

使用 positioning.placement 属性控制菜单的位置。

¥Use the positioning.placement prop to control the placement of the menu.

头像

¥Avatar

以下是组合 MenuAvatar 组件以在头像下方显示菜单的示例。

¥Here's an example that composes the Menu with the Avatar component to display a menu underneath an avatar.

锚点

¥Anchor Point

使用 positioning.anchorPoint 属性控制菜单的锚点。

¥Use the positioning.anchorPoint prop to control the anchor point of the menu.

你可以从 DOM 元素的 getBoundingClientRect 派生它,或者使用类似 DOMRect.fromRect({ x: 0, y: 0, width: 1, height: 1 }) 的方法来创建新的矩形。

¥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.

Anchor

混合布局

¥Mixed Layout

以下是如何创建混合布局菜单项的示例。在此布局中,顶部水平菜单包含常用菜单项。

¥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.

溢出

¥Overflow

当您有一长串菜单项时,您可以在 Menu.Content 上设置 maxH 属性来创建可滚动菜单。

¥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

内容具有默认的 maxHeight: "var(--available-height)",即内容相对于视口的最大可用高度。

¥The content has a default maxHeight: "var(--available-height)", which is the maximum available height for the content relative to the viewport.

分离时隐藏

¥Hide When Detached

当菜单在滚动容器中渲染时,将 positioning.hideWhenDetached 设置为 true,以便在触发器滚动出视图时隐藏菜单。

¥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.

Item0

Item1

Item2

Item3

Item4

Item5

在对话框中

¥Within Dialog

要在 Dialog 中使用 Menu,你需要避免将 Menu.Positioner 传送到文档正文。

¥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>

如果你已在 Dialog 上设置了 scrollBehavior="inside",则需要:

¥If you have set scrollBehavior="inside" on the Dialog, you need to:

  • 将菜单定位设置为 fixed,以避免菜单被对话框裁剪。

  • hideWhenDetached 设置为 true,以便在触发器滚动出视图时隐藏菜单。

<Menu.Root positioning={{ strategy: "fixed", hideWhenDetached: true }}>
  {/* ... */}
</Menu.Root>

属性

¥Props

根元素

¥Root

PropDefaultType
closeOnSelect true
boolean

Whether to close the menu when an option is selected

composite true
boolean

Whether the menu is a composed with other composite widgets like a combobox or tabs

lazyMount false
boolean

Whether to enable lazy mounting

loopFocus false
boolean

Whether to loop the keyboard navigation.

skipAnimationOnMount false
boolean

Whether to allow the initial presence animation.

typeahead true
boolean

Whether the pressing printable characters should trigger typeahead navigation

unmountOnExit false
boolean

Whether to unmount on exit.

colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

variant 'subtle'
'subtle' | 'solid'

The variant of the component

size 'md'
'sm' | 'md'

The size of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

anchorPoint
Point

The positioning point for the menu. Can be set by the context menu trigger or the button trigger.

aria-label
string

The accessibility label for the menu

defaultHighlightedValue
string

The initial highlighted value of the menu item when rendered. Use when you don't need to control the highlighted value of the menu item.

defaultOpen
boolean

The initial open state of the menu when rendered. Use when you don't need to control the open state of the menu.

highlightedValue
string

The controlled highlighted value of the menu item.

id
string

The unique identifier of the machine.

ids
Partial<{ trigger: string contextTrigger: string content: string groupLabel: (id: string) => string group: (id: string) => string positioner: string arrow: string }>

The ids of the elements in the menu. Useful for composition.

immediate
boolean

Whether to synchronize the present change immediately or defer it to the next frame

navigate
(details: NavigateDetails) => void

Function to navigate to the selected item if it's an anchor element

onEscapeKeyDown
(event: KeyboardEvent) => void

Function called when the escape key is pressed

onExitComplete
VoidFunction

Function called when the animation ends in the closed state

onFocusOutside
(event: FocusOutsideEvent) => void

Function called when the focus is moved outside the component

onHighlightChange
(details: HighlightChangeDetails) => void

Function called when the highlighted menu item changes.

onInteractOutside
(event: InteractOutsideEvent) => void

Function called when an interaction happens outside the component

onOpenChange
(details: OpenChangeDetails) => void

Function called when the menu opens or closes

onPointerDownOutside
(event: PointerDownOutsideEvent) => void

Function called when the pointer is pressed down outside the component

onSelect
(details: SelectionDetails) => void

Function called when a menu item is selected.

open
boolean

The controlled open state of the menu

positioning
PositioningOptions

The options used to dynamically position the menu

present
boolean

Whether the node is present (controlled by the user)

项目

¥Item

PropDefaultType
value *
string

The unique value of the menu item option.

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
closeOnSelect
boolean

Whether the menu should be closed when the option is selected.

disabled
boolean

Whether the menu item is disabled

onSelect
VoidFunction

The function to call when the item is selected

valueText
string

The textual value of the option. Used in typeahead navigation of the menu. If not provided, the text content of the menu item will be used.

Previous

悬停卡片

Next

叠加管理器