ToggleSwitch
ToggleSwitch는 compound composition 방식으로 만들어진, styled가 없는 접근성 높은 토글 스위치 컴포넌트예요. 레이아웃과 스타일을 완전히 제어해 커스텀한 토글 스위치 컨트롤을 만들 수 있어요.
출처: 문서
본문
기능 (Features)
- 3개의 하위 컴포넌트(
Root,Control,Handle)로 구성된 compound 컴포넌트 API - 완전한 접근성을 위한
role="switch"를 가진 숨겨진 네이티브 체크박스 - 제어(controlled) 및 비제어(uncontrolled) 체크 상태
- 데이터 속성을 통한 invalid/disabled 상태 지원
사용법 (Usage)
import { ToggleSwitch } from 'primereact/toggleswitch';
<ToggleSwitch.Root>
<ToggleSwitch.Control>
<ToggleSwitch.Handle />
</ToggleSwitch.Control>
</ToggleSwitch.Root>
동작 (Behavior)
다형적 렌더링 (Polymorphic Rendering)
아무 하위 컴포넌트에 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.
<ToggleSwitch.Root as="label">
<ToggleSwitch.Control as="span">
<ToggleSwitch.Handle as="span" />
</ToggleSwitch.Control>
</ToggleSwitch.Root>
기본 요소: Root=div, Control=div, Handle=span.
렌더 함수 자식 (Render Function Children)
ToggleSwitch.Handle은 렌더 함수를 자식으로 받아 컴포넌트 인스턴스에 접근할 수 있게 해 줘요.
<ToggleSwitch.Handle>{(instance) => <span>{instance.toggleSwitch?.state.checked ? 'ON' : 'OFF'}</span>}</ToggleSwitch.Handle>
패스 스루 (Pass Through)
<DocPTViewer name="toggleswitch-pt" components={['ToggleSwitchRoot']} />
API
ToggleSwitchRoot
| Attribute | Value |
|---|---|
data-scope |
"toggleswitch" |
data-part |
"root" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
data-invalid |
Present when invalid |
| Label | Type | Description |
|---|---|---|
| root | ToggleSwitchRootPassThroughType<HTMLAttributes> | Used to pass attributes to the root's DOM element. |
| input | ToggleSwitchRootPassThroughType<HTMLAttributes> | Used to pass attributes to the input's DOM element. |
| control | ToggleSwitchRootPassThroughType<HTMLAttributes> | Used to pass attributes to the control's DOM element. |
| handle | ToggleSwitchRootPassThroughType<HTMLAttributes> | Used to pass attributes to the handle's DOM element. |
ToggleSwitchControl
| Attribute | Value |
|---|---|
data-scope |
"toggleswitch" |
data-part |
"control" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
data-invalid |
Present when invalid |
| Label | Type | Description |
|---|---|---|
| root | ToggleSwitchControlPassThroughType<HTMLAttributes> | Used to pass attributes to the root's DOM element. |
ToggleSwitchHandle
| Attribute | Value |
|---|---|
data-scope |
"toggleswitch" |
data-part |
"handle" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
data-invalid |
Present when invalid |
| Label | Type | Description |
|---|---|---|
| root | ToggleSwitchHandlePassThroughType<HTMLAttributes> | Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
ToggleSwitch.Root는 보조 기술이 스위치로 발표하는 숨겨진 네이티브 <input type="checkbox" role="switch">를 렌더링해요. 이 입력은 aria-checked로 체크 상태를 반영해요. aria-label, aria-labelledby, 또는 inputId를 이용한 연결된 <label> 요소로 접근 가능한 이름을 제공해 주세요.
<label htmlFor="switch1">Notifications</label>
<ToggleSwitch.Root inputId="switch1" />
<span id="switch2">Dark Mode</span>
<ToggleSwitch.Root ariaLabelledby="switch2" />
<ToggleSwitch.Root ariaLabel="Enable notifications" />
키보드 지원 (Keyboard Support)
| Key | Function |
|---|---|
tab |
Moves focus to the toggle switch. |
space |
Toggles the checked state. |