Files
heroui-vue/apps/docs/components/button.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

190 lines
7.2 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: Button 按钮
description: 可点击的按钮组件,支持多种变体与状态
component: button
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Button } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="button-basic" />
## 示例
### 变体
<ComponentPreview name="button-variants" />
### 尺寸
<ComponentPreview name="button-sizes" />
### 带图标
<ComponentPreview name="button-with-icons" />
### 仅图标
<ComponentPreview name="button-icon-only" />
### 加载中
此例固定展示上传中的外观;加载期间阻止重复提交,但保留键盘焦点。要体验开始、完成和再次上传,请使用下一例。
<ComponentPreview name="button-loading" />
### 加载中(点击触发)
点击“上传文件”后进入加载状态,2 秒后恢复,可重复操作。
<ComponentPreview name="button-loading-state" />
### 宽度充满
<ComponentPreview name="button-full-width" />
### 禁用状态
<ComponentPreview name="button-disabled" />
### 社交媒体按钮
<ComponentPreview name="button-social" />
### 渲染函数
Vue 使用 `asChild` 将按钮属性与交互合并到一个自定义原生 button,作用域插槽提供状态。对应上游 render 函数示例,不将 render prop 原样搬入 Vue。
<ComponentPreview name="button-render-function" />
### 添加自定义变体
可通过包装组件并添加自定义变体来扩展组件。下面使用与上游相同的 `tv` 扩展方式,提供额外的圆角与尺寸配置。
<ComponentPreview name="button-custom-variants" />
### 添加涟漪效果
通过组合添加涟漪。本示例使用 Vue 和 CSS 实现,支持指针、Enter 和空格触发;不直接依赖上游 React 的 m3-ripple,减少动态效果时缩短动画。
<ComponentPreview name="button-ripple-effect" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="button-custom-styles" />
使用标准 `class` 属性添加工具类。组件保持上游的尺寸、间距、BEM 结构与状态钩子;类名前缀改为 `hv-`。
```vue
<Button class="rounded-lg">自定义圆角</Button>
```
### 全局 CSS
可使用 `@layer components` 指令自定义 Button 组件类;下面的选择器与语义变量都是当前样式表中真实存在的入口:
```css
@layer components {
/* 覆盖变体语义变量 */
.hv-button--primary {
--button-bg: oklch(0.6 0.2 300);
--button-bg-hover: oklch(0.55 0.2 300);
--button-bg-pressed: oklch(0.55 0.2 300);
--button-fg: white;
}
/* 或直接覆盖类规则 */
.hv-button--icon-only {
@apply rounded-lg;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Button 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/button.css)):
#### 基础与尺寸类 [!toc]
- `.hv-button` - 基础按钮样式
- `.hv-button--sm` - 小尺寸变体
- `.hv-button--md` - 中尺寸变体(默认)
- `.hv-button--lg` - 大尺寸变体
#### 变体类 [!toc]
- `.hv-button--primary`
- `.hv-button--secondary`
- `.hv-button--tertiary`
- `.hv-button--outline`
- `.hv-button--ghost`
- `.hv-button--danger`
- `.hv-button--danger-soft`
#### 修饰符类 [!toc]
- `.hv-button--icon-only`
- `.hv-button--icon-only.hv-button--sm`
- `.hv-button--icon-only.hv-button--lg`
- `.hv-button--full-width`
### 交互状态
按钮同时支持 CSS 伪类与 data 属性:
- **Hover**:`:hover` 或 `[data-hovered="true"]`
- **Active/Pressed**:`:active` 或 `[data-pressed="true"]`(含缩放变换)
- **Focus**:`:focus-visible` 或 `[data-focus-visible="true"]`(显示焦点环)
- **Disabled**:`:disabled` 或 `[aria-disabled="true"]`(降低透明度,禁用指针事件)
- **Pending**:`[data-pending="true"]`(加载期间禁用指针事件)
加载期间保留焦点并阻止重复点击,通过 `aria-disabled` 呈现半透明的原变体颜色。
## API 参考
### Button
| Prop | 类型 | 默认值 | 描述 |
| ------------ | --------------------------------------------------------------------------------------------- | ----------- | --------------------------------------------- |
| `variant` | `'primary' \| 'secondary' \| 'tertiary' \| 'outline' \| 'ghost' \| 'danger' \| 'danger-soft'` | `'primary'` | 视觉样式变体 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 按钮尺寸 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `isDisabled` | `boolean` | `false` | 是否禁用 |
| `isPending` | `boolean` | `false` | 是否处于加载状态 |
| `isIconOnly` | `boolean` | `false` | 是否仅包含图标 |
| `asChild` | `boolean` | `false` | 将属性与交互合并到默认插槽中唯一的原生 button |
| `type` | `'button' \| 'submit' \| 'reset'` | `'button'` | 原生按钮类型 |
### 事件与插槽
Vue 使用 `@click` 和默认作用域插槽。插槽提供 `isPending`、`isPressed`、`isHovered`、`isFocused`、`isFocusVisible`、`isDisabled`。原生属性、class、style、事件透传至 button。
`onPress` / PressEvent 与 `@click` 的事件模型不同,不宣称完全等价。初版 `disabled` / `loading` 继续作为兼容别名;新代码使用 `isDisabled` / `isPending`。`isPending` 不自动插入加载图标,采用上游的组合方式。`loading` 是相同加载语义的兼容别名,额外自动显示旋转图标;不会设置原生 disabled。`asChild` 下内容由调用者完整组合,默认插槽必须只包含一个原生 button。
### 作用域插槽状态
| 参数 | 类型 | 描述 |
| ---------------- | --------- | -------------------------------- |
| `isPending` | `boolean` | 是否处于加载状态 |
| `isPressed` | `boolean` | 是否正在按下 |
| `isHovered` | `boolean` | 是否悬停 |
| `isFocused` | `boolean` | 是否聚焦 |
| `isFocusVisible` | `boolean` | 是否显示键盘焦点指示器 |
| `isDisabled` | `boolean` | 是否由禁用属性禁用,不与加载混用 |
## 相关组件
[Popover 弹出框](./popover) · [Tooltip 工具提示](./tooltip) · [Form 表单](./form)