Switch
Usage
v-model 지시어를 사용하여 스위치의 확인된 상태를 제어합니다.
<script setup lang="ts">
const value = ref(true)
</script>
<template>
<USwitch v-model="value" />
</template><script setup lang="ts">
import { ref } from 'vue'
const value = ref(true)
</script>
<template>
<USwitch v-model="value" />
</template>상태를 제어할 필요가 없을 때 default-value prop을 사용하여 초기 값을 설정합니다.
<template>
<USwitch default-value />
</template>Label 태그
label prop을 사용하여 Switch의 레이블을 설정합니다.
<template>
<USwitch label="Check me" />
</template>required 소품을 사용할 때 레이블 옆에 별표가 추가됩니다.
<template>
<USwitch required label="Check me" />
</template>Description
description prop 를 사용하여 Switch 에 대한 설명을 설정합니다.
This is a checkbox.
<template>
<USwitch label="Check me" description="This is a checkbox." />
</template>Icon
checked-icon 및 unchecked-icon props를 사용하여 스위치 아이콘을 선택하거나 선택하지 않을 때 설정합니다.
<template>
<USwitch
unchecked-icon="i-lucide-x"
checked-icon="i-lucide-check"
default-value
label="Check me"
/>
</template>Loading 중
loading prop를 사용하여 스위치에 로드 아이콘을 표시합니다.
<template>
<USwitch loading default-value label="Check me" />
</template>loading 아이콘
loading-icon 소품을 사용하여 로드 아이콘을 사용자 정의합니다. 기본값은 i-lucide-loader-circle입니다.
<template>
<USwitch loading loading-icon="i-lucide-loader" default-value label="Check me" />
</template>Color 이미지
color Prop을 사용하여 스위치의 색상을 변경합니다.
<template>
<USwitch color="neutral" default-value label="Check me" />
</template>Size
size prop을 사용하여 스위치의 크기를 변경합니다.
<template>
<USwitch size="xl" default-value label="Check me" />
</template>Disabled 사용 안 함
disabled prop을 사용하여 스위치를 비활성화합니다.
<template>
<USwitch disabled label="Check me" />
</template>API 파일
Props (### Props)
| Prop | Default | Type |
|---|---|---|
as | 'div' | anyThe element or component this component should render as. |
color | 'primary' | "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" |
size | 'md' | "md" | "xs" | "sm" | "lg" | "xl" |
highlight | boolean Highlight the ring color like a focus state. | |
loading | boolean When | |
loadingIcon | appConfig.ui.icons.loading | anyThe icon when the |
checkedIcon | anyDisplay an icon when the switch is checked. | |
uncheckedIcon | anyDisplay an icon when the switch is unchecked. | |
label | string | |
description | string | |
disabled | boolean When | |
id | string | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
required | boolean When | |
value | stringThe value given as data when submitted with a | |
defaultValue | TThe state of the switch when it is initially rendered. Use when you do not need to control its state. | |
modelValue | null | TThe controlled state of the switch. Can be bind as | |
trueValue | TThe value used when the switch is on. Defaults to | |
falseValue | TThe value used when the switch is off. Defaults to | |
autofocus | false | true | "true" | "false" | |
ui | { root?: SlotClass; base?: SlotClass; container?: SlotClass; thumb?: SlotClass; icon?: SlotClass; wrapper?: SlotClass; label?: SlotClass; description?: SlotClass; } |
슬롯
| Slot | Type |
|---|---|
label | { label: string | undefined; } |
description | { description: string | undefined; } |
Emits
| Event | Type |
|---|---|
change | [event: Event] |
update:modelValue | [payload: T] |
Theme 테마
export default defineAppConfig({
ui: {
switch: {
slots: {
root: 'relative flex items-start',
base: [
'inline-flex items-center shrink-0 rounded-full border-2 border-transparent focus-visible:outline-3 data-[state=unchecked]:bg-accented',
'transition-[background] duration-200 ease-out'
],
container: 'flex items-center',
thumb: 'group pointer-events-none rounded-full bg-default shadow-lg ring-0 transition-transform duration-200 ease-out motion-reduce:transition-none data-[state=unchecked]:translate-x-0 data-[state=unchecked]:rtl:-translate-x-0 flex items-center justify-center',
icon: [
'absolute shrink-0 group-data-[state=unchecked]:text-dimmed opacity-0 size-10/12',
'transition-[color,opacity] duration-200 ease-out'
],
wrapper: 'ms-2',
label: 'block font-medium text-default',
description: 'text-muted'
},
variants: {
color: {
primary: {
base: 'data-[state=checked]:bg-primary outline-primary/25',
icon: 'group-data-[state=checked]:text-primary'
},
secondary: {
base: 'data-[state=checked]:bg-secondary outline-secondary/25',
icon: 'group-data-[state=checked]:text-secondary'
},
success: {
base: 'data-[state=checked]:bg-success outline-success/25',
icon: 'group-data-[state=checked]:text-success'
},
info: {
base: 'data-[state=checked]:bg-info outline-info/25',
icon: 'group-data-[state=checked]:text-info'
},
warning: {
base: 'data-[state=checked]:bg-warning outline-warning/25',
icon: 'group-data-[state=checked]:text-warning'
},
error: {
base: 'data-[state=checked]:bg-error outline-error/25',
icon: 'group-data-[state=checked]:text-error'
},
neutral: {
base: 'data-[state=checked]:bg-inverted outline-inverted/25',
icon: 'group-data-[state=checked]:text-highlighted'
}
},
size: {
xs: {
base: 'w-7',
container: 'h-4',
thumb: 'size-3 data-[state=checked]:translate-x-3 data-[state=checked]:rtl:-translate-x-3',
wrapper: 'text-xs'
},
sm: {
base: 'w-8',
container: 'h-4',
thumb: 'size-3.5 data-[state=checked]:translate-x-3.5 data-[state=checked]:rtl:-translate-x-3.5',
wrapper: 'text-xs'
},
md: {
base: 'w-9',
container: 'h-5',
thumb: 'size-4 data-[state=checked]:translate-x-4 data-[state=checked]:rtl:-translate-x-4',
wrapper: 'text-sm'
},
lg: {
base: 'w-10',
container: 'h-5',
thumb: 'size-4.5 data-[state=checked]:translate-x-4.5 data-[state=checked]:rtl:-translate-x-4.5',
wrapper: 'text-sm'
},
xl: {
base: 'w-11',
container: 'h-6',
thumb: 'size-5 data-[state=checked]:translate-x-5 data-[state=checked]:rtl:-translate-x-5',
wrapper: 'text-base'
}
},
checked: {
true: {
icon: 'group-data-[state=checked]:opacity-100'
}
},
unchecked: {
true: {
icon: 'group-data-[state=unchecked]:opacity-100'
}
},
loading: {
true: {
icon: 'animate-spin'
}
},
highlight: {
true: ''
},
required: {
true: {
label: "after:content-['*'] after:ms-0.5 after:text-error"
}
},
disabled: {
true: {
root: 'opacity-75',
base: 'cursor-not-allowed',
label: 'cursor-not-allowed',
description: 'cursor-not-allowed'
}
}
},
compoundVariants: [
{
color: 'primary',
highlight: true,
class: {
base: 'ring ring-primary'
}
},
{
color: 'neutral',
highlight: true,
class: {
base: 'ring ring-inverted'
}
}
],
defaultVariants: {
color: 'primary',
size: 'md'
}
}
}
})import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
switch: {
slots: {
root: 'relative flex items-start',
base: [
'inline-flex items-center shrink-0 rounded-full border-2 border-transparent focus-visible:outline-3 data-[state=unchecked]:bg-accented',
'transition-[background] duration-200 ease-out'
],
container: 'flex items-center',
thumb: 'group pointer-events-none rounded-full bg-default shadow-lg ring-0 transition-transform duration-200 ease-out motion-reduce:transition-none data-[state=unchecked]:translate-x-0 data-[state=unchecked]:rtl:-translate-x-0 flex items-center justify-center',
icon: [
'absolute shrink-0 group-data-[state=unchecked]:text-dimmed opacity-0 size-10/12',
'transition-[color,opacity] duration-200 ease-out'
],
wrapper: 'ms-2',
label: 'block font-medium text-default',
description: 'text-muted'
},
variants: {
color: {
primary: {
base: 'data-[state=checked]:bg-primary outline-primary/25',
icon: 'group-data-[state=checked]:text-primary'
},
secondary: {
base: 'data-[state=checked]:bg-secondary outline-secondary/25',
icon: 'group-data-[state=checked]:text-secondary'
},
success: {
base: 'data-[state=checked]:bg-success outline-success/25',
icon: 'group-data-[state=checked]:text-success'
},
info: {
base: 'data-[state=checked]:bg-info outline-info/25',
icon: 'group-data-[state=checked]:text-info'
},
warning: {
base: 'data-[state=checked]:bg-warning outline-warning/25',
icon: 'group-data-[state=checked]:text-warning'
},
error: {
base: 'data-[state=checked]:bg-error outline-error/25',
icon: 'group-data-[state=checked]:text-error'
},
neutral: {
base: 'data-[state=checked]:bg-inverted outline-inverted/25',
icon: 'group-data-[state=checked]:text-highlighted'
}
},
size: {
xs: {
base: 'w-7',
container: 'h-4',
thumb: 'size-3 data-[state=checked]:translate-x-3 data-[state=checked]:rtl:-translate-x-3',
wrapper: 'text-xs'
},
sm: {
base: 'w-8',
container: 'h-4',
thumb: 'size-3.5 data-[state=checked]:translate-x-3.5 data-[state=checked]:rtl:-translate-x-3.5',
wrapper: 'text-xs'
},
md: {
base: 'w-9',
container: 'h-5',
thumb: 'size-4 data-[state=checked]:translate-x-4 data-[state=checked]:rtl:-translate-x-4',
wrapper: 'text-sm'
},
lg: {
base: 'w-10',
container: 'h-5',
thumb: 'size-4.5 data-[state=checked]:translate-x-4.5 data-[state=checked]:rtl:-translate-x-4.5',
wrapper: 'text-sm'
},
xl: {
base: 'w-11',
container: 'h-6',
thumb: 'size-5 data-[state=checked]:translate-x-5 data-[state=checked]:rtl:-translate-x-5',
wrapper: 'text-base'
}
},
checked: {
true: {
icon: 'group-data-[state=checked]:opacity-100'
}
},
unchecked: {
true: {
icon: 'group-data-[state=unchecked]:opacity-100'
}
},
loading: {
true: {
icon: 'animate-spin'
}
},
highlight: {
true: ''
},
required: {
true: {
label: "after:content-['*'] after:ms-0.5 after:text-error"
}
},
disabled: {
true: {
root: 'opacity-75',
base: 'cursor-not-allowed',
label: 'cursor-not-allowed',
description: 'cursor-not-allowed'
}
}
},
compoundVariants: [
{
color: 'primary',
highlight: true,
class: {
base: 'ring ring-primary'
}
},
{
color: 'neutral',
highlight: true,
class: {
base: 'ring ring-inverted'
}
}
],
defaultVariants: {
color: 'primary',
size: 'md'
}
}
}
})
]
})