Switch

즉시 적용되는 켜기/끄기 설정을 위한 손그림 스타일의 토글 스위치입니다.

개요

Switch는 설정을 즉시 켜거나 꺼요 — 나중에 제출되는 폼에 속하는 Checkbox와 달리, Switch의 효과는 바로 적용되는 것으로 기대돼요.

구조

실제 <input type="checkbox" role="switch">(React/Web Components) 또는 accessibilityRole="switch"를 가진 Pressable(React Native) 뒤에 스케치 스타일의 트랙이 그려져요. 두 번째로 작은 채워진 썸(@ghds/sketch-core에서 직접 계산된 타원)이 트랙 안에 렌더링되는데, 꺼졌을 때는 왼쪽, 켜졌을 때는 오른쪽에 위치해요. 썸 색은 상태를 따라가요. 꺼짐 트랙 위에서는 트랙과 대비되는 comp.switch.thumb.color.default, 채워진 켜짐 트랙 위에서는 comp.switch.thumb.color.checked를 써서 두 테마 모두에서 트랙과 3:1 대비를 지켜요.

변형 및 상태

State Status Notes
꺼짐 Implemented —
켜짐 Implemented 트랙이 채워지고, 썸이 밝아지며 오른쪽으로 이동해요.
비활성화 Implemented —
로딩 Not implemented —

사용법

Do

  • 즉시 적용되는 설정(예: "알림 활성화")에 Switch를 사용하세요.
  • Switch에 현재 상태가 아닌 설정을 설명하는 명확하고 정적인 라벨을 함께 사용하세요 ("알림", "켜짐" X).

Don't

  • 명시적인 저장/제출 동작이 필요한 폼 안에서 Switch를 사용하지 마세요 — 거기에는 Checkbox를 사용하세요.
  • 썸 위치를 애니메이션하지 마세요 — v1은 토글 시 즉시, 애니메이션 없는 위치 전환을 제공해요. 이 시스템의 어떤 컴포넌트도 아직 전환 시스템이 없어요.

접근성

콘텐츠

라벨은 설정 자체의 이름이에요 (“알림 활성화”), 현재 상태나 동작이 아니에요.

Props API

React@ghds/react

PropTypeDefaultDescription
labelstring—보이는 라벨, Radix Label을 통해 연결돼요.
checkedboolean—제어되는 체크 상태.
defaultCheckedbooleanfalse비제어 시 초기 체크 상태.
disabledbooleanfalse인터랙션을 비활성화해요.
...restInputHTMLAttributes—나머지 모든 네이티브 input 속성이 그대로 전달돼요.

Web Components@ghds/web-components

PropTypeDefaultDescription
checkedbooleanfalse반영된 속성.
disabledbooleanfalse인터랙션과 폼 참여를 비활성화해요.
namestring—제출되는 컨트롤 이름.
valuestring'on'체크 시 제출되는 값.
labelstring—선택적 보이는 라벨, 접근성 이름이기도 해요.

React Native@ghds/react-native

PropTypeDefaultDescription
labelstring—보이는 접근성 라벨 — 필수 항목이에요.
checkedboolean—제어되는 체크 상태.
defaultCheckedboolean—비제어 시 초기 체크 상태.
onCheckedChange(checked: boolean) => void—토글 시 다음 체크 상태와 함께 발생해요.
disabledbooleanfalse인터랙션을 비활성화해요.
testIDstring—쿼리용 테스트 핸들.

코드 예시

// React — @ghds/react
import { Switch } from '@ghds/react/switch';

<Switch
  label="Enable notifications"
  checked={notifications}
  onChange={(event) => setNotifications(event.target.checked)}
/>;
<!-- Web Components — @ghds/web-components -->
<script type="module">
  import '@ghds/web-components/switch';
</script>
<gh-switch id="notify" label="Enable notifications"></gh-switch>
<script>
  document.getElementById('notify').addEventListener('change', (e) => {
    console.log(e.target.checked);
  });
</script>
// React Native — @ghds/react-native
import { Switch } from '@ghds/react-native/switch';

<Switch label="Enable notifications" checked={notifications} onCheckedChange={setNotifications} />;

라이브 데모

React와 Web Components가 아래에 라이브로 렌더링돼요. React Native는 추가적인 react-native-web 연결 없이는 브라우저에서 실행할 수 없어요 (이 사이트에는 설정되어 있지 않아요). 그래서 사용법을 코드 샘플로 보여드릴게요.

React @ghds/react

Web Components @ghds/web-components