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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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><code>button[role="switch"]</code> Toggle Pattern</h1>
<p>See: <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_switch_role">MDN, Using the <code>switch </code> role</a></p>

<h2>Demo</h2>

<div class="toggle">
  <label for="switch" class="switch">Subscribe to the thing</label>
  <button role="switch" value="test val" aria-pressed="true" aria-checked="true" id="switch" aria-describedby="state"></button>
  <span id="state" aria-live="assertive" aria-atomic="true">Subscribed</span>
  <input type="hidden" aria-hidden="true" value="Subscribed">
</div>

<h2>What is the Deal</h2>
<p><code>button[role="switch"][aria-pressed]</code> works, but unless I’m doing something wrong, our ol’ buddy JAWS seems to need <code>aria-checked</code> to effectively communicate the state. That redundant state then gets read aloud by NVDA, giving us the following narration:</p>

<h3>VoiceOver:</h3>
<blockquote>
  <p>“ON, SUBSCRIBE TO THE THING, SWITCH. SUBSCRIBED.”</p>
  <p>“OFF, SUBSCRIBE TO THE THING, SWITCH. NOT SUBSCRIBED.”</p>
</blockquote>
          
<h3>JAWS:</h3>
<blockquote>
  <p>“SUBSCRIBE TO THE THING, <strong>PRESS(ED?)</strong>, ON.”</p>
  <p>“SUBSCRIBE TO THE THING, <strong>PRESS(ED?)</strong>, OFF.”</p>
</blockquote>

<h3>NVDA:</h3>
<blockquote>
  <p>“SUBSCRIBE TO THE THING. SUBSCRIBED. PRESSED, <strong>CHECKED</strong>.”</p>
  <p>“SUBSCRIBE TO THE THING. NOT SUBSCRIBED. NOT PRESSED, NOT <strong>CHECKED</strong>.”</p>
</blockquote>
          
<h3>Narrator:</h3>
<blockquote>
  <p>“ON. SUBSCRIBE TO THE THING, BUTTON. SUBSCRIBED.”</p>
  <p>“OFF. SUBSCRIBE TO THE THING, BUTTON. NOT SUBSCRIBED.”</p>
</blockquote>

<p>I mean… there’s always checkboxes, for now.</p>
              
            
!

CSS

              
                button {
  margin: 0;
  padding: 0;
  width: 5em;
  height: 2.5em;
  border: 2px solid black;
  display: inline-block;
  vertical-align: middle;
  text-align: center;
  border-radius: 1.2em;
  position: relative;
  transition: background .15s ease-in-out;
}
button:after {
  content: "";
  position: absolute;
  height: 1.8em;
  width: 1.8em;
  border-radius: 50%;
  top: .075em;
  border: 1px solid #777;
  transition: left .15s ease-in-out;
  will-change: left;
}

label {
  display: block;
  margin-bottom: .25em;
}

button[aria-pressed="false"] {
  background: #ccc;
}
button[aria-pressed="false"]:after {
  background: #eee;
  left: 2.5em;
}
button[aria-pressed="true"] {
  background: blue; 
}
button[aria-pressed="true"]:after {
  background: #fff;
  left: .1em;
}
              
            
!

JS

              
                var toggles = document.querySelectorAll( ".toggle" );

for(i = 0, l = toggles.length; i < l; i++ ){ 
  var tog = toggles[ i ];
  tog.addEventListener( "click", function( e ) {
    var el = this.querySelector( "button" ),
        state = el.getAttribute( "aria-pressed" ) == "true",
        val = state ? "Not Subscribed" : "Subscribed",
        realInput = this.querySelector( "input" )

      el.setAttribute( "aria-pressed", state ? "false" : "true" );
      el.setAttribute( "aria-checked", state ? "false" : "true" );
 
      el.nextElementSibling.innerHTML = val;
      realInput.value = val;
  });
}
              
            
!
999px

Console