backdrop
组件

开关

一种可以打开或关闭的双态按钮。

功能特性

  • 支持全键盘导航
  • 支持受控与非受控模式

安装

通过命令行安装该组件。

sh
$ npm add reka-ui

结构组成

导入组件。

vue
<script setup>
import { Toggle } from 'reka-ui'
</script>

<template>
  <Toggle />
</template>

API 参考

根元素

开关组件。

PropDefaultType
as
'button'
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

defaultValue
boolean

The pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state.

disabled
false
boolean

When true, prevents the user from interacting with the toggle.

modelValue
boolean | null

The controlled pressed state of the toggle. Can be bind as v-model.

name
string

The name of the field. Submitted with its owning form as part of a name/value pair.

required
boolean

When true, indicates that the user must set the value before the owning form can be submitted.

EmitPayload
update:modelValue
[value: boolean]

Event handler called when the value of the toggle changes.

Slots (default)Payload
modelValue
boolean

Current value

state
'on' | 'off'

Current state

pressed
boolean

Current pressed state

disabled
boolean

Current disabled state

Data AttributeValue
[data-state]"开启" | "关闭"
[data-disabled]禁用时出现

无障碍访问

键盘交互

KeyDescription
空格键
激活/停用开关
回车键
激活/停用开关