开关
基于 zw-switch 基础组件构建的样式化开关。
@zeus-web/switch数据录入zweb add switch自动生成
交互演示
源码
请选择与你的应用框架匹配的入口。
html
<script type="module">
import '@zeus-web/switch/wc/auto'
</script>
<zw-switch default-checked>Dark mode</zw-switch>tsx
import { Switch } from '@zeus-web/switch/react'
export function SwitchExample() {
return <Switch defaultChecked>Dark mode</Switch>
}vue
<script setup lang="ts">
import { Switch } from '@zeus-web/switch/vue'
</script>
<template>
<Switch default-checked>Dark mode</Switch>
</template>添加
bash
zweb add switch导入
样式化 Registry 组件:
tsx
import { Switch } from '@/components/ui/switch'基础组件 React 包装器:
tsx
import { Switch } from '@zeus-web/switch/react'Web Component 入口:
ts
import '@zeus-web/switch/wc/auto'属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
checked | boolean | - | 受控选中状态。 |
defaultChecked | boolean | - | 初始选中状态。 |
size | SwitchSize可选值: sm, md, lg | md | 开关尺寸。 |
required | boolean | - | 将开关标记为必填。 |
ariaLabel | string | - | 无可见标签开关的无障碍标签。 |
ariaDescribedby | string | - | 额外无障碍说明的 ID 引用。 |
事件
| 事件 | React 属性 | 详情 | 说明 |
|---|---|---|---|
checked-change | onCheckedChange | checked: boolean, nativeEvent: Event | 选中状态变化时触发。 |
插槽
| 插槽 | 说明 |
|---|---|
default | 标签内容。 |
样式
使用 Tailwind: 是
主题变量:
bg-primarybg-input
内部选择器:
[&_[data-slot=switch-track]][&_[data-slot=switch-thumb]]
Registry
Registry 源码:可用。
添加命令: zweb add switch
类型: component
依赖:
@zeus-web/switchclass-variance-authority
Registry 依赖:
cnglobals
文件:
templates/react/switch.tsx→components/ui/switch.tsxtemplates/vue/switch.ts→components/ui/switch.ts
AI 使用规则
建议:
- 开启或关闭设置使用 Switch。
- 字段属于多选列表时应改用 Checkbox。
避免:
- 不要用 Switch 提交立即执行的破坏性操作。
示例
React 样式化用法
tsx
import { Switch } from '@/components/ui/switch'
export function Example() {
return <Switch>Enable notifications</Switch>
}