shadcn.io is an independent project built on shadcn/ui
Components
Free
Tree View for Ark UI
A tree of folders and files that people walk with the arrow keys, with selection, checkboxes, renaming and lazy loading, for React and Ark UI projects.
On this page9 sections
A tree view shows a hierarchy that people move through and act on: the files of a project, the pages of a docs site, the folders of a mail client. The whole tree is one Tab stop, the arrow keys walk and open its rows, and a row can be selected, checked, renamed or followed as a link.
For one section that shows and hides, a Collapsible is enough, and a flat list of sections reads better as an Accordion.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The tree reads its nodes from a collection. Each node's id is the value that selection and expansion report, and its name is the label that typing a letter searches. TreeNode renders itself again for a folder's children, so the same component draws any depth.
TreeViewTree has the tree role and every node is a treeitem that reports its level and whether it is selected, and for a folder, whether it is open. Only the focused row is in the tab order. Name the tree with TreeViewLabel, as the Links demo does: without one, a screen reader announces it as Tree View. Shift with the arrow keys extends the selection only when selectionMode is multiple. The checkboxes in the Checkbox tree demo stay out of the tab order, and Space on a row checks or unchecks its box.
KeyAction
Tab
Moves focus to the tree view, placing the first tree view item in focus.
Enter
Selects the item or branch node.
Space
Selects the item or branch node. On a row with a checkbox, checks or unchecks it instead.
Arrow Down
Moves focus to the next node.
Arrow Up
Moves focus to the previous node.
Arrow Right
When focus is on a closed branch node, opens the branch. When focus is on an open branch node, moves focus to the first item node.
Arrow Left
When focus is on an open branch node, closes the node. When focus is on a item or branch node, moves focus to its parent branch node.
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
Focus moves to the next node with a name that starts with the typed character, except for a, which the select-all shortcut takes. The search logic ignores nodes that are descendants of closed branch.
A-Z
Focus moves to the next node with a name that starts with the typed character. The search logic ignores nodes that are descendants of closed branch.
*
Expands all sibling nodes that are at the same depth as the focused node.
Shift+Arrow Down
Moves focus to and toggles the selection state of the next node.
Shift+Arrow Up
Moves focus to and toggles the selection state of the previous node.
⌘Command+A
In a tree that allows multiple selection, selects every node and moves focus to the last one. Ctrl + A does nothing in the tree.
F2
When canRename allows the focused node, opens its name for editing. Enter saves the new name, and Escape cancels.
Demos
Checkbox tree
Every folder and file carries a TreeViewCheckbox, and the checked ids are held in state and printed beside the tree, with readme.md checked from the start. Checking a folder checks everything inside it, and a folder with only some files checked shows a dash. Use it to pick files for an export or a sync. From the keyboard, Space on a row checks or unchecks its box.
Context menu
A right click on a folder offers Add folder, Add file, Rename and Delete, and on a file, Rename and Delete. ContextMenuTrigger wraps each row, so the menu belongs to the row that was clicked. Use it for actions that don't deserve a button on every row. The items here change nothing, so connect each one to your collection before shipping it.
Controlled
A prompt asks for input.tsx, and the tree's selectedValue lives in state, set from onSelectionChange. The mark under the tree turns from a cross to a check once components/input.tsx is the selected id. Control the selection when something outside the tree reads it, such as a toolbar or a preview pane. The value is an array even with one node selected, so read its first id.
Custom icons
fileIcons on TreeView, built with createFileIcons, maps an extension to an icon: code for .tsx, a JSON file for .json and text for .md. Folders keep their default icons. Use the map when the file type is in the suffix. The extension is read from each node's id, and one the map leaves out gets the default file icon.
Custom icons folder
The app folder shows a package icon while closed and an open package while expanded, from icon and expandedIcon on its TreeViewBranchItem. The components folder sets both to null, so only its chevron and name show. Use a custom pair when a branch is a package or a drive rather than a folder, and null when a second icon adds nothing.
Custom icons item
Every file passes a star as icon to TreeViewItem, while the folders keep their default icons. Use the item's icon for a marker the extension doesn't decide, such as a favorite or a pinned file. When fileIcons on TreeView also maps that file's extension, the map wins over the item's icon.
Editor
Selecting a file opens it in a tab beside the tree, with a placeholder line for its contents and a button to close it, while selecting a folder only opens or closes it. Each new file replaces the tab. Use the split for an editor or a mail sidebar, where the tree picks what the main pane shows. The handler reads selectedNodes from onSelectionChange and skips folders.
Lazy loading
The app and components folders arrive empty, each with a childrenCount of 2, and opening one calls loadChildren, which answers after a short delay as an API would. A spinner shows while the folder reports aria-busy, and onLoadChildrenComplete keeps the new collection in state. Use it when a tree is too large to send at once, such as a repository or a shared drive.
Links
The rows are anchors: Introduction and Components under Documentation stay on the site, GitHub Repository under External Links opens in a new tab with an external-link icon, and llms.txt is a link at the top level. TreeViewLabel names the tree Docs. Use links for a docs sidebar or a site map, and selection when choosing a row shouldn't navigate.
Multiple selection
With selectionMode set to multiple, a click selects one row, a click with Ctrl or Cmd adds or removes one, and a click with Shift selects a range. Shift with the arrow keys extends the selection from the keyboard. Use it for a batch action, such as moving several files. A highlight is easy to lose while scrolling, so checkboxes say it more clearly when the set must stay obvious.
Rename
canRename returns true for every node, and onRenameComplete writes the new name into the collection with replace. Focus a row and press F2 to edit its name in place, then Enter to save or Escape to cancel. Use it where people own the labels, such as folders they created. When a server owns the name, save it there before replacing the node.
FAQ
How do I render a file tree from my data with the Tree View in React?
Build a collection with createTreeCollection from a root node whose children are your folders and files, each with a unique id and a name, and pass it to TreeView as collection. Render the root's children with a component that draws a branch for a node with children and an item for one without, calling itself for each child, as the TreeNode in Usage does.
The tree counts a node as a branch only when it has at least one child. Give an empty folder a childrenCount of 0 to keep it a branch.
How do I control which Tree View item is selected?
Keep the selected ids in state, pass them as selectedValue and update them from onSelectionChange, which receives an object with the new selectedValue. The value is always an array, even with one node selected, as the Controlled demo shows.
For a starting selection without your own state, use defaultSelectedValue. Set selectionMode to multiple to let several nodes stay selected, as in the Multiple selection demo.
How do I expand Tree View folders by default?
List the ids of the folders to open in defaultExpandedValue on TreeView, such as ["app"]. To open or close them from your own code, hold the ids in state and pass them as expandedValue, updated from onExpandedChange, and set it to collection.getBranchValues() for an Expand all button.
A click on a folder selects it and toggles it. With expandOnClick set to false, a click or Enter only selects it, and since these rows have no separate toggle, only the keyboard can then open the folder: the Right arrow key, or * for it and its siblings.
How do I add checkboxes to the Tree View?
Put a TreeViewCheckbox in each row and hold the checked ids in checkedValue, updated from onCheckedChange, as the Checkbox tree demo does. Checking a folder checks every node inside it, and the folder's box shows a dash while only some are checked.
The checkboxes stay out of the tab order, so the tree is still a single Tab stop. Space on the focused row checks or unchecks its box, and Enter selects the row.
How do I load Tree View children lazily from an API?
Pass loadChildren, an async function that receives the folder being opened as node, with an abort signal, and returns its children. Give each unloaded folder a childrenCount so the tree treats it as a branch, and in your node component test for a folder with collection.isBranchNode(node) rather than node.children, which stays undefined until the load. The Lazy loading demo does both.
When the children arrive, onLoadChildrenComplete hands you the new collection to keep in state. A failed load calls onLoadChildrenError, and a folder reports aria-busy while it loads.
How do I change the file and folder icons in a Tree View?
Pass fileIcons to TreeView, built with createFileIcons, to pick a file's icon by its extension, as the Custom icons demo does, reading the extension from the node's id. For one file, set icon on its TreeViewItem, though a match in fileIcons wins over it. Map an extension to null to show no icon for it.
A folder takes icon for its closed state and expandedIcon for its open state on TreeViewBranchItem, and null hides either one. A row with a checkbox hides its file icon to make room.
How do I add, rename or delete nodes in a Tree View?
Keep the collection in state and replace it with a new one. A collection never changes in place: replace, remove, insertBefore, insertAfter and move each return a new collection, and they take index paths, which collection.getIndexPath finds from an id.
The Rename demo does this for renaming, with canRename to allow F2 and onRenameComplete calling replace. For a search box above the tree, filter returns a copy that keeps the matching nodes and the folders above them.
Is the Tree View accessible with the keyboard?
Mostly. The tree is one Tab stop, Up and Down move between visible rows, Right opens a folder or steps into it, Left closes it or steps out to its parent, and typing a letter jumps to the next row whose name starts with it. Enter and Space select the focused row, and on a folder they open or close it too.
On a row with a checkbox, Space checks or unchecks it instead. One gap remains: the letter a never jumps, because the select-all shortcut takes it. The Accessibility section lists every key.
The initial checked node value when rendered.
Use when you don't need to control the checked node value.
defaultExpandedValue?string[]
The initial expanded node ids when rendered.
Use when you don't need to control the expanded node value.
defaultFocusedValue?string | null
The initial focused node value when rendered.
Use when you don't need to control the focused node value.
Type
string | null
defaultSelectedValue?string[]
The initial selected node value when rendered.
Use when you don't need to control the selected node value.
dir?"ltr" | "rtl"
The document's text/writing direction.
Type
"ltr" | "rtl"
Default
"ltr"
expandedValue?string[]
The controlled expanded node ids.
expandOnClick?boolean
Whether clicking on a branch should open it or not.
Default
true
focusedValue?string | null
The value of the focused node.
Type
string | null
hideMode?"display-none" | "activity"
How to hide content when mounted but not present.
- display-none: HTML hidden attribute. Effects stay alive.
- activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.
Type
"display-none" | "activity"
Default
"display-none"
id?string
The unique identifier of the machine.
ids?object
The ids of the tree elements. Useful for composition.