스위치 (Switch)
이진 선택(binary selection)을 가능하게 하는 제어 요소입니다. 켜짐/꺼짐 상태를 토글할 수 있습니다.
출처: 문서
본문
해부 구조 (Anatomy)
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label />
<Switch.HiddenInput />
</Switch.Root>
예시 (Examples)
import { Switch } from '@ark-ui/react/switch'
import styles from 'styles/switch.module.css'
export const Basic = () => (
<Switch.Root className={styles.Root}>
<Switch.Control className={styles.Control}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Control>
<Switch.Label className={styles.Label}>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
제어 (Controlled)
제어되는(controlled) Switch 컴포넌트의 경우, 토글 상태가 checked prop으로 관리되며 onCheckedChange 이벤트 핸들러가 호출될 때 업데이트됩니다:
import { Switch } from '@ark-ui/react/switch'
import { useState } from 'react'
import styles from 'styles/switch.module.css'
export const Controlled = () => {
const [checked, setChecked] = useState(false)
return (
<Switch.Root className={styles.Root} checked={checked} onCheckedChange={(e) => setChecked(e.checked)}>
<Switch.Control className={styles.Control}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Control>
<Switch.Label className={styles.Label}>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
}
루트 프로바이더 (Root Provider)
스위치를 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useSwitch 훅을 사용하는 것입니다. 이렇게 하면 컴포넌트 외부에서 상태와 메서드에 접근할 수 있습니다.
import { Switch, useSwitch } from '@ark-ui/react/switch'
import button from 'styles/button.module.css'
import styles from 'styles/switch.module.css'
export const RootProvider = () => {
const _switch = useSwitch()
return (
<div className="stack">
<button className={button.Root} onClick={() => _switch.toggleChecked()}>
Toggle
</button>
<Switch.RootProvider className={styles.Root} value={_switch}>
<Switch.Control className={styles.Control}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Control>
<Switch.Label className={styles.Label}>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.RootProvider>
</div>
)
}
필드 (Field)
Field 컴포넌트는 스위치의 폼 관련 상태와 접근성 속성을 관리하는 데 도움을 줍니다. ARIA 레이블, 도움말 텍스트, 오류 텍스트를 처리하여 적절한 접근성을 보장합니다.
import { Field } from '@ark-ui/react/field'
import { Switch } from '@ark-ui/react/switch'
import field from 'styles/field.module.css'
import styles from 'styles/switch.module.css'
export const WithField = () => (
<Field.Root className={field.Root}>
<Switch.Root className={styles.Root}>
<Switch.Control className={styles.Control}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Control>
<Switch.Label className={styles.Label}>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
<Field.HelperText className={field.HelperText}>Additional Info</Field.HelperText>
<Field.ErrorText className={field.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
컨텍스트 (Context)
Switch.Context 또는 useSwitchContext 훅으로 스위치의 상태에 접근할 수 있습니다. 이를 통해 현재 상태에 따라 컴포넌트를 사용자 지정할 수 있습니다:
import { Switch } from '@ark-ui/react/switch'
import styles from 'styles/switch.module.css'
export const Context = () => (
<Switch.Root className={styles.Root}>
<Switch.Control className={styles.Control}>
<Switch.Thumb className={styles.Thumb} />
</Switch.Control>
<Switch.Context>
{(context) => (
<Switch.Label className={styles.Label}>Feature is {context.checked ? 'enabled' : 'disabled'}</Switch.Label>
)}
</Switch.Context>
<Switch.HiddenInput />
</Switch.Root>
)
가이드 (Guides)
asChild
스위치의 Switch.Root 요소는 label 요소입니다. 스위치는 폼 컨트롤이므로 사용자에게 맥락과 의미를 제공하기 위해 레이블과 연결되어야 하기 때문입니다. 그렇지 않으면 HTML과 접근성 구조가 유효하지 않게 됩니다.
asChild속성을 사용해야 한다면,label요소가Switch.Root컴포넌트의 직접적인 자식 요소인지 확인하세요.
API 참조 (API Reference)
Props
Root
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
checked
Type: boolean
Required: false
Default Value: undefined
Description: 스위치의 제어(controlled)된 체크 상태입니다.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: 스위치가 비활성화되었는지 여부입니다.
ids
Type: Partial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string }>
Required: false
Default Value: undefined
Description: 스위치 내 요소들의 id입니다. 구성(composition)에 유용합니다.
invalid
Type: boolean
Required: false
Default Value: undefined
Description: true인 경우, 스위치는 유효하지 않은 것으로 표시됩니다.
label
Type: string
Required: false
Default Value: undefined
Description: 접근성 요소와 해당 상태를 식별하는 현지화된 문자열을 지정합니다.
name
Type: string
Required: false
Default Value: undefined
Description: 스위치에 있는 입력 필드의 이름입니다.
(폼 제출에 유용합니다).
onCheckedChange
Type: (details: CheckedChangeDetails) => void
Required: false
Default Value: undefined
Description: 스위치가 클릭될 때 호출되는 함수입니다.
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: 스위치가 읽기 전용인지 여부입니다.
required
Type: boolean
Required: false
Default Value: undefined
Description: true인 경우, 스위치 입력이 필수로 표시됩니다.
value
Type: string | number
Required: false
Default Value: "on"
Description: 스위치 입력의 값입니다. 폼 제출에 유용합니다.
데이터 속성 (Data Attributes)
data-active: 활성화되거나 눌렸을 때 표시됩니다.
data-focus: 포커스되었을 때 표시됩니다.
data-focus-visible: 키보드로 포커스되었을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
data-hover: 호버되었을 때 표시됩니다.
data-disabled: 비활성화되었을 때 표시됩니다.
data-state: "checked" | "unchecked"
data-invalid: 유효하지 않을 때 표시됩니다.
data-required: 필수일 때 표시됩니다.
Control
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-active: 활성화되거나 눌렸을 때 표시됩니다.
data-focus: 포커스되었을 때 표시됩니다.
data-focus-visible: 키보드로 포커스되었을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
data-hover: 호버되었을 때 표시됩니다.
data-disabled: 비활성화되었을 때 표시됩니다.
data-state: "checked" | "unchecked"
data-invalid: 유효하지 않을 때 표시됩니다.
data-required: 필수일 때 표시됩니다.
HiddenInput
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
Label
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-active: 활성화되거나 눌렸을 때 표시됩니다.
data-focus: 포커스되었을 때 표시됩니다.
data-focus-visible: 키보드로 포커스되었을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
data-hover: 호버되었을 때 표시됩니다.
data-disabled: 비활성화되었을 때 표시됩니다.
data-state: "checked" | "unchecked"
data-invalid: 유효하지 않을 때 표시됩니다.
data-required: 필수일 때 표시됩니다.
RootProvider
Props
value
Type: UseSwitchReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
Thumb
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-active: 활성화되거나 눌렸을 때 표시됩니다.
data-focus: 포커스되었을 때 표시됩니다.
data-focus-visible: 키보드로 포커스되었을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
data-hover: 호버되었을 때 표시됩니다.
data-disabled: 비활성화되었을 때 표시됩니다.
data-state: "checked" | "unchecked"
data-invalid: 유효하지 않을 때 표시됩니다.
data-required: 필수일 때 표시됩니다.
Context
API:
| Property | Type | Description |
|---|---|---|
checked |
boolean |
스위치가 체크되었는지 여부입니다. |
disabled |
`boolean | undefined` |
focused |
`boolean | undefined` |
setChecked |
(checked: boolean) => void |
스위치의 체크 상태를 설정합니다. |
toggleChecked |
VoidFunction |
스위치의 체크 상태를 토글합니다. |
접근성 (Accessibility)
Switch WAI-ARIA 디자인 패턴을 준수합니다.
키보드 지원 (Keyboard Support)
Space + Enter
Description: 스위치를 토글합니다.