Navigation Treeview Example
About This Example
Caution!
Before considering use of the Tree View Pattern for site navigation, it is important to understand:
- Correct implementation of the
tree
role requires implementation of complex functionality that is not needed for typical site navigation that is styled to look like a tree with expandable sections. - A pattern more suited for typical site navigation with expandable groups of links is the disclosure pattern.
The below example demonstrates how the Tree View Pattern can be used to build a navigation tree for a set of hierarchically organized web pages.
It illustrates navigation of a mythical university web site that is comparable to the navigation illustrated in the
Example Disclosure Navigation Menu.
As noted above, the disclosure pattern is better suited for most web sites because few sites need the additional keyboard functionality required to support the ARIA tree
role.
This example relies on the browser to compute values for aria-setsize
, aria-posinset
, and aria-level
.
The ARIA specification for these properties states that browsers can, but are not required to, compute their values.
So, some browser and assistive technology combinations may not compute or report correct position and level information if it is not explicitly declared.
If testing reveals gaps in support for these properties, override automatic computation by explicitly declaring their values as demonstrated in the example of a File Directory Treeview Example Using Declared Properties.
Similar examples include:
Example
Mythical University
Accessibility Features
Focus Movement After Content Load
An important aspect of designing a navigation tree experience is the behavior of keyboard focus when an item in the tree is activated. If activating a tree item changes content on the page without triggering a browser page load, i.e., works like typical single-page apps, the focus position after the content load significantly affects efficiency for keyboard and assistive technology users. Accessible navigation trees typically implement one of the following two behaviors:
- Activating a tree item moves focus to the beginning of the new content, ideally a level one heading with content that matches the name of the tree item that was activated. Focusing on the heading informs screen reader users that navigation is complete and confirms the destination. This behavior is appropriate when common or important use case scenarios assume users want to start interacting with the loaded content after activating the tree item. Note: Keyboard users will need to navigate back to the navigation tree to view other pages. To optimize keyboard efficiency, design a layout that logically locates the tree immediately before the content display area in the Tab sequence.
-
Activating an item in the tree keeps focus on the activated item in the tree.
In this case, the movement of
aria-current
to the currently focused tree item informs screen reader users that navigation is complete and confirms the destination. This behavior is appropriate when common or important use case scenarios assume users are likely to need to peruse content from multiple nodes in the tree before deciding to interact with the loaded content. Note: screen reader users will need to navigate to the content to read it. In some cases, it might be possible to help screen reader users more quickly perceive the nature of the loaded content without navigating to it by referencing a portion of the content with anaria-describedby
attribute on the tree item.
The example on this page illustrates the first technique of focusing the level one heading in the newly loaded content.
Assistive Technology Support Features
- Since the tree presents a site navigation system, it is wrapped in a navigation region implemented with a
nav
element that has anaria-label
that matches the label on the tree. -
To ensure assistive technology users can identify and easily locate the tree item associated with the currently displayed page:
- The
aria-current="page"
attribute is applied to the item in the tree associated with the currently displayed page. -
The tree item with
aria-current
is also the only item withtabindex="0"
. That is, when tabbing into the tree, focus always lands on the item representing the current page. - When focus leaves the tree, if the item representing the current page is in a branch that has been collapsed, that branch is expanded to ensure the current page item is both visible and exposed as active to assistive technologies.
- The
Visual design and high contrast features
- To help communicate that the arrow keys are available for navigation within the tree, a border is added to the tree container when focus is within the tree.
-
To support operating system high contrast settings:
- Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused an other elements. Instead of using transparency, the focused element has a thicker border and less padding. When an element receives focus, its border changes from 1 to 3 pixels and padding is reduced by 2 pixels. When an element loses focus, its border changes from 3 pixels to 1 and padding is increased by 2 pixels.
-
To ensure the arrow icons used to indicate the expanded or collapsed state have sufficient contrast with the background when high contrast settings invert colors, the CSS
currentcolor
value for thefill
andstroke
properties of the SVGpolygon
element is used to synchronize the color with text content. If specific colors are used to specify thefill
andstroke
properties, these colors will remain the same in high contrast mode, which could lead to insufficient contrast between the icon and the background or even make the icon invisible if its color matches the high contrast mode background.
Terms Used to Describe Trees
A tree item that can be expanded to reveal child items is called a parent node. It is a closed node when the children are hidden and an open node when it is expanded. An end node does not have any children. For a complete list of terms and definitions, see the Tree View Pattern.
Keyboard Support
Key | Function |
---|---|
Enter or Space |
|
Down arrow |
|
Up arrow |
|
Right Arrow |
|
Left Arrow |
|
Home | Moves focus to first node without opening or closing a node. |
End | Moves focus to the last node that can be focused without expanding any nodes that are closed. |
a-z, A-Z |
|
* (asterisk) |
|
Role, Property, State, and Tabindex Attributes
Landmarks
Role | Attribute | Element | Usage |
---|---|---|---|
banner |
header |
|
|
navigation |
nav |
Identifies the region containing the navigation tree. | |
aria-label="navigation label"
|
nav |
The aria-label attribute provides an accessible name for the navigation landmark. |
|
region |
section |
|
|
aria-labelledby="IDREFs" |
section |
The aria-labelledby attribute provides an accessible name for the region landmark by concatenating the website and page titles. |
|
contentinfo |
role="contentinfo" |
footer |
|
Tree
Role | Attribute | Element | Usage |
---|---|---|---|
tree |
ul |
|
|
aria-label="Mythical University" |
ul |
Provides an accessible name for the tree . |
|
treeitem |
a |
Identifies the element as a treeitem . |
|
tabindex="-1" |
a |
|
|
tabindex="0" |
a |
|
|
aria-current="page" |
a |
|
|
aria-expanded="false" |
a |
|
|
aria-expanded="true" |
a |
|
|
aria-owns="IDREF" |
a |
Refers to the element with role group that contains the set of child treeitem elements that belong to this parent treeitem . |
|
group |
ul |
|
|
none
|
li
|
|
JavaScript and CSS Source Code
- CSS: treeview-navigation.css
- Javascript: treeview-navigation.js
HTML Source Code