159 lines
5.0 KiB
Markdown
159 lines
5.0 KiB
Markdown
---
|
|
title: ButtonGroup 按钮组
|
|
description: 将相关按钮组合在一起,保持一致的样式与间距
|
|
component: button-group
|
|
group: 组件
|
|
---
|
|
|
|
## 用法
|
|
|
|
```vue
|
|
<script setup lang="ts">
|
|
import { ButtonGroup, Button } from "@heroui-vue/components";
|
|
</script>
|
|
```
|
|
|
|
<ComponentPreview name="button-group-basic" />
|
|
|
|
## 组件结构
|
|
|
|
```vue
|
|
<script setup lang="ts">
|
|
import {
|
|
ButtonGroup,
|
|
Button,
|
|
ButtonGroupSeparator,
|
|
} from "@heroui-vue/components";
|
|
</script>
|
|
|
|
<template>
|
|
<ButtonGroup>
|
|
<Button>First</Button>
|
|
<Button>
|
|
<ButtonGroupSeparator />
|
|
Second
|
|
</Button>
|
|
<Button>
|
|
<ButtonGroupSeparator />
|
|
Third
|
|
</Button>
|
|
</ButtonGroup>
|
|
</template>
|
|
```
|
|
|
|
> **ButtonGroup** 将多个 Button 组合在一起,应用一致的样式、间距与自动圆角处理。它通过 provide/inject 向直接子按钮传递 `size`、`variant` 与 `isDisabled` 属性。
|
|
|
|
## 示例
|
|
|
|
### 变体
|
|
|
|
<ComponentPreview name="button-group-variants" />
|
|
|
|
### 尺寸
|
|
|
|
<ComponentPreview name="button-group-sizes" />
|
|
|
|
### 方向
|
|
|
|
使用 `orientation` 属性可水平或垂直排列按钮。
|
|
|
|
<ComponentPreview name="button-group-orientation" />
|
|
|
|
### 带图标
|
|
|
|
<ComponentPreview name="button-group-with-icons" />
|
|
|
|
### 宽度充满
|
|
|
|
<ComponentPreview name="button-group-full-width" />
|
|
|
|
### 禁用状态
|
|
|
|
<ComponentPreview name="button-group-disabled" />
|
|
|
|
### 无分隔线
|
|
|
|
省略按钮中的 `<ButtonGroupSeparator />` 组件即可。
|
|
|
|
<ComponentPreview name="button-group-without-separator" />
|
|
|
|
## 自定义样式
|
|
|
|
### Tailwind CSS
|
|
|
|
<ComponentPreview name="button-group-custom-styles" />
|
|
|
|
### 全局 CSS
|
|
|
|
可使用 `@layer components` 指令自定义 ButtonGroup 组件类。
|
|
|
|
```css
|
|
@layer components {
|
|
.hv-button-group {
|
|
@apply gap-2 rounded-lg;
|
|
}
|
|
|
|
.hv-button-group__separator {
|
|
@apply opacity-25;
|
|
}
|
|
}
|
|
```
|
|
|
|
## 样式参考
|
|
|
|
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
|
|
|
### CSS 类
|
|
|
|
ButtonGroup 组件使用以下 CSS 类:
|
|
|
|
#### 基础类 [!toc]
|
|
|
|
- `.hv-button-group` - 按钮组根容器
|
|
- `.hv-button-group--full-width` - 全宽修饰符
|
|
- `.hv-button-group__separator` - 按钮之间的分隔元素
|
|
|
|
ButtonGroup 组件自动为按钮应用圆角:
|
|
|
|
- 第一个按钮圆角左/起始边
|
|
- 最后一个按钮圆角右/结束边
|
|
- 中间按钮无圆角
|
|
- 单个按钮四边全圆角
|
|
|
|
在每个 Button 内(第一个除外)添加 `<ButtonGroupSeparator />` 以显示分隔线。
|
|
|
|
## API 参考
|
|
|
|
### ButtonGroup
|
|
|
|
| Prop | 类型 | 默认值 | 描述 |
|
|
| ------------- | --------------------------------------------------------------------------------------------- | -------------- | -------------------------------------------- |
|
|
| `variant` | `'primary' \| 'secondary' \| 'tertiary' \| 'outline' \| 'ghost' \| 'danger' \| 'danger-soft'` | - | 应用于组内所有直接子按钮的视觉变体 |
|
|
| `size` | `'sm' \| 'md' \| 'lg'` | - | 应用于组内所有直接子按钮的尺寸 |
|
|
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | 按钮组方向 |
|
|
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
|
|
| `isDisabled` | `boolean` | `false` | 是否禁用组内所有直接子按钮(单个按钮可覆盖) |
|
|
| `as` | `string` | `'div'` | 渲染的根元素 |
|
|
| `class` | `string` | - | 附加 CSS 类 |
|
|
|
|
### ButtonGroupSeparator
|
|
|
|
| Prop | 类型 | 默认值 | 描述 |
|
|
| ------- | -------- | ------ | ----------- |
|
|
| `class` | `string` | - | 附加 CSS 类 |
|
|
|
|
## 说明
|
|
|
|
- ButtonGroup 通过 provide/inject 向**直接子 Button** 传递 `size`、`variant` 与 `isDisabled` 属性
|
|
- **仅直接子按钮接收 ButtonGroup 属性** - 嵌套在其他组件(如 Modal、Dropdown 等)内的 Button 即使位于 ButtonGroup 后代也不会继承组属性
|
|
- 单个 Button 可通过自身属性覆盖组的默认值
|
|
- 组件自动处理按钮之间的圆角
|
|
- 在每个 Button 内(第一个除外)添加 `<ButtonGroupSeparator />`,可在按钮之间显示分隔线
|
|
- 组内按钮移除 active/pressed 缩放变换,外观更统一
|
|
|
|
## 相关组件
|
|
|
|
- [Button 按钮](./button)
|
|
- [ToggleButton 切换按钮](./toggle-button)
|
|
- [ToggleButtonGroup 切换按钮组](./toggle-button-group)
|