AI CSS Animations

Toggle Switch with Loading State

For settings that save to a server: switching on shows a spinner in the knob over a pale track, then a check mark draws in and the track fills. The pending beat is timed here with transition-delay.

Also known as: loading toggle switch, async toggle switch css, toggle switch with spinner.

HTML

<label class="tgl-loading">
  <input type="checkbox" role="switch" aria-label="Cloud sync">
  <span class="tgl-loading-track" aria-hidden="true">
    <span class="tgl-loading-knob">
      <span class="tgl-loading-spinner"></span>
      <svg class="tgl-loading-check" viewBox="0 0 24 24" fill="none" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M6.5 12.5l3.5 3.5 7.5-8"/></svg>
    </span>
  </span>
</label>

CSS

.tgl-loading {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;

}
.tgl-loading input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-loading-track {
  position: relative;
  display: block;
  width: 3.8em;
  height: 2em;
  border-radius: 1em;
  background: color-mix(in srgb, currentColor 13%, transparent);
  transition: background-color 0.3s ease;
}
.tgl-loading-track::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--c);
  opacity: 0;
  transition: opacity 0.3s ease;
}
.tgl-loading-knob {
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  display: grid;
  place-items: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.22);
  transition: transform 0.35s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-loading-spinner,
.tgl-loading-check {
  grid-area: 1 / 1;
  width: 1em;
  height: 1em;
}
.tgl-loading-spinner {
  border: 0.14em solid color-mix(in srgb, var(--c) 22%, transparent);
  border-top-color: var(--c);
  border-radius: 50%;
  opacity: 0;
}
.tgl-loading-check {
  stroke: var(--c);
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
  transition: stroke-dashoffset 0.15s ease;
}
/* on: pale track and a spinner first, then the fill and the check */
.tgl-loading input:checked + .tgl-loading-track { background: color-mix(in srgb, var(--c) 28%, transparent); }
.tgl-loading input:checked + .tgl-loading-track::before {
  opacity: 1;
  transition-delay: 1.1s;
}
.tgl-loading input:checked + .tgl-loading-track .tgl-loading-knob { transform: translateX(1.8em); }
.tgl-loading input:checked + .tgl-loading-track .tgl-loading-spinner { animation: tgl-loading-spin 1.2s linear forwards; }
.tgl-loading input:checked + .tgl-loading-track .tgl-loading-check {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.3s ease 1.15s;
}
.tgl-loading input:focus-visible + .tgl-loading-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}
@keyframes tgl-loading-spin {
  0% { opacity: 0; transform: rotate(0deg); }
  8%, 88% { opacity: 1; }
  100% { opacity: 0; transform: rotate(720deg); }
}

@media (prefers-reduced-motion: reduce) {
  .tgl-loading, .tgl-loading *, .tgl-loading::before, .tgl-loading::after, .tgl-loading *::before, .tgl-loading *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --tgl-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --tgl-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 24px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar toggle switches