Lists do not contain only <li> elements and script supporting elements (<script> and <template>)

Viewed 1797

I am testing the accessibility of my webpage in Google chrome light house and getting the following error. I understand the importance of lists being structured properly to help improve the accessibility of the webpage. I am wondering why do I get the following error when I have the list structured properly.

enter image description here

HTML CODE

 <div>
    <ul class="oss-tabs custom-tab">
      <li class="tab-item" [ngClass]="{active: tab.type === currentTab}" 
        *ngFor="let tab of tabs" 
        (click)="switchTab(tab.type)" role="button" 
        id="{{tab.title}}">{{tab.title}}</li>
     </ul>
 </div>

2 Answers

If you specify role="button" it's no longer a list element but a <button> . And you only can have <li> inside your list.

Better solution is following markup:

  <li id="{{tab.title}}" class="tab-item" [ngClass]="{active: tab.type === currentTab}" *ngFor="let tab of tabs" >
    <button type="button" (click)="switchTab(tab.type)">{{tab.title}}</button>
  </li>

Reasons why this is better: By adding role="button" you say that this element is a button but I'm not sure this also adds all accessibility-benefits to it. Like you can tab to it with your keyboard, you can click enter to trigger the button click, stuff like this.

Also don't forget to specify type="button" for the button inside of your list, otherwise it would be a submit button (and this would be wrong for this place)

You can style your button any way you want, so styling is not an excuse for not using a button element here.

Relevant WAI-ARIA attributes for tabs

There are aria attributes that are designed for tabs.

The main ones you need to worry about are role="tablist" (on the tab container) ,role="tab" (for the tab control) and role="tabpanel" (for the actual tab content).

This signals to some assistive technology to switch into tabs mode so you can navigate with arrow keys etc. (although you still need to add navigation via arrow keys etc. via JavaScript as most screen readers do not do this automatically), it also makes the associations clear etc. etc.

W3C have a great example of how to set up tabs, the below is how you could adapt your mark-up to match that pattern.

 <div>
    <ul class="oss-tabs custom-tab" role="tablist">
      <li class="tab-item" [ngClass]="{active: tab.type === currentTab}" 
        *ngFor="let tab of tabs" 
        (click)="switchTab(tab.type)"
        id="{{tab.title}}"
        role="tab">{{tab.title}}</li>
     </ul>
 </div>

A better way to do tabs

The downside of the above is you need to manage focus states yourself etc.

Even better is to make the list items presentational as a tablist looks for children with role="tab" and doesn't need the unnecessary noise of announcing a list item.

Inclusive components did a great writeup on creating accessible tabbed interfaces, an example of the mark-up they recommend is as follows:-

<div class="tabbed">
  <ul role="tablist">
    <li role="presentation">
      <a href="#section1" role="tab" id="tab1" aria-selected="true">Section 1</a>
    </li>
    <li role="presentation">
      <a href="#section2" role="tab" id="tab2" tabindex="-1">Section 2</a>
    </li>
  </ul>
  <section id="section1" role="tabpanel" tabindex="-1" aria-labelledby="tab1">
    <h2>Section 1</h2>
    <p>Section 1 text</p>
  </section>
  <section id="section2" role="tabpanel" tabindex="-1" aria-labelledby="tab2" hidden="">
    <h2>Section 2</h2>
    <p>Section 2 text</p>
  </section>
</div>

The full example from inclusive components

Below is a fully working version of the example from inclusive components.

There is no difference between this and the example HTML shown above, it is just that all the aria attributes, roles etc. are applied (and updated) by JavaScript in the below example.

Things to note in the example

The use of aria-selected to signal the current tab

The use of anchors instead of buttons linked to a relevant section with a corresponding ID (semantically correct and a great fallback for no JS).

keyboard controls - adding arrow keys to swap tabs.

tab order - if you press Tab and the corresponding tabpanel has focusable content it correctly tabs to that content and not the next tab.

(function() {
  // Get relevant elements and collections
  const tabbed = document.querySelector('.tabbed');
  const tablist = tabbed.querySelector('ul');
  const tabs = tablist.querySelectorAll('a');
  const panels = tabbed.querySelectorAll('[id^="section"]');
  
  // The tab switching function
  const switchTab = (oldTab, newTab) => {
    newTab.focus();
    // Make the active tab focusable by the user (Tab key)
    newTab.removeAttribute('tabindex');
    // Set the selected state
    newTab.setAttribute('aria-selected', 'true');
    oldTab.removeAttribute('aria-selected');
    oldTab.setAttribute('tabindex', '-1');
    // Get the indices of the new and old tabs to find the correct
    // tab panels to show and hide
    let index = Array.prototype.indexOf.call(tabs, newTab);
    let oldIndex = Array.prototype.indexOf.call(tabs, oldTab);
    panels[oldIndex].hidden = true;
    panels[index].hidden = false;
  }
  
  // Add the tablist role to the first <ul> in the .tabbed container
  tablist.setAttribute('role', 'tablist');
  
  // Add semantics are remove user focusability for each tab
  Array.prototype.forEach.call(tabs, (tab, i) => {
    tab.setAttribute('role', 'tab');
    tab.setAttribute('id', 'tab' + (i + 1));
    tab.setAttribute('tabindex', '-1');
    tab.parentNode.setAttribute('role', 'presentation');
    
    // Handle clicking of tabs for mouse users
    tab.addEventListener('click', e => {
      e.preventDefault();
      let currentTab = tablist.querySelector('[aria-selected]');
      if (e.currentTarget !== currentTab) {
        switchTab(currentTab, e.currentTarget);
      }
    });
    
    // Handle keydown events for keyboard users
    tab.addEventListener('keydown', e => {
      // Get the index of the current tab in the tabs node list
      let index = Array.prototype.indexOf.call(tabs, e.currentTarget);
      // Work out which key the user is pressing and
      // Calculate the new tab's index where appropriate
      let dir = e.which === 37 ? index - 1 : e.which === 39 ? index + 1 : e.which === 40 ? 'down' : null;
      if (dir !== null) {
        e.preventDefault();
        // If the down key is pressed, move focus to the open panel,
        // otherwise switch to the adjacent tab
        dir === 'down' ? panels[i].focus() : tabs[dir] ? switchTab(e.currentTarget, tabs[dir]) : void 0;
      }
    });
  });
  
  // Add tab panel semantics and hide them all
  Array.prototype.forEach.call(panels, (panel, i) => {
    panel.setAttribute('role', 'tabpanel');
    panel.setAttribute('tabindex', '-1');
    let id = panel.getAttribute('id');
    panel.setAttribute('aria-labelledby', tabs[i].id);
    panel.hidden = true; 
  });
  
  // Initially activate the first tab and reveal the first tab panel
  tabs[0].removeAttribute('tabindex');
  tabs[0].setAttribute('aria-selected', 'true');
  panels[0].hidden = false;
})();
body {
  max-width: 40rem;
  padding: 0 1rem;
  font-size: 125%;
  line-height: 1.5;
  margin: 1.5rem auto;
  font-family: Arial, sans-serif;
}

* {
  color: inherit;
  margin: 0;
}

[role="tablist"] {
  padding: 0;
}

[role="tablist"] li, [role="tablist"] a {
  display: inline-block;
}

[role="tablist"] a {
  text-decoration: none;
  padding: 0.5rem 1em;
}

[role="tablist"] [aria-selected] {
  border: 2px solid;
  background: #fff;
  border-bottom: 0;
  position: relative;
  top: 2px;
}

[role="tabpanel"] {
  border: 2px solid;
  padding: 1.5rem;
}

[role="tabpanel"] * + * {
  margin-top: 0.75rem;
}

*:focus {
  outline: none;
  box-shadow: inset 0 0 0 4px lightBlue;
}

@media (max-width: 550px) {
  
  [role="tablist"] li, [role="tablist"] a {
    display: block;
    position: static;
  }
  
  [role="tablist"] a {
    border: 2px solid #222 !important;
  }
  
  [role="tablist"] li + li a {
    border-top: 0 !important;
  }
  
  [role="tablist"] [aria-selected] {
    position: static;
  }
  
  [role="tablist"] [aria-selected]::after {
    content: '\0020⬅';
  }
  
  [role="tabpanel"] {
    border-top: 0;
  }
  
}
<div class="tabbed">
  <ul>
    <li>
      <a href="#section1">Section 1</a>
    </li>
    <li>
      <a href="#section2">Section 2</a>
    </li>
    <li>
       <a href="#section3">Section 3</a>
    </li>
     <li>
       <a href="#section4">Section 4</a>
    </li>
  </ul>
  <section id="section1">
    <h2>Section 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod, tortor nec pharetra ultricies, ante erat imperdiet velit, nec laoreet enim lacus a velit. <a href="#">Nam luctus</a>, enim in interdum condimentum, nisl diam iaculis lorem, vel volutpat mi leo sit amet lectus. Praesent non odio bibendum magna bibendum accumsan.</p>
  </section>
  <section id="section2">
    <h2>Section 2</h2>
    <p>Nullam at diam nec arcu suscipit auctor non a erat. Sed et magna semper, eleifend magna non, facilisis nisl. Proin et est et lorem dictum finibus ut nec turpis. Aenean nisi tortor, euismod a mauris a, mattis scelerisque tortor. Sed dolor risus, varius a nibh id, condimentum lacinia est. In lacinia cursus odio a aliquam. Curabitur tortor magna, laoreet ut rhoncus at, sodales consequat tellus.</p>
  </section>
  <section id="section3">
    <h2>Section 3</h2>
    <p>Phasellus ac tristique orci. Nulla maximus <a href="">justo nec dignissim consequat</a>. Sed vehicula diam sit amet mi efficitur vehicula in in nisl. Aliquam erat volutpat. Suspendisse lorem turpis, accumsan consequat consectetur gravida, <a href="#">pellentesque ac ante</a>. Aliquam in commodo ligula, sit amet mollis neque. Vestibulum at facilisis massa.</p>
  </section>
  <section id="section4">
    <h2>Section 4</h2>
    <p>Nam luctus, enim in interdum condimentum, nisl diam iaculis lorem, vel volutpat mi leo sit amet lectus. Praesent non odio bibendum magna bibendum accumsan. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod, tortor nec pharetra ultricies, ante erat imperdiet velit, nec laoreet enim lacus a velit. </p>
  </section>
</div>

Related