Files
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

230 lines
8.0 KiB
Markdown

---
title: Accordion 手风琴
description: 用于在紧凑空间中组织信息的可折叠内容面板
component: accordion
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Accordion,
AccordionBody,
AccordionHeading,
AccordionIndicator,
AccordionItem,
AccordionPanel,
AccordionTrigger,
} from "@heroui-vue/components";
</script>
<template>
<Accordion>
<AccordionItem>
<AccordionHeading>
<AccordionTrigger>
标题
<AccordionIndicator />
</AccordionTrigger>
</AccordionHeading>
<AccordionPanel>
<AccordionBody>内容</AccordionBody>
</AccordionPanel>
</AccordionItem>
</Accordion>
</template>
```
<ComponentPreview name="accordion-basic" minHeight="400px" />
## 组件结构
```vue
<script setup lang="ts">
import {
Accordion,
AccordionBody,
AccordionHeading,
AccordionIndicator,
AccordionItem,
AccordionPanel,
AccordionTrigger,
} from "@heroui-vue/components";
</script>
<template>
<Accordion>
<AccordionItem>
<AccordionHeading>
<AccordionTrigger>
<AccordionIndicator />
</AccordionTrigger>
</AccordionHeading>
<AccordionPanel>
<AccordionBody />
</AccordionPanel>
</AccordionItem>
</Accordion>
</template>
```
Accordion 复用 DisclosureGroup 的展开键协议(数组、字符串/数字 key),部件与布局按手风琴基线独立。
## 示例
### 表面样式
<ComponentPreview name="accordion-surface" minHeight="400px" />
### 无分隔线
<ComponentPreview name="accordion-without-separator" minHeight="400px" />
### 多项展开
<ComponentPreview name="accordion-multiple" minHeight="524px" />
### 禁用状态
<ComponentPreview name="accordion-disabled" minHeight="524px" />
### 受控组件
<ComponentPreview name="accordion-controlled" minHeight="400px" />
### 自定义指示器
<ComponentPreview name="accordion-custom-indicator" minHeight="320px" />
### 渲染函数
上游 `render` 映射为 `as` 与透传属性。
<ComponentPreview name="accordion-render-function" minHeight="400px" />
### 常见问题布局
<ComponentPreview name="accordion-faq" minHeight="720px" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="accordion-custom-styles" />
### 全局 CSS
```css
@layer components {
.hv-accordion {
@apply rounded-xl bg-gray-50;
}
.hv-accordion__trigger {
@apply font-semibold text-lg;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Accordion 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/accordion.css)):
#### 基础类 [!toc]
- `.hv-accordion` - Accordion 根容器
- `.hv-accordion__item` - 单个 Accordion 项(含分隔线)
- `.hv-accordion__heading` - 标题包裹层
- `.hv-accordion__trigger` - 可点击的触发按钮
- `.hv-accordion__indicator` - 展开/收起指示图标
- `.hv-accordion__panel` - 可折叠面板容器(高度动画宿主)
- `.hv-accordion__body` / `.hv-accordion__body-inner` - 正文外层与内层包裹
#### 变体类 [!toc]
- `.hv-accordion--surface` - 表面变体(边框、背景与分隔线收口)
#### 状态类 [!toc]
- `.hv-accordion__trigger[aria-expanded="true"]` - 展开状态
- `.hv-accordion__trigger[aria-disabled="true"]` - 禁用状态
- `.hv-accordion__panel[data-expanded="true"]` / `.hv-accordion__indicator[data-expanded="true"]` - 面板与指示器的展开标记
### 交互状态
触发器以 CSS 伪类为主(组件不输出 `data-hovered` / `data-focus-visible` 标记,可通过 attrs 传入这些标记作为自定义样式钩子):
- **悬停**:触发器上 `:hover`(仅未展开时上色)
- **聚焦**:触发器上 `:focus-visible`(显示焦点环)
- **禁用**:触发器上 `:disabled` 或 `[aria-disabled="true"]`(同时输出 `[data-disabled="true"]` 标记)
- **展开**:触发器上 `[aria-expanded="true"]`
## API 参考
### Accordion
| 属性 | 类型 | 默认值 | 描述 |
| ------------------------ | --------------------------- | ----------- | -------------------- |
| `expandedKeys` | `Array<string \| number>` | - | 受控展开 key |
| `defaultExpandedKeys` | `Array<string \| number>` | `[]` | 初始展开 key |
| `allowsMultipleExpanded` | `boolean` | `false` | 是否允许多项同时展开 |
| `isDisabled` | `boolean` | `false` | 禁用整个 Accordion |
| `variant` | `"default" \| "surface"` | `"default"` | 视觉变体 |
| `hideSeparator` | `boolean` | `false` | 隐藏项之间的分隔线 |
| `as` | `string` | `"div"` | 根元素 |
| `class` | `string \| Array \| Object` | - | 额外的 CSS 类 |
### AccordionItem
| 属性 | 类型 | 默认值 | 描述 |
| ----------------- | ------------------ | ------- | -------------------- |
| `id` | `string \| number` | - | 该项唯一标识(组键) |
| `expanded` | `boolean` | - | 受控展开状态 |
| `defaultExpanded` | `boolean` | `false` | 初始展开 |
| `isDisabled` | `boolean` | `false` | 禁用该项 |
| `as` | `string` | `"div"` | 项根元素 |
### AccordionHeading
| 属性 | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | -------------- |
| `as` | `string` | `'h3'` | 标题根元素标签 |
| `level` | `number` | `3` | 语义标题级别 |
| `class` | `string \| Array \| Object` | - | 额外的 CSS 类 |
| 默认插槽 | - | - | 标题内容 |
### AccordionTrigger
| 属性 | 类型 | 默认值 | 描述 |
| -------------- | --------------------------- | ---------- | ------------------------------------------------ |
| `as` | `string` | `'button'` | 触发器根元素;为 `button` 时输出 `type="button"` |
| `asChild` | `boolean` | `false` | 将展开切换与状态合并到默认插槽中唯一的根元素 |
| `class` | `string \| Array \| Object` | - | 额外的 CSS 类 |
| 默认作用域插槽 | - | - | 提供 `isExpanded`、`isDisabled` |
### AccordionPanel / AccordionBody
| 属性 | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------- | ---------------------------------------------------------- |
| `as` | `string` | `'div'` | 根元素;Panel 负责高度动画,Body 的 `class` 落在内层包裹上 |
| `class` | `string \| Array \| Object` | - | 额外的 CSS 类 |
| 默认插槽 | - | - | 面板 / 正文内容 |
### AccordionIndicator
| 属性 | 类型 | 默认值 | 描述 |
| -------------- | ---- | ------ | ------------------------------------------------------------------ |
| 默认作用域插槽 | - | - | 自定义指示图标;提供 `isExpanded`。缺省时渲染基线 ChevronDown 图标 |
与 Disclosure 对应部件语义一致,使用 `hv-accordion__*` 样式。
## 相关组件
[DisclosureGroup 折叠面板组](./disclosure-group) · [Disclosure 折叠面板](./disclosure)