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.

            
              <main>
  <h1>HTML details as a progressive enhancement tactic</h1>
  <p>The details element can be used as a baseline for a collapsible  menu. 
  <br>
    It could be further enhanced to support collapsing the menu via the Esc key, navigating the menu items via the arrow keys and setting a keyboard trap on the expanded menu.
  </p>
<details>
  <summary aria-haspopup="menu">About me</summary>
  <ul role="menu" id="menu">
  </ul>
</details>
</main>
            
          
!
            
              body {
  background-color: #555;
  width: 680px;
  margin: 0 auto;
}
h1 {
  line-height: 1.4;
}
main {
  background-color: #eee;
  line-height: 1.5;
  padding: 3rem;
  min-height: 100vh;
}
details {
  display: inline-block;
}
summary {
  background-color: #ddd;
  padding: 0.5rem;
  border-bottom: solid 2px transparent;
}

[role="menu"] {
  background-color: #ddd;
  list-style: none;
  margin: 0;
  padding: 0;
}

summary, [role="menuitem"] {
  color: black;
  display: block;
  line-height: 2;
  padding: 0.5rem 2rem 0.5rem 1rem;
  text-decoration: none;
}

summary:focus, [role="menuitem"]:focus {
  outline: 0;
  background-color: #777;
  border-bottom: solid 2px #000;
  color: #fff;
}

            
          
!
            
              const obj = {"items": [
   {"name": "kind", "target": "/kind", "index": 1, "selected": false},
   {"name": "gentle", "target": "/gentle", "index": 2, "selected": true},
   {"name": "funny", "target": "/funny", "index": 3, "selected": false}   
]}
const details = document.querySelector("details");
const summary = document.querySelector("summary");

function init() {
  let html = "";
  function setSelected (i) {
    if (obj.items[i].selected) {
      return "<li id='mi_" + obj.items[i].index + "' tabindex='-1' role='menuitem' aria-selected='true'>" + obj.items[i].name + "</li>";
    } else {
      return "<li id='mi_" + obj.items[i].index + "' tabindex='-1' role='menuitem'>" + obj.items[i].name + "</li>";
    }
  } 
  for (let i = 0; i < obj.items.length; i++) {  
    html += setSelected(i);
  }
  document.getElementById("menu").innerHTML = html;
}


function kb(e) {
  if (e.target === summary || details.contains(e.target)) {
    e.preventDefault();
    e.stopPropagation();
    let kc = e.keyCode;
    let selectedItem = getSelectedItem();
    let selectedItemIndex = selectedItem["index"];
    const firstMenuItem = 1;
    const lastMenuItem = obj.items["length"];
    function getSelectedItem () {
      for (let i = 0; i < obj.items.length; i++) {
        if (obj.items[i].selected === true) return obj.items[i];
      }
    }  
    function undateMenu(selectedMenuItem) {
      function setSelected (i) {
        if (obj.items[i].index === selectedMenuItem) {
          obj.items[i].selected = true;
        } else {
          obj.items[i].selected = false;
        }
      } 
      for (let i = 0; i < obj.items.length; i++) {  
        setSelected(i);
      }
 
    }
    
    function nextMenuItem () {
      if(selectedItemIndex + 1 > lastMenuItem) {
        undateMenu(1);
        document.getElementById("mi_" + selectedItemIndex).focus();
      } else {
        undateMenu(selectedItemIndex + 1);
        document.getElementById("mi_" + selectedItemIndex++).focus();
      }      
    }
    function previousMenuItem () {
       if(selectedItemIndex - 1 < firstMenuItem) {
        undateMenu(lastMenuItem);
        document.getElementById("mi_" + lastMenuItem).focus();
      } else {
        undateMenu(selectedItemIndex--);
        document.getElementById("mi_" + selectedMenuItem - 1).focus();
      }      
    }

    if(details.open) {
      switch (kc) {
        case 13:
          details.open = false;
          undateMenu(selectedItemIndex);
          summary.focus();          
          break;
        case 32:
          details.open = false;
          undateMenu(selectedItemIndex);
          summary.focus();          
          break;
        case 40:
          nextMenuItem();
          break;
        case 38:
          previousMenuItem();
          break;
        case 27:
          details.open = false;
          summary.focus();          
          break;
        default:
          return;
      }
    } else {
      switch(kc) {
        case 13:
          details.open = true;
          break;
        case 32:
          details.open = true;
          break;
        default:
          return;
      }
    }    
  }
}

document.addEventListener("keydown", kb);
init();
            
          
!
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