JSFiddle - React, Tailwind, and code Playground

by kthornbloom

HTML

<div>
  <input type="checkbox" id="mfa" class="account-toggle" aria-label="Toggle Multi Factor Authentication On or Off">
  <label aria-hidden for="mfa" class="account-toggle__container">
  </label>
</div>

CSS

/* HIDE REAl CHECKBOX */
.account-toggle {

}

/* TOGGLE OFF */
.account-toggle__container {
  box-sizing: border-box;
  display: flex;
  background: #ccc;
  border-radius: 50px;
  width: 50px;
  height: 20px;
  position: relative;
  justify-content: flex-end;
  align-items: center;
  padding: 5px;
  transition: .1s;
}

.account-toggle__container:hover {
  background: #B3B3B3;
  cursor: pointer;
}

/* THUMB */
.account-toggle__container::after {
  content:'';
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  position: absolute;
  left: 2px;
  top: 2px;
  transition: .1s;
}

/* LABEL */
.account-toggle__container::before {
  content:'OFF';
  color: #545454;
  font-size: 12px;
  transition: .1s;
}

/* FAKE FOCUS */
.account-toggle:focus-visible + label {
  box-shadow: 0 0 0 1px #fff, 0 0 0 3px #0072ba;  
}


/* TOGGLE ON */
.account-toggle:checked + label {
  background: #0072ba;
  justify-content: flex-start;
}

.account-toggle:checked + label::before {
  content: 'ON';
  color: #fff;
}

.account-toggle:checked + label::after {
  left: calc(100% - 18px);
}

body {
  font-family: sans-serif;
  padding: 50px;
}

.spin + .account-toggle__container::after {
    animation: spin-thumb 1s infinite linear;
    width: 10px;
    height: 10px;
    border: 3px dotted white;
    background: none;
}

@keyframes spin-thumb {
  0% {transform: rotate(0deg)}
  100% {transform: rotate(360deg)}
}

JavaScript

const mfaToggle = document.getElementById('mfa');

mfaToggle.addEventListener('click', function(e){
	e.preventDefault();
  console.log('whoa');
	mfaToggle.classList.add('spin');
  
  setTimeout(function(){
  
  if (mfaToggle.checked == true) {
    mfaToggle.checked = false;
  } else {
    mfaToggle.checked = true;
    }
  }, 100);
  
})