¥Dropdown Menu
向用户显示菜单,例如一组操作或功能,由按钮触发。
import * as React from "react";import { DropdownMenu } from "radix-ui";import { HamburgerMenuIcon, DotFilledIcon, CheckIcon, ChevronRightIcon, } from "@radix-ui/react-icons";import "./styles.css";const DropdownMenuDemo = () => {const [bookmarksChecked, setBookmarksChecked] = React.useState(true);const [urlsChecked, setUrlsChecked] = React.useState(false);const [person, setPerson] = React.useState("pedro");return (<DropdownMenu.Root><DropdownMenu.Trigger asChild><button className="IconButton" aria-label="Customise options"><HamburgerMenuIcon /></button></DropdownMenu.Trigger><DropdownMenu.Portal><DropdownMenu.Content className="DropdownMenuContent" sideOffset={5}><DropdownMenu.Item className="DropdownMenuItem">New Tab <div className="RightSlot">⌘+T</div></DropdownMenu.Item><DropdownMenu.Item className="DropdownMenuItem">New Window <div className="RightSlot">⌘+N</div></DropdownMenu.Item><DropdownMenu.Item className="DropdownMenuItem" disabled>New Private Window <div className="RightSlot">⇧+⌘+N</div></DropdownMenu.Item><DropdownMenu.Sub><DropdownMenu.SubTrigger className="DropdownMenuSubTrigger">More Tools<div className="RightSlot"><ChevronRightIcon /></div></DropdownMenu.SubTrigger><DropdownMenu.Portal><DropdownMenu.SubContent className="DropdownMenuSubContent" sideOffset={2} alignOffset={-5} ><DropdownMenu.Item className="DropdownMenuItem">Save Page As… <div className="RightSlot">⌘+S</div></DropdownMenu.Item><DropdownMenu.Item className="DropdownMenuItem">Create Shortcut…</DropdownMenu.Item><DropdownMenu.Item className="DropdownMenuItem">Name Window…</DropdownMenu.Item><DropdownMenu.Separator className="DropdownMenu.Separator" /><DropdownMenu.Item className="DropdownMenuItem">Developer Tools</DropdownMenu.Item></DropdownMenu.SubContent></DropdownMenu.Portal></DropdownMenu.Sub><DropdownMenu.Separator className="DropdownMenuSeparator" /><DropdownMenu.CheckboxItem className="DropdownMenuCheckboxItem" checked={bookmarksChecked} onCheckedChange={setBookmarksChecked} ><DropdownMenu.ItemIndicator className="DropdownMenuItemIndicator"><CheckIcon /></DropdownMenu.ItemIndicator>Show Bookmarks <div className="RightSlot">⌘+B</div></DropdownMenu.CheckboxItem><DropdownMenu.CheckboxItem className="DropdownMenuCheckboxItem" checked={urlsChecked} onCheckedChange={setUrlsChecked} ><DropdownMenu.ItemIndicator className="DropdownMenuItemIndicator"><CheckIcon /></DropdownMenu.ItemIndicator>Show Full URLs</DropdownMenu.CheckboxItem><DropdownMenu.Separator className="DropdownMenuSeparator" /><DropdownMenu.Label className="DropdownMenuLabel">People</DropdownMenu.Label><DropdownMenu.RadioGroup value={person} onValueChange={setPerson}><DropdownMenu.RadioItem className="DropdownMenuRadioItem" value="pedro" ><DropdownMenu.ItemIndicator className="DropdownMenuItemIndicator"><DotFilledIcon /></DropdownMenu.ItemIndicator>Pedro Duarte</DropdownMenu.RadioItem><DropdownMenu.RadioItem className="DropdownMenuRadioItem" value="colm" ><DropdownMenu.ItemIndicator className="DropdownMenuItemIndicator"><DotFilledIcon /></DropdownMenu.ItemIndicator>Colm Tuite</DropdownMenu.RadioItem></DropdownMenu.RadioGroup><DropdownMenu.Arrow className="DropdownMenuArrow" /></DropdownMenu.Content></DropdownMenu.Portal></DropdownMenu.Root>);};export default DropdownMenuDemo;
Can be controlled or uncontrolled.
Supports submenus with configurable reading direction.
Supports items, labels, groups of items.
Supports checkable items (single or multiple) with optional indeterminate state.
Supports modal and non-modal modes.
Customize side, alignment, offsets, collision handling.
Optionally render a pointing arrow.
Focus is fully managed.
Full keyboard navigation.
Typeahead support.
Dismissing and layering behavior is highly customizable.
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入所有部分并将它们组合在一起。
¥Import all parts and piece them together.
¥API Reference
¥Root
包含下拉菜单的所有部分。
¥Contains all the parts of a dropdown menu.
¥Trigger
切换下拉菜单的按钮。默认情况下,DropdownMenu.Content
将根据触发器进行定位。
¥The button that toggles the dropdown menu. By default, the DropdownMenu.Content
will position itself against the trigger.
¥Portal
使用时,将内容部分传送到 body
中。
¥When used, portals the content part into the body
.
¥Content
下拉菜单打开时弹出的组件。
¥The component that pops out when the dropdown menu is open.
¥Arrow
一个可选的箭头元素,用于与下拉菜单一起渲染。这可以用来帮助在视觉上将触发器与 DropdownMenu.Content
链接起来。必须在 DropdownMenu.Content
中渲染。
¥An optional arrow element to render alongside the dropdown menu. This can be used to help visually link the trigger with the DropdownMenu.Content
. Must be rendered inside DropdownMenu.Content
.
¥Item
包含下拉菜单项的组件。
¥The component that contains the dropdown menu items.
¥Group
用于对多个 DropdownMenu.Item
进行分组。
¥Used to group multiple DropdownMenu.Item
s.
¥Label
用于渲染标签。它无法通过方向键获得焦点。
¥Used to render a label. It won't be focusable using arrow keys.
可以像复选框一样控制和渲染的项目。
¥An item that can be controlled and rendered like a checkbox.
用于对多个 DropdownMenu.RadioItem
进行分组。
¥Used to group multiple DropdownMenu.RadioItem
s.
可以像单选按钮一样控制和渲染的项目。
¥An item that can be controlled and rendered like a radio.
当父级 DropdownMenu.CheckboxItem
或 DropdownMenu.RadioItem
被选中时渲染。你可以直接设置此元素的样式,也可以将其用作封装器来放置图标,或者两者兼而有之。
¥Renders when the parent DropdownMenu.CheckboxItem
or DropdownMenu.RadioItem
is checked. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.
¥Separator
用于在下拉菜单中以视觉方式分隔项目。
¥Used to visually separate items in the dropdown menu.
包含子菜单的所有部分。
¥Contains all the parts of a submenu.
打开子菜单的项目。必须在 DropdownMenu.Sub
中渲染。
¥An item that opens a submenu. Must be rendered inside DropdownMenu.Sub
.
在子菜单打开时弹出的组件。必须在 DropdownMenu.Sub
中渲染。
¥The component that pops out when a submenu is open. Must be rendered inside DropdownMenu.Sub
.
¥Examples
¥With submenus
你可以将 DropdownMenu.Sub
及其各个部分结合使用来创建子菜单。
¥You can create submenus by using DropdownMenu.Sub
in combination with its parts.
¥With disabled items
你可以通过 data-disabled
属性为禁用项目添加特殊样式。
¥You can add special styles to disabled items via the data-disabled
attribute.
¥With separators
使用 Separator
组件在项目之间添加分隔符。
¥Use the Separator
part to add a separator between items.
¥With labels
使用 Label
组件帮助标记某个部分。
¥Use the Label
part to help label a section.
¥With checkbox items
使用 CheckboxItem
组件添加可勾选的项目。
¥Use the CheckboxItem
part to add an item that can be checked.
¥With radio items
使用 RadioGroup
和 RadioItem
部件添加一个可在众多选项中勾选的项目。
¥Use the RadioGroup
and RadioItem
parts to add an item that can be checked amongst others.
¥With complex items
你可以在 Item
部件中添加额外的装饰元素,例如图片。
¥You can add extra decorative elements in the Item
parts, such as images.
¥Constrain the content/sub-content size
你可能需要限制内容(或子内容)的宽度,使其与触发器(或子触发器)的宽度匹配。你可能还需要限制其高度,使其不超过视口。
¥You may want to constrain the width of the content (or sub-content) so that it matches the trigger (or sub-trigger) width. You may also want to constrain its height to not exceed the viewport.
为了支持此功能,我们公开了几个 CSS 自定义属性,例如 --radix-dropdown-menu-trigger-width
和 --radix-dropdown-menu-content-available-height
。使用它们来限制内容尺寸。
¥We expose several CSS custom properties such as --radix-dropdown-menu-trigger-width
and --radix-dropdown-menu-content-available-height
to support this. Use them to constrain the content dimensions.
¥Origin-aware animations
我们公开了一个 CSS 自定义属性 --radix-dropdown-menu-content-transform-origin
。使用它来根据 side
、sideOffset
、align
、alignOffset
以及任何碰撞,从其计算出的原点为内容设置动画。
¥We expose a CSS custom property --radix-dropdown-menu-content-transform-origin
. Use it to animate the content from its computed origin based on side
, sideOffset
, align
, alignOffset
and any collisions.
¥Collision-aware animations
我们公开了 data-side
和 data-align
属性。它们的值将在运行时更改以反映碰撞。使用它们来创建碰撞和方向感知动画。
¥We expose data-side
and data-align
attributes. Their values will change at runtime to reflect collisions. Use them to create collision and direction-aware animations.
¥Accessibility
遵循 菜单按钮 WAI-ARIA 设计模式 并使用 移动 tabindex 管理菜单项之间的焦点移动。
¥Adheres to the Menu Button WAI-ARIA design pattern and uses roving tabindex to manage focus movement among menu items.
¥Keyboard Interactions
¥Custom APIs
通过将原始部分抽象到你自己的组件中来创建你自己的 API。
¥Create your own API by abstracting the primitive parts into your own component.
¥Abstract the arrow and item indicators
此示例抽象了 DropdownMenu.Arrow
和 DropdownMenu.ItemIndicator
部分。它还封装了 CheckboxItem
和 RadioItem
的实现细节。
¥This example abstracts the DropdownMenu.Arrow
and DropdownMenu.ItemIndicator
parts. It also wraps implementation details for CheckboxItem
and RadioItem
.
¥Usage
¥Implementation