JGuang

右键菜单

ContextMenu · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

右键菜单 · ContextMenu

右键菜单用于操作场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 右键菜单 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 右键菜单 · 综合展示 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 右键菜单 · 基础用法 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 右键菜单 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 右键菜单 · 基础用法 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Context Menu

右键点击时在光标位置出现的上下文菜单。用于为特定元素或区域提供上下文操作,而不使 UI 杂乱。

属性类型默认值说明
children *ReactNodeThe trigger area: right-click on this content to open the menu.
items *ContextMenuOption[]Array of menu entries. Each entry is one of: an action item {label, onClick?, icon?, isDisabled?, variant?, items?} (nested items open a flyout in popover presentation and drill into a new view in bottom-sheet presentation; variant "destructive" renders it in the error color), a divider {type: "divider"}, or a section {type: "section", title?, items: [...action items]}.
menuContentReactNodeCustom JSX menu content for compound mode. Use instead of items for dynamic or stateful menus.
menuWidthnumber | string'160px'Custom menu width.
size'sm' | 'md' | 'lg''md'Size of menu items: controls padding density.
labelstring'Context menu'Accessible name for the menu surface, announced when it opens.
onOpenChange(isOpen: boolean) => voidCallback fired when the menu opens or closes.
presentation'popover' | 'bottom-sheet' | 'adaptive''popover'Presentation policy. popover opens at the pointer position, bottom-sheet always uses an action sheet, and adaptive uses the BottomSheet at 768px and below when the primary pointer is coarse.
isDisabledbooleanfalseWhen true, right-click shows the native browser context menu instead.
triggerXstyleStyleXStyles | StyleXStyles[]Styles applied to the trigger wrapper. Accepts stylex.create() values, not inline style objects.
triggerAs'div' | 'span''div'The element the trigger wrapper renders as. 'div' is a block; 'span' an inline wrapper, so a reference inside prose can own a context menu without breaking the text flow.

使用建议

  • 保持菜单项简洁和面向操作。
  • 有很多项时使用分组和分隔线。
  • 在桌面端需要光标定位、紧凑触控设备上需要 BottomSheet 时,使用 presentation="adaptive"。
  • 确保所有上下文菜单操作也可通过其他 UI 元素访问。
  • 重要的移动端操作还应提供可见的 MoreMenu 或同等入口,不要只依赖长按。
  • 避免:将上下文菜单作为访问重要操作的唯一方式。
  • 避免:在单个菜单中放置超过 10-12 个项而不分组。

主题 API

{
  "targets": [
    {
      "className": "astryx-context-menu"
    }
  ],
  "vars": [
    {
      "name": "--_dropdown-menu-radius",
      "description": "Border radius of the menu popup",
      "default": "var(--radius-container)",
      "private": true
    },
    {
      "name": "--_dropdown-menu-padding",
      "description": "Inner padding of the menu popup",
      "default": "var(--spacing-1)",
      "private": true
    }
  ],
  "derived": [
    {
      "property": "borderRadius",
      "vars": [
        "--_dropdown-menu-radius"
      ]
    },
    {
      "property": "padding",
      "vars": [
        "--_dropdown-menu-padding"
      ]
    }
  ]
}

ContextMenuItem

组件结构

  • Trigger area: Caller-provided region that accepts right-click, keyboard context-menu, and long-press input.
  • Pointer menu surface: Cursor-positioned menu panel used by the pointer presentation.
  • Pointer action row: DropdownMenu-owned action, selectable option, or submenu row in the pointer presentation.
  • Touch sheet frame: BottomSheet panel, content area, handle, and optional scrim that host touch actions.
  • Touch menu surface: ContextMenu-owned content panel rendered inside the touch sheet frame.
  • Touch action list: Spacious List that groups data-driven touch actions.
  • Touch action row: ListItem button used for a data-driven action or drill-in entry in the touch presentation.

Context Menu Item

复合模式的菜单项组件。从 DropdownMenuItem 重新导出以便发现。

属性类型默认值说明
iconIconType显示在标签前的图标。
labelReactNode主标签文本。
descriptionReactNode显示在标签下方的次要描述文本。
endContentReactNode在标签和描述之后渲染的附加内容。
xstyleStyleXStyles根容器的 StyleX 样式。

完整示例的额外依赖

部分示例使用独立图标包。示例清单中的每条安装命令包含该示例实际需要的依赖;若要运行本页全部示例,还需安装:

npm install @heroicons/react@2.2.0