Navigate a File Tree with Arrow Keys

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Build a file tree with the WAI-ARIA tree view pattern. The tree prop is a list of nodes, { name } for a file or { name, children } for a folder, where children holds more nodes (the starter has a default tree). The whole tree is a role="tree" element with aria-label="Files". Every file and folder is a role="treeitem" whose accessible name is just its own name, not including its children's: if you nest items, name each one with aria-label or aria-labelledby; a flat list of items with aria-level works too. Folders have aria-expanded, "false" at first and "true" while open; files have none. A folder's children show only while it's open (leave them out, or hide them with the hidden attribute); nested children go in a role="group". Clicking a folder's name opens or closes it. The tree is a single Tab stop with a roving tabindex: the focused item has tabIndex={0} and every other item tabIndex={-1}, starting with the first item. While an item has focus, Down and Up move focus to the next or previous shown item, stopping at the ends. Right opens a closed folder, moves to the first child of an open one, and does nothing on a file. Left closes an open folder, and otherwise moves to the item's parent folder. Home and End move to the first and last shown items.

Your code must pass

  • The tree is named "Files", with its folders closed at first
  • The tree is one Tab stop, starting at the first item
  • Down and Up move focus through the shown items
  • Right opens a folder, then moves to its first child
  • Left closes a folder, or moves to its parent
  • Home and End jump to the first and last shown items
  • Clicking a folder's name opens and closes it