third_party/aria-practices/src/content/patterns/treeview/examples/treeview-1b.html
The following example implementation of the Tree View Pattern simulates a widget for selecting a file or folder from within a hierarchical file system for viewing in a file viewer. In the My Documents tree, each parent node represents a folder and each end node represents a file. Activating a node selects the node and puts the name of the folder or file in the read-only edit field that represents the file viewer.
The code in this example explicitly declares values for aria-setsize, aria-posinset and aria-level, which overrides browser computation of values for these properties. The ARIA specification for these properties states that browsers can, but are not required to, compute these values.
Similar examples include:
Projects
project-1.docx
project-2.docx
project-3
project-4.docx
project-5
Reports
report-1
report-2
report-3
Letters
letter-1
letter-2
letter-3
File or Folder Selected:
To make the focus indicator easier to see, nodes in the tree have custom focus and hover styling created using CSS focus and hover pseudo-classes.
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.
Note that in this example, selection and focus are distinct; moving focus does not change which node is selected. Because selection does not follow focus, keyboard and screen reader users can navigate and explore the tree without changing the content of the file viewer. To learn more about this aspect of the design, read the guidance section about Deciding When to Make Selection Automatically Follow Focus.
| Key | Function |
|---|---|
| Enter | Performs the default action, which is to select the node, causing the name of the node to appear in the File or Folder Selected textbox. |
| Space | Performs the default action, which is to select the node, causing the name of the node to appear in the File or Folder Selected textbox. |
| 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 | Attribute | Element | Usage |
|---|---|---|---|
tree | ul |
ul element as a tree widget.|
| | aria-labelledby="ID_REFERENCE" | ul | Refers to the heading element that contains the label that identifies the purpose of the tree. |
| treeitem | | li | Identifies the element as a treeitem. |
| | tabindex="-1" | li |
treeitem element focusable without including it in the tab sequence of the page.treeitem elements are focusable, but only one is included in the tab sequence.|
| | tabindex="0" | li |
treeitem element in the tab sequence.treeitem in the tree has tabindex="0".treeitem in the tree is included in the tab sequence when the page loads.tree, the element included in the tab sequence changes to the element with focus as described in Managing Focus Within Components Using a Roving tabindex.|
| | aria-expanded="false" | li |
treeitem elements that are parent nodes, i.e., they contain a ul with the group role.|
| | aria-expanded="true" | li |
treeitem elements that are parent nodes, i.e., they contain a ul with the group role.|
| | aria-selected="false" | li |
treeitem elements.|
| | aria-selected="true" | li |
treeitem elements.|
| | aria-setsize="number" | li | Defines the number of treeitem elements in the set of treeitem elements that are in the same branch and at the same level within the hierarchy. |
| | aria-posinset="number" | li |
treeitem elements that are in the same branch and at the same level within the hierarchy.|
| | aria-level="number" | li |
treeitem in the hierarchical tree structure.treeitem elements have aria-level="1".|
| group | | ul |
ul element as a container of treeitem elements that form a branch of the tree.treeitem.aria-level, aria-setsize and aria-posinset values for the nodes contained in the branch if those properties are not specified explicitly in the code.|
To copy the following HTML code, please open it in CodePen.