Skip to content

ContextMenu 右键菜单

AuContextMenu 只负责菜单展示与交互,业务确认通过 beforeSelect 注入。

基础用法

下面的演示包含图标行、按钮组、二级菜单、禁用项、分隔线和危险操作。

配置式菜单

在演示区点击右键;删除动作会先进入 beforeSelect 注入的确认流程。

在这里点击右键菜单坐标来自鼠标事件,并自动限制在浏览器视口内
vue
<template>
  <div>
    <div class="context-menu-demo" @contextmenu.prevent="openMenu">
      <AuIcon :icon="IconMenu2" :size="28" />
      <strong>在这里点击右键</strong>
      <span>菜单坐标来自鼠标事件,并自动限制在浏览器视口内</span>
    </div>
    <AuContextMenu
      v-model="visible"
      :items="menuSections"
      :position="position"
      :before-select="beforeSelect"
      @select="handleSelect"
      @cancel="handleCancel"
    />
  </div>
</template>
<script setup>
import { ref } from 'vue';
import {
  AuContextMenu,
  AuIcon,
  AuMessage,
  AuMessageBox,
  IconCopy,
  IconMenu2,
  IconStar,
  IconTrash,
} from 'aurora-plus';
const visible = ref(false);
const position = ref({ x: 0, y: 0 });
const menuSections = [
  {
    id: 'quick-actions',
    type: 'icon-row',
    ariaLabel: '快捷操作',
    items: [
      { id: 'copy-link', label: '复制链接', icon: IconCopy },
      { id: 'favorite', label: '收藏项目', icon: IconStar },
    ],
  },
  { id: 'separator-1', type: 'separator' },
  {
    id: 'main-actions',
    type: 'button-group',
    items: [
      { id: 'copy', label: '复制', icon: IconCopy, shortcut: 'Ctrl+C' },
      { id: 'favorite-menu', label: '收藏', icon: IconStar },
    ],
  },
  {
    id: 'more',
    type: 'submenu',
    label: '更多操作',
    items: [
      { id: 'rename', label: '重命名' },
      { id: 'archive', label: '归档项目' },
      { id: 'submenu-separator', kind: 'separator' },
      { id: 'disabled', label: '不可用操作', disabled: true },
    ],
  },
  { id: 'separator-2', type: 'separator' },
  {
    id: 'delete-section',
    type: 'button',
    item: {
      id: 'delete',
      label: '删除',
      icon: IconTrash,
      danger: true,
      confirmMessage: '确定删除这个示例项目吗?',
    },
  },
];
function openMenu(event) {
  position.value = { x: event.clientX, y: event.clientY };
  visible.value = true;
}
async function beforeSelect(item) {
  if (!item.confirmMessage) return true;
  return AuMessageBox.confirm({
    title: '危险操作',
    message: item.confirmMessage,
    confirmButtonType: 'danger',
  });
}
function handleSelect(item) {
  AuMessage.success(`已执行:${item.label}`);
}
function handleCancel(item) {
  AuMessage.info(`已取消:${item.label}`);
}
</script>
<style scoped>
.context-menu-demo {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 220px;
  border: 1px dashed var(--au-color-border);
  border-radius: 10px;
  color: var(--au-color-text-secondary);
  background: var(--au-color-bg-soft);
  flex-direction: column;
  gap: 9px;
  user-select: none;
}
.context-menu-demo strong {
  color: var(--au-color-text-primary);
  font-size: 15px;
}
.context-menu-demo span {
  color: var(--au-color-text-secondary);
  font-size: 12px;
}
</style>

数据结构

items 接收 AuContextMenuSection[]。Section 决定布局,Section 内的 item 决定具体动作。

Section 类型

type使用字段说明
button / itemitem,也兼容直接写在 section 上单个菜单项
button-group / groupitems纵向普通菜单项组
icon-rowitemsariaLabel单行图标按钮
icon-griditemsariaLabel图标网格
submenuidlabelitems二级菜单;内部 item 可用 kind: 'separator'
separator分隔线
字段说明类型
id业务动作标识,建议必填且唯一string / number
label展示文本及图标按钮提示string
title原生 title,主要用于普通菜单项string
icon图标组件Component
shortcut右侧快捷键提示,仅展示、不监听键盘string
danger是否使用危险色boolean
disabled是否禁用boolean
kind / type子菜单分隔项可设为 separatorstring
其他字段原样保留并随事件返回,例如 confirmMessageunknown

ContextMenu API

Attributes

属性说明类型默认值
modelValue / v-model是否显示菜单booleantrue
items菜单 Section 数据AuContextMenuSection[][]
position视口坐标{ x: number, y: number }{ x: 0, y: 0 }
iconColor图标行统一颜色string''
ariaLabel菜单无障碍名称string上下文菜单
beforeSelect选择前守卫;返回 false 取消动作(item) => boolean | Promise<boolean>
hideOnSelect成功选择后是否关闭booleantrue
closeOnClickOutside点击外部是否关闭booleantrue
teleported是否传送到 appendTobooleantrue
appendToTeleport 目标string / HTMLElementbody
zIndex菜单层级number2200

Events

事件名说明参数
update:modelValue内部关闭时更新绑定值(visible: boolean)
select守卫通过后触发(item: AuContextMenuItem)
actionselect 的兼容事件,参数相同(item: AuContextMenuItem)
cancelbeforeSelect 返回 false 时触发(item: AuContextMenuItem)
close菜单关闭时触发(reason: string)

关闭原因包括 apiselectoutsideescape

Exposes

属性或方法说明
close(reason?)主动关闭菜单
updatePosition()根据最新坐标和菜单尺寸执行视口避让
menuRef菜单根元素引用

交互说明

菜单打开后会获得焦点。ArrowUpArrowDownHomeEnd 可在可用按钮间移动,Escape 关闭菜单。组件只展示 shortcut,快捷键监听仍应由业务层实现。

Aurora Plus · Vue 3 Component Library