Preview Updated 2026-05-10

Switch

Binary toggle — 3 sizes, loading and disabled states, native `role=switch` semantics preserved.

Basic usage

Built on <CfInput type="checkbox" role="switch">, so keyboard, form, and screen reader behavior all work out of the box.

<script setup lang="ts">
import { ref } from 'vue';
import { CfSwitch } from '@chufix-design/vue';

const wifi = ref(true);
</script>

<template>
<CfSwitch v-model="wifi">Wi-Fi {{ wifi ? 'on' : 'off' }}</CfSwitch>
</template>
import { useState } from 'react';
import { CfSwitch } from '@chufix-design/react';

export default function Demo() {
const [wifi, setWifi] = useState(true);
return (
  <CfSwitch checked={wifi} onChange={(e) => setWifi(e.target.checked)}>
    Wi-Fi {wifi ? 'on' : 'off'}
  </CfSwitch>
);
}

Sizes

<CfSwitch size="sm">Small</CfSwitch>
<CfSwitch size="md">Medium</CfSwitch>
<CfSwitch size="lg">Large</CfSwitch>
<CfSwitch size="sm">Small</CfSwitch>
<CfSwitch size="md">Medium</CfSwitch>
<CfSwitch size="lg">Large</CfSwitch>

States

loading shows a spinner inside the thumb and temporarily blocks toggling. disabled disables the whole switch.

<script setup lang="ts">
import { ref } from 'vue';
import { CfSwitch } from '@chufix-design/vue';

const sync = ref(false);
const loading = ref(false);

async function toggle(v: boolean) {
loading.value = true;
await new Promise((r) => setTimeout(r, 1000));
sync.value = v;
loading.value = false;
}
</script>

<template>
<CfSwitch :model-value="sync" :loading="loading" @change="toggle">
  Auto sync (1s async on click)
</CfSwitch>
<CfSwitch :model-value="true" disabled>Disabled · on</CfSwitch>
</template>
import { useState } from 'react';
import { CfSwitch } from '@chufix-design/react';

export default function Demo() {
const [sync, setSync] = useState(false);
const [loading, setLoading] = useState(false);

async function toggle(e) {
  setLoading(true);
  await new Promise((r) => setTimeout(r, 1000));
  setSync(e.target.checked);
  setLoading(false);
}

return (
  <>
    <CfSwitch checked={sync} loading={loading} onChange={toggle}>
      Auto sync (1s async on click)
    </CfSwitch>
    <CfSwitch checked disabled>Disabled · on</CfSwitch>
  </>
);
}

API

PropTypeDefaultDescription
size'sm' | 'md' | 'lg''md'Overall size
disabledbooleanfalseDisabled
loadingbooleanfalseShow spinner and block toggling

Two-way binding

  • Vue: v-model binds a boolean; @change receives the boolean.
  • React: controlled via checked + onChange; uncontrolled via defaultChecked. onChange is the native ChangeEvent.

反馈与讨论

Switch · Discussion

0
0 / 600
一键发送
正在加载评论...