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

5.3 KiB
Raw Permalink Blame History

title, description, component, group
title description component group
ToggleButton 切换按钮 用于在开启/关闭或已选中/未选中状态之间切换的交互式切换控件。 toggle-button 组件

用法

<script setup lang="ts">
import { ToggleButton } from "@heroui-vue/components";
</script>

示例

变体

仅图标

尺寸

禁用

受控组件

自定义样式

Tailwind CSS

全局 CSS

若要自定义组件类,可使用 @layer components 指令。

@layer components {
  .hv-toggle-button {
    @apply bg-purple-500 text-white;
  }

  .hv-toggle-button--icon-only {
    @apply rounded-lg;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

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 或 [aria-disabled="true"](降低透明度,禁用指针事件;组件同时输出 [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 与作用域插槽适配

相关组件

Button 按钮 · Switch 开关 · Checkbox 复选框