JGuang

开关

Switch · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

开关 · Switch

开关用于表单输入场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 开关 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 开关 · 禁用状态 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 开关 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 开关 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 开关 · 状态 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Switch

A toggle control for on/off states that take effect immediately. Supports labels, descriptions, loading states, and validation. Use it for settings or preferences that apply instantly. For changes requiring a form submission, use a checkbox instead.

属性类型默认值说明
refReact.Ref<HTMLInputElement>转发至底层 <input> 元素的 ref。
label *string开关的标签文本(始终渲染以确保无障碍性)。
value *boolean开关是开启还是关闭。
onChange(checked: boolean, e: ChangeEvent<HTMLInputElement>) => void开关状态变化时触发的回调。
changeAction(checked: boolean, e: ChangeEvent<HTMLInputElement>) => void | Promise<void>在 onChange 之后触发的异步操作。触发乐观 UI 并显示加载旋转器直到 Promise 完成。
isLoadingbooleanfalse开关是否处于加载状态,在滑块内显示旋转器。
isLabelHiddenbooleanfalse视觉上隐藏标签,同时保持屏幕阅读器的无障碍性。
descriptionstring显示在标签下方的描述文本。
isDisabledbooleanfalse开关是否被禁用。
size'sm' | 'md''md'Size variant controlling track and thumb dimensions. sm (32x20px) matches sm checkbox/radio vertical rhythm; md (40x24px, default) matches md checkbox/radio vertical rhythm.
htmlNamestring底层复选框输入的 HTML name 属性,用于表单提交(开启时提交 "on")。
disabledMessagestringExplains why the switch is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the switch focusable via aria-disabled (toggling stays blocked). Use this instead of wrapping a disabled Switch in Tooltip. Disabled controls swallow the hover events an external Tooltip needs.
isOptionalbooleanfalse字段是否为可选。与 isRequired 互斥。
isRequiredbooleanfalse开关是否为必填。与 isOptional 互斥。
status{type: 'warning' | 'error' | 'success', message?: string}带类型和消息的状态指示器。在开关下方显示彩色消息框,当类型为 "error" 时设置 aria-invalid。
onFocus(e: FocusEvent<HTMLInputElement>) => void开关获得焦点时触发的回调。
onBlur(e: FocusEvent<HTMLInputElement>) => void开关失去焦点时触发的回调。
labelIconIconType显示在标签文本前面的图标。
labelTooltipstring在标签末尾的信息图标中显示的工具提示文本。
labelPosition'start' | 'end''end'标签出现在开关的哪一侧。"start" 将标签放在开关前面。
labelSpacing'hug' | 'spread''hug'标签和开关之间的间距行为。"hug" 将它们并排放置;"spread" 将它们推到容器的两端(全宽)。"default" 是 "hug" 的已弃用别名。
widthSizeValueWidth of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned.

使用建议

  • Use for settings that apply immediately; the toggle should take effect without a separate save action.
  • Pair with a clear, concise label that describes the setting being controlled.
  • 避免:Use for options that require a form submission to take effect; use a checkbox instead.
  • 避免:Use a switch for multi-state values; it's strictly on/off.
  • 避免:Wrap a disabled switch in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.

主题 API

{
  "targets": [
    {
      "className": "astryx-switch",
      "visualProps": [
        "size"
      ],
      "states": [
        "checked",
        "disabled"
      ]
    },
    {
      "className": "astryx-switch-thumb",
      "visualProps": [
        "size"
      ],
      "states": [
        "checked"
      ]
    },
    {
      "className": "astryx-switch-field",
      "visualProps": [
        "labelPosition",
        "labelSpacing"
      ]
    },
    {
      "className": "astryx-switch-label"
    }
  ]
}

无障碍

  • Track and thumb: The on and off tracks must each have at least 3:1 contrast with the surface behind them, and the thumb with its track. For Hover and Pointer down, measure the final colors after the tint and the pressed overlay are applied. — 3:1

组件结构

  • Field: Container arranging the switch, label, and feedback.
  • Track: Pill-shaped surface that shows the off or on state.
  • Thumb: Indicator that moves across the track when state changes.
  • Label: Text identifying the setting controlled by the switch.
  • Description: Helper text below the label.
  • Spinner: Loading indicator shown inside the thumb while busy.
  • Status message: Error, warning, or success message below the switch.