File Directory Treeview Example Using Declared Properties
Read This First
The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.
This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.
- There may be support gaps in some browser and assistive technology combinations, especially for mobile/touch devices. Testing code based on this example with assistive technologies is essential before considering use in production systems.
- The ARIA and Assistive Technologies Project is developing measurements of assistive technology support for APG examples.
- Robust accessibility can be further optimized by choosing implementation patterns that maximize use of semantic HTML and heeding the warning that No ARIA is better than Bad ARIA.
About This Example
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:
Example
My Documents
-
Projects
- project-1.docx
- project-2.docx
-
project-3
- project-3A.docx
- project-3B.docx
- project-3C.docx
- project-4.docx
-
project-5
- project-5A.docx
- project-5B.docx
- project-5C.docx
- project-5D.docx
- project-5E.docx
- project-5F.docx
-
Reports
-
report-1
- report-1A.docx
- report-1B.docx
- report-1C.docx
-
report-2
- report-2A.docx
- report-2B.docx
- report-2C.docx
- report-2D.docx
-
report-3
- report-3A.docx
- report-3B.docx
- report-3C.docx
- report-3D.docx
-
report-1
-
Letters
-
letter-1
- letter-1A.docx
- letter-1B.docx
- letter-1C.docx
-
letter-2
- letter-2A.docx
- letter-2B.docx
- letter-2C.docx
- letter-2D.docx
-
letter-3
- letter-3A.docx
- letter-3B.docx
- letter-3C.docx
- letter-3D.docx
-
letter-1
Accessibility Features
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.
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
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 Selectedtextbox. |
Space | Performs the default action, which is to select the node, causing the name of the node to appear in the File or Folder Selectedtextbox. |
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
Role | Attribute | Element | Usage |
---|---|---|---|
tree |
ul |
|
|
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 |
|
|
tabindex="0" |
li |
|
|
aria-expanded="false" |
li |
|
|
aria-expanded="true" |
li |
|
|
aria-selected="false" |
li |
|
|
aria-selected="true" |
li |
|
|
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 |
|
|
aria-level="number" |
li |
|
|
group |
ul |
|
JavaScript and CSS Source Code
- CSS: tree.css
- Javascript: tree.js
- Javascript: treeitem.js
- Javascript: treeitemClick.js
HTML Source Code
To copy the following HTML code, please open it in CodePen.