MenuItem 菜单项
在菜单中展示可执行的操作。
基础用法
在默认插槽中填写操作名称,使用 icon 插槽添加图标。
查看代码收起代码
vue
<script setup lang="ts">
import { ZMenuItem } from '@codex-proxy/ui'
import { Copy, Pencil, Trash2 } from '@lucide/vue'
import { shallowRef } from 'vue'
const action = shallowRef('尚未操作')
</script>
<template>
<div class="demo-stack">
<div class="w-full max-w-64 rounded-z bg-z-bg-elevated p-1">
<ZMenuItem @click="action = '编辑配置'">
<template #icon>
<Pencil :size="14" />
</template>
编辑
</ZMenuItem>
<ZMenuItem disabled>
<template #icon>
<Copy :size="14" />
</template>
复制(不可用)
</ZMenuItem>
<ZMenuItem loading>
处理中
</ZMenuItem>
<ZMenuItem type="danger" @click="action = '移除示例项'">
<template #icon>
<Trash2 :size="14" />
</template>
移除
</ZMenuItem>
</div>
<output aria-label="当前操作">{{ action }}</output>
</div>
</template>Dropdown 操作菜单
在 ZDropdown 的 reference 插槽中,将 trigger 绑定到触发按钮。默认插槽放置 ZMenuItem,选中后自动关闭。用 menu-class 设置菜单的宽度和内边距,placement 沿用 Popover 的位置,默认右下对齐
键盘打开时聚焦菜单项,上下方向键移动,Home / End 跳至首尾,Esc 关闭并返回按钮
查看代码收起代码
vue
<script setup lang="ts">
import { ZButton, ZDropdown, ZMenuItem } from '@codex-proxy/ui'
import { shallowRef } from 'vue'
const action = shallowRef('尚未操作')
</script>
<template>
<ZDropdown menu-class="w-36 p-1.5">
<template #reference="{ trigger }">
<ZButton v-bind="trigger">
更多操作
</ZButton>
</template>
<ZMenuItem @click="action = '编辑'">
编辑
</ZMenuItem>
<ZMenuItem disabled>
复制
</ZMenuItem>
<ZMenuItem @click="action = '导出'">
导出
</ZMenuItem>
</ZDropdown>
<output aria-label="当前操作">{{ action }}</output>
</template>MenuItem API
属性
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
type | 普通或危险操作 | 'default' | 'danger' | 'default' |
disabled / loading | 禁用或加载时阻止点击 | boolean | false |
事件
| 事件 | 说明 |
|---|---|
click | 原生 MouseEvent,加载与禁用时不会触发 |
插槽
| 插槽 | 说明 |
|---|---|
default | 菜单文字 |
icon / loading | 普通图标或加载图标 |