123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="body">
<div id="app">
  <h1>
    Settings
  </h1>
  <p style="font-size: .825em; margin-top: -.5em;">
    This demo works in Firefox 63+, Chrome, and Safari 12+.
  </p>
  <ul class="settings-list">
    <li>
      <switch-btn
                  id="enable_all"
                  text="Enable Settings"
                  switch="force"
                  labelled-state>
      </switch-btn>
    </li>
    <li>
      <switch-btn
                  id="drk_mode"
                  text="Enable Dark Mode"
                  switch="force"
                  check
                  disable>
      </switch-btn>
    </li>
    <li>
      <switch-btn
                  id="remove_transitions"
                  text="Remove Background Transition"
                  switch="force"
                  disable>
      </switch-btn>
    </li>
    <li>
      <switch-btn
                  id="reveal"
                  text='Show "On/Off" Labels'
                  switch="force"
                  disable>
      </switch-btn>
    </li>
  </ul>
</div>

<style id="switch_styles">
  button::-moz-focus-inner {
    border-style: none;
    padding: 0;
  }

  .switch-btn {
    background: none;
    border: 0;
    color: inherit;
    font: inherit;
    line-height: 1;
    padding: .75em 3.75em .75em .5em;
    position: relative;
    text-align: left;
    width: 100%;
  }

  .switch-btn span {
    pointer-events: none;
  }

  .switch-btn__knob {
    background: #fff;
    border-radius: 1em;
    border: 1px solid #2a2a2a;
    bottom: .5em;
    box-shadow: inset -1.625em 0 0 1px rgba(120, 120, 120, 1);
    height: 1.5em;
    margin: auto;
    position: absolute;
    right: 0;
    top: .5em;
    transition:
      border .2s ease-in-out,
      box-shadow .2s ease-in-out;
    width: 3em;
  }

  .switch-btn[aria-checked="true"] .switch-btn__knob {
    border: 1px solid rgba(0, 2, 120, 1);
    box-shadow: inset 1.625em 0 0 1px #2196f3;
  }

  .switch-btn[disabled] {
    opacity: .525;
  }

  .switch-btn__knob:before {
    border: 2px solid;
    border-color: rgba(33, 150, 243, 0);
    border-radius: 1em;
    bottom: 0;
    content: "";
    left: 0em;
    position: absolute;
    right: 0em;
    top: 0em;
    transition:
      border-color .2s ease-in-out,
      bottom .2s ease-in-out,
      left .2s ease-in-out,
      right .2s ease-in-out,
      top .2s ease-in-out;
  }

  .switch-btn:focus .switch-btn__knob:before {
    border-color: rgba(33, 150, 243, 1);
    bottom: -.325em;
    left: -.325em;
    right: -.325em;
    top: -.325em;
  }

  .switch-btn--show-labels {
    padding-right: 4.5em;
  }

  .switch-btn--show-labels .switch-btn__knob {
    box-shadow: inset -2.05em 0 0 1px rgba(120, 120, 120, 1);
    width: 3.5em;
  }

  .switch-btn--show-labels[aria-checked="true"] .switch-btn__knob {
    box-shadow: inset 2.05em 0 0 1px #2196f3;
  }

  .switch-btn--show-labels .switch-btn__knob:after {
    color: #fff;
    content: "Off";
    display: block;
    font-size: .825em;
    line-height: 1.825;
    padding: 0 .5em;
    text-align: right;
  }

  .switch-btn--show-labels[aria-checked="true"] .switch-btn__knob:after {
    content: "On";
    text-align: left;
  }

  @media ( prefers-reduced-motion: reduce ) {
    .switch-btn__knob:before,
    .switch-btn__knob {
      transition: none;
    }
  }

  @media screen and ( -ms-high-contrast: active ) {
    .switch-btn__knob:after {
      background-color: windowText;
    }
  }
</style>
</div> <!-- /#body -->
            
          
!
            
              /**
 * Demo styles
 */
*,
*:before,
*:after {
  box-sizing: border-box;
}

body {
  background: #ccc;
  font-family: Helvetica, Arial;
}

body.transition {
  transition: background .42s ease-in-out;
}

h1 {
  margin-top: 0;
}

#app {
  background: #fefefe;
  border-radius: 2px;
  color: #111;
  margin: 1em auto;
  max-width: 25em;
  padding: 1.5em;
}

#app.transition {
  transition:
    background .42s ease-in-out,
    color .42s ease-in-out;
}

.settings-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.setting-list li:before {
  content: "\200B";
  position: absolute;
  /*
    see: unfetteredthoughts.net/2017/09/26/voiceover-and-list-style-type-none
  */
}

.settings-list li:not(:last-child) {
  border-bottom: 1px solid #555;
  margin: 0;
}

.desc-ex {
  font-size: .825rem;
  margin-top: .125em;
  padding-left: .5rem;
}

/**
 * Dark Mode Styles
 */
.dark-mode {
  background: #1a2a2a;
}

.dark-mode #app {
  background: rgba(100,100,100,.2);
  color: #eee;
}


/**
 * Reduced Motion Prefs
 */
@media ( prefers-reduced-motion: reduce ) {
  body.transition,
  .transition #app {
    transition: none;
  }
}

            
          
!
            
              /**
  Creating the custom Switch 
*/
(function ( doc, w ) {
  /**
   * Write the CSS as CSS, because CSS.
   * Store it for pulling into the Switch's template.
   */
  const switchStyles = doc.getElementById('switch_styles').innerHTML;

  /**
   * Build a reusable template to serve as the
   * base for each instance of the switch.
   *
   * A button element serves a the base here due to unexpected
   * announcements (redundant label announcements, and the 
   * incorrect state of "read only") that were being made when
   * testing with the JAWS screen reader.
   */
  const switchTemplate = doc.createElement('template');
  switchTemplate.innerHTML = '<style>' + switchStyles + '</style>' + `
    <button type="button" class="switch-btn">
      <!-- role presentation is needed to ensure NVDA won't have redundant announcements -->
      <span class="switch-btn__text" role="presentation"></span>
      <span class="switch-btn__knob" aria-hidden="true"></span>
      <!-- hide the knob, especially since it can have "on" and "off" CSS content that 
           we don't want announced -->
    </button>
  `;

  /**
   * Define the custom element
   */
  let btn;
  let text;

  class switchBtn extends HTMLElement {
    constructor () {
      super();

      this._shadowRoot = this.attachShadow({ mode: 'open' });
      this._shadowRoot.appendChild(switchTemplate.content.cloneNode(true));
      btn = this.$btn = this._shadowRoot.querySelector('.switch-btn');
      text = this.$text = this._shadowRoot.querySelector('.switch-btn__text');

      btn.addEventListener('click', function () {
        this.setAttribute('aria-checked', this.getAttribute('aria-checked') === 'true' ? 'false' : 'true');
      });
    }

    connectedCallback() {
      const accName = this.getAttribute('text');

      if ( accName && accName !== '' ) {
        text.innerHTML = accName;
      }
      else {
        // Controls need names. Give it a name!
        text.innerHTML = "Give me a name!";
        console.warn('Provide a "text" value.')
      }
      if ( this.hasAttribute('check') ) {
        btn.setAttribute('aria-checked', 'true');
      }
      else {
        btn.setAttribute('aria-checked', 'false');
      }
      if ( this.hasAttribute('disable') ) btn.disabled = true;
      if ( this.hasAttribute('switch') ) btn.setAttribute('role', 'switch');
      if ( this.hasAttribute('labelled-state') ) btn.classList.add('switch-btn--show-labels');
    }
  }

  customElements.define('switch-btn', switchBtn);
})( document, window, undefined );






/**
  The following creates the individual funtionality 
  for each switch control. It is not essential to 
  the general creation of the switch custom element
  and its baseline functionality.
*/
(function ( doc, w ) {
/**
 * When custom elements have open Shadow DOMs, this means that
 * these elements can be written in a generic manner without
 * worrying about how each instance might need to be used
 * beyond their general UX.
 *
 * This demo file shows how once all the custom elements are in
 * place, targeted JavaScript can be written to pierce unique
 * custom element's Shadow DOMs, and change the state of the
 * web components, as well as content outside of them.
 */


  /**
   * Keep track of all the necessary elements.
   */
  /* Light DOM */
  const body              = doc.getElementsByTagName('BODY')[0];
  const app               = doc.getElementById('app');

  const darkModeDesc      = doc.getElementById('dark_desc');
  const revealDesc        = doc.getElementById('reveal_desc');

  const settingsSwitch    = doc.getElementById('enable_all');
  const darkSwitch        = doc.getElementById('drk_mode')
  const transitionSwitch  = doc.getElementById('remove_transitions');
  const descSwitch        = doc.getElementById('reveal');

  /* Shadow DOM */
  const settingsBtn     = settingsSwitch.shadowRoot.querySelector('.switch-btn');
  const darkModeBtn     = darkSwitch.shadowRoot.querySelector('.switch-btn');
  const transitionsBtn  = transitionSwitch.shadowRoot.querySelector('.switch-btn');
  const descriptionsBtn = descSwitch.shadowRoot.querySelector('.switch-btn');


  /**
   * Just to have a function to toggle the
   * enabled/disabled state of the other
   * switches in the document.
   */
  const enableAll = function () {
    if ( settingsBtn.getAttribute('aria-checked') === 'true' ) {
      darkModeBtn.disabled = false;
      transitionsBtn.disabled = false;
      descriptionsBtn.disabled = false;
    }
    else {
      darkModeBtn.disabled = true;
      transitionsBtn.disabled = true;
      descriptionsBtn.disabled = true;
    }
  };

  settingsBtn.addEventListener('click', enableAll);



  /**
   * Don't want a flash of color changing on page load,
   * so add CSS transition after the fact via a timeout.
   *
   * Also check for if reduced motion is on.  Because if so,
   * then don't even add the transition.
   */
  const motionQuery = w.matchMedia('(prefers-reduced-motion)');
  const checkMotion = function () {
    if ( !motionQuery.matches ) {
      app.classList.add('transition');
      body.classList.add('transition');
    }
  };

  const removeTransitionClass = function () {
    if ( transitionsBtn.getAttribute('aria-checked') === 'true' ) {
      app.classList.remove('transition');
      body.classList.remove('transition');
    }
    else {
      checkMotion();
    }
  };

  setTimeout(function () {
    checkMotion();
  }, 10);

  transitionsBtn.addEventListener('click', removeTransitionClass);


  /**
   * Toggle the class on the body element based
   * on whether the darkModeInput switch is On (checked)
   * or not.  Run function on load to immediately set.
   */
  const setMode = function () {
    if ( darkModeBtn.getAttribute('aria-checked') === 'true' ) {
      body.classList.add('dark-mode');
    }
    else {
      body.classList.remove('dark-mode');
    }
  };

  setMode();
  darkModeBtn.addEventListener('click', setMode);



  /**
   * Show or hide the on/off knob labels
   */
  const labelKnobs = function () {
    if ( descriptionsBtn.getAttribute('aria-checked') === 'true' ) {
      settingsBtn.classList.add('switch-btn--show-labels');
      darkModeBtn.classList.add('switch-btn--show-labels');
      transitionsBtn.classList.add('switch-btn--show-labels');
      descriptionsBtn.classList.add('switch-btn--show-labels');
    }
    else {
      settingsBtn.classList.remove('switch-btn--show-labels');
      darkModeBtn.classList.remove('switch-btn--show-labels');
      transitionsBtn.classList.remove('switch-btn--show-labels');
      descriptionsBtn.classList.remove('switch-btn--show-labels');
    }
  };

  descriptionsBtn.addEventListener('click', labelKnobs);

})( document, window, undefined );

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console