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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="c-accordion">
  <div class="c-accordion__header js-accordion" aria-expanded="false" data-state="aria-expanded" data-state-element="js-accordion" data-state-scope="js-accordion" tabindex="0">First Accordion - toggling ARIA attributes</div>
  <div class="c-accordion__content-wrapper">
    <div class="c-accordion__content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce laoreet ultricies risus, sit amet congue nulla mollis et. Suspendisse bibendum eros sed sem facilisis ornare. Donec sit amet erat vel dui semper pretium facilisis eget nisi. Fusce consectetur vehicula libero vitae faucibus. Nullam sed orci leo. Fusce dapibus est velit, at maximus turpis iaculis in. Pellentesque ultricies ultrices nisl, eu consequat est molestie sit amet. Phasellus laoreet magna felis, ut vulputate justo tempor eu. Nam commodo aliquam vulputate.
    </div>
  </div>
</div>

<div class="c-accordion js-accordion">
  <div class="c-accordion__header js-accordion-header" aria-expanded="false" data-state="aria-expanded, is-active" data-state-element="js-accordion-header, js-accordion" data-state-scope="js-accordion, js-accordion" tabindex="0">Second Accordion - toggling both ARIA attributes and stateful classes</div>
  <div class="c-accordion__content-wrapper">
    <div class="c-accordion__content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce laoreet ultricies risus, sit amet congue nulla mollis et. Suspendisse bibendum eros sed sem facilisis ornare. Donec sit amet erat vel dui semper pretium facilisis eget nisi. Fusce consectetur vehicula libero vitae faucibus. Nullam sed orci leo. Fusce dapibus est velit, at maximus turpis iaculis in. Pellentesque ultricies ultrices nisl, eu consequat est molestie sit amet. Phasellus laoreet magna felis, ut vulputate justo tempor eu. Nam commodo aliquam vulputate.
    </div>
  </div>
</div>
              
            
!

CSS

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

.c-accordion {
  margin-bottom:20px;
}

.c-accordion__header {
  user-select:none;
  background-color:#ccc;
  padding:10px;
  cursor:pointer;
}

.c-accordion__content {
  padding:10px;
}

.c-accordion__content-wrapper {
  max-height:0;
  overflow:hidden;
  transition:max-height .5s;
}

.c-accordion__header[aria-expanded="true"],
.c-accordion.is-active .c-accordion__header {
  background-color:darken(#ccc, 25%);
}

.c-accordion__header[aria-expanded="true"] + .c-accordion__content-wrapper,
.c-accordion.is-active .c-accordion__content-wrapper {
  max-height:500px;
}
              
            
!

JS

              
                (function(){
  // CLOSEST PARENT HELPER FUNCTION
  //----------------------------------------------
  var closestParent = function(child, match) {
    if (!child || child == document) {
      return null;
    }
    if (child.classList.contains(match) || child.nodeName.toLowerCase() == match) {
      return child;
    }
    else {
      return closestParent(child.parentNode, match);
    }
  },

  // REUSABLE FUNCTION
  //----------------------------------------------

  // Change function
  processChange = function(elem){
    // Grab classes list and convert to array
    var dataState = elem.getAttribute('data-state');
    dataState = dataState.split(", ");

    // Grab linked elements list and convert to array
    var dataStateElement = elem.getAttribute('data-state-element');
    dataStateElement = dataStateElement.split(", ");

    // Grab data-state-behaviour list if present and convert to array
    if(elem.getAttribute("data-state-behaviour")) {
      var dataStateBehaviour = elem.getAttribute("data-state-behaviour");
      dataStateBehaviour = dataStateBehaviour.split(", ");
    }

    // Grab data-scope list if present and convert to array
    if(elem.getAttribute("data-state-scope")) {
      var dataStateScope = elem.getAttribute("data-state-scope");
      dataStateScope = dataStateScope.split(", ");
    }

    // Loop through all our dataStateElement items
    for(var b = 0; b < dataStateElement.length; b++) {
      // Grab elem references, apply scope if found
      if(dataStateScope && dataStateScope[b] !== "false") {
        // Grab parent
        var elemParent = closestParent(elem, dataStateScope[b]),

        // Grab all matching child elements of parent
        elemRef = elemParent.querySelectorAll("." + dataStateElement[b]);

        // Convert to array
        elemRef = Array.prototype.slice.call(elemRef);

        // Add parent if it matches the data-state-element and fits within scope
        if(dataStateScope[b] === dataStateElement[b] && elemParent.classList.contains(dataStateElement[b])) {
          elemRef.unshift(elemParent);
        }
      }
      else {
        var elemRef = document.querySelectorAll("." + dataStateElement[b]);
      }
      // Grab state we will add
      var elemState = dataState[b];
      // Grab behaviour if any exists
      if(dataStateBehaviour) {
        var elemBehaviour = dataStateBehaviour[b];
      }
      // Cycle through target elements
      for(var c = 0; c < elemRef.length; c++) {
          // Find out if we're manipulating aria-attributes or classes
          var toggleAttr;
          if(elemRef[c].getAttribute(elemState)) {
              toggleAttr = true;
          }
          else {
              toggleAttr = false;
          }
          if(elemBehaviour === "add") {
              if(toggleAttr) {
                  elemRef[c].setAttribute(elemState, true);
              }
              else {
                  elemRef[c].classList.add(elemState);
              }
          }
          else if(elemBehaviour === "remove") {
              if(toggleAttr) {
                  elemRef[c].setAttribute(elemState, false);
              }
              else {
                  elemRef[c].classList.remove(elemState);
              }
          }
          else {
              if(toggleAttr) {
                  if(elemRef[c].getAttribute(elemState) === "true") {
                      elemRef[c].setAttribute(elemState, false);
                  }
                  else {
                      elemRef[c].setAttribute(elemState, true);
                  }
              }
              else {
                  elemRef[c].classList.toggle(elemState);
              }
          }
      }
    }
  };

  // Run when DOM has finished loading
  document.addEventListener("DOMContentLoaded", function() {

    // Grab all elements with required attributes
    var elems = document.querySelectorAll("[data-state][data-state-element]");

    // Loop through if any are found
    for(var i = 0; i < elems.length; i++){
      // Add event listeners to each one
      elems[i].addEventListener("click", function(e){
        // Prevent default action of element
        e.preventDefault();
        // Run state function
        processChange(this);
      });    
      // Add keyboard event for enter key to mimic anchor functionality
      elems[i].addEventListener("keypress", function(e){
          if(e.which === 13) {
              // Prevent default action of element
              e.preventDefault();
              // Run state function
              processChange(this);
          }
      });
    }
  });
})();
              
            
!
999px

Console