Back to Heroui

Switch 开关

apps/docs/content/docs/cn/react/components/(controls)/switch.mdx

3.2.47.0 KB
Original Source

用法

tsx
import { Switch, SwitchGroup, Label } from '@heroui/react';

<ComponentPreview name="switch-basic" />

组件结构

tsx
import { Switch, Label, Description } from '@heroui/react';

export default () => (
  <Switch>
    <Switch.Control>
      <Switch.Thumb>
        <Switch.Icon/>
      </Switch.Thumb>
    </Switch.Control>
    <Switch.Content>
      <Label />
      <Description />
    </Switch.Content>
  </Switch>
);

要对多个 Switch 进行分组,请使用 SwitchGroup 组件:

tsx
import { Switch, SwitchGroup, Label } from '@heroui/react';

export default () => (
  <SwitchGroup>
    <Switch>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Label>Option 1</Label>
    </Switch>
    <Switch>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Label>Option 2</Label>
    </Switch>
  </SwitchGroup>
);

示例

尺寸

<ComponentPreview name="switch-sizes" />

带图标

<ComponentPreview name="switch-with-icons" />

禁用

<ComponentPreview name="switch-disabled" />

无标签

<ComponentPreview name="switch-without-label" />

带描述

<ComponentPreview name="switch-with-description" />

默认选中

<ComponentPreview name="switch-default-selected" />

受控组件

<ComponentPreview name="switch-controlled" />

标签位置

<ComponentPreview name="switch-label-position" />

分组

<ComponentPreview name="switch-group" />

横向分组

<ComponentPreview name="switch-group-horizontal" />

表单集成

<ComponentPreview name="switch-form" />

渲染属性

<ComponentPreview name="switch-render-props" />

渲染函数

<ComponentPreview name="switch-render-function" />

自定义样式

Tailwind CSS

<ComponentPreview name="switch-custom-styles" />

全局 CSS

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

css
@layer components {
  .switch {
    @apply inline-flex gap-3 items-center;
  }

  .switch__control {
    @apply h-5 w-8 bg-gray-400 data-[selected=true]:bg-blue-500;
  }

  .switch__thumb {
    @apply bg-white shadow-sm;
  }

  .switch__content {
    @apply flex flex-col gap-1;
  }

  .switch__icon {
    @apply h-3 w-3 text-current;
  }
}

样式参考

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

CSS 类

Switch 类 [!toc]

Switch 组件使用以下 CSS 类(查看源码样式):

  • .switch - Switch 根容器(字段)
  • .switch__content - 包裹控件与标签文本的可点击 label
  • .switch__control - Switch 轨道
  • .switch__thumb - 可移动的滑块
  • .switch__icon - 滑块内可选图标
  • .switch--sm - 小尺寸变体
  • .switch--md - 中尺寸变体(默认)
  • .switch--lg - 大尺寸变体

SwitchGroup 类 [!toc]

SwitchGroup 组件使用以下 CSS 类(查看源码样式):

  • .switch-group - Switch 组容器
  • .switch-group__items - Switch 项容器
  • .switch-group--horizontal - 横向布局
  • .switch-group--vertical - 纵向布局(默认)

交互状态

该 Switch 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:

  • 已选中[data-selected="true"](滑块位置与背景色变化)
  • 悬停:hover[data-hovered="true"](作用于 Switch.Control / 按钮)
  • 聚焦:focus-visible[data-focus-visible="true"](在按钮上显示轨道焦点环)
  • 禁用[data-disabled="true"](降低透明度,包括帮助文本)
  • 按压:active[data-pressed="true"]

API 参考

Switch

继承自 React Aria SwitchField

Prop类型默认值描述
size'sm' | 'md' | 'lg''md'Switch 尺寸。
isSelectedbooleanfalseSwitch 是否打开。
defaultSelectedbooleanfalse默认是否打开(非受控)。
isDisabledbooleanfalseSwitch 是否禁用。
isInvalidbooleanfalseSwitch 是否无效。
isReadOnlybooleanfalseSwitch 是否只读。
isRequiredbooleanfalseSwitch 是否必须打开。
validate(value: boolean) => ValidationError | true | null | undefined-自定义校验函数。
validationBehavior'native' | 'aria''native'使用原生 HTML 校验或 ARIA 校验。
namestring-输入元素名称,用于提交 HTML 表单。
valuestring-输入元素值,用于提交 HTML 表单。
onChange(isSelected: boolean) => void-Switch 值变化时的事件处理函数。
onPress(e: PressEvent) => void-Switch 被按下时的事件处理函数。
childrenReact.ReactNode | (values: SwitchFieldRenderProps) => React.ReactNode-Switch 内容或字段级渲染 prop。
renderDOMRenderFunction<keyof React.JSX.IntrinsicElements, SwitchFieldRenderProps>-使用自定义渲染函数覆盖默认 DOM 元素。

Switch.Content

包裹控件与标签文本的可点击 <label>。请把 Switch.ControlLabel 放在它内部;Description/FieldError 作为 Switch.Content 的兄弟节点。对于没有标签的 switch,省略 Label 并在 Switch 上传入 aria-label

Prop类型默认值描述
childrenReact.ReactNode | (values: SwitchButtonRenderProps) => React.ReactNode-按钮内容(控件 + 标签),或按钮级渲染 prop
classNamestring | (values: SwitchButtonRenderProps) => string-应用到可点击 label 的类名

SwitchFieldRenderProps

在根 Switch 上使用渲染 prop 时,提供以下字段级值:

Prop类型描述
isSelectedbooleanSwitch 当前是否打开。
isDisabledbooleanSwitch 是否禁用。
isReadOnlybooleanSwitch 是否只读。
isInvalidbooleanSwitch 是否无效。
isRequiredbooleanSwitch 是否必填。
stateToggleStateSwitch 的状态。

SwitchButtonRenderProps

Switch.Control 使用按钮级渲染 prop(isHoveredisPressedisFocusVisible 等)。将函数作为 Switch.Control 的子元素即可访问。

SwitchGroup

Prop类型默认值描述
orientation'horizontal' | 'vertical''vertical'Switch 组方向。
childrenReact.ReactNode-要渲染的 Switch 项。
classNamestring-额外的 CSS 类。

相关组件

<RelatedComponents component="switch" />