1.0.6 • Published 2 years ago
@zendeskgarden/container-treeview v1.0.6
@zendeskgarden/container-treeview 
This package includes containers relating to Treeview in the Garden Design System.
This package offers a headless implementation to W3C TreeView Design Pattern and offers accessible navigation
WAI-ARIA support
For a vertically oriented tree (horizontal and RTL are supported as well):
Keyboard interactions
- When a single-select tree receives focus:
- If none of the nodes are selected before the tree receives focus, focus is set on the first node.
- If a node is selected before the tree receives focus, focus is set on the selected node.
- ❌ When a multi-select tree receives focus:
- If none of the nodes are selected before the tree receives focus, focus is set on the first node.
- If one or more nodes are selected before the tree receives focus, focus is set on the first selected node.
- ✅
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.
- ✅
Down Arrow: Moves focus to the next node that is focusable without opening or closing a node. - ✅
Up Arrow: Moves focus to the previous node that is focusable without opening or closing a node. - ✅
Home: Moves focus to the first node in the tree without opening or closing a node. - ✅
End: Moves focus to the last node in the tree that is focusable without opening a node. - ✅
Enter: activates a node, i.e., performs its default action. For parent nodes, one possible default action is to open or close the node. In single-select trees where selection does not follow focus (see note below), the default action is typically to select the focused node. - ❌ Type-ahead is recommended for all trees, especially for trees with more than 7 root nodes:
- Type a character: focus moves to the next node with a name that starts with the typed character.
- Type multiple characters in rapid succession: focus moves to the next node with a name that starts with the string of characters typed.
- ❌
*(Optional): Expands all siblings that are at the same level as the current node. - ❌ Selection in multi-select trees:
WAI-ARIA Roles, States, and Properties
- ✅ All tree nodes are contained in or owned by an element with role
tree. - ✅ Each element serving as a
treenode has roletreeitem. - ✅ Each root node is contained in the element with role
treeor ❌❓referenced by anaria-ownsproperty set on thetreeelement. - ✅ Each parent node contains or owns an element with role
group. - ✅ Each child node is contained in or owned by an element with role
groupthat is contained in or owned by the node that serves as the parent of that child. - ✅ Each element with role
treeitemthat serves as a parent node hasaria-expandedset to false when the node is in a closed state and set to true when the node is in an open state. End nodes do not have thearia-expandedattribute because, if they were to have it, they would be incorrectly described to assistive technologies as parent nodes. - ❌ The
treerole supports the aria-activedescendant property, which provides an alternative to moving DOM focus amongtreeitemelements when implementing keyboard navigation. - ❌ If the
treesupports selection of more than one node, the element with roletreehasaria-multiselectableset to true. Otherwise,aria-multiselectableis either set to false or the default value of false is implied. - ❌ If the
treedoes not support multiple selection,aria-selectedis set to true for the selected node and it is not present on any other node in the tree. - ❌ if the
treesupports multiple selection:- All selected nodes have
aria-selectedset to true. - All nodes that are selectable but not selected have
aria-selectedset to false. - If the
treecontains nodes that are not selectable, those nodes do not have thearia-selectedstate.
- All selected nodes have
- ✅ ❓The element with role
treehas either a visible label referenced byaria-labelledbyor a value specified foraria-label. - ❌ If the complete set of available nodes is not present in the DOM due to dynamic loading as the
user moves focus in or scrolls the tree, each node has
aria-level,aria-setsize, andaria-posinsetspecified. - ✅ If the
treeelement is horizontally oriented, it hasaria-orientationset tohorizontal. The default value ofaria-orientationfor a tree isvertical.
Installation
npm install @zendeskgarden/container-treeviewUsage
Check out storybook for live examples.
useTreeview
import { useTreeview } from '@zendeskgarden/container-treeview';
const Treeview = () => {
const { getTreeviewProps } = useTreeview();
return <div {...getTreeviewProps()} />;
};TreeviewContainer
import { TreeviewContainer } from '@zendeskgarden/container-treeview';
<TreeviewContainer>{({ getTreeviewProps }) => <div {...getTreeviewProps()} />}</TreeviewContainer>;