Skip to content

File Directory Treeview Example Using Computed Properties

File Directory Treeview Example Using Computed 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.

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.

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 using declared properties.

Similar examples include:

Example

My Documents

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 Treeview 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, see 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
  • Moves focus to the next node that is focusable without opening or closing a node.
  • If focus is on the last node, does nothing.
Up arrow
  • Moves focus to the previous node that is focusable without opening or closing a node.
  • If focus is on the first node, does nothing.
Right Arrow
  • When focus is on a closed node, opens the node; focus does not move.
  • When focus is on a open node, moves focus to the first child node.
  • When focus is on an end node, does nothing.
Left Arrow
  • When focus is on an open node, closes the node.
  • When focus is on a child node that is also either an end node or a closed node, moves focus to its parent node.
  • When focus is on a root node that is also either an end node or a closed node, does nothing.
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
  • Focus moves to the next node with a name that starts with the typed character.
  • Search wraps to first node if a matching name is not found among the nodes that follow the focused node.
  • Search ignores nodes that are descendants of closed nodes.
* (asterisk)
  • Expands all closed sibling nodes that are at the same level as the focused node.
  • Focus does not move.

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
  • Makes the treeitem element focusable without including it in the tab sequence of the page.
  • All treeitem elements are focusable, but only one is included in the tab sequence.
tabindex="0" li
  • Includes the treeitem element in the tab sequence.
  • Only one treeitem in the tree has tabindex="0".
  • In this implementation, the first treeitem in the tree is included in the tab sequence when the page loads.
  • When the user moves focus in the 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
  • Applied only to treeitem elements that are parent nodes, i.e., they contain a ul with the group role.
  • Indicates the parent node is closed, i.e., the descendant elements are not visible.
  • The visual indication of the collapsed state is synchronized by a CSS attribute selector.
aria-expanded="true" li
  • Applied only to treeitem elements that are parent nodes, i.e., they contain a ul with the group role.
  • Indicates the parent node is open, i.e., the descendant elements are visible.
  • The visual indication of the expanded state is synchronized by a CSS attribute selector.
aria-selected="false" li
  • Applied to treeitem elements.
  • Indicates the file or folder for the item is not currently selected.
aria-selected="true" li
  • Applied to treeitem elements.
  • Indicates the file or folder for the item is currently selected.
group ul
  • Identifies the ul element as a container of treeitem elements that form a branch of the tree.
  • The group is contained in the element that serves as the parent treeitem.
  • Browsers use the grouping to compute aria-level, aria-setsize and aria-posinset values for the nodes contained in the branch.
  • The grouping also prevents browsers from including the content of the nodes in the group in the accessible name for the parent node.

JavaScript and CSS Source Code

HTML Source Code

Back to Top