Toggle Switch Component

A CSS implementation of the Toggle Switch component.

Example

html
<div class="toggle-switch-container">
  <label class="toggle-switch">
    <input type="checkbox" aria-label="toggle-switch">
    <span class="toggle-slider"></span>
  </label>
  <p class="toggle-label">On/Off</p>
</div>

Compiled CSS

Example

scss
@import 'components/toggle-switch';
css compiled
@keyframes openPopover {
  from {
    transform: scale(0.9);
  }
  to {
    transform: scale(1);
  }
}

.spr-icon-rotate-90 {
  transform: rotate(90deg);
}

.spr-icon-rotate-180 {
  transform: rotate(180deg);
}

.spr-icon-rotate-270 {
  transform: rotate(270deg);
}

label.toggle-switch {
  margin-bottom: 0;
}

label.toggle-switch.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 3rem;
  height: 1.5rem;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch input:checked + .toggle-slider {
  background-color: #006fcf;
}

.toggle-switch input:focus-visible + .toggle-slider {
  outline: 2px solid #006fcf;
  outline-offset: 0.125rem;
}

.toggle-switch input:checked + .toggle-slider:before {
  -webkit-transform: translateX(1.5rem);
  -ms-transform: translateX(1.5rem);
  transform: translateX(1.5rem);
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #737373;
  transition-property: background-color;
  transition-duration: 0.24s;
  transition-timing-function: ease-in;
  border-radius: 1.25rem;
}

.toggle-slider:before {
  border-radius: 50%;
  position: absolute;
  content: '';
  height: 1.125rem;
  width: 1.125rem;
  left: 0.125rem;
  bottom: 0.125rem;
  background-color: #fff;
  transition-property: transform;
  transition-duration: 0.24s;
  transition-timing-function: ease-in;
  box-shadow: 0 4px 8px 0 rgba(26, 26, 26, 0.15);
  border: 0.0375rem solid #ecedee;
}

.toggle-switch-container {
  align-items: center;
}

.toggle-label {
  font-family: "BentonSansBook", "Helvetica Neue", Arial, sans-serif;
  font-size: 0.875rem;
  line-height: 1.25rem;
  letter-spacing: 0;
  font-weight: 400;
  color: #53565a;
}