Skip to content

Toggle Button

开关按钮。按一下在开和关之间切。基于 Reka UI Toggle。有 icon 时用 AnimatedToggleIcon 做进出场。

Installation

pnpm dlx @weme-ui/weme-ui add toggle-button

Usage

Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex items-center">
    <ToggleButton label="Bookmark" icon="ri:bookmark-line" active-icon="ri:bookmark-fill" />
  </div>
</template>

Examples

Variants

Vueexamples/variants.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex flex-wrap gap-3 items-center">
    <ToggleButton label="Soft" />
    <ToggleButton label="Outline" variant="outline" />
    <ToggleButton label="Ghost" variant="ghost" />
  </div>
</template>

Sizes

Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex flex-wrap gap-3 items-center">
    <ToggleButton label="Small" size="sm" />
    <ToggleButton label="Medium" size="md" />
    <ToggleButton label="Large" size="lg" />
  </div>
</template>

With Icon

Vueexamples/with-icon.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex flex-wrap gap-3 items-center">
    <ToggleButton label="Mute" icon="ri:volume-mute-line" active-icon="ri:volume-up-line" />
    <ToggleButton
      icon="ri:heart-line"
      active-icon="ri:heart-fill"
      aria-label="Favorite"
      :ui="{
        icon: 'data-[active]:text-ruby',
      }"
    />
  </div>
</template>

Disabled

Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex flex-wrap gap-3 items-center">
    <ToggleButton label="Disabled" disabled />
  </div>
</template>

Unstyled

Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import ToggleButton from '../toggle-button.vue'
</script>

<template>
  <div class="flex flex-wrap gap-3 items-center">
    <ToggleButton label="Unstyled" unstyled />
  </div>
</template>

Props

PropTypeDefault
asAsTag | Component'button'
根节点渲染为的元素或组件(Reka UI Primitive)
AsTag | Component
'button'
asChildbooleanfalse
合并到子元素上渲染(Reka UI composition)
boolean
false
modelValueboolean | null-
受控开关状态;可用 v-model
boolean | null
-
defaultValueboolean-
非受控初始开关状态
boolean
-
variant'soft' | 'outline' | 'ghost''soft'
外观变体
'soft' | 'outline' | 'ghost'
'soft'
size'sm' | 'md' | 'lg''md'
尺寸;无 label 时为正方形
'sm' | 'md' | 'lg'
'md'
labelstring-
按钮文案;未传且无默认插槽时不渲染标签
string
-
iconIconifyIconProps['icon']-
未按下态图标(Iconify);有值时用 AnimatedToggleIcon
IconifyIconProps['icon']
-
activeIconIconifyIconProps['icon']-
按下态图标(Iconify);未传时按下态回退到 icon
IconifyIconProps['icon']
-
scalablebooleantrue
指针按下时是否缩放(禁用时不生效)
boolean
true
unstyledboolean-
移除样式变体类,仅保留结构
boolean
-
disabledbooleanfalse
是否禁用
boolean
false
namestring-
随表单提交的字段名
string
-
requiredboolean-
是否必填
boolean
-
classany-
根节点额外 class
any
-
uiPartial<ToggleButtonStyleSlots>-
覆盖 slots 样式(root / icon / label)
Partial<ToggleButtonStyleSlots>
-

Events

EventType
update:modelValuevalue: boolean
开关状态变更时触发(含 v-model);由 Reka Toggle 转发
value: boolean

Slots

SlotDescription
default自定义内容;作用域参数 { state, pressed }(state 为 'on' | 'off',pressed 为指针按下);未传时回退到图标与 label
自定义内容;作用域参数 { state, pressed }(state 为 'on' | 'off',pressed 为指针按下);未传时回退到图标与 label

Accessibility

默认渲染为原生 button(Reka UI Toggle / Primitive)。开关状态走 data-state(on / off)。指针按下时 aria-pressed(给缩放动效用)。根节点设置 data-slot="toggle-button"。禁用时带 disabled 与 data-disabled。有 name 且在表单里时,Reka 会再画一个隐藏的 checkbox 用来提交。仅图标、没有可见文案时请加 aria-label。

Keyboard Interactions

KeyDescription
Enter切换开关状态(as 为原生 button 时)
切换开关状态(as 为原生 button 时)
Space切换开关状态(as 为原生 button 时)
切换开关状态(as 为原生 button 时)

Files

  • src/components/toggle-button/toggle-button.vue
  • src/components/toggle-button/toggle-button.props.ts
  • src/components/toggle-button/toggle-button.style.ts