본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

ToggleSwitch — 불리언 값을 선택하는 스위치 컴포넌트

원문 보기 위키 갱신

ToggleSwitch는 켜고 끄는 불리언(boolean) 값을 선택하는 데 쓰는 스위치 컴포넌트예요. 제어/비제어 모드를 모두 지원하고, 내부에 커스텀 콘텐츠를 넣을 수도 있어요.

출처: 문서

본문

ToggleSwitch는 불리언 값을 선택하는 데 사용해요.

사용법 (Usage)

import { ToggleSwitch } from '@primereact/ui/toggleswitch';
<ToggleSwitch.Root>
    <ToggleSwitch.Control>
        <ToggleSwitch.Handle />
    </ToggleSwitch.Control>
</ToggleSwitch.Root>

예시

기본 (Basic)

불리언 설정을 활성화/비활성화 상태 사이에서 토글해요.

제어 (Controlled)

제어되는 ToggleSwitch는 상태 변수로 checked 상태를 관리하고 변경 이벤트를 직접 처리해야 해요. 이렇게 하면 ToggleSwitch의 동작을 완전히 제어할 수 있어요.

비제어 (Uncontrolled)

비제어 ToggleSwitch에서는 defaultChecked로 초기 상태를 정하고, 컴포넌트가 내부적으로 자체 상태를 관리해요.

템플릿 (Template)

ToggleSwitch.Handle 안에도 커스텀 콘텐츠를 표시할 수 있어요.

커스터마이즈 (Customization)

ToggleSwitch 컴포넌트는 CSS 클래스를 통해 커스터마이즈를 지원해요. 컴포넌트에 커스텀 클래스를 적용하면 색상 등 시각적 속성을 포함한 모양을 바꿀 수 있어요.

유효하지 않음 (Invalid)

invalid prop으로 유효성 검사에 실패했음을 나타내는 상태를 표시해요. 폼 검증 라이브러리와 연동할 때 유용한 스타일이에요.

비활성화 (Disabled)

disabled가 있으면 요소는 편집할 수 없고 포커스도 받지 못해요.

관련 자료

하위 컴포넌트 (Sub-Components)

전체 하위 컴포넌트 API는 ToggleSwitch Primitive에서 확인할 수 있어요.

훅 (Hooks)

Headless 훅 API는 useToggleSwitch에서 확인할 수 있어요.

접근성 (Accessibility)

WAI-ARIA 준수 사항과 키보드 지원에 대한 자세한 내용은 ToggleSwitch Primitive에서 볼 수 있어요.

테마 (Theming)

CSS 클래스 (CSS Classes)

styled 모드에서 사용하는 클래스 이름 목록이에요.

ClassName Description
p-toggleswitch Class name of the root element
p-toggleswitch-input Class name of the input element
p-toggleswitch-slider Class name of the control element
p-toggleswitch-handle Class name of the handle element

디자인 토큰 (Design Tokens)

디자인 토큰 목록이에요.

Token CSS Variable Description
toggleswitch.width --p-toggleswitch-width Width of root
toggleswitch.height --p-toggleswitch-height Height of root
toggleswitch.border.radius --p-toggleswitch-border-radius Border radius of root
toggleswitch.gap --p-toggleswitch-gap Gap of root
toggleswitch.shadow --p-toggleswitch-shadow Shadow of root
toggleswitch.focus.ring.width --p-toggleswitch-focus-ring-width Focus ring width of root
toggleswitch.focus.ring.style --p-toggleswitch-focus-ring-style Focus ring style of root
toggleswitch.focus.ring.color --p-toggleswitch-focus-ring-color Focus ring color of root
toggleswitch.focus.ring.offset --p-toggleswitch-focus-ring-offset Focus ring offset of root
toggleswitch.focus.ring.shadow --p-toggleswitch-focus-ring-shadow Focus ring shadow of root
toggleswitch.border.width --p-toggleswitch-border-width Border width of root
toggleswitch.border.color --p-toggleswitch-border-color Border color of root
toggleswitch.hover.border.color --p-toggleswitch-hover-border-color Hover border color of root
toggleswitch.checked.border.color --p-toggleswitch-checked-border-color Checked border color of root
toggleswitch.checked.hover.border.color --p-toggleswitch-checked-hover-border-color Checked hover border color of root
toggleswitch.invalid.border.color --p-toggleswitch-invalid-border-color Invalid border color of root
toggleswitch.transition.duration --p-toggleswitch-transition-duration Transition duration of root
toggleswitch.slide.duration --p-toggleswitch-slide-duration Slide duration of root
toggleswitch.background --p-toggleswitch-background Background of root
toggleswitch.disabled.background --p-toggleswitch-disabled-background Disabled background of root
toggleswitch.hover.background --p-toggleswitch-hover-background Hover background of root
toggleswitch.checked.background --p-toggleswitch-checked-background Checked background of root
toggleswitch.checked.hover.background --p-toggleswitch-checked-hover-background Checked hover background of root
toggleswitch.handle.border.radius --p-toggleswitch-handle-border-radius Border radius of handle
toggleswitch.handle.size --p-toggleswitch-handle-size Size of handle
toggleswitch.handle.background --p-toggleswitch-handle-background Background of handle
toggleswitch.handle.disabled.background --p-toggleswitch-handle-disabled-background Disabled background of handle
toggleswitch.handle.hover.background --p-toggleswitch-handle-hover-background Hover background of handle
toggleswitch.handle.checked.background --p-toggleswitch-handle-checked-background Checked background of handle
toggleswitch.handle.checked.hover.background --p-toggleswitch-handle-checked-hover-background Checked hover background of handle
toggleswitch.handle.color --p-toggleswitch-handle-color Color of handle
toggleswitch.handle.hover.color --p-toggleswitch-handle-hover-color Hover color of handle
toggleswitch.handle.checked.color --p-toggleswitch-handle-checked-color Checked color of handle
toggleswitch.handle.checked.hover.color --p-toggleswitch-handle-checked-hover-color Checked hover color of handle

더 알아보기 (Learn more)