Files
heroui-vue/apps/docs/components/toggle-button.md
T

140 lines
5.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: ToggleButton 切换按钮
description: 用于在开启/关闭或已选中/未选中状态之间切换的交互式切换控件。
component: toggle-button
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { ToggleButton } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="toggle-button-basic" />
## 示例
### 变体
<ComponentPreview name="toggle-button-variants" />
### 仅图标
<ComponentPreview name="toggle-button-icon-only" />
### 尺寸
<ComponentPreview name="toggle-button-sizes" />
### 禁用
<ComponentPreview name="toggle-button-disabled" />
### 受控组件
<ComponentPreview name="toggle-button-controlled" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="toggle-button-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-toggle-button {
@apply bg-purple-500 text-white;
}
.hv-toggle-button--icon-only {
@apply rounded-lg;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
ToggleButton 组件使用以下 CSS 类:
#### 基础与尺寸类 [!toc]
- `.hv-toggle-button` - 切换按钮基础样式
- `.hv-toggle-button--sm` - 小尺寸变体
- `.hv-toggle-button--md` - 中尺寸变体(默认)
- `.hv-toggle-button--lg` - 大尺寸变体
#### 变体类 [!toc]
- `.hv-toggle-button--default` - 默认变体(填充背景)
- `.hv-toggle-button--ghost` - 幽灵变体(透明背景)
#### 修饰符类 [!toc]
- `.hv-toggle-button--icon-only` - 仅图标切换按钮
- `.hv-toggle-button--icon-only.hv-toggle-button--sm` - 小尺寸仅图标
- `.hv-toggle-button--icon-only.hv-toggle-button--lg` - 大尺寸仅图标
### 交互状态
该切换按钮同时支持 CSS 伪类与 data 属性,以便灵活控制状态:
- **已选中**:`[data-selected="true"]`(强调色背景与前景)
- **悬停**`:hover` 或 `[data-hovered="true"]`
- **激活/按下**:`:active` 或 `[data-pressed="true"]`(包含缩放变换)
- **聚焦**:`:focus-visible` 或 `[data-focus-visible="true"]`(显示焦点环)
- **禁用**:`:disabled` 或 `[data-disabled="true"]`(降低透明度,禁用指针事件)
## API 参考
### ToggleButton
| Prop | 类型 | 默认值 | 描述 |
| ---------------------------------- | ---------------------- | ----------- | --------------------------------------------- |
| `variant` | `'default' \| 'ghost'` | `'default'` | 视觉样式变体 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 切换按钮尺寸 |
| `isIconOnly` | `boolean` | `false` | 按钮是否仅包含图标 |
| `v-model` / `modelValue` | `boolean` | - | 受控的已选中状态 |
| `isSelected` | `boolean` | - | 受控的已选中状态(与 `modelValue` 等价别名) |
| `defaultValue` / `defaultSelected` | `boolean` | `false` | 默认已选中状态(非受控) |
| `isDisabled` | `boolean` | `false` | 是否禁用切换按钮 |
| `id` | `string \| number` | - | 组内成员键,对应 ToggleButtonGroup 的选中 key |
| `value` | `string \| number` | - | 与 `id` 等价的组内成员键 |
| `asChild` | `boolean` | `false` | 将属性与交互合并到默认插槽中唯一的原生 button |
### 事件
| 事件 | 参数 | 描述 |
| ------------------- | --------- | --------------------------------------------------- |
| `update:modelValue` | `boolean` | 已选中状态变化时触发 |
| `change` | `boolean` | 已选中状态变化时触发(与 `update:modelValue` 同时) |
### 作用域插槽
| 名称 | 参数 | 描述 |
| --------- | ----------------------------------------------------------------------------- | ------------------ |
| `default` | `{ isSelected, isPressed, isHovered, isFocused, isFocusVisible, isDisabled }` | 按钮内容与渲染状态 |
## 说明
- 独立使用时通过 `v-model` 或 `isSelected`/`change` 控制选中状态,对应上游 `isSelected`/`defaultSelected`/`onChange`
- 在 `ToggleButtonGroup` 内使用时,需提供唯一 `id`(或 `value`)作为选中 key;组内 `size` 可被继承
- `selectionMode="single"` 的组内按钮渲染为 `role="radio"` 并同步 `aria-checked`;其余情况为 `role="button"` 与 `aria-pressed`
- React Aria 的 `onPress`/`PressEvent` 与渲染函数 props 由 `@click` 与作用域插槽适配
## 相关组件
- [ToggleButtonGroup 切换按钮组](./toggle-button-group)
- [ButtonGroup 按钮组](./button-group)
- [Switch 开关](./switch)