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

Switch (스위치)

원문 보기 위키 갱신

순수 CSS만으로 만든 스위치 컴포넌트예요. 체크박스 문법 그대로 쓰면서 role="switch"만 추가하면 토글 형태의 켜기/끄기 컨트롤이 완성돼요.

Pico에서는 토글이 별도 라이브러리를 필요로 하지 않고, 네이티브 체크박스의 접근성까지 그대로 가져갈 수 있다는 게 가장 큰 장점이에요. 비활성화 상태와 유효성 검사 상태도 함께 살펴볼게요.

출처: 문서

본문

체크박스 문법을 사용해 만든, 순수 CSS의 스위치 컴포넌트예요.

<fieldset>
  <label>
    <input name="terms" type="checkbox" role="switch" />
    I agree to the Terms
  </label>
  <label>
    <input name="opt-in" type="checkbox" role="switch" checked />
    Receive news and offers
  </label>
</fieldset>

비활성화 (Disabled)

disabled 속성을 추가하면 스위치를 비활성화할 수 있어요. checked와 함께 쓰면 비활성화된 상태로 켜져 있는 모습도 표현할 수 있어요.

<fieldset>
  <label>
    <input name="publish" type="checkbox" role="switch" disabled />
    Publish on my profile
  </label>
  <label>
    <input name="change-password" type="checkbox" role="switch" checked disabled />
    Change my password at next login
  </label>
</fieldset>

유효성 검사 상태 (Validation states)

유효성 검사 상태는 aria-invalid로 표현해요.

<fieldset>
  <label>
    <input name="2fa" type="checkbox" role="switch" aria-invalid="false" />
    Enable two-factor authentication
  </label>
  <label>
    <input name="subscription" type="checkbox" role="switch" aria-invalid="true" />
    Automatic subscription renewal
  </label>
</fieldset>

더 알아보기 (Learn more)