Switch 开关
基于 reka-ui 的 Switch 原语构建的新粗野主义风格滑动开关,采用粗边框、固定硬阴影与实心滑钮,支持 v-model。
预览
滑动开关
状态标签与切换音效
自定义状态标签
尺寸矩阵 (sm / default / lg)
原生表单
禁用状态
安装
pnpm dlx brutx-vue@latest add switch用法
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'
const enabled = ref(false)
</script>
<template>
<div class="flex items-center gap-3">
<Switch id="airplane-mode" v-model="enabled" />
<Label for="airplane-mode">飞行模式</Label>
</div>
</template>非受控用法通过 defaultValue 设置挂载初值:
<Switch :default-value="true" aria-label="接收通知" />升级时,将 defaultChecked 改为 defaultValue,并移除 shape。自定义开关时,轨道使用 switchRootVariants({ variant, size }),滑钮调用 switchThumbVariants(),尺寸由轨道决定。两个函数均从 brutx-ui-vue/switch 导出。
状态标签
开启 showLabels 可在滑钮内显示当前状态,内置标签为开启 I、关闭 O。通过 checkedLabel 和 uncheckedLabel 可自定义文字或符号;未提供时使用 locale 的 switch.checkedLabel 和 switch.uncheckedLabel 文案。
滑钮空间有限,建议使用“开 / 关”或“✓ / ×”等单字符标签,较长说明放在外侧 Label。标签颜色与滑钮背景配对,并通过 aria-hidden 隐藏装饰文本;无障碍状态由 aria-checked 表达,控件名称通过 ariaLabel 设置。
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'
const power = ref(true)
const sync = ref(false)
</script>
<template>
<div class="flex items-center gap-3">
<Switch id="device-power" v-model="power" show-labels />
<Label for="device-power">设备主电源</Label>
</div>
<div class="flex items-center gap-3">
<Switch id="auto-sync" v-model="sync" show-labels checked-label="开" unchecked-label="关" />
<Label for="auto-sync">自动同步</Label>
</div>
</template>切换动效
外框和硬阴影在悬停、按住及切换状态时保持固定。滑钮使用 180ms 的 ease-brutal-snap 缓动横向移动,轨道使用 120ms 渐变表达状态;键盘焦点环立即显示。
系统开启“减弱动态效果”(prefers-reduced-motion: reduce)时,滑钮位置和轨道颜色直接切换。
禁用状态
<template>
<Switch disabled />
</template>自定义无障碍标签
通过 id 与 Label 的 for 属性关联可见名称,也可以使用包裹式 Label。点击关联标签即可切换开关。使用 ariaLabelledby 引用其他名称元素时,其优先级最高;显式 ariaLabel 用于设置独立名称。没有显式名称和关联标签时,才通过 locale 回退到“开关”。
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'
const sync = ref(false)
</script>
<template>
<div class="flex items-center gap-3">
<Switch v-model="sync" aria-label="自动同步数据" />
<Label>自动同步</Label>
</div>
</template>原生表单
设置 name 后,开关开启且未禁用时以 name/value 随所属原生 <form> 提交,value 缺省为 on。关闭或禁用的字段不提交。required 与 name 配合,要求开关开启后才通过原生必填校验。
<script setup>
import { Button, Switch, Label } from 'brutx-ui-vue'
</script>
<template>
<form>
<Switch id="notifications" name="notifications" value="enabled" :default-value="true" required />
<Label for="notifications">接收通知</Label>
<Button type="submit">提交</Button>
<Button type="reset">重置</Button>
</form>
</template>原生表单重置会将非受控开关恢复为挂载时的 defaultValue,不播放切换音效。受控状态继续由 v-model 决定;应用需要重置绑定值时,在表单的 reset 处理器中更新该值。取消 reset 事件会保留当前状态。
API 参考
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
class | string | undefined | undefined | 追加到 Switch 原语根元素的 CSS 类。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
modelValue可空 | boolean | null | undefined | undefined | 受控开关状态,通过 v-model 绑定;传入 null 时按关闭状态处理。 |
详情声明类型boolean | null | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
defaultValue | boolean | undefined | undefined | 非受控模式的挂载初值;所属原生表单重置时恢复此值。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
disabled | boolean | undefined | false | 禁用开关交互。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
variant | "accent" | "danger" | "default" | "primary" | "secondary" | undefined | "default" | 设置开关的颜色和状态样式变体。 |
详情声明类型NonNullable<"default" | "primary" | "secondary" | "danger" | "accent" | null | undefined> | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | 设置开关轨道和滑块的尺寸。 |
详情声明类型NonNullable<"default" | "lg" | "sm" | null | undefined> | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
showLabels | boolean | undefined | false | 在滑钮内显示当前状态标签,内容由 checkedLabel 和 uncheckedLabel 设置;标签仅为装饰并从无障碍树中隐藏。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
checkedLabel | string | undefined | undefined | 开启状态标签,仅在 showLabels 为 true 时显示;未提供时使用 locale 文案,内置默认为 I。建议使用单字符。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
uncheckedLabel | string | undefined | undefined | 关闭状态标签,仅在 showLabels 为 true 时显示;未提供时使用 locale 文案,内置默认为 O。建议使用单字符。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
id | string | undefined | undefined | 控件 ID,通过 Label 的 for 属性关联可见名称。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
name | string | undefined | undefined | 表单字段名;开启且未禁用时随所属原生 form 提交。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
value | string | undefined | undefined | 开启时提交的字符串值;未提供时使用原语默认值 on。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
required | boolean | undefined | false | 要求开关开启后才能通过所属原生表单的必填校验;需同时设置 name。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
ariaLabelledby | string | undefined | undefined | 关联名称元素的 ID,作为优先的无障碍名称来源。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
ariaLabel | string | undefined | undefined | 显式设置无障碍名称;缺少显式名称和关联 Label 时才使用当前语言的默认文案。 |
详情声明类型string | undefined 默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
sound | boolean | undefined | false | 显式启用切换状态时的继电器音效;默认不播放。 |
详情声明类型boolean | undefined 字面量
默认值详情
来源packages/ui/src/components/switch/Switch.vue | |||
| 名称 | 签名或类型 | 说明 |
|---|---|---|
@update:modelValue | [value: boolean] | 用户切换状态后发出新的布尔值,用于同步 v-model。 |
详情声明类型[value: boolean] 字面量
来源packages/ui/src/components/switch/Switch.vue | ||
可访问性
- 键盘操作:支持
Space/Enter切换开关状态 - ARIA 属性:自动管理
role="switch"、aria-checked和aria-required;支持关联可见 Label、ariaLabelledby与显式ariaLabel - 焦点管理:可通过 Tab 键聚焦,使用
--brutal-ring令牌显示可见聚焦环 - 动效降级:尊重
prefers-reduced-motion,关闭滑钮和轨道过渡 - 标签隔离:
showLabels状态标签使用aria-hidden="true",由aria-checked提供开关状态