스위치
스위치 (Switch)
두 상태 사이를 전환하는 컨트롤 컴포넌트예요. 레이블, 설명, 아이콘, 로딩, 색상, 크기 등을 지원해 체크박스처럼 설정 토글을 만드는 데 유용해요.
출처: Nuxt UI
본문
사용법 (Usage)
v-model 디렉티브를 사용해 Switch의 체크 상태를 제어해요.
<script setup lang="ts">
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 prop을 사용하면 레이블 옆에 별표가 추가돼요.
<template>
<USwitch required label="Check me" />
</template>
설명 (Description)
description prop을 사용해 Switch의 설명을 설정해요.
<template>
<USwitch label="Check me" description="This is a checkbox." />
</template>
아이콘 (Icon)
checked-icon과 unchecked-icon prop을 사용해 체크/체크 해제 상태의 아이콘을 설정해요.
<template>
<USwitch unchecked-icon="i-lucide-x" checked-icon="i-lucide-check" default-value label="Check me" />
</template>
로딩 (Loading)
loading prop을 사용해 Switch에 로딩 아이콘을 표시해요.
<template>
<USwitch loading default-value label="Check me" />
</template>
로딩 아이콘 (Loading Icon)
loading-icon prop을 사용해 로딩 아이콘을 커스텀해요. 기본값은 i-lucide-loader-circle예요.
<template>
<USwitch loading loading-icon="i-lucide-loader" default-value label="Check me" />
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
You can customize this icon globally in your
app.config.tsunderui.icons.loadingkey.
[!TIP]
이 아이콘은
app.config.ts의ui.icons.loading키에서 전역으로 커스텀할 수 있어요. 자세한 내용은 /docs/getting-started/integrations/icons/nuxt#theme 문서를 참고해 주세요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
You can customize this icon globally in your
vite.config.tsunderui.icons.loadingkey.
[!TIP]
이 아이콘은
vite.config.ts의ui.icons.loading키에서 전역으로 커스텀할 수 있어요. 자세한 내용은 /docs/getting-started/integrations/icons/vue#theme 문서를 참고해 주세요.
색상 (Color)
color prop을 사용해 Switch의 색상을 변경해요.
<template>
<USwitch color="neutral" default-value label="Check me" />
</template>
크기 (Size)
size prop을 사용해 Switch의 크기를 변경해요.
<template>
<USwitch size="xl" default-value label="Check me" />
</template>
비활성화 (Disabled)
disabled prop을 사용해 Switch를 비활성화해요.
<template>
<USwitch disabled label="Check me" />
</template>
API
Props
/**
* Props for the Switch component
*/
interface SwitchProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
/**
* @default 'primary'
*/
color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
/**
* @default 'md'
*/
size?: "md" | "xs" | "sm" | "lg" | "xl" | undefined;
/**
* Highlight the ring color like a focus state.
*/
highlight?: boolean | undefined;
/**
* When `true`, the loading icon will be displayed.
*/
loading?: boolean | undefined;
/**
* The icon when the `loading` prop is `true`.
* @default appConfig.ui.icons.loading
*/
loadingIcon?: any;
/**
* Display an icon when the switch is checked.
*/
checkedIcon?: any;
/**
* Display an icon when the switch is unchecked.
*/
uncheckedIcon?: any;
label?: string | undefined;
description?: string | undefined;
ui?: { root?: SlotClass; base?: SlotClass; container?: SlotClass; thumb?: SlotClass; icon?: SlotClass; wrapper?: SlotClass; label?: SlotClass; description?: SlotClass; } | undefined;
/**
* When `true`, prevents the user from interacting with the switch.
*/
disabled?: boolean | undefined;
id?: string | undefined;
/**
* The name of the field. Submitted with its owning form as part of a name/value pair.
*/
name?: string | undefined;
/**
* When `true`, indicates that the user must set the value before the owning form can be submitted.
*/
required?: boolean | undefined;
/**
* The value given as data when submitted with a `name`.
*/
value?: string | undefined;
/**
* The state of the switch when it is initially rendered. Use when you do not need to control its state.
*/
defaultValue?: T | undefined;
/**
* The controlled state of the switch. Can be bind as `v-model`.
*/
modelValue?: null | T | undefined;
/**
* The value used when the switch is on. Defaults to `true`.
*/
trueValue?: T | undefined;
/**
* The value used when the switch is off. Defaults to `false`.
*/
falseValue?: T | undefined;
autofocus?: false | true | "true" | "false" | undefined;
form?: string | undefined;
formaction?: string | undefined;
formenctype?: string | undefined;
formmethod?: string | undefined;
formnovalidate?: false | true | "true" | "false" | undefined;
formtarget?: string | undefined;
}
[!NOTE] See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes
This component also supports all native
<button>HTML attributes.
[!NOTE]
이 컴포넌트는 네이티브
<button>HTML 속성도 모두 지원해요. 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes 문서를 참고해 주세요.
Slots
/**
* Slots for the Switch component
*/
interface SwitchSlots {
label(): any;
description(): any;
}
Emits
/**
* Emitted events for the Switch component
*/
interface SwitchEmits {
change: (payload: [event: Event]) => void;
update:modelValue: (payload: [payload: T]) => void;
}
테마 (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: 'secondary',
highlight: true,
class: {
base: 'ring ring-secondary'
}
},
{
color: 'success',
highlight: true,
class: {
base: 'ring ring-success'
}
},
{
color: 'info',
highlight: true,
class: {
base: 'ring ring-info'
}
},
{
color: 'warning',
highlight: true,
class: {
base: 'ring ring-warning'
}
},
{
color: 'error',
highlight: true,
class: {
base: 'ring ring-error'
}
},
{
color: 'neutral',
highlight: true,
class: {
base: 'ring ring-inverted'
}
}
],
defaultVariants: {
color: 'primary',
size: 'md'
}
}
}
})
변경 내역 (Changelog)
사이트맵 (Sitemap)
전체 사이트맵에서 모든 페이지를 확인할 수 있어요.