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

              
                <main>
  <h1>Keyboard-navigable tab</h1>
  
  <div id="my-tabset">
    
    <ul class="my-tabs">
      <li id="tab-1" aria-controls="tabpanel-1" role="tab" tabindex="0" class="my-tab" aria-selected="true">tab 1</li>
      <li id="tab-2" aria-controls="tabpanel-2" role="tab" tabindex="0" class="my-tab" aria-selected="false">tab 2</li>
      <li id="tab-3" aria-controls="tabpanel-3" role="tab" tabindex="0" class="my-tab" aria-selected="false">tab 3</li>
    </ul>
    <div class="my-tabpanels">
      <div id="tabpanel-1" aria-labelledby="tab-1" role="tabpanel" class="my-tabpanel" aria-hidden="false">
        <ul>
          <li><a href="#">child<br>1-1</a></li>
          <li><a href="#">child<br>1-2</a></li>
          <li><a href="#">child<br>1-3</a></li>
        </ul>
      </div>
      <div id="tabpanel-2" aria-labelledby="tab-2" role="tabpanel" class="my-tabpanel" aria-hidden="true">
        <p>plane text</p>
      </div>
      <div id="tabpanel-3" aria-labelledby="tab-3" role="tabpanel" class="my-tabpanel" aria-hidden="true">
        <ul>
          <li><a href="#">child<br>3-1</a></li>
          <li><a href="#">child<br>3-2</a></li>
          <li><a href="#">child<br>3-3</a></li>
        </ul>
      </div>
    </div>
  </div>
  
  <h2>tab</h2>
  <p>
    keydown <code>Enter</code> or <code>Space</code><br>
    focus ... The first focusable element in the tab panel
  </p>
  
  <h2>The first focusable element in the tab panel</h2>
  <p>
    keydown <code>shift</code> + <code>tab</code><br>
    focus ... Parent tab
  </p>
  
  <h2>The last focusable element in the tab panel</h2>
  <p>
    keydown <code>tab</code><br>
    focus ... Parent tab
  </p>
</main>


              
            
!

CSS

              
                @import url('//fonts.googleapis.com/css?family=Roboto:100,400,700');
html, * {
  font-family: 'Roboto';
  box-sizing: border-box;
}

body {
  background: black;
  color: white;
}

main {
  margin: 2rem auto;
  max-width: 600px;
  width: calc(100% - 2rem);
}

h1 {
  line-height: 1.4;
  font-size: 2rem;
  margin: 1rem 0;
  font-weight: 100; 
}
h2 {
  line-height: 1.4;
  border-bottom: 1px solid dimgray;
  margin: 2rem 0 0;
}

p {
  margin: 1rem;
  line-height: 1.8;
}

code {
  background: dimgray;
  padding: 0 0.25em;
  margin: 0 0.25em;
  border-radius: 0.1em;
}

.my-tabs {
  position: relative;
  display: flex;
  align-items: flex-end;
  height: 4rem;
}
.my-tab {
  display: flex;
  background: lightgray;
  color: black;
  width: 50%;
  flex-grow: 1;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  cursor: pointer;
  border-radius: 0.2rem 0.2rem 0 0;
  transition: padding .5s;
  
  ~ .my-tab {
    margin-left: 2px;
  }
  
  &[aria-selected="true"] {
    background: white;
    padding: 1.5rem 1rem;
  }
}

.my-tabpanels {
  position: relative;
  background: white;
  color: black;
}

.my-tabpanel {
  display: block;
  overflow: hidden;
  
  &[aria-hidden="true"] {
    height: 0;
    opacity: 0;
  }
  
  
  ul {
    margin: 1rem;
    display: flex;
    li {
      flex-grow: 1;
      
      a {
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0.5rem;
        padding: 1rem 0.5rem;
        background: whitesmoke;
        line-height: 1.4;
        text-align: center;
        text-decoration: none;
      }
    }
  }
}
              
            
!

JS

              
                class SetupTab {
  withShiftKey = false;

  constructor($target) {
    if (!$target) return;
    this.$target = $target;

    const tabs = $target.querySelectorAll('[role="tab"]');
    if (!tabs.length) return;
    this.$tabs = tabs;

    const tabpanels = $target.querySelectorAll('[role="tabpanel"]');
    if (!tabpanels.length) return;
    this.$tabpanels = tabpanels;
    
    // init
    this.init();
  }

  init() {
    [].slice.call(this.$tabs).map((tab, index) => {
      tab.addEventListener("click", this.setCurrent.bind(this), false);
      tab.addEventListener("keydown", this.setCurrent.bind(this), false);
    });
    [].slice.call(this.$tabpanels).map((tabpanel) => {
      const $focusableElements = tabpanel.querySelectorAll('a,input,area,button,select,[tabindex],textarea');
      if($focusableElements.length) {
        const $parentTab = this.$target.querySelector(`#${tabpanel.getAttribute("aria-labelledby")}`);
        
        const firstTarget = $focusableElements[0];

        // with shift key ?
        firstTarget.addEventListener('keydown', this.setWithShift.bind(this), false);
        firstTarget.addEventListener('keyup', this.setWithShift.bind(this), false);
        // Focus returns to tab
        firstTarget.details = {target: $parentTab, direction: -1};
        firstTarget.addEventListener('keydown', this.returnsFocusParentTab.bind(this), false);
          
        const lastTarget = $focusableElements[$focusableElements.length - 1];
        
        // with shift key ?
        lastTarget.addEventListener('keydown', this.setWithShift.bind(this), false);
        lastTarget.addEventListener('keyup', this.setWithShift.bind(this), false);
        // Focus returns to tab
        lastTarget.details = {target: $parentTab, direction: 1};
        lastTarget.addEventListener('keydown', this.returnsFocusParentTab.bind(this), false);
      }
    });
  }

  setCurrent(e) {
    let isValid = e.type === 'click';
    const isValidKey = (e.code === "Space" || e.key === "Spacebar" || e.code === "Enter" || e.key === "Enter");
    if((e.type === 'keydown') && isValidKey) {
      isValid = true;
      e.preventDefault();
    }
    if(isValid) {
      const tabpanelName = e.currentTarget.getAttribute("aria-controls");
      const $target = this.$target.querySelector(`#${tabpanelName}`);

      [].slice.call(this.$tabs).map((tab) => {
        tab.setAttribute("aria-selected", false);
      });

      [].slice.call(this.$tabpanels).map((tabpanel) => {
        tabpanel.setAttribute("aria-hidden", true);
        const $child = tabpanel.querySelectorAll('a,input,area,button,select,[tabindex],textarea');
        if ($child.length) {
          [].slice.call($child).map(el => {
            el.setAttribute("tabindex", -1);
          });
        }
      });

      e.currentTarget.setAttribute("aria-selected", true);
      $target.setAttribute("aria-hidden", false);
      const $targetChild = $target.querySelectorAll('a,input,area,button,select,[tabindex],textarea');
      if ($targetChild.length) {
        [].slice.call($targetChild).map(el => {
          el.setAttribute("tabindex", 0);
        });
      }

      //  Focus on the first focusable element
      if(isValidKey && e.type === 'keydown' && $targetChild.length)
        $targetChild[0].focus();
    }
  }

  setWithShift(e) {
    if (e.code.match(/Shift/g) || e.key === "Shift") {
      if(e.type === 'keydown')  this.withShiftKey = true;
      if(e.type === 'keyup')  this.withShiftKey = false;
    }
  }

  returnsFocusParentTab(e) {
    const option = e.currentTarget.details;
    const validKey = e.code === "Tab" || e.key === "Tab";
    if(
      validKey &&
      ((option.direction === -1 && this.withShiftKey) ||
      (option.direction === 1 && !this.withShiftKey))
    ) {
      e.preventDefault();
      option.target.focus();
      this.withShiftKey = false;
    }
  }
  
}

/**
 * LOAD
 **/

// target
const $target = document.getElementById("my-tabset");

// instance
const myTab = new SetupTab($target);

              
            
!
999px

Console