Trên trang này

Công tắc

Cài Switch của shadcn-vue, các prop, và ví dụ trực tiếp.

Tab Ví dụ cho thấy switch bằng HTML và CSS thuần. Trong ứng dụng Vue có shadcn-vue, hãy cài component của registry: nó vẽ cùng một switch từ cùng các token switch.*.

Cài đặt

Chạy lệnh này trong ứng dụng, sau shadcn-vue init và theme Beehexa (Button › Code):

npx shadcn-vue@latest add https://beehexa.design/r/switch.json

Prop

Prop Giá trị Ghi chú
v-model boolean Bật hoặc tắt
size default, sm
disabled boolean Màu vô hiệu, không dùng độ mờ

Thiết lập có hiệu lực ngay

SwitchSettings.vue
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@/components/ui/switch'

const settings = ref([
  { id: 'sync-orders', label: 'Tự động đồng bộ đơn hàng', on: true },
  { id: 'sync-stock', label: 'Cập nhật tồn kho mỗi 15 phút', on: false },
  { id: 'email-failures', label: 'Gửi email khi đồng bộ lỗi', on: true },
])
</script>

<template>
  <ul class="m-0 grid list-none gap-(--bh-space-150) p-0">
    <li v-for="setting in settings" :key="setting.id" class="flex items-center gap-(--bh-space-100)">
      <Switch :id="setting.id" v-model="setting.on" />
      <label :for="setting.id" class="text-(--bh-color-text) [font:var(--bh-font-body-default)]">{{ setting.label }}</label>
    </li>
  </ul>
</template>

Vô hiệu

Hãy bật đồng bộ đơn hàng trước.

Chỉ quản trị viên workspace mới đổi được thiết lập này.

SwitchDisabled.vue
<script setup lang="ts">
import { Switch } from '@/components/ui/switch'
</script>

<template>
  <div class="grid gap-(--bh-space-150)">
    <div class="grid gap-(--bh-space-050)">
      <div class="flex items-center gap-(--bh-space-100)">
        <Switch id="sync-inventory" disabled aria-describedby="sync-inventory-why" />
        <label for="sync-inventory" class="text-(--bh-color-text-disabled) [font:var(--bh-font-body-default)]">Đồng bộ tồn kho</label>
      </div>
      <p id="sync-inventory-why" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Hãy bật đồng bộ đơn hàng trước.</p>
    </div>
    <div class="grid gap-(--bh-space-050)">
      <div class="flex items-center gap-(--bh-space-100)">
        <Switch id="sync-orders-locked" :model-value="true" disabled aria-describedby="sync-orders-locked-why" />
        <label for="sync-orders-locked" class="text-(--bh-color-text-disabled) [font:var(--bh-font-body-default)]">Đồng bộ đơn hàng</label>
      </div>
      <p id="sync-orders-locked-why" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Chỉ quản trị viên workspace mới đổi được thiết lập này.</p>
    </div>
  </div>
</template>

Dựng trên

SwitchRoot và SwitchThumb của reka-ui, như Switch của shadcn-vue.