- Add the registry
- Add json-viewer
Usage
Pass data any value JSON can hold. A top-level object or array shows its children as the first
level, and defaultExpandDepth sets how many levels start open, counted from the first data
with any children, so data that arrives after a loading state still opens. To show a JSON string,
parse it first. Values JSON leaves out, such as undefined, functions and references back to an
object that holds them, show in a muted color rather than breaking the tree. Dates show as the
strings JSON.stringify would write, and like it, a Map or Set shows as an empty object.
Set a height on JsonViewer, such as max-h-96, and the tree scrolls inside it. Give it a label
for a header with buttons to expand, collapse and copy everything. The label names the tree too, or
pass an aria-label to name it instead.
The viewer remembers what's open by path, so a new data with the same shape keeps the same rows
open. Pass a key that changes with the data, such as a record's id, to start over with each one,
and keep data from changing on every render, with useMemo or a value from outside the
component, since the viewer reads the whole value whenever it does.
Each row is named by its path, written the way you'd reach it in code, such as
deployment.regions[0] or delivery.headers["content-type"], and a key that isn't a plain name,
such as "content-type", shows in quotes. Pointing at a row, or moving to it, shows buttons to copy
its value or its path. A string's value is copied without its quotes, an object's or array's as
formatted JSON, and any other value as it's shown. Clicking a string that's cut off wraps it in
place.
The tree is a single stop in the tab order. Inside it, the up and down arrows move between rows,
right opens an object or array or moves into it, and left closes it or moves to its parent. Home
and End jump to the first and last rows, * opens every object and array beside the current one,
and Enter or Space opens or closes one. C copies the current row's value, and P its path.
Examples
Searchable
searchable adds a field that finds keys and values as you type, and opens whatever holds the
current match, including a row past pageSize. The current match's row wraps, so a match far along
a long string stays in view. Enter and Shift+Enter step through the matches, and Escape clears the
search.
Expand depth
defaultExpandDepth is 1 by default, which opens the top-level objects and arrays. Pass 0 to
start with everything closed, or Infinity to open everything.
Controlled
Pass expanded and onExpandedChange to control which objects and arrays are open, by their paths.
Open: deployment
Long arrays
An object or array with more children than pageSize, 100 by default, shows that many and a row to
show more. Stepping to a search match past them shows enough to reach it.
As raw text
highlightJson colors formatted JSON line by line, for Code Block's highlightedLines. Pass it the
same string you pass Code Block as code.
In a dialog
A dialog that switches between the tree and the raw text.
Colors
Keys and values are colored with the theme's syntax tokens: syntax-property for keys,
syntax-string, syntax-number, and syntax-constant for true, false and null. They follow
GitHub's light and dark themes, as the docs' code blocks do, so keys, numbers and constants share a
blue, and quotes and colons tell them apart. Change them in your CSS to recolor every JSON Viewer,
and Code Blocks highlighted with highlightJson. See Theming.
Props
JsonViewer takes the props of a <div>, apart from children.
The file also exports highlightJson(code), which takes formatted JSON and returns one element per
line, colored with the syntax tokens, for Code Block's highlightedLines.
The tables leave out className, style and render. Parts built on Base UI take them as its styling and composition guides describe.