JSON tree

Slant / Content

JSON as a tree of typed values, open to a chosen depth, with copy actions on every row and long arrays paged a hundred at a time.

Demo

Usage

@tuttn/slant
import { JsonTree } from '@tuttn/slant'

// Uncontrolled: open to a depth (1, the default, shows the root's children).
<JsonTree label="Response body" data={payload} defaultExpandDepth={2} />

// Or start from chosen paths.
<JsonTree label="Response body" data={payload} defaultExpanded={['$', '$.user', '$.items[0]']} />

// Controlled: you hold the open paths. onCopy hears every copy from the row menu.
const [open, setOpen] = useState(['$'])
<JsonTree label="Config" data={config} expanded={open} onExpandedChange={setOpen}
  onCopy={(kind, text) => track(kind)} />

// Long arrays and objects show 100 children at a time; pageSize changes it.
// copyable={false} drops the row menu.
<JsonTree label="Samples" data={samples} pageSize={50} copyable={false} size="md" />

Props

PropTypeDefaultDescription
dataunknown—The value to show: parsed JSON, or anything JSON.stringify could write. A `toJSON` method is used as JSON.stringify uses it, so a Date shows as its ISO string.
labelstring—The tree's accessible name. Required, unless `aria-labelledby` names the tree instead.
defaultExpandDepthnumber1How many levels start open, when uncontrolled and without `defaultExpanded`: 0 shows the root row alone, 1 the root's children, 2 their children too. Only the first page of each opens.
expandedstring[]—The paths of the open objects and arrays (`$`, `$.user`, `$.items[2]`). Pass it with onExpandedChange to control them.
defaultExpandedstring[]—The paths open at first, when uncontrolled. Given, it replaces `defaultExpandDepth`.
onExpandedChange(paths: string[]) => void—Called with the path of every open object and array whenever one opens or closes.
pageSizenumber100How many children an object or array shows at a time. A last row after them shows the next page.
copyablebooleantrueA menu on every row, from its ⋯ button, Shift+F10, the menu key or a right-click: Copy value, Copy path and Copy JSON.
onCopy(kind: 'value' | 'path' | 'json', text: string) => void—Called after a copy from the row menu succeeds, with which copy it was and the text written.
size'sm' | 'md''sm'The row height, on the control scale, with the type to match.
…restHTMLAttributes<HTMLUListElement>—className, style, the ref and other attributes reach the role="tree" list (Tree's), which is dir="ltr" unless you pass a dir.

Keyboard

↓ / ↑moves to the next or previous visible row
Home / Endmoves to the first or last visible row
→opens a closed object or array; on an open one, moves to its first child
←closes an open object or array; otherwise moves to its parent
Enter / Spaceopens or closes an object or array; on a “show more” row, shows the next page and moves to its first row
*opens every object and array beside the focused row
a–z, 0–9moves to the next visible row whose key or index starts with the letters typed
Shift+F10 / Menu keyopens the focused row’s menu: Copy value, Copy path, Copy JSON
↓ / ↑ / Home / End, Enterin the menu, moves between the copies and makes one; focus returns to the row
Escapecloses the menu without copying; focus returns to the row

Accessibility

  • It is Slant’s Tree: a role="tree" named by label (or aria-labelledby), one treeitem per row with aria-level and aria-expanded on objects and arrays, and one tab stop that moves with focus. The tree keeps no selection, so no row is ever aria-selected.
  • Each row is named as it reads, without the brackets: status: "succeeded", user: 3 keys, tags: 3 items; the root by its size. Strings keep their JSON quotes, so "3" and 3 stay apart.
  • The ⋯ button is tabIndex −1, named “Row actions” (messages.jsonRowActions) with aria-haspopup="menu", so the tree keeps one tab stop; the keyboard opens the same menu with Shift+F10 or the menu key. A press on the button doesn’t move focus, and the menu returns it to the row.
  • A copy is announced through the root’s polite announcer (“Copied”, or that copying failed).
  • Colour carries the value’s type alone, through the code-token colours, which hold 4.5:1 on the tree’s --sl-bg in every theme; the text says the same thing (quotes, true, null).

Paths

Every row’s id is its path from the root, JSONPath-like: $ for the root, .name for a key that is an identifier, ["first name"] for any other key, [2] for an index, so $.user.tags[2]. expanded, defaultExpanded and onExpandedChange carry these paths, and Copy path writes one.

Values

Strings show with their quotes and escapes, so a string stays on one line; numbers, and true, false and null as keywords. A long key or value ends in an ellipsis. An object or array shows {…} or […] and its size; an empty one can’t open. Anything JSON.stringify could write works: a toJSON method is used (a Date shows as its ISO string), a bigint shows with its n, undefined as a keyword. Copy value writes a string without its quotes and anything else as compact JSON; Copy JSON writes JSON.stringify(value, null, 2).

Opening

Enter, Space and a click on a row open or close it; the chevron does too. Children are only built while their parent is open, and defaultExpandDepth opens the first page of each level only.

Paging

An object or array with more than pageSize children (100) shows the first page, then a last row reading “Show 100 more of 250”. Enter, Space or a click on it shows the next page and moves focus to the first row it added.

Direction

JSON reads left to right in every language, so the tree is dir="ltr" under dir="rtl" too: children indent to the right and Right opens. The counts and the “show more” row take their own direction from their words, and the menu follows the page.