For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt, and this page is available as Markdown at https://docs.halo.run/developer-guide/plugin/api-reference/ui/base-components/dropdown.md.

VDropdown 下拉菜单

下拉菜单用于收纳一组次要操作,点击触发元素后展开。

从 2.28.0 开始,VDropdown 基于 Reka UI 的 DropdownMenu 构建,提供菜单语义、方向键与 Home/End 导航,按 Esc 关闭后焦点回到触发元素。此前的版本重新导出了 floating-vue 的 Dropdown 组件;VDropdown、VDropdownItem、VDropdownDivider、v-close-popper 指令和 VDropdownPlacement 类型的导出保持不变。

迁移提示:弹出层的 .v-popper__popper 和 .v-popper__inner class 仍然保留,针对下拉菜单的插件样式无需调整;floating-vue 特有的 theme、disabled 属性以及 triggers 的 hover、focus 取值不再生效,需要禁用时请直接控制触发元素,例如给 VButton 设置 disabled。

引入

import {
  VDropdown,
  VDropdownDivider,
  VDropdownItem,
} from "@halo-dev/components";

示例

基础用法

<script lang="ts" setup>
import { VButton, VDropdown, VDropdownItem, Toast } from "@halo-dev/components";

function handleSelect(action: string) {
  Toast.success(`选择了:${action}`);
}
</script>

<template>
  <VDropdown placement="bottom-start" :distance="6">
    <VButton>更多操作</VButton>
    <template #popper>
      <VDropdownItem @click="handleSelect('编辑')">编辑</VDropdownItem>
      <VDropdownItem @click="handleSelect('复制')">复制</VDropdownItem>
      <VDropdownItem @click="handleSelect('移动')">移动</VDropdownItem>
    </template>
  </VDropdown>
</template>

选中与禁用

一组互斥选项(例如排序方式)使用 checkable 标记每个菜单项,它会渲染为单选菜单项并输出 aria-checked,选中样式仍由 selected 控制。

<script lang="ts" setup>
import { VButton, VDropdown, VDropdownItem } from "@halo-dev/components";
import { ref } from "vue";

const sort = ref("created");
</script>

<template>
  <div class="space-y-3">
    <VDropdown placement="bottom-start" :distance="6">
      <VButton>排序方式</VButton>
      <template #popper>
        <VDropdownItem
          checkable
          :selected="sort === 'created'"
          @click="sort = 'created'"
        >
          最近创建
        </VDropdownItem>
        <VDropdownItem
          checkable
          :selected="sort === 'updated'"
          @click="sort = 'updated'"
        >
          最近更新
        </VDropdownItem>
        <VDropdownItem checkable disabled>访问量最多</VDropdownItem>
      </template>
    </VDropdown>
    <p class="text-sm text-gray-500">当前排序:{{ sort }}</p>
  </div>
</template>

键盘操作

菜单展开后,↓ 和 ↑ 在菜单项之间移动,Home 和 End 跳到第一项和最后一项,Enter 或空格执行当前项,Esc 关闭菜单并把焦点还给触发元素。禁用的菜单项会被跳过,子菜单用 → 展开。

菜单以非模态方式打开:点击外部会关闭,把 auto-hide 设为 false 可以阻止。菜单关闭后,焦点、Esc 等按键会交还给外层弹窗或抽屉,不会被隐藏的菜单拦截。

子菜单

在菜单项内嵌套 VDropdown,并把 VDropdownItem 作为它的触发器,就可以展开第二级菜单。选中子菜单中的项目时,两级菜单会一起关闭。

<script lang="ts" setup>
import { VButton, VDropdown, VDropdownItem } from "@halo-dev/components";
</script>

<template>
  <VDropdown placement="bottom-start" :distance="6">
    <VButton>更多操作</VButton>
    <template #popper>
      <VDropdownItem>编辑</VDropdownItem>
      <VDropdown :triggers="['click']">
        <VDropdownItem>移动到</VDropdownItem>
        <template #popper>
          <VDropdownItem>草稿</VDropdownItem>
          <VDropdownItem>回收站</VDropdownItem>
        </template>
      </VDropdown>
    </template>
  </VDropdown>
</template>

分割线与危险操作

<script lang="ts" setup>
import {
  VButton,
  VDropdown,
  VDropdownDivider,
  VDropdownItem,
} from "@halo-dev/components";
</script>

<template>
  <VDropdown placement="bottom-start" :distance="6">
    <VButton>操作</VButton>
    <template #popper>
      <VDropdownItem>查看详情</VDropdownItem>
      <VDropdownItem>导出</VDropdownItem>
      <VDropdownDivider />
      <VDropdownItem type="danger">删除</VDropdownItem>
    </template>
  </VDropdown>
</template>

带图标

<script lang="ts" setup>
import {
  IconDeleteBin,
  IconExternalLinkLine,
  IconSave,
  VButton,
  VDropdown,
  VDropdownItem,
} from "@halo-dev/components";
</script>

<template>
  <VDropdown placement="bottom-start" :distance="6">
    <VButton>操作</VButton>
    <template #popper>
      <VDropdownItem>
        <template #prefix-icon>
          <IconSave />
        </template>
        保存
      </VDropdownItem>
      <VDropdownItem>
        <template #prefix-icon>
          <IconExternalLinkLine />
        </template>
        在新窗口打开
      </VDropdownItem>
      <VDropdownItem type="danger">
        <template #prefix-icon>
          <IconDeleteBin />
        </template>
        删除
      </VDropdownItem>
    </template>
  </VDropdown>
</template>

VDropdown Props

属性类型默认值说明
shownboolean-受控展开状态,支持 v-model:shown;不传时由组件自己管理
triggersstring[]["click"]打开菜单的触发方式,目前支持 click;传空数组时只能通过 show() 或 shown 打开
placementVDropdownPlacement"bottom"弹出位置,可选 top、bottom、left、right 及 -start、-end 组合
distancenumber5弹出层与触发元素的间距,单位为像素
arrowbooleantrue是否显示指向触发元素的箭头
autoHidebooleantrue点击外部或焦点移出时是否关闭
popperClassstring | string[] | Record<string, boolean>-追加到弹出层的 class
unmountOnHidebooleantrue关闭后是否卸载弹出层内容;菜单内含弹窗等有状态组件时设为 false
popperTriggersstring[][]为兼容 floating-vue 保留,暂不影响行为
disposeTimeoutnumber | null150已废弃,为兼容 floating-vue 保留,不再影响行为

VDropdownItem Props

属性类型默认值说明
selectedbooleanfalse是否显示选中样式
disabledbooleanfalse是否禁用,禁用项无法聚焦,也会跳过键盘移动
type"default" | "danger""default"菜单项类型
checkablebooleanfalse是否为一组互斥选项中的一项,输出 menuitemradio 与 aria-checked

Emits

组件事件参数说明
VDropdownItemclickMouseEvent点击菜单项时触发,disabled 时不会触发
VDropdownshow-弹出层显示时触发
VDropdownhide-弹出层隐藏时触发
VDropdownapply-show-弹出层内容渲染完成并显示后触发
VDropdownupdate:shownboolean弹出层显示状态变化时触发

方法

通过模板 ref 调用 show() 和 hide() 可以手动控制展开状态,适合把 triggers 设为空数组后完全由代码控制的场景。

<script lang="ts" setup>
import { useTemplateRef } from "vue";
import { VButton, VDropdown, VDropdownItem } from "@halo-dev/components";

const dropdown = useTemplateRef<InstanceType<typeof VDropdown>>("dropdown");
</script>

<template>
  <VDropdown ref="dropdown" :triggers="[]">
    <VButton @click="dropdown?.show()">打开菜单</VButton>
    <template #popper>
      <VDropdownItem @click="dropdown?.hide()">关闭</VDropdownItem>
    </template>
  </VDropdown>
</template>

Slots

组件插槽说明
VDropdowndefault触发元素,作用域参数为 shown、show、hide
VDropdownpopper弹出层内容,作用域参数为 shown、hide
VDropdownItemdefault菜单项内容
VDropdownItemprefix-icon菜单项左侧图标
VDropdownItemsuffix-icon菜单项右侧图标

指令

vClosePopper 可以在弹出层内的元素上使用,点击后关闭当前弹出层。

<VButton v-close-popper>点击后关闭</VButton>

VDropdown 的根元素使用 display: contents,直接写在组件上的 v-permission 等指令仍然生效。