Files
heroui-vue/apps/docs/components/dropdown.md
T
maoyingjie 53b4639b0e fix: portal dropdown surfaces and close the review capability gaps
- Root and submenu surfaces render through the dropdown portal with the overlay
  z-index, so clipping/isolation ancestors no longer cut the menu; panel height
  is capped by the floating available height and the animation origin maps the
  real transform origin.
- Standalone Menu is a static in-flow list: no scroll lock, no outside pointer
  blocking, no floating placement and no focus stealing.
- Selection ownership is isolated per level: section disabled keys add to the
  menu list, and a nested Menu resets the section owner.
- closeOnSelect resolves item > menu policy > per-mode default for selection
  items; actions and radio items close, checkboxes stay open.
- Business keys no longer become DOM ids (instance ids + data-key), and submenu
  surfaces label themselves from the real trigger.
- Long-press keydown runs the caller's handlers first and only opens when not
  cancelled; submenu panels merge caller keydown with the internal Escape.
- Link items render a real anchor, aria labels land on the role=menu node,
  submenu panels forward placement/offset/collision props and caller attrs, and
  the popover provides the default Surface variant.
- Wrapper parts declare real props/emits, plus the missing official capability
  props (disallowEmptySelection, escapeKeyBehavior, shouldFocusWrap, autoFocus,
  shouldCloseOnSelect, close, empty slot, isKeyboardDismissDisabled,
  shouldCloseOnInteractOutside, placement/offset family, scoped slots).
- Ship an independent consumer fixture for browser-level verification.
2026-10-24 22:56:44 +08:00

22 KiB
Raw Blame History

title, description, component, group
title description component group
Dropdown 下拉菜单 展示用户可选择的操作或选项列表 dropdown 组件

基于固定 HeroUI 3.2.6(ce90ea3)迁移:React 对象部件改为具名导出,Selection/Set 改为 MenuKey[],回调改为 Vue 事件;预览、复制与高亮来自同一份 SFC。

用法

<script setup lang="ts">
import {
  DropdownRoot,
  DropdownTrigger,
  DropdownPopover,
  DropdownMenu,
  DropdownItem,
  Button,
  Label,
} from "@heroui-vue/components";
</script>

组件结构

<script setup lang="ts">
import {
  Button,
  Description,
  DropdownItem,
  DropdownItemIndicator,
  DropdownMenu,
  DropdownPopover,
  DropdownRoot,
  DropdownSection,
  DropdownSubmenuIndicator,
  DropdownSubmenuTrigger,
  DropdownTrigger,
  Header,
  Kbd,
  Label,
  Separator,
} from "@heroui-vue/components";
</script>

<template>
  <DropdownRoot>
    <DropdownTrigger>
      <Button />
    </DropdownTrigger>
    <DropdownPopover>
      <DropdownMenu>
        <DropdownItem>
          <Label />
          <Description />
          <Kbd slot="keyboard" />
          <DropdownItemIndicator />
        </DropdownItem>
        <Separator />
        <DropdownSection>
          <Header />
          <DropdownItem />
        </DropdownSection>
        <DropdownSubmenuTrigger>
          <DropdownItem>
            <Label />
            <DropdownSubmenuIndicator />
          </DropdownItem>
          <DropdownPopover>
            <DropdownMenu>
              <DropdownItem />
            </DropdownMenu>
          </DropdownPopover>
        </DropdownSubmenuTrigger>
      </DropdownMenu>
    </DropdownPopover>
  </DropdownRoot>
</template>

示例

带图标

带描述

含禁用项

分组选项

多选

受控组件

受控展开状态

单选

单选自定义指示器

分组级选择

带快捷键

含子菜单

自定义子菜单指示器

自定义触发器

长按触发

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 Dropdown 组件类。了解更多。

@layer components {
  .hv-dropdown {
    @apply flex flex-col gap-1;
  }

  .hv-dropdown__trigger {
    @apply outline-none;
  }

  .hv-dropdown__popover {
    @apply rounded-3xl border border-border bg-overlay p-2;
  }

  .hv-dropdown__menu {
    @apply flex flex-col gap-1;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

CSS 类

Dropdown 组件使用以下 CSS 类(类名统一加 hv- 前缀):

基础类

  • .hv-dropdown — Dropdown 根容器(Vue 端渲染的包裹元素)
  • .hv-dropdown__trigger — 触发 Dropdown 的按钮或元素
  • .hv-dropdown__popover — Popover 容器
  • .hv-dropdown__menu — Popover 内的菜单容器

状态类

  • .hv-dropdown__trigger[data-focus-visible="true"] — 触发器聚焦状态
  • .hv-dropdown__trigger[data-disabled="true"] — 触发器禁用状态
  • .hv-dropdown__trigger[data-pressed="true"] — 触发器按下状态
  • .hv-dropdown__popover[data-entering="true"] — 进入动画状态
  • .hv-dropdown__popover[data-exiting="true"] — 退出动画状态
  • .hv-dropdown__popover[data-side="top|bottom|left|right"] — 定位方向(进入动画的位移方向)
  • .hv-dropdown__menu[data-selection-mode="single"] — 单选模式
  • .hv-dropdown__menu[data-selection-mode="multiple"] — 多选模式

菜单组件类

Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类名也可用于自定义:

菜单类

  • .hv-menu — 菜单容器
    • [data-slot="separator"] — 菜单内的分隔线元素

菜单项类

  • .hv-menu-item — 菜单项容器
  • .hv-menu-item__indicator — 选中指示器(对勾或圆点)
    • [data-slot="menu-item-indicator--checkmark"] — 对勾指示器 SVG
    • [data-slot="menu-item-indicator--dot"] — 圆点指示器 SVG
  • .hv-menu-item__indicator--submenu — 子菜单指示器(箭头)
  • .hv-menu-item--default — 默认样式变体
  • .hv-menu-item--danger — 危险样式变体

菜单项状态类

  • .hv-menu-item[data-focus-visible="true"] — 聚焦状态(键盘焦点)
  • .hv-menu-item[data-focused] — 聚焦状态
  • .hv-menu-item[data-pressed] — 按下状态
  • .hv-menu-item[data-hovered] — 悬停状态
  • .hv-menu-item[data-selected="true"] — 选中状态
  • .hv-menu-item[data-disabled] — 禁用状态(存在即禁用)
  • .hv-menu-item[data-has-submenu="true"] — 带子菜单的项
  • .hv-menu-item[data-selection-mode="single"] — 单选模式
  • .hv-menu-item[data-selection-mode="multiple"] — 多选模式
  • .hv-menu-item[aria-checked="true"] — 已勾选(ARIA)

菜单分区类

  • .hv-menu-section — 菜单分区容器

交互状态

组件同时支持 CSS 伪类与 data 属性:

  • Hover:触发器与菜单项上 :hover 或 [data-hovered]
  • Focus:触发器与菜单项上 :focus-visible 或 [data-focus-visible="true"];菜单项另有 [data-focused]
  • Disabled:触发器上 :disabled/[aria-disabled="true"],菜单项上 [data-disabled]
  • Pressed:菜单项与触发器上 :active 或 [data-pressed]
  • Selected:菜单项上 [data-selected="true"] 或 [aria-checked="true"]
  • Open/Close:DropdownPopover 上 [data-state="open"];进入/退出期间另有 [data-entering="true"] / [data-exiting="true"]

API 参考

所有部件接受 class、style、ARIA 与原生事件(DropdownSubmenuTrigger 会把它们合并到它的条目元素上)。选择集合使用 Vue 数组模型(MenuKey[]),不使用 React Set/Selection。标记「Vue 扩展」的属性在官方 HeroUI 中没有对应项。

弹层默认通过 Portal 挂载到 document.body(可用 to 指定目标),层级为 --z-index-overlay,因此不会被祖先的 overflow、contain 或 isolation 裁切;面板高度受底层可用的浮动高度约束并自动滚动。

DropdownRoot

Prop 类型 默认值 描述
open boolean — 设置菜单展开状态(受控)。官方名 isOpen
defaultOpen boolean — 设置菜单默认展开状态(非受控)。
trigger 'press' | 'longPress' 'press' 触发菜单的交互类型。
update:open 事件 (open: boolean) — 展开状态变化时触发。官方名 onOpenChange
modal boolean true Vue 扩展:模态焦点管理
dir 'ltr' | 'rtl' — Vue 扩展:文本方向
longPressDuration number 500 Vue 扩展:长按时长 ms;移动超过 10px 取消

长按模式下 Enter/Space 仍可键盘打开,并且真实触发器上的 keydown 处理器先执行,可 preventDefault() 取消打开;pointerup/pointercancel/pointerleave/卸载会清理计时。

DropdownTrigger

Prop 类型 默认值 描述
asChild boolean true 将触发合并到唯一子节点(默认行为)
as string — 自定义触发元素标签

默认插槽会收到 { isOpen }。页面里的 Button props 写在内部的 Button 上。子节点若不是原生可聚焦元素(例如 Avatar),组件会补 role="button" 与 tabindex="0",保持官方「触发器可键盘操作」的行为。

DropdownPopover

Prop 类型 默认值 描述
placement OverlayPlacement 'bottom' 相对于触发器的位置(复用本库 Popover 的取值)。
offset number 0 与触发器的距离。
crossOffset number 0 交叉轴偏移。
shouldFlip boolean true 空间不足时翻转。
containerPadding number 12 与视口边缘保留的距离。
isKeyboardDismissDisabled boolean false 禁用 Escape 关闭。
shouldCloseOnInteractOutside (event) => boolean — 返回 false 时外部交互不关闭菜单。
to string | HTMLElement — Portal 目标,默认 document.body。
side / align string — 底层扩展:直接指定浮动方向,优先于 placement。
sideOffset / alignOffset number — 底层扩展:覆盖 offset / crossOffset。
avoidCollisions boolean — 底层扩展:覆盖 shouldFlip。
collisionPadding number — 底层扩展:覆盖 containerPadding。
loop boolean — 底层扩展:焦点循环。

默认插槽会收到 { isEntering, isExiting, placement }。

DropdownMenu

Prop 类型 默认值 描述
selectionMode 'none' | 'single' | 'multiple' 'none' 是否启用单选、多选或不启用选择。
selectedKeys MenuKey[] — 当前选中的 key(受控)。
defaultSelectedKeys MenuKey[] [] 初始选中的 key(非受控)。
disabledKeys MenuKey[] [] 禁用项的 key。
disallowEmptySelection boolean false 禁止用户清空已有选择。
shouldCloseOnSelect boolean | (key) => boolean — 菜单级关闭策略;默认动作项与单选关闭、多选保持打开。
shouldFocusWrap boolean — 键盘导航在首尾循环。
autoFocus boolean true 打开时聚焦菜单。
escapeKeyBehavior 'clearSelection' | 'none' 'none' clearSelection 时 Escape 清空选择且不关闭菜单。
update:selectedKeys / selectionChange 事件 (keys: MenuKey[]) — 选中变化时触发。官方名 onSelectionChange
action 事件 (key: MenuKey) — 激活菜单项时触发。官方名 onAction
close 事件 () — 该层菜单关闭时触发。官方名 onClose

默认插槽渲染条目;empty 插槽在该菜单没有条目时渲染(对应官方 renderEmptyState)。数据驱动的 items 用 v-for 表达。

DropdownSection

Prop 类型 默认值 描述
selectionMode 'single' | 'multiple' — 该分组内菜单项的选择模式;省略则继承菜单。
selectedKeys MenuKey[] — 当前选中的 key(受控)。
defaultSelectedKeys MenuKey[] [] 初始选中的 key(非受控)。
disabledKeys MenuKey[] [] 禁用项的 key,与菜单级 disabledKeys 叠加。
disallowEmptySelection boolean false 禁止用户清空该分组的选择。
update:selectedKeys / selectionChange 事件 (keys: MenuKey[]) — 选中变化时触发。官方名 onSelectionChange

分组内嵌套的 Header 会自动成为分组的可访问名称来源(aria-labelledby)。分组内再嵌套的 Menu 会重置选择作用域,不会继承外层分组的 owner。

DropdownItem

Prop 类型 默认值 描述
id MenuKey — 业务 key(选择与动作使用)。DOM id 为实例级,与业务 key 无关。
textValue string — 用于首字母导航的文本内容。
variant 'default' | 'danger' 'default' 菜单项视觉变体。
isDisabled boolean false 禁用该项。
closeOnSelect boolean — 覆盖关闭策略;false 保持打开,true 关闭。官方名 shouldCloseOnSelect
href string — 渲染为链接型菜单项(真实 <a>)。
target / rel / download / hrefLang / referrerPolicy — — 透传到链接元素。
select 事件 (key: MenuKey) — Vue 扩展:条目激活回调

默认插槽会收到 { isSelected, isFocused, isDisabled, isPressed },对应官方的 Render Props。禁用链接不会导航;href 项的原生导航不受 click 处理器影响(除非调用 preventDefault())。

DropdownItemIndicator

Prop 类型 默认值 描述
type 'checkmark' | 'dot' 'checkmark' 指示器类型

默认插槽会收到 { isSelected, isIndeterminate, isFocused, isDisabled, isPressed }(菜单项没有不确定态,isIndeterminate 恒为 false):

<DropdownItemIndicator v-slot="{ isSelected }">
  <MyCheckIcon v-if="isSelected" />
</DropdownItemIndicator>

DropdownSubmenuIndicator

内容 说明
默认 官方内置的 chevron-right 图标
插槽 自定义指示器内容;带子菜单的项才渲染

DropdownSubmenuTrigger

Slots / Props 说明
默认插槽 第一个子节点是触发条目,其余子节点渲染进子菜单内容
class/ARIA 合并到触发条目元素上
面板参数 嵌套 DropdownPopover 的 offset/crossOffset/shouldFlip/containerPadding/loop/to/class 会传给子菜单面板

每层子菜单独立持有 open 状态并各自 Portal;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。子菜单的 side/align 由底层固定为「贴着父项向右展开」(RTL 反向),只有偏移与碰撞参数可调。

Render Props

在 DropdownItem 中使用作用域插槽时,会收到以下值:

Prop 类型 描述
isSelected boolean 该项是否选中。
isFocused boolean 该项是否聚焦。
isDisabled boolean 该项是否禁用。
isPressed boolean 该项是否处于按下状态。

独立 Menu

Menu 可以在 DropdownPopover 之外直接使用:渲染为静态内联列表(data-slot="menu"),保留 role="menu"、条目语义、键盘导航与选择;不会锁定页面滚动、不会拦截外部指针事件、不会抢焦点,也不做浮动定位。

无障碍

Dropdown 组件实现 ARIA 菜单模式,并提供:

  • 完整键盘导航(方向键、Home/End、首字母导航,shouldFocusWrap 控制首尾循环)
  • 屏幕阅读器对操作与选中变化的播报:aria-label/aria-labelledby/aria-describedby 落在真正的 role="menu" 节点上
  • 合理的焦点管理(关闭后焦点回到触发器,Esc 逐层关闭)
  • 禁用态支持([data-disabled] + aria-disabled)
  • 长按交互支持
  • 子菜单导航

菜单使用 role="menu" / menuitem(选择项为 menuitemradio / menuitemcheckbox),每个弹层只有一个 role="menu";禁用项跳过,不进入 Tab 顺序。

上游差异

  • 部件导出为具名组件(DropdownRoot / DropdownTrigger / …),触发器需要显式包裹,官方是把 Button 直接放在 Dropdown 下。
  • Selection/Set 改为 MenuKey[];onAction/onSelectionChange/onClose 改为 Vue 事件;isOpen/onOpenChange 改为 open + v-model:open。
  • 官方 items + 渲染函数的数据驱动集合用 v-for 表达;renderEmptyState 对应 empty 插槽。不提供 React Collection 对象模型。
  • 子菜单悬停展开延迟由底层固定为 100ms,未暴露官方 delay(默认 200ms);子菜单面板的 side/align 由底层固定,只有偏移与碰撞参数可调。
  • DropdownTrigger 的作用域插槽只提供 { isOpen },DropdownPopover 只提供 { isEntering, isExiting, placement };官方 Button/Popover 的完整 render props 未全部覆盖。
  • DropdownRoot 额外渲染一层 .hv-dropdown 包裹元素(官方该类没有 DOM 宿主),用于承载根级 class 与方向。
  • 条目 id 是业务 key,DOM id 为实例级;官方把 key 直接作为 DOM id(Vue 端避免数字 0 与字符串 "0" 冲突)。
  • 条目 onClick 中 preventDefault() 会取消动作、选择与关闭;stopImmediatePropagation() 会终止后续处理器,均为原生事件语义。

相关组件