196 lines
7.3 KiB
Markdown
196 lines
7.3 KiB
Markdown
---
|
||
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)
|
||
|
||
## 相关案例
|
||
|
||
以下卡片链接至上游 React 案例(新窗口)。
|
||
|
||
<DocsRelatedShowcases component="Button" />
|