Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Multi-Function Button</h1>

<fieldset>
  <legend>Activating the button gets you…</legend>
  <div>
    <input type="radio" value="Success" name="status" id="status_Success" checked>
    <label for="status_Success">
      Success (sandwich)
    </label>
  </div>
  <div>
    <input type="radio" value="Failure" name="status" id="status_Failure">
    <label for="status_Failure">
      Failure (no sandwich)
    </label>
  </div>
</fieldset>

<div class="Wrapper">

  <span role="status"></span>

  <button type="button" class="multi"
          data-status-label="Preparing…"
          data-status-region="r"
          data-busy-msg="Preparing."
          data-finished-msg="Yay Sandwich!"
          data-failure-msg="You Got Salad"
          data-retry-msg="Re-do Sandwich">
    <span class="icon"></span>
    <span class="status">Make Sandwich</span>
  </button>

</div>

<p>
  This example is used in my post <a href="https://adrianroselli.com/2021/01/multi-function-button.html"><cite>Multi-Function Button</cite></a>.
</p>
              
            
!

CSS

              
                :root {
  --button-color: #33c;
  --wrapper-color: #555;
}

body {
  background-color: #f9f9f9;
  margin: 0;
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto,
    Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  line-height: 1.4;
  /*   font-size: 200%; */
/*  For easy 1.4.12 testing  */
  /*   line-height: 1.5; */
  /*   letter-spacing: 0.12em; */
  /*   word-spacing: 0.16em; */
}

/* Now to the headline act */

.Wrapper {
  background-color: #555;
  background: var(--wrapper-color);
  margin: 1em 0 0 0;
  padding: 4em;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.multi {
  background: #555;
  background: var(--button-color);
  border: 0.15em solid #fff;
  border-radius: 2em;
  color: #fff;
  font: inherit;
  letter-spacing: inherit;
  word-spacing: inherit;
  font-weight: bold;
  font-size: 110%;
  min-width: 16em;
  overflow: hidden;
  padding: 0.75em 2.5em;
  position: relative;
  transition: text-indent 0.2s ease-in-out;
  outline: none;
}

.busy span {
  color: #666;
}

.success {
  background: #060;
}

.failure {
  background: #c00;
}

.multi:focus,
.multi:hover {
  box-shadow: 0 0 0.8em rgba(0, 0, 0, 0.8), 0 0 1em rgba(51, 51, 51, 1);
}

.multi:active {
  transform: scale(.98);
  box-shadow: 0.15em 0.15em 0 var(--wrapper-color),
    -0.15em 0.15em 0 var(--wrapper-color), 0.15em -0.15em 0 var(--wrapper-color),
    -0.15em -0.15em 0 var(--wrapper-color), 0.25em 0.25em 0 #fff,
    -0.25em 0.25em 0 #fff, 0.25em -0.25em 0 #fff, -0.25em -0.25em 0 #fff,
    -0.3em 0 0 #fff, 0.3em 0 0 #fff;
  box-shadow: 0 0 .2em rgba(0,0,0,1),
    0 0 .2em rgba(0,0,0,1),
    -.1em .1em .2em rgba(255,255,255,.5),
    -.2em .2em .1em rgba(255,255,255,.5),
    .1em -.1em .2em rgba(0,0,0,.5),
    .2em -.2em .1em rgba(0,0,0,.5);
}

.multi:focus .icon,
.multi:hover .icon {
  transform: scale(1.9);
  border-width: 0.07em;
}

.multi.busy {
  background: #fff;
  color: #555;
  color: var(--button-color);
  border-color: #ccc;
}

.multi.busy:active {
  transform: none;
  border-color: #f00;
  box-shadow: 0 0 1em #f00;
  /* note firefox doesn't respect :active on keydown */
}

.multi.busy:focus .icon,
.multi.busy:hover .icon {
  transform: none;
}

.icon {
  position: absolute;
  left: 0.7em;
  top: 0.55em;
  width: 1.5em;
  height: 1.5em;
  overflow: hidden;
  border-radius: 100%;
  border: 0.1em solid #fff;
  background: transparent;
  transform-origin: 55% 44%;
  /*  Sandwich SVG  */
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg height='100' width='100' fill='%23fff' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M90.8 35.3a224 224 0 00-34-14c-2.7-.9-5.7-1.3-9-1.3-3.8 0-7.4.6-10.2 1.8-2.7 1-3.4 2.7-3.6 3.8l-.5.3L8.6 36.1C5.5 37.4 5 39.4 5 40.5v20c0 1 .5 3.1 3.6 4.4l34.2 14c1.6.7 3.7 1.1 6 1.1s4.3-.4 6-1l24.8-10.3a13.7 13.7 0 014-.7 18.8 18.8 0 006-1c4-1.6 5.1-3.9 5.4-5.4V51.2 41.3v-.2c0-1.3-.4-3.8-4.2-5.8zm-23.6 4.3c1.6-.7 4.2-.7 5.9 0 1.6.7 1.6 1.8 0 2.4-1.7.7-4.3.7-6 0-1.6-.6-1.6-1.7 0-2.4zm-11.8-8.5c.8-.3 2.1-.3 3 0 .7.3.7.9 0 1.2-.9.4-2.2.4-3 0-.8-.3-.8-.9 0-1.2zm-3 18.2c.9-.3 2.2-.3 3 0 .8.3.8.9 0 1.2-.8.4-2.1.4-3 0-.8-.3-.8-.9 0-1.2zm-7.3-9c.8.2.8.8 0 1.1-.8.4-2.2.4-3 0-.8-.3-.8-.9 0-1.2.8-.3 2.2-.3 3 0zm-8.9-7.4c1.7-.6 4.3-.6 6 0 1.5.7 1.5 1.8 0 2.5-1.7.6-4.3.6-6 0-1.6-.7-1.6-1.8 0-2.5zM26 42c.8-.3 2.2-.3 3 0 .8.4.8 1 0 1.2-.8.4-2.2.4-3 0-.8-.3-.8-.8 0-1.2zM90 61c0 .2-.7.9-2.2 1.5-.5.2-1 .4-1.8.5l-2.3.2a18.7 18.7 0 00-6 1L52.9 74.3c-2 .9-6 .9-8.1 0l-34.2-14-.5-.3v-4.5L42.8 69c1.6.6 3.7 1 6 1s4.3-.4 6-1l24.8-10.3a13.7 13.7 0 014-.7 18.8 18.8 0 006-1l.4-.1v4zm0-10c0 .2-.7.9-2.2 1.5-.5.2-1 .4-1.8.5l-2.3.2a18.7 18.7 0 00-6 1L52.9 64.3c-2 .9-6 .9-8.1 0l-34.2-14-.5-.3v-4.5L42.8 59c1.6.6 3.7 1 6 1s4.3-.4 6-1l24.8-10.3c1-.4 2.5-.6 4-.6 2.3 0 4.4-.4 6-1l.4-.2v4z'/%3E%3C/svg%3E");
  background-position: center center;
  background-size: 1.25em;
  background-repeat: no-repeat;
  transition: background-position 0.2s ease-in-out,
    background-image 0.5s ease-in-out, background-size 0.1s ease-in-out,
    transform 0.1s ease-out;
}

.busy .icon {
  background-image: none;
  animation: bang 1s ease-out;
  animation-iteration-count: 5;
}

.icon::before {
  content: "";
  display: block;
  opacity: 0;
  transition: opacity 0.7s ease-in-out;
}

.busy .icon::before {
  opacity: 1;
  position: absolute;
  left: 0.4em;
  top: 0.3em;
  width: 0.79em;
  height: 1.2em;
  /*  Hammer SVG  */
  content: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100%' width='66%' viewBox='0 0 66.07 100'%3E%3Cpath d='M62 16C48-1 38 0 33 0H22l-2 1c0 1-1 2-3 2h-3l-3-2-2-1H2L0 2v14c0 2 1 3 2 3h7l2-1c0-2 2-3 3-3h3c2 0 4 2 4 4v7h14v-4c0-8 9-10 24-2 2 1 4-2 3-4zM21 32l-2 59c0 2 2 3 4 3h9c2 1 4-1 4-3l-1-59z'/%3E%3C/svg%3E");
  animation: spin 1s ease-in;
  animation-iteration-count: 5;
}

.success .icon {
  background-position: 0 center;
  background-size: 1.5em;
  /*  Happy Sandwich  */
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg height='100' width='100' fill='%23fff' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M86 29.5a16.5 16.5 0 00-17.7-15.2l-39.1 3A16.5 16.5 0 0014 35c.4 4.6 2.6 8.9 6.3 11.7L23 81.3c.1 1.3.7 2.4 1.7 3.3a5 5 0 003.5 1.1l51.7-4c1.3-.1 2.4-.7 3.3-1.7a5 5 0 001.1-3.5L81.6 42c3.2-3.4 4.8-7.9 4.4-12.5zM35.8 46.6c-.1-.7-1-1.2-1.6-1.1-1 .1-1.4 1.3-1.6 2.1-.3 1.9-3.2 1.3-2.9-.6.4-2.3 2-4.4 4.5-4.5 2.3 0 4.1 1.6 4.5 3.8.4 1.9-2.5 2.1-2.9.3zm17 21.4c-4.9.4-9.1-3.8-9.4-8.5-.1-1.9 2.8-2.1 2.9-.2.2 3.2 2.9 6.1 6.3 5.8 3.4-.3 5.5-3.7 5.2-6.8-.2-1.9 2.7-2.1 2.9-.2.5 4.6-3 9.5-7.9 9.9zm16.7-23.2c-.3-.8-.8-1.9-1.9-1.9-.7 0-1.4.7-1.4 1.3-.1 1.9-3 2.1-2.9.2.1-2.2 1.6-4.2 3.9-4.5 2.5-.3 4.4 1.6 5.1 3.8.6 1.9-2.2 2.9-2.8 1.1z'/%3E%3C/svg%3E");
}

.failure .icon {
  background-size: 1em;
  background-position: center center;
  /*  Salad SVG  */
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg height='100' width='100' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 68 64'%3E%3Cpath d='M5 42h58a30 30 0 01-58 0zM3 26h62c2 0 3 1 3 3v6c0 2-1 3-3 3H3c-2 0-3-1-3-3v-6c0-2 1-3 3-3zm50-14l4-1c4 0 7 5 8 11h-7v-1c0-3-4-3-4-1v2H42l2-4c1-2-3-4-4-1l-2 5h-9v-7a2 2 0 00-4 0v7H15l-1-3c-1-2-5-1-4 2l1 1H4C2 16 4 11 8 9c3-1 6 0 9 3 1-7 5-12 10-12 4 0 7 3 9 8 3-4 8-6 12-4 2 1 4 4 5 8z' fill='%23fff' fill-rule='nonzero'/%3E%3C/svg%3E");
}

[role="status"] {
  opacity: 0.00001; /* 1 */
  position: absolute; /* 2 */
  pointer-events: none; /* 3 */
  /*
    1. mitigate situations where some screen readers erroneously don't announce opacity :0;
    2. remove from document flow
    3. ensure the absolutely positioned text won't get in the way of clicking elements
  */
}

@keyframes spin {
  0% {
    transform: rotate(45deg);
  }
  50% {
    transform: rotate(-90deg);
  }
  100% {
    transform: rotate(45deg);
  }
}

@keyframes bang {
  0% {
    box-shadow: 0 0 0 rgba(0, 0, 0, 0);
  }
  49% {
    box-shadow: 0 0 0 rgba(0, 0, 0, 0);
  }
  50% {
    box-shadow: -0.1em 0.1em 0 rgba(51, 0, 0, 1);
  }
  75% {
    box-shadow: -0.2em 0.2em 0.5em rgba(0, 0, 0, 0.2);
  }
  100% {
    box-shadow: -0.5em 0.5em 1em rgba(0, 0, 0, 0);
  }
}

@media screen and (prefers-reduced-motion: reduce) {
  .icon,
  .icon::before {
    transition: none;
  }
  .busy .icon,
  .busy .icon::before {
    animation: none;
  }
}

/* Windows High Contrast Mode Support */
/* https://blogs.windows.com/msedgedev/2020/09/17/styling-for-windows-high-contrast-with-new-standards-for-forced-colors/ */
@media screen and (-ms-high-contrast: active),
  screen and (forced-colors: active) {
  .multi:hover,
  .multi:focus {
    text-decoration: underline;
    box-shadow: 0.1em 0.1em 0 ButtonFace,
      -0.1em 0.1em 0 ButtonFace,
      0.1em -0.1em 0 ButtonFace,
      -0.1em -0.1em 0 ButtonFace,
      0.2em 0.2em 0 ButtonText,
      -0.2em 0.2em 0 ButtonText,
      0.2em -0.2em 0 ButtonText,
      -0.2em -0.2em 0 ButtonText;
  }
  .busy .icon {
    border-color: ButtonFace;
  }
  .busy .icon::before {
    content: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100%' width='66%' viewBox='0 0 66.07 100' fill='ButtonText'%3E%3Cpath d='M62 16C48-1 38 0 33 0H22l-2 1c0 1-1 2-3 2h-3l-3-2-2-1H2L0 2v14c0 2 1 3 2 3h7l2-1c0-2 2-3 3-3h3c2 0 4 2 4 4v7h14v-4c0-8 9-10 24-2 2 1 4-2 3-4zM21 32l-2 59c0 2 2 3 4 3h9c2 1 4-1 4-3l-1-59z'/%3E%3C/svg%3E");
  }
}

/* For the radio buttons */

h1, p {
  margin: 1rem 2rem;
}

h1 {
  line-height: 1;
}

fieldset {
  margin: 2rem;
  border: 0.1em solid #ddd;
}

input[type="radio"] {
  position: absolute;
  top: auto;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  width: 1px;
  height: 1px;
  white-space: nowrap;
}

input[type="radio"] + label {
  position: relative;
  display: block;
  padding: 0.5em;
  padding-left: 2em;
  max-width: calc(100% - 2em);
}

fieldset > div input[type="radio"] + label {
  margin: 0 0;
}

input[type="radio"]:focus + label {
  color: #00f;
  outline: 1px dotted #00f;
}

input[type="radio"] + label::before {
  content: "";
  background: #fff;
  border: 0.1em solid rgba(0, 0, 0, 0.75);
  background-color: rgba(255, 255, 255, 0.8);
  display: block;
  box-sizing: border-box;
  float: left;
  width: 1em;
  height: 1em;
  margin-left: -1.5em;
  margin-top: 0.15em;
  vertical-align: top;
  cursor: pointer;
  text-align: center;
  transition: all 0.1s ease-out;
}

input[type="radio"] + label::before {
  border-radius: 100%;
}

input[type="radio"]:disabled + label::before {
  border: 0.1em solid rgba(0, 0, 0, 0.1);
  background-color: rgba(0, 0, 0, 0.1);
}

input[type="radio"]:disabled + label {
  color: #ccc;
}

input[type="radio"]:checked + label::before {
  background-color: #00f;
  box-shadow: inset 0 0 0 0.15em rgba(255, 255, 255, 0.95);
}

@media screen and (-ms-high-contrast: active),
  screen and (forced-colors: active) {
  input[type="radio"] + label::after {
    display: block;
    content: "";
    position: absolute;
    top: 1.05em;
    left: 0.9em;
    width: 0.2em;
    height: 0.2em;
    border: none;
    border-radius: 50%;
    background-color: transparent;
    transition: all 0.1s ease-out;
  }
  input[type="radio"]:checked + label::after {
    top: 0.85em;
    left: 0.7em;
    width: 0.6em;
    height: 0.6em;
    background-color: windowText;
    box-sizing: border-box;
    border: 0.3em solid windowText;
    box-shadow: inset 0 0 windowText;
  }
}

              
            
!

JS

              
                var doc = document;

var result = doc.querySelector("#status_Failure");

var btn = doc.querySelector(".multi");
var btnLabel = doc.querySelector(".status");
var btnDefaultLabel = btnLabel.innerHTML;
var btnBusyLabel = btn.getAttribute("data-status-label");

var spin = doc.querySelector(".icon");
var live =
  doc.getElementById(btn.getAttribute("data-status-region")) ||
  doc.querySelector('[role="status"]');

var busyMsg = btn.getAttribute("data-busy-msg");
var finishedMsg = btn.getAttribute("data-finished-msg");
var failureMsg = btn.getAttribute("data-failure-msg");
var retryMsg = btn.getAttribute("data-retry-msg");

// let busyMsg = btn.dataset.busyMsg;
// let finishedMsg = btn.dataset.finishedMsg;
// let failureMsg = btn.dataset.failureMsg;
// let retryMsg = btn.dataset.retryMsg;

/*
 * The click event needs to:
 * 1. Update the live region (role=status) with appropriate announcements
 * 2. Remove the default click event(s) from the button while it's busy
 * 3. Add a busy click event to negate any repeats of the default function, as well
 *    as to provide an announcement to screen readers that the previous function
 *    is still working (busyMsg).
 */
var clickEvent = function () {
  this.classList.add("busy");
  btn.classList.remove("success");
  btn.classList.remove("failure");

  if (btnBusyLabel) {
    btnLabel.innerHTML = btnBusyLabel;
  }

  live.innerHTML = busyMsg;

  this.removeEventListener("click", clickEvent);
  this.addEventListener("click", busyEvent);

  // arbitrary timeout to demonstrate functionality.
  setTimeout(clearStatus, 4000);
};

// when the button is busy, add the busy event
// which will update the live region to announce
// that the process is still busy.
var busyEvent = function () {
  live.innerHTML = busyMsg;
};

/**
 * [clearStatus description]
 * @return {[type]} [description]
 */
var clearStatus = function () {
  if (result.checked) {
    // update the live region with a finished message
    // (if the function behind the button doesn't result
    // in a route/page change. e.g. a success screen.)
    live.innerHTML = failureMsg;

    // reset the button style and original text label
    btn.classList.remove("busy");
    btn.classList.add("failure");
    // btnLabel.innerHTML = btnDefaultLabel;
    btnLabel.innerHTML = failureMsg;

    // add the original clickEvent back and remove
    // the busyEvent.
    btn.addEventListener("click", clickEvent);
    btn.removeEventListener("click", busyEvent);

    setTimeout(function () {
      btn.classList.remove("failure");
      btnLabel.innerHTML = retryMsg;
      live.innerHTML = retryMsg;
    }, 2500);
  } else {
    // update the live region with a finished message
    // (if the function behind the button doesn't result
    // in a route/page change. e.g. a success screen.)
    live.innerHTML = finishedMsg;

    // reset the button style and original text label
    btn.classList.remove("busy");
    btn.classList.add("success");
    // btnLabel.innerHTML = btnDefaultLabel;
    btnLabel.innerHTML = finishedMsg;

    // add the original clickEvent back and remove
    // the busyEvent.
    btn.addEventListener("click", clickEvent);
    btn.removeEventListener("click", busyEvent);
  }

  /**
   * clear out the live region so that the content
   * won't be accessed by screen reader users
   * when navigating the screen.
   */
  setTimeout(function () {
    live.innerHTML = "";
  }, 4000);
};

btn.addEventListener("click", clickEvent);

              
            
!
999px

Console