Tab Panel, the right way...
Published on (09-23-2016).
Tab Panels have been my pet peeve for years , mostly because of the arguments people come up with to rationalize the use of their companion jump links.
Let me explain…
In preparation of a new article...— Thierry (@thierrykoblentz) September 2, 2016
Why do we use "jump links" for Tab Panels?
and the answer is…
We use jump links for styling!
Yes, jump links are nothing more than a hack. We did not adopt them for a noble cause like Accessibility or Semantics, we embraced them because they facilitate the display of tabs atop panels.
This is the reason why “all” Tab Panels use jump links today; why we have a ARIA role for it; and why we’re told that’s “How [it] Should Work”; …
We, as a community, decided that the ends justified the means and it was okay to go with this convoluted solution. We sacrificed Semantics and Separation of Concern simply because we needed to keep the tabs in flow while displaying them side-by-side.
Actually, we’ve sacrificed more than that as these jump links do not help with keyboard navigation. This is because most people expect to tab (no pun intended) through Tab Panels following this simple sequence:
- Tab 1
- Panel 1
- Tab 2
- Panel 2
- Tab 3
- Panel 3
But since the jump links come first, source order is more like:
- Tab 1
- Tab 2
- Tab 3
- Panel 1
- Panel 2
- Panel 3
This the reason why we have to manage focus in Tab Panels via
tabindex="-1", to make sure tabbing from a tab sends the user to its associated panel instead of the next tab. Note that this keyboard behavior is even more confusing when that markup is styled as an accordion.
The TAB key is preserved for moving between the active TAB and the open panel. This means inactive tabs should have tabindex=”-1” and be focused/activated only programmatically (via the aforementioned arrow keys).
The reason for this is that keyboard and AT users should be afforded a direct route to the chosen content without having to cycle through all the tabs in the tablist each time.
This is a poor fix though because even if it helps users reach the panel it does not prevent them from leaving the widget as they leave that panel. And the experience is worse if there is no focusable element inside the selected panel, because in this case users go directly from the tab to the next focusable element on the page—wherever that is!
TL;DR: see demo below.
In my opinion, the fact that we do not use jump links with Accordions is the strongest argument against their use in Tab Panels. If we do not use jump links with Accordion it is simply because we do not need to.
The markup for Accordions is a simple succession of heading/div pairs (or even
dd) which facilates both styling and keyboard navigation:
<section> <h2>Accordion</h2> <div> <h3>Header 1</h3> <div>Content related to header 1</div> <h3>Header 2</h3> <div>Content related to header 2</div> <h3>Header 3</h3> <div>Content related to header 3</div> </div> </section>
That’s POSH (Plain Old Semantic HTML) in all its beauty. Everybody wins! All users can build a simple mental model and authors can implement a basic loop for creating heading/div pairs.
Now, because we can’t easily style the above as a Tab Panel, we got creative:
<section> <h2>Tab Panel</h2> <ul> <li><a href="#panel-1">Header 1</a></li> <li><a href="#panel-2">Header 2</a></li> <li><a href="#panel-3">Header 3</a></li> </ul> <div id="panel-1"> <h3>Header 1</h3> <div>Content related to header 1</div> </div> <div id="panel-2"> <h3>Header 2</h3> <div>Content related to header 2</div> </div> <div id="panel-3"> <h3>Header 3</h3> <div>Content related to header 3</div> </div> </section>
That’s a big step up in terms of mental model. And note that these jump links may not even make sense at all depending on the content of the widget and its positioning on the page (i.e. jump links that can’t make the page scroll).
As a side note, check the Simply Accessible example to see how jump links create redundancy between the headings in the panels and their related tab.
Designers came up with widgets like Accordions and Tab Panels to reduce a component visual “footprint”. In other words, the difference between these widgets is related to display, not to semantics; so from a markup perspective, the HTML should be identical for both:
<section> <h2>Tab Panel or Accordion, it doesn't matter</h2> <div class="widget tab-panel | accordion"> <h3 class="tab | header">Header 1</h3> <div class="panel">Content related to header 1</div> <h3 class="tab | header">Header 2</h3> <div class="panel">Content related to header 2</div> <h3 class="tab | header">Header 3</h3> <div class="panel">Content related to header 3</div> </div> </section>
That’s it! A common structure allows us to easily style the component as a Tab Panel or an Accordion, think CSS Zen Garden! #win
The best approach may be to start with jump-link-less markup to which we’d add ARIA attributes like in this demo from Leonie et al with the exception of the keyboard mechanism which—to me—seems to be more geared toward advanced users. Instead, we could rely on the arrow keys to cycle through the tab items and at the same time support tabbing through those items and allow users to select a tab to move focus to its associated panel—the same way things work on Paul Adam’s excellent demo.
The recipe for the best user experience:
- Start with POSH, a simple set of heading/div pairs
- Add ARIA (
- Style the widget using the selector inserted by the script, this way containers are not hidden if there is no script support.
cursorto style the tabs because those behave like buttons (
default), not links (
- In case the component is displayed as a Tab Panel, style it as an Accordion before its tabs start wrapping (to accomodate user’s font-size) or at narrow widths.
- Do not mess with the tab key, let users tab through the tabs/headers and visible panel(s), all according to source order.
- Allow users to switch between tabs/headers using arrow keys (left/right and top/bottom). home/end keys should focus on the first/last tab.
- Reveal panels as a user cycles through the tabs
- For users who navigate between the tabs via the tab key—reveal the associated panel through click and keypress events (enter and space bar).
- Move focus onto the relevant panel whenever keyboard users select a tab via the enter or space bar keys.
- Provide a means for keyboard users to escape the widget (as we do with “modals”). This way users have the option to leave the component with a specific panel opened or to go back to the first tab if they wish.
Yes, I know about #11, discoverability is an issue but I think this approach is better than not having anything at all. Besides, the esc key could become a common implementation across widgets as a means to “step out” of them. Maybe it would be best to make the widget focusable and use
aria-label to convey some extra info?
- We could use hash URLs to let users share specific links according to which panel shows,
- It’d be great to have screen-readers announce the location/size of tabs/tablist (i.e. “2 out of 4”).
I really dislike the fact that we start with a simple succession of heading/div pairs—which is simplification of the information at its best—but then manage to create a much more complicated model for SR users. It’s as if progressive enhancement went wrong…
We use these widgets and hide their panels to preserve screen estate—but screen-reader users could not care less about that.
tabindex="0" to allow keyboard users to navigate through the tabs—but screen-reader users already have a means to navigate through headings.
At this point, I’d argue that SR users would be better off if the script did not attach
tabindex, ARIA attributes, and events to the markup.
Note that the problem is not related to the fact that we hide the panels—as we could do that for sighted users only. No, the main issues are related to
tabindex, which signals that these elements are actionable, and to the same paradigm principle:
@thierrykoblentz In user testing of web-based apps, strong preference to have same paradigm, for when interacting with sighted colleagues.— Sarah Bourne (@sarahebourne) May 21, 2016
Even though it may worth ignoring that principle sometime:
@thierrykoblentz I thought it would be confusing, but not a single complaint. It has challenged my convictions.— Sarah Bourne (@sarahebourne) May 21, 2016
I’ve always challenged this idea of serving the same experience no matter what; because I think one cannot succeed in offering the best experience to a wide variety of users across different scenarios while strictly obeying that principle. It just can’t work. Also, because the display of components on the web may vary depending on many factors: desktop, mobile, screen size, device orientation, browsers, login state, etc.
Unfortunately, we cannot really detect SR users to cater for them by disabling the script. And I don’t think this is going to change anytime soon because of various concerns.
There are many valid points in that piece from Leonie Watson, but there is one I don’t agree with:
Most things that make a website usable with a screen reader are achieved by conforming to web standards, and the rest require relatively little modification. In these days of responsive design, including a media feature for screen readers would automatically double the work involved. You’d need to serve up a screen reader alternative for every break point version of your website.
In the case of these widgets, there would be no extra work involved because we could achieve true progressive enhancement by leveraging the “very first layer”, from which we get the semantics that provides a very basic/simple mental model.
So in the absence of a straight mechanism to “dumb down” the widget for SR users, we could offer a “kill switch” in the form of:
<div class="widget" tabindex="0" aria-label="Use 'Ctrl + T' to toggle the interface of this section (from a Tab Panel widget to a simple succession of headings and their respective sections)>
If you think you have a valid case for a Tab Panel that uses jump links, then you may be thinking of Nav Tabs, for example: