shadcn.io is an independent project built on shadcn/ui
Components
Free
Tree View for UI SDK
A tree of folders and files that people walk with the arrow keys, with selection, checkboxes, renaming and lazy loading, for React and UI SDK 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.
This component is built on UI SDK's Tree, and keeps the collection, nodes and parts the other versions have.
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, or with an aria-label on TreeViewTree: without either, the tree has no name. 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 into the tree, onto the selected node or else the first one, and later back to the node focused last.
Enter
Selects the node, or in a tree that allows multiple selection, adds or removes it. On a branch node, also opens or closes it unless expandOnClick is false, and on a link, follows it.
Space
Selects the node, or clears it when it is the one selected, without opening or closing anything. On a row with a checkbox, checks or unchecks it instead.
Arrow Down
Moves focus to the next visible node.
Arrow Up
Moves focus to the previous visible 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 its first child. Right to left, ArrowLeft does this.
Arrow Left
When focus is on an open branch node, closes it. Otherwise moves focus to its parent branch node. Right to left, ArrowRight does this.
Alt+Arrow Right
Opens the branch node and every branch inside it.
Alt+Arrow Left
Closes the branch node and every branch inside it.
Home
Moves focus to the first node.
End
Moves focus to the last visible node.
a-z
Moves focus to the next visible node whose name starts with the typed text.
*
Opens every branch node beside the focused node, except those whose children haven't loaded yet.
Shift+Arrow Down
In a tree that allows multiple selection, moves focus to the next node and extends the selection to it.
Shift+Arrow Up
In a tree that allows multiple selection, moves focus to the previous node and extends the selection to it.
⌘Command+A
In a tree that allows multiple selection, selects every visible node, or clears the selection once they all are. Ctrl + A does the same.
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 adds or removes a row, with or without Ctrl or Cmd, 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 the folder then opens from its chevron, which toggles it without selecting it, or from the keyboard: 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, and Space on the focused row checks or unchecks its box. A tree with checkboxes selects nothing unless you also pass selectionMode or a selection prop, so there Enter only opens or closes a folder.
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?
Yes. 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 jumps to the next row whose name starts with the text. Enter selects the focused row and opens or closes a folder, while Space selects or clears it without opening anything.
On a row with a checkbox, Space checks or unchecks it instead. Alt with Right or Left opens or closes a folder and everything inside it. The Accessibility section lists every key.
Whether moving focus with the keyboard selects, with one item selectable: "automatic" selects the focused item, "manual" waits for Enter, Space or a press.
What screen readers hear when a branch expands, collapses, starts loading or fails to load. Return null for nothing. Expanding and collapsing are announced on Apple platforms only, where VoiceOver doesn't read the change.
Type
(change: TreeAnnouncement) → string | null
Default
"Expanded", "Collapsed", "Loading", or "Couldn't load src."
Parameters
change – TreeAnnouncement
Returns
string | null
checkable?boolean
Gives every item a tri-state checkbox, kept apart from the selection. Checking a branch checks everything in it, and a branch shows mixed while only some of it is checked. Space checks the focused item.
Default
false
checkStrategy?TreeCheckStrategy
Which items the checked value lists: "leaf" the checked leaves, "trunk" the outermost fully checked items, "branch" every fully checked item, and "independent" each item on its own, with no cascading.
Default
"leaf"
controller?TreeController
Drives the tree from anywhere. Create one with createTreeController().
current?string | null
The value of the item for the current page. It gets aria-current="page", takes the tab stop first, and its ancestors expand, at first and whenever it changes.
Type
string | null
debug?boolean
Logs every state change with its reason to the console, in development builds.
disabled?boolean
Disables every item. Items stay focusable unless focusableWhenDisabled is false.
Default
false
expandOnPress?boolean
Whether pressing a branch's row expands or collapses it. Its indicator always does.
Default
true
focusableWhenDisabled?boolean
Whether disabled items take focus, so a screen reader still finds them.
Default
true
hiddenUntilFound?boolean
Keeps collapsed groups in the page as hidden="until-found", so the browser's find in page can match their text and expand them.
Default
false
items?TreeItemData[]
The items as data: each a value, a label and, for a branch, children. Renders them for you, and gives the tree its whole shape even where rows aren't rendered.
keepMounted?boolean
Whether collapsed branches keep their groups in the page, hidden.
Loads a branch's children the first time it expands while it has none. The branch shows data-loading and expands once the Promise resolves; a rejection marks it data-failed and the next expand tries again. Expanding it again while loading cancels through signal.
Runs when an item is pressed or Enter is pressed on it, apart from selecting it, like opening a file. Return a Promise to mark the item pending until it settles.
Runs when the keyboard moves focus to an item, with its row among the items that show, from 0. A virtualized list scrolls that row into view here, so an item it hasn't rendered can take focus once it does.
Items from file paths, like ["src/index.ts", "README.md"]: each folder becomes a branch, folders first. A path ending in / is an empty folder.
pendingAnnouncement?string
What screen readers hear once an item's action has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.