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>Checkboxes with expandable content</h1>

<h2>Using a button</h2>

<p>
  Checkbox can be clicked to change state. Button controls the collapse state.
</p>

<p><em>WCAG issue</em>:</p>
<ul>
  <li>user expect to click a label to toggle the checkbox.</li>
</ul>

<input type="checkbox" id="opt1" name="option1" checked=false aria-labelledby="opt1-lbl">
<button id="opt1-lbl" class="label expand-control" aria-expanded="false" aria-control='details1'>
  Option 1
  <span class="state btn-plain" aria-hidden="true"></span>
</button>
<div id="details1" class="collapsable" hidden aria-live="polite">
  <div>Make checkbox great again!</div>
</div>

<h2>Using a text element</h2>

<p><em>WCAG issues</em>:</p>
<ul>
  <li>user expect to click a label to toggle the checkbox but label is controlling neither the checkbox or the expand/collapse.</li>
  <li>The label is read 3 times: as checkbox label, as text and as expand/collapse label.</li>
</ul>

<input type="checkbox" id="opt2" name="option2" checked=false aria-labelledby="opt2-lbl">
<span id="opt2-lbl" class="label">
  Option 2
</span>
<button class="btn-plain expand-control" aria-labelledby="opt2-lbl" aria-expanded="false" aria-control='details2'>
  <span class="state" aria-hidden="true"></span>
</button>

<div id="details2" class="collapsable" hidden aria-live="polite">
  <div>Make checkbox great again!</div>
</div>

<h2>Checkbox with label and a button for expand/collapse</h2>

<p><em>WCAG issues</em>:</p>
<ul>
  <li>The expand/collapse button could be a small target. Some padding can make it more easy to target.</li>
</ul>

<input type="checkbox" id="opt3" name="option3" checked="false" />
<label id="opt3-lbl" for="opt3" class="label">Option 3</label>
<button class="btn-plain expand-control" 
        aria-labelledby="opt3-lbl details3" aria-expanded="false" 
        aria-control='details3' >
  <span class="state" aria-hidden="true"></span>
</button>

<div id="details3" class="collapsable" hidden aria-live="polite" >
  <div>Make checkbox great again!</div>
</div>

<h2>Conclusion</h2>

<p>Best option seems to be the third one.</p>

<p>When a node is collapsed, to be sure that polite behavior is properly respected, the node children are detached from the DOM. They will be reinserted on expand. This ensure that all contant is properly read on expand.</p>

<p>To for Nvda reading the content on expand the solution is not to reference content with aria-owns, but rather reference content as label (so aria-labelledby references both checkbox label and expanded content). User can make force content to be read using Nvda shortcut INS + [NumPad 5].</p>
              
            
!

CSS

              
                
// Expand/collapse button

.label,
.btn-plain {
  background: none;
  text-align: left;
  padding: 0;
  border: none;
  font-weight: bold;
}

.btn-plain {
  display: inline-block;
  background-color: #eee;
  &:hover {
    background-color: #aae;
  }
  
  & > span {
    display: block;
  }
}

// Enpand/collapse state rendering

.expand-control {
  
  .state {
    padding: .25em .5em;
    font-size: 1.25em;
    border: 1px solid #ddd;
  }
  
  &[aria-expanded=true] .state:before {
    content: '-';
  }
  &[aria-expanded=false] .state:before {
    content: '+';
  }
}

// Collapsable block

.collapsable {
  border: 1px solid #ddd;
  padding: .5em;
  margin: 1em 2em 1em 1em;
}



              
            
!

JS

              
                $('.expand-control').on('click', function(e) {
  var $btn = $(e.currentTarget);
  var state = $btn.attr('aria-expanded');
  
  var blockId = $btn.attr('aria-control');
  var $block = $('#'+blockId);
  
  var isExpanding = 'true' != state;
  $btn.attr('aria-expanded', (isExpanding) ? 'true' : 'false');
  
  $block.toggle((isExpanding) ? true : false);
  if ( isExpanding ) {
    var $content = $block.data('expandContent');
    $block.append($content);
  }
  else {
    $block.children().detach();
  }
});

// Collapse all collapsable elements

$(".collapsable[hidden]").each(function(idx, node) {
  var $node = $(node);
  var $content = $node.children();
  $node.data('expandContent', $content);
  $content.detach();
});
              
            
!
999px

Console