Files
heroui-vue/apps/docs/components/dropdown.md
T
maoyingjie 06ddb6662a feat: migrate Menu and Dropdown with selection and submenus
Port HeroUI menu family and Dropdown from ce90ea3: Reka DropdownMenu base,
MenuKey[] selection with section owners, 17 official demos, dropdown docs
and B01 tests. Long-press stays demo-level; submenu close is level-scoped.
2026-10-24 20:43:04 +08:00

237 lines
7.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Dropdown 下拉菜单
description: 展示用户可选择的操作或选项列表
component: dropdown
group: 组件
---
> 基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。
## 用法
```vue
<script setup lang="ts">
import {
DropdownRoot,
DropdownPopover,
DropdownMenu,
DropdownItem,
Label,
Button,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="dropdown-default" />
## 组件结构
```vue
<DropdownRoot>
<Button variant="secondary" aria-label="菜单">操作</Button>
<DropdownPopover>
<DropdownMenu>
<DropdownItem id="new-file" text-value="新建文件">
<Label>新建文件</Label>
</DropdownItem>
</DropdownMenu>
</DropdownPopover>
</DropdownRoot>
```
## 示例
### 带图标
<ComponentPreview name="dropdown-with-icons" />
### 带描述
<ComponentPreview name="dropdown-with-descriptions" />
### 含禁用项
<ComponentPreview name="dropdown-with-disabled-items" />
### 分组选项
<ComponentPreview name="dropdown-with-sections" />
### 多选
<ComponentPreview name="dropdown-with-multiple-selection" />
### 受控组件
<ComponentPreview name="dropdown-controlled" />
### 受控展开状态
<ComponentPreview name="dropdown-controlled-open-state" />
### 单选
<ComponentPreview name="dropdown-with-single-selection" />
### 单选自定义指示器
<ComponentPreview name="dropdown-single-with-custom-indicator" />
### 分组级选择
<ComponentPreview name="dropdown-with-section-level-selection" />
### 带快捷键
<ComponentPreview name="dropdown-with-keyboard-shortcuts" />
### 含子菜单
<ComponentPreview name="dropdown-with-submenus" />
### 自定义子菜单指示器
<ComponentPreview name="dropdown-with-custom-submenu-indicator" />
### 自定义触发器
<ComponentPreview name="dropdown-custom-trigger" />
### 长按触发
<ComponentPreview name="dropdown-long-press-trigger" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="dropdown-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-dropdown {
@apply flex flex-col gap-1;
}
.hv-dropdown__popover {
@apply rounded-2xl bg-overlay shadow-lg;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
#### 基础类
- `.hv-dropdown` — 根容器
- `.hv-dropdown__trigger` — 触发器
- `.hv-dropdown__popover` — 浮层
- `.hv-dropdown__menu` — 菜单容器
#### 菜单组件类
- `.hv-menu` — 菜单列表
- `.hv-menu-item` — 菜单项
- `.hv-menu-item__indicator` — 选中指示器
- `.hv-menu-item__indicator--submenu` — 子菜单指示器
- `.hv-menu-item--default` / `.hv-menu-item--danger` — 变体
- `.hv-menu-section` — 分组
### 交互状态
- **悬停**:`:hover` 或 `[data-hovered="true"]`
- **聚焦**:`:focus-visible` 或 `[data-focus-visible="true"]`
- **按下**:`[data-pressed="true"]`
- **选中**:`[aria-checked="true"]` / `[data-selected="true"]`
- **禁用**:`[data-disabled="true"]`
- **进入/退出**:`[data-entering]` / `[data-exiting]`
- **放置**:`[data-placement="*"]`
## API 参考
所有部件接受 `class`、`style`、ARIA 与原生事件。选择集合使用 Vue 数组模型(`MenuKey[]`),不使用 React `Set`/`Selection`。
### DropdownRoot
| Prop | 类型 | 默认值 | 描述 |
| ------------- | ---------------- | ------ | -------------- |
| `open` | `boolean` | — | 受控展开 |
| `defaultOpen` | `boolean` | — | 非受控初始展开 |
| `modal` | `boolean` | `true` | 模态焦点管理 |
| `dir` | `'ltr' \| 'rtl'` | — | 文本方向 |
| `update:open` | 事件 | — | 展开变更 |
### DropdownTrigger
| Prop | 类型 | 默认值 | 描述 |
| --------- | --------- | ------ | ---------------------- |
| `asChild` | `boolean` | `true` | 将触发合并到唯一子节点 |
### DropdownPopover
| Prop | 类型 | 默认值 | 描述 |
| ----------------- | --------- | ------ | ---------------- |
| `side` / `align` | `string` | — | 相对触发器的位置 |
| `sideOffset` | `number` | — | 侧边偏移 |
| `avoidCollisions` | `boolean` | — | 碰撞翻转 |
| `loop` | `boolean` | — | 焦点循环 |
### DropdownMenu
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------- | ---------------------------------- | -------- | ----------- |
| `selectionMode` | `'none' \| 'single' \| 'multiple'` | `'none'` | 选择模式 |
| `selectedKeys` / `defaultSelectedKeys` | `MenuKey[]` | — / `[]` | 受控 / 初始 |
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项 |
| `update:selectedKeys` / `selectionChange` | 事件 | — | 选择变更 |
| `action` | 事件 `(key: MenuKey)` | — | 动作项激活 |
### DropdownSection
与 `DropdownMenu` 相同的选择 Props,可在分组内独立拥有 `selectionMode` / `selectedKeys`。
### DropdownItem
| Prop | 类型 | 默认值 | 描述 |
| ------------ | ----------------------- | ----------- | -------------- |
| `id` | `MenuKey` | — | 条目标识 |
| `textValue` | `string` | — | 首字母导航文本 |
| `variant` | `'default' \| 'danger'` | `'default'` | 视觉变体 |
| `isDisabled` | `boolean` | `false` | 禁用 |
| `select` | 事件 `(key: MenuKey)` | — | 激活回调 |
### DropdownItemIndicator
| Prop | 类型 | 默认值 | 描述 |
| ------ | ---------------------- | ------------- | ---------- |
| `type` | `'checkmark' \| 'dot'` | `'checkmark'` | 指示器类型 |
### DropdownSubmenuIndicator / DropdownSubmenuTrigger
子菜单指示器默认 chevron-right;`DropdownSubmenuTrigger` 包裹条目与嵌套 `DropdownPopover`,每层子菜单独立 open 状态。
## 无障碍
菜单使用 `role="menu"` / `menuitem`。上下键与首尾导航、Enter/Space 激活、Esc 关闭并恢复触发器焦点。禁用项跳过;隐藏/退出条目不进入 Tab 顺序。
## 上游差异
- React 对象部件改具名导出;`Selection`/`Set` 改 `MenuKey[]`;`onAction`/`onSelectionChange` 改 Vue 事件。
- `Kbd slot="keyboard"` 为上游原生 slot 透传;本库 Kbd 支持同结构。
- Menu/Item/Section 无独立官方文档页,契约折叠于 Dropdown 页;本库提供同名导出供组合。
- 长按触发由 Reka `DropdownMenuRoot` 的 trigger 策略提供;与上游 `trigger="longPress"` 对应关系见演示。
## 相关组件
- [Button 按钮](./button)
- [Popover 弹出层](./popover)
- [ListBox 列表框](./list-box)
- [Separator 分隔线](./separator)