Switch 开关
功能特性
- 支持完整的键盘导航。
- 可受控或非受控使用。
安装
通过命令行安装该组件。
$ npm add reka-ui结构
导入所有部件并组装在一起。
<script setup>
import { SwitchRoot, SwitchThumb } from 'reka-ui'
</script>
<template>
<SwitchRoot>
<SwitchThumb />
</SwitchRoot>
</template>API 参考
Root 根组件
包含开关的所有组成部分。在 form 表单内使用时,还会渲染一个 input 元素以确保事件正确传播。
| Prop | Default | Type |
|---|---|---|
as | 'button' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultValue | booleanThe state of the switch when it is initially rendered. Use when you do not need to control its state. | |
disabled | booleanWhen | |
id | string | |
modelValue | boolean | nullThe controlled state of the switch. Can be bind as | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
required | booleanWhen | |
value | 'on' | stringThe value given as data when submitted with a |
| Emit | Payload |
|---|---|
update:modelValue | [payload: boolean]Event handler called when the value of the switch changes. |
| Slots (default) | Payload |
|---|---|
modelValue | booleanCurrent value |
| Data Attribute | Value |
|---|---|
[data-state] | "checked(已选中)" | "unchecked(未选中)" |
[data-disabled] | 禁用时出现 |
Thumb 开关滑块
用于视觉指示开关开启或关闭状态的滑块。
| Prop | Default | Type |
|---|---|---|
as | 'span' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Data Attribute | Value |
|---|---|
[data-state] | "checked(已选中)" | "unchecked(未选中)" |
[data-disabled] | 禁用时出现 |
无障碍访问
遵循 switch 角色要求。
键盘交互
| Key | Description |
|---|---|
空格键 | 切换组件的状态。 |
回车键 | 切换组件的状态。 |
