For AI agents: the complete documentation index is available at https://a3s-lab.github.io/UI/llms.txt, the full documentation bundle is available at https://a3s-lab.github.io/UI/llms-full.txt, and this page is available as Markdown at https://a3s-lab.github.io/UI/components/switch.md.

开关

开关立即切换一个独立设置的开启或关闭状态,不用于需要提交表单后才生效的选择。若远程更新可能失败,控件应保持可交互,播报进行中或被拒绝的结果,并在拒绝时恢复先前值。

飞行模式

飞行模式已关闭。

用法

引入 CSS

导入 Tailwind 和一个完整的 A3S UI 样式包。

@import "tailwindcss";
@import "@a3s-lab/ui/a3s.css";

或者仅导入基本 CSS、Switch 组件 CSS 和一个样式包。

@import "tailwindcss";
@import "@a3s-lab/ui/base.css";
@import "@a3s-lab/ui/components/switch.css";
@import "@a3s-lab/ui/styles/a3s.css";

使用CDN或批量程序导入?请参见安装页。

添加开关 HTML

Add class="input" and role="switch" to a native checkbox. Use a native <label> or aria-label for accessible naming.

<input type="checkbox" role="switch" class="input" />

HTML 结构

<input type="checkbox" role="switch" class="input">
The switch control. Use native checkbox state: add checked for an initially enabled switch, disabled for a disabled switch, and aria-invalid="true" for invalid state.
data-size="sm" Optional
Renders the smaller switch size.

示例

说明

实时预览

Focus is shared across devices, and turns off when you leave the app.

卡片式选项

需要点击整张卡片来切换开关时,请使用 <label> 包裹卡片。

实时预览

远程接受与拒绝

持久化由宿主负责。这些固定示例证明:接受时保留新值,拒绝时恢复先前值。

同步专注设置

此示例始终拒绝远程更新并恢复先前值。

准备同步。

此示例始终接受远程更新。

准备同步。

已禁用

当周围字段应使用禁用样式时,将 disabled 添加到开关,将 data-disabled="true" 添加到字段包装器。保留标签与原因可见。

实时预览

仅组织所有者可以更改此设置。

无效

将 aria-invalid="true" 添加到开关,并将 data-invalid="true" 添加到字段包装器。

实时预览

必须接受条款与条件才能继续。

尺寸

使用data-size="sm"渲染较小的开关尺寸。

实时预览

RTL

切换字段布局使用文档方向。在字段或祖先上设置 dir="rtl"。

实时预览

يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.