开关
功能特性
- 支持全键盘导航
- 支持受控与非受控模式
安装
通过命令行安装该组件。
$ npm add reka-ui结构组成
导入组件。
<script setup>
import { Toggle } from 'reka-ui'
</script>
<template>
<Toggle />
</template>API 参考
根元素
开关组件。
| 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 pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state. | |
disabled | false | booleanWhen |
modelValue | boolean | nullThe controlled pressed state of the toggle. Can be bind as | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
required | booleanWhen |
| Emit | Payload |
|---|---|
update:modelValue | [value: boolean]Event handler called when the value of the toggle changes. |
| Slots (default) | Payload |
|---|---|
modelValue | booleanCurrent value |
state | 'on' | 'off'Current state |
pressed | booleanCurrent pressed state |
disabled | booleanCurrent disabled state |
| Data Attribute | Value |
|---|---|
[data-state] | "开启" | "关闭" |
[data-disabled] | 禁用时出现 |
无障碍访问
键盘交互
| Key | Description |
|---|---|
空格键 | 激活/停用开关 |
回车键 | 激活/停用开关 |
