¥Context Menu
显示位于指针位置的菜单,由右键单击或长按触发。
import * as React from "react";import { ContextMenu } from "radix-ui";import { DotFilledIcon, CheckIcon, ChevronRightIcon, } from "@radix-ui/react-icons";import "./styles.css";const ContextMenuDemo = () => {const [bookmarksChecked, setBookmarksChecked] = React.useState(true);const [urlsChecked, setUrlsChecked] = React.useState(false);const [person, setPerson] = React.useState("pedro");return (<ContextMenu.Root><ContextMenu.Trigger className="ContextMenuTrigger">Right-click here.</ContextMenu.Trigger><ContextMenu.Portal><ContextMenu.Content className="ContextMenuContent" sideOffset={5} align="end" ><ContextMenu.Item className="ContextMenuItem">Back <div className="RightSlot">⌘+[</div></ContextMenu.Item><ContextMenu.Item className="ContextMenuItem" disabled>Forward <div className="RightSlot">⌘+]</div></ContextMenu.Item><ContextMenu.Item className="ContextMenuItem">Reload <div className="RightSlot">⌘+R</div></ContextMenu.Item><ContextMenu.Sub><ContextMenu.SubTrigger className="ContextMenuSubTrigger">More Tools<div className="RightSlot"><ChevronRightIcon /></div></ContextMenu.SubTrigger><ContextMenu.Portal><ContextMenu.SubContent className="ContextMenuSubContent" sideOffset={2} alignOffset={-5} ><ContextMenu.Item className="ContextMenuItem">Save Page As… <div className="RightSlot">⌘+S</div></ContextMenu.Item><ContextMenu.Item className="ContextMenuItem">Create Shortcut…</ContextMenu.Item><ContextMenu.Item className="ContextMenuItem">Name Window…</ContextMenu.Item><ContextMenu.Separator className="ContextMenuSeparator" /><ContextMenu.Item className="ContextMenuItem">Developer Tools</ContextMenu.Item></ContextMenu.SubContent></ContextMenu.Portal></ContextMenu.Sub><ContextMenu.Separator className="ContextMenuSeparator" /><ContextMenu.CheckboxItem className="ContextMenuCheckboxItem" checked={bookmarksChecked} onCheckedChange={setBookmarksChecked} ><ContextMenu.ItemIndicator className="ContextMenuItemIndicator"><CheckIcon /></ContextMenu.ItemIndicator>Show Bookmarks <div className="RightSlot">⌘+B</div></ContextMenu.CheckboxItem><ContextMenu.CheckboxItem className="ContextMenuCheckboxItem" checked={urlsChecked} onCheckedChange={setUrlsChecked} ><ContextMenu.ItemIndicator className="ContextMenuItemIndicator"><CheckIcon /></ContextMenu.ItemIndicator>Show Full URLs</ContextMenu.CheckboxItem><ContextMenu.Separator className="ContextMenuSeparator" /><ContextMenu.Label className="ContextMenuLabel">People</ContextMenu.Label><ContextMenu.RadioGroup value={person} onValueChange={setPerson}><ContextMenu.RadioItem className="ContextMenuRadioItem" value="pedro" ><ContextMenu.ItemIndicator className="ContextMenuItemIndicator"><DotFilledIcon /></ContextMenu.ItemIndicator>Pedro Duarte</ContextMenu.RadioItem><ContextMenu.RadioItem className="ContextMenuRadioItem" value="colm" ><ContextMenu.ItemIndicator className="ContextMenuItemIndicator"><DotFilledIcon /></ContextMenu.ItemIndicator>Colm Tuite</ContextMenu.RadioItem></ContextMenu.RadioGroup></ContextMenu.Content></ContextMenu.Portal></ContextMenu.Root>);};export default ContextMenuDemo;
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.
Focus is fully managed.
Full keyboard navigation.
Typeahead support.
Dismissing and layering behavior is highly customizable.
Triggers with a long press on touch devices
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入所有部分并将它们组合在一起。
¥Import all parts and piece them together.
¥API Reference
遵循 菜单 WAI-ARIA 设计模式 并使用 移动 tabindex 管理菜单项之间的焦点移动。
¥Adheres to the Menu WAI-ARIA design pattern and uses roving tabindex to manage focus movement among menu items.
¥Root
包含上下文菜单的所有部分。
¥Contains all the parts of a context menu.
¥Trigger
打开上下文菜单的区域。将其封装在你希望右键单击(或使用相关的键盘快捷键)时打开上下文菜单的目标周围。
¥The area that opens the context menu. Wrap it around the target you want the context menu to open from when right-clicking (or using the relevant keyboard shortcuts).
¥Portal
使用时,将内容部分传送到 body
中。
¥When used, portals the content part into the body
.
¥Content
在打开的上下文菜单中弹出的组件。
¥The component that pops out in an open context menu.
¥Arrow
一个可选的箭头元素,用于与子菜单一起渲染。这可以用来帮助在视觉上将触发器项与 ContextMenu.Content
链接起来。必须在 ContextMenu.Content
中渲染。
¥An optional arrow element to render alongside a submenu. This can be used to help visually link the trigger item with the ContextMenu.Content
. Must be rendered inside ContextMenu.Content
.
¥Item
包含上下文菜单项的组件。
¥The component that contains the context menu items.
¥Group
用于对多个 ContextMenu.Item
进行分组。
¥Used to group multiple ContextMenu.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.
用于对多个 ContextMenu.RadioItem
进行分组。
¥Used to group multiple ContextMenu.RadioItem
s.
可以像单选按钮一样控制和渲染的项目。
¥An item that can be controlled and rendered like a radio.
当父级 ContextMenu.CheckboxItem
或 ContextMenu.RadioItem
被选中时渲染。你可以直接设置此元素的样式,也可以将其用作封装器来放置图标,或者两者兼而有之。
¥Renders when the parent ContextMenu.CheckboxItem
or ContextMenu.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 context menu.
包含子菜单的所有部分。
¥Contains all the parts of a submenu.
打开子菜单的项目。必须在 ContextMenu.Sub
中渲染。
¥An item that opens a submenu. Must be rendered inside ContextMenu.Sub
.
在子菜单打开时弹出的组件。必须在 ContextMenu.Sub
中渲染。
¥The component that pops out when a submenu is open. Must be rendered inside ContextMenu.Sub
.
¥Examples
¥With submenus
你可以将 ContextMenu.Sub
及其各个部分结合使用来创建子菜单。
¥You can create submenus by using ContextMenu.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-context-menu-trigger-width
和 --radix-context-menu-content-available-height
。使用它们来限制内容尺寸。
¥We expose several CSS custom properties such as --radix-context-menu-trigger-width
and --radix-context-menu-content-available-height
to support this. Use them to constrain the content dimensions.
¥Origin-aware animations
我们公开了一个 CSS 自定义属性 --radix-context-menu-content-transform-origin
。使用它来根据 side
、sideOffset
、align
、alignOffset
以及任何碰撞,从其计算出的原点为内容设置动画。
¥We expose a CSS custom property --radix-context-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
使用 移动 tabindex 管理菜单项之间的焦点移动。
¥Uses roving tabindex to manage focus movement among menu items.
¥Keyboard Interactions