Sorted file tree sidebar with ARIA tree roles and states
A code review tool shows the files of a pull request in a sidebar. Sighted users see indentation and folder arrows, but a screen reader announces a flat list of names: nothing says which items are folders, whether they are open, how deep they are, or which file is selected. Build the sidebar as a tree view with the ARIA roles and states that carry that information.
The starter code includes the file data. A node with a children array is a folder (even when the array is empty). Any other node is a file. The data is deliberately unsorted.
const FILE_TREE = [
{ id: 'readme', name: 'README.md' },
{ id: 'src', name: 'src', children: [
{ id: 'src-index', name: 'index.ts' },
{ id: 'utils', name: 'utils', children: [
{ id: 'utils-index', name: 'index.ts' },
{ id: 'format', name: 'format.ts' },
] },
{ id: 'app', name: 'App.tsx' },
{ id: 'components', name: 'components', children: [
{ id: 'button', name: 'Button.tsx' },
{ id: 'avatar', name: 'avatar.tsx' },
] },
] },
{ id: 'assets', name: 'assets', children: [] },
{ id: 'package', name: 'package.json' },
{ id: 'gitignore', name: '.gitignore' },
];
At every level, show folders first, then files. Within each group, sort by name ignoring case (compare name.toLowerCase()). Don’t mutate FILE_TREE.
assets
src
.gitignore
package.json
README.md
role="tree" and an accessible name: an aria-label, or aria-labelledby pointing at a visible Project files heading.role="treeitem" (an <li> works well) that has:
aria-level: 1 for top-level items, 2 for their children, and so on.aria-setsize: how many items share its parent, and aria-posinset: its 1-based position among them, in the sorted order.role="group" placed inside the folder’s treeitem.<button type="button">, or an element with role="button" or a click handler. Put the click handler on the row, not on the treeitem that also wraps the children. An arrow or icon in the row is fine with aria-hidden="true".aria-expanded="false" when collapsed and "true" when expanded. Files have no aria-expanded.src and utils, collapse src, expand src again, and utils is still expanded.aria-expanded; it just has nothing to show.aria-selected="true". No other treeitem has aria-selected="true" (others can have "false" or no attribute).Selected: {path}, where the path joins the names from the top level down with /, for example Selected: src/utils/index.ts. Before anything is selected, show No file selected.Load 5 top-level items, folders collapsed, "No file selected"
Click "src" src expands: components, utils, App.tsx, index.ts (level 2, setsize 4)
Click "utils" utils expands: format.ts, index.ts (level 3)
Click the level-3 "index.ts"
"Selected: src/utils/index.ts"
Click "src" src collapses, selection unchanged
index.ts. Track the selection by id or full path, not by name.avatar.tsx sorts before Button.tsx because the comparison ignores case..gitignore starts with a dot, which sorts before letters.Keyboard navigation inside the tree is out of scope for this problem.
The tests read the page through ARIA, the way a screen reader does. You don’t need test ids or class names. They:
role="tree" element and check it has an aria-label or aria-labelledby,role="treeitem" elements and read each one’s name as its text, leaving out nested role="group" content and aria-hidden="true" icons,aria-level, aria-setsize, aria-posinset, aria-expanded and aria-selected on those treeitems,Selected: {path} and No file selected text.Capitalization and extra spacing don’t matter.