JSON Tree View
A component that displays JSON data in an interactive, collapsible tree structure.
Anatomy
Each part has a data-tree-view-{part} attribute in the DOM.
Examples
Different Data Types
Objects, arrays, primitives, and special values are all supported.
Functions and Methods
Functions, async functions, and generators are displayed too.
Regular Expressions
Regular expressions show their pattern and flags.
Error Objects
Error objects show their stack traces.
Map and Set Objects
Native Map and Set objects are supported.
Controlling Expand Level
Use defaultExpandedDepth to set how many levels start expanded.
Custom Value Rendering
Use renderValue to customize how values render. This example makes email addresses clickable.
Configuration Options
These props customize the display:
<JsonTreeView.Root
data={data}
defaultExpandedDepth={2}
quotesOnKeys={true}
showNonenumerable={true}
maxPreviewItems={5}
collapseStringsAfterLength={50}
groupArraysAfterLength={100}
>
<JsonTreeView.Tree arrow={<ChevronRightIcon />} />
</JsonTreeView.Root>
Configuration Options:
quotesOnKeys: show quotes around object keysshowNonenumerable: show non-enumerable propertiesmaxPreviewItems: max items in object/array previewscollapseStringsAfterLength: collapse strings longer than thisgroupArraysAfterLength: group items of arrays longer than this
Using the Root Provider
Use RootProvider with the useJsonTreeView hook to access the state and methods from outside the component.
With
RootProvider, you don't needRoot.
API Reference
JsonTreeViewRoot
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
canRename | (node: JsonNode<any>, indexPath: IndexPath) => booleanFunction to determine if a node can be renamed | |
checkedValue | string[]The controlled checked node value | |
collapseStringsAfterLength | number | |
data | {}The data to display in the tree. | |
defaultCheckedValue | string[]The initial checked node value when rendered. Use when you don't need to control the checked node value. | |
defaultExpandedDepth | numberThe default expand level. | |
defaultExpandedValue | string[]The initial expanded node ids when rendered. Use when you don't need to control the expanded node value. | |
defaultFocusedValue | stringThe initial focused node value when rendered. Use when you don't need to control the focused node value. | |
defaultSelectedValue | string[]The initial selected node value when rendered. Use when you don't need to control the selected node value. | |
expandedValue | string[]The controlled expanded node ids | |
expandOnClick | true | booleanWhether clicking on a branch should open it or not |
focusedValue | stringThe value of the focused node | |
groupArraysAfterLength | number | |
hideMode | 'display-none' | HideModeHow 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+. |
ids | ElementIdsThe ids of the tree elements. Useful for composition. | |
lazyMount | false | booleanWhether to enable lazy mounting |
loadChildren | (details: LoadChildrenDetails<JsonNode<any>>) => Promise<JsonNode<any>[]>Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding. | |
maxPreviewItems | number | |
onBeforeRename | (details: RenameCompleteDetails) => booleanCalled before a rename is completed. Return false to prevent the rename. | |
onCheckedChange | (details: CheckedChangeDetails) => voidCalled when the checked value changes | |
onExpandedChange | (details: ExpandedChangeDetails<JsonNode<any>>) => voidCalled when the tree is opened or closed | |
onFocusChange | (details: FocusChangeDetails<JsonNode<any>>) => voidCalled when the focused node changes | |
onLoadChildrenComplete | (details: LoadChildrenCompleteDetails<JsonNode<any>>) => voidCalled when a node finishes loading children | |
onLoadChildrenError | (details: LoadChildrenErrorDetails<JsonNode<any>>) => voidCalled when loading children fails for one or more nodes | |
onRenameComplete | (details: RenameCompleteDetails) => voidCalled when a node label rename is completed | |
onRenameStart | (details: RenameStartDetails<JsonNode<any>>) => voidCalled when a node starts being renamed | |
onSelectionChange | (details: SelectionChangeDetails<JsonNode<any>>) => voidCalled when the selection changes | |
quotesOnKeys | booleanWhether to show quotes on the keys. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
scrollToIndexFn | (details: ScrollToIndexDetails<JsonNode<any>>) => voidFunction to scroll to a specific index. Useful for virtualized tree views. | |
selectedValue | string[]The controlled selected node value | |
selectionMode | 'single' | 'single' | 'multiple'Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected |
showNonenumerable | boolean | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
typeahead | true | booleanWhether the tree supports typeahead search |
unmountOnExit | false | booleanWhether to unmount on exit. |
JsonTreeViewRootProvider
| Prop | Default | Type |
|---|---|---|
value | UseJsonTreeViewReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
hideMode | 'display-none' | HideModeHow 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+. |
lazyMount | false | booleanWhether to enable lazy mounting |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
unmountOnExit | false | booleanWhether to unmount on exit. |
JsonTreeViewTree
| Prop | Default | Type |
|---|---|---|
arrow | ReactElement<unknown, string | JSXElementConstructor<any>>The icon to use for the arrow. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
indentGuide | boolean | ReactElement<unknown, string | JSXElementConstructor<any>>The indent guide to use for the tree. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
renderValue | (node: JsonNodeHastElement) => ReactNodeThe function to render the value of the node. |
Accessibility
Built on Tree View and complies with the Tree View WAI-ARIA design pattern.
Keyboard Support
| Key | Description |
|---|---|
Tab | Moves focus to the tree view, placing the first tree view item in focus. |
EnterSpace | Selects the item or branch node |
ArrowDown | Moves focus to the next node |
ArrowUp | Moves focus to the previous node |
ArrowRight | 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. |
ArrowLeft | When focus is on an open branch node, closes the node. When focus is on an 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-zA-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 + ArrowDown | Moves focus to and toggles the selection state of the next node. |
Shift + ArrowUp | Moves focus to and toggles the selection state of the previous node. |
Ctrl + A | Selects all nodes in the tree. If all nodes are selected, unselects all nodes. |