탐색

v2.1.0
폼

스위치

즉시 적용되는 설정을 전환할 때 진주빛 손잡이가 잉크색 영역 위로 미끄러져 이동합니다.

이 페이지의 내용

스위치는 진주빛 손잡이가 잉크색 영역 위로 미끄러져 이동하는 오목한 영역입니다. 변경 즉시 적용되는 설정에는 기본 체크박스에 .switch 클래스와 role="switch" 속성을 함께 적용하세요. 폼과 함께 제출하는 선택 항목에는 체크박스가 의미를 더 명확히 전달합니다.

스위치

<label class="check">  <input type="checkbox" role="switch" class="switch" checked> Email notifications</label>
<label className="check">  <input type="checkbox" role="switch" className="switch" defaultChecked /> Email notifications</label>
<label class="check">  <input type="checkbox" role="switch" class="switch" checked> Email notifications</label>
<template>  <label class="check">    <input type="checkbox" role="switch" class="switch" checked> Email notifications  </label></template>

#크기

크기

<input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked><input type="checkbox" role="switch" class="switch" aria-label="Medium" checked><input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked>
<>  <input type="checkbox" role="switch" className="switch switch-sm" aria-label="Small" defaultChecked />  <input type="checkbox" role="switch" className="switch" aria-label="Medium" defaultChecked />  <input type="checkbox" role="switch" className="switch switch-lg" aria-label="Large" defaultChecked /></>
<input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked><input type="checkbox" role="switch" class="switch" aria-label="Medium" checked><input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked>
<template>  <input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked>  <input type="checkbox" role="switch" class="switch" aria-label="Medium" checked>  <input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked></template>

#설정 목록의 스위치

설정 목록

<div class="sheet flex flex-col p(0)">  <label class="check justify-between gap-lg px-lg py-md">    <span>Automatic updates</span>    <input type="checkbox" role="switch" class="switch" checked>  </label>  <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair">    <span>Usage analytics</span>    <input type="checkbox" role="switch" class="switch">  </label></div>
<div className="sheet flex flex-col p(0)">  <label className="check justify-between gap-lg px-lg py-md">    <span>Automatic updates</span>    <input type="checkbox" role="switch" className="switch" defaultChecked />  </label>  <label className="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair">    <span>Usage analytics</span>    <input type="checkbox" role="switch" className="switch" />  </label></div>
<div class="sheet flex flex-col p(0)">  <label class="check justify-between gap-lg px-lg py-md">    <span>Automatic updates</span>    <input type="checkbox" role="switch" class="switch" checked>  </label>  <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair">    <span>Usage analytics</span>    <input type="checkbox" role="switch" class="switch">  </label></div>
<template>  <div class="sheet flex flex-col p(0)">    <label class="check justify-between gap-lg px-lg py-md">      <span>Automatic updates</span>      <input type="checkbox" role="switch" class="switch" checked>    </label>    <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair">      <span>Usage analytics</span>      <input type="checkbox" role="switch" class="switch">    </label>  </div></template>

#비활성 상태

비활성 상태

<input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled><input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled>
<>  <input type="checkbox" role="switch" className="switch" aria-label="Beta features" disabled />  <input type="checkbox" role="switch" className="switch" aria-label="Security alerts" defaultChecked disabled /></>
<input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled><input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled>
<template>  <input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled>  <input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled></template>

#접근성

  • 보조 기술이 체크 여부 대신 켜짐과 꺼짐을 알리도록 role="switch" 속성을 유지하세요.
  • 모든 스위치에는 레이블이 필요합니다. 스위치를 <label> 요소로 감싸거나, <label for>로 레이블을 연결하거나, aria-label 속성을 지정하세요.
  • 입력 요소는 기본 컨트롤과 마찬가지로 포커스를 받을 수 있으며 Space 키로 상태를 전환합니다.

#클래스

클래스설명
switchrole="switch"를 지정한 네이티브 체크박스입니다. 트랙의 높이는 22px이고 손잡이는 트랙 가장자리에서 3px 안쪽에 놓입니다. 터치 화면에서는 같은 트랙 주변에 44px 크기의 터치 영역을 확보하며, 텍스트를 오른쪽에서 왼쪽으로 읽는 환경에서는 손잡이가 반대 방향으로 움직입니다.
switch-sm18px 트랙. 기본값은 22px입니다.적용할 클래스는 switch입니다
switch-lg26px 트랙입니다. 기본값은 22px입니다.적용할 클래스는 switch입니다

#소스

@master/ui/components/switch.css
/* * Switch: a native checkbox with role="switch", a well whose pearl thumb slides onto ink. * Thumb and track are concentric: the thumb keeps an even 3px from every edge, 2px in the small track. */@layer components.ui {  .switch {    --switch-width: 2.25rem;    --switch-height: 1.375rem;    --switch-inset: 3px;    --switch-thumb: calc(var(--switch-height) - var(--switch-inset) * 2);    /* Pressing stretches the thumb toward its travel, like a finger on a physical toggle. */    --switch-stretch: 0px;    position: relative;    flex: none;    width: var(--switch-width);    height: var(--switch-height);    margin: max(0px, calc((1lh - var(--switch-height)) / 2)) 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-surface-inset);    /* The unchecked track doubles its half-pixel contour so it reads on a white sheet. */    box-shadow: var(--shadow-pressed), inset 0 0 0 .5px var(--color-line-contour);    vertical-align: middle;    appearance: none;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition-property: background-color, box-shadow;    transition-duration: var(--duration-base);    transition-timing-function: var(--easing-glide);    &::before {      content: '';      position: absolute;      inset-block-start: var(--switch-inset);      inset-inline-start: var(--switch-inset);      width: calc(var(--switch-thumb) + var(--switch-stretch));      height: var(--switch-thumb);      border-radius: 999px;      corner-shape: round;      @apply --surface-thumb;      box-shadow: 0 0 0 .5px var(--color-line-strong), 0 1px 2px -.5px var(--color-shade-1), 0 2px 4px -1px var(--color-shade-2);      transition-property: translate, width;      transition-duration: var(--duration-base);      transition-timing-function: var(--easing-glide);    }    /*     * Off, under the pointer, the well darkens a step, as a checkbox's and a radio's do. Forced colors keep their     * own field, since the switch opts out of them.     */    @media (hover: hover) and (forced-colors: none) {      &:hover:not(:checked, :disabled) {        background: var(--color-surface-inset-hover);      }    }    &:active:not(:disabled) {      --switch-stretch: 3px;    }    &:checked {      background: var(--color-surface-inverse);      box-shadow: var(--shadow-ink-well);      &::before {        box-shadow: var(--shadow-thumb-on-ink);        translate: calc(var(--switch-width) - var(--switch-height) - var(--switch-stretch)) 0;        background: linear-gradient(180deg, var(--color-thumb-on-high), var(--color-thumb-on-low));      }    }    /* Where words run from the right, on is at the left: the thumb travels the other way. */    &:checked:dir(rtl)::before {      translate: calc(-1 * (var(--switch-width) - var(--switch-height) - var(--switch-stretch))) 0;    }    &:focus-visible {      @apply --focus-ring;    }    &:disabled {      opacity: .5;      cursor: not-allowed;    }    /* On touch the track keeps its size and its target grows to 44px around it. */    @media (pointer: coarse) {      &::after {        content: '';        position: absolute;        inset: calc((var(--switch-height) - 2.75rem) / 2) min(0px, calc((var(--switch-width) - 2.75rem) / 2));      }    }    &.switch-sm {      --switch-width: 1.75rem;      --switch-height: 1.125rem;      --switch-inset: 2px;    }    &.switch-lg {      --switch-width: 2.75rem;      --switch-height: 1.625rem;    }    /*     * Reduced motion: the thumb does not stretch and does not glide; it stands at its end at once, as a segmented     * control's sheet does, and the track's fill still fades between off and on.     */    @media (prefers-reduced-motion: reduce) {      &::before {        transition-property: none;      }      &:active:not(:disabled) {        --switch-stretch: 0px;      }    }    @media (forced-colors: active) {      forced-color-adjust: none;      background: Field;      box-shadow: inset 0 0 0 1px FieldText;      &::before {        background: FieldText;        box-shadow: none;      }      &:checked {        background: Highlight;        box-shadow: none;        &::before {          background: HighlightText;        }      }      &:disabled {        opacity: 1;        box-shadow: inset 0 0 0 1px GrayText;        &::before {          background: GrayText;        }      }      /* On and disabled: the track takes the disabled system color, as a checked checkbox's fill does. */      &:checked:disabled {        background: GrayText;        &::before {          background: Field;        }      }      &:focus-visible {        outline-color: Highlight;      }    }  }}