shadcn.io is an independent project built on shadcn/ui
On this page5 sections
Installation
Usage
<script setup lang="ts">
import { JsonTreeView } from "@/components/ui/json-tree-view";
</script>
const data = {
name: "John Doe",
age: 30,
email: "john.doe@example.com",
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const Example = () => (
<div className="w-full max-w-md">
<JsonTreeView data={data} defaultExpandedDepth={1} />
</div>
);Demos
Data Types
Expand Depth
Map Set
FAQ
How do I choose how much of the tree is open?
Set defaultExpandedDepth. With 1, the top level's children are open and deeper levels stay closed. The Expand Depth demo shows the values side by side.
Which values can it show?
Strings, numbers, booleans, null, arrays and objects, and also Maps and Sets, as the Map Set demo shows. Strings, numbers, booleans and null each get their own color from your theme's tokens.
How do I render a value my own way?
Pass renderValue. It is handed to the tree and receives each value's node, so you can return your own output, such as a link or a swatch, in place of the raw text.
Is it fast with a large JSON document?
It mounts lazily: branches are rendered when opened, and unmounted again when closed (lazyMount and unmountOnExit default to true). A closed branch of thousands of rows costs nothing until the user opens it.
API Reference
JsonTreeView
dataunknown
The data to show.
canRename?(node: T, indexPath: IndexPath) → boolean
- Type
(node: T, indexPath: IndexPath) → boolean- Parameters
node– TindexPath– IndexPath- Returns
boolean
checkedValue?string[]collapseStringsAfterLength?number
How many characters of a string show in a preview.
- Default
30
defaultCheckedValue?string[]defaultExpandedDepth?number
How many levels open at first; the root value is level 1.
defaultExpandedValue?string[]defaultSelectedValue?string[]expandedValue?string[]expandOnClick?boolean
Whether pressing a branch's row expands or collapses it. Its indicator always does.
- Default
true
fileIcons?Record<string, Component | null>
Custom extension icons.
- Type
Record<string, Component | null>
groupArraysAfterLength?number
Arrays longer than this split into groups of this many items.
- Default
100
lazyMount?boolean
Mount a branch's children only once it's first expanded. A branch's
children always mount when it expands; unmountOnExit keeps them after.
- Default
true
loadChildren?(details: LoadChildrenDetails<T>) → Promise<T[]>
Loads a branch's children the first time it expands while it has none.
- Type
(details: LoadChildrenDetails<T>) → Promise<T[]>- Parameters
details– LoadChildrenDetails<T>- Returns
Promise<T[]>
maxPreviewItems?number
How many entries an object, array, map or set lists in its preview.
- Default
3
quotesOnKeys?boolean
Whether keys show in quotes.
renderValue?RenderValueselectedValue?string[]selectionMode?"single" | "multiple"
How many nodes can be selected.
- Type
"single" | "multiple"- Default
"single"
showNonenumerable?boolean
Show properties that aren't enumerable, like a map's entries.
- Default
true
typeahead?boolean
Whether typing moves to the next node whose name starts with the text.
- Default
true
unmountOnExit?boolean
Unmount a branch's children when it collapses.
- Default
true
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
@before-rename(details: RenameCompleteDetails) → boolean
- Type
(details: RenameCompleteDetails) → boolean- Parameters
details– { indexPath: IndexPath; label: string; value: string }- Returns
boolean
@checked-change(details: CheckedChangeDetails) → void
Called when the checked state changes.
- Type
(details: CheckedChangeDetails) → void- Parameters
details– { checkedValue: string[] }- Returns
void
@expanded-change(details: ExpandedChangeDetails<T>) → void
- Type
(details: ExpandedChangeDetails<T>) → void- Parameters
details– ExpandedChangeDetails<T>- Returns
void
@focus-change(details: FocusChangeDetails<T>) → void
- Type
(details: FocusChangeDetails<T>) → void- Parameters
details– FocusChangeDetails<T>- Returns
void
@load-children-complete(details: { collection: TreeCollection<T> }) → void
The collection with the loaded children in place, to keep as the new one.
- Type
(details: { collection: TreeCollection<T> }) → void- Parameters
details– { collection: TreeCollection<T> }- Returns
void
@load-children-error(details: { nodes: NodeWithError<T>[] }) → void
- Type
(details: { nodes: NodeWithError<T>[] }) → void- Parameters
details– { nodes: NodeWithError<T>[] }- Returns
void
@rename-complete(details: RenameCompleteDetails) → void
- Type
(details: RenameCompleteDetails) → void- Parameters
details– { indexPath: IndexPath; label: string; value: string }- Returns
void
@rename-start(details: RenameStartDetails<T>) → void
- Type
(details: RenameStartDetails<T>) → void- Parameters
details– RenameStartDetails<T>- Returns
void
@selection-change(details: SelectionChangeDetails<T>) → void
- Type
(details: SelectionChangeDetails<T>) → void- Parameters
details– SelectionChangeDetails<T>- Returns
void