Files
heroui-vue/apps/docs/components/dropdown.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

23 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 }。onKeydown/onEscapeKeyDown/onInteractOutside/onOpenAutoFocus/onAnimationend 与调用方处理器按 Vue 数组语义合并,调用方先执行,preventDefault() 后内部逻辑不再动作。

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/style/id/data-*/ARIA 与事件都会传给子菜单面板;isKeyboardDismissDisabled 为真时 Esc 既不关子层也不关父层,shouldCloseOnInteractOutside 返回 false 时外部交互不关闭

每层子菜单独立持有 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、style、id、ARIA、data 属性与原生事件。作为 ButtonGroup 直接子级时仍保留该容器,接缝样式按容器在组内的位置适配内部触发按钮;根级属性与捕获事件的行为不变。
  • 条目 id 是业务 key,DOM id 为实例级;官方把 key 直接作为 DOM id(Vue 端避免数字 0 与字符串 "0" 冲突)。
  • 条目 onClick 中 preventDefault() 会取消动作、选择与关闭;stopImmediatePropagation() 会终止后续处理器,均为原生事件语义。

相关组件

Button 按钮 · Popover 弹出框 · Separator 分隔符