Skip to content

JsonTree

Inspect-only foldable tree for a JSON object or array. The Consumer owns the value. Not TreeView, not CodeBlock, and not a parser.

import { JsonTree } from "phoundry-ui";

Object

Loading interactive example…

View source
object.svelte
<script lang="ts">
import { JsonTree } from "phoundry-ui";
const sample = {
name: "Phials",
count: 3,
ok: true,
nested: {
theme: "dark",
tags: ["app", "files"],
},
};
</script>
<div class="rounded-md border border-border-muted bg-surface-sunken px-3 py-2">
<JsonTree value={sample} />
</div>

Array

Loading interactive example…

View source
array.svelte
<script lang="ts">
import { JsonTree } from "phoundry-ui";
const arraySample = [
{ id: 1, label: "alpha" },
{ id: 2, label: "beta" },
];
</script>
<div class="rounded-md border border-border-muted bg-surface-sunken px-3 py-2">
<JsonTree value={arraySample} />
</div>

Deeper default expand

Loading interactive example…

View source
deeper-default-expand.svelte
<script lang="ts">
import { JsonTree } from "phoundry-ui";
const sample = {
name: "Phials",
count: 3,
ok: true,
nested: {
theme: "dark",
tags: ["app", "files"],
},
};
</script>
<p class="mb-2 text-xs text-txt-secondary">
<code>defaultExpandDepth</code> of 2 opens the first nested object. The default
is 1: root keys visible, nested containers collapsed.
</p>
<div class="rounded-md border border-border-muted bg-surface-sunken px-3 py-2">
<JsonTree value={sample} defaultExpandDepth={2} />
</div>

API

From phoundry-ui@0.4.24

NameTypeDefaultDescription
valuerequiredunknown—Already-parsed JSON. Objects and arrays render as a foldable tree of keys or indices. JsonTree does not parse strings.
defaultExpandDepthnumber1How many levels start open. 1 lists the value's own keys or indices and leaves nested objects and arrays collapsed.
depthnumber0Recursive depth; leave at zero for a top-level tree.
classClassValue—Additional CSS classes on the root tree.
stylestring—Inline styles on the stable component root.

Usage tips

  • Pass a parsed object or array. Detection and JSON.parse belong in the Consumer.
  • There is no wrapper { / [ row. The tree starts at the value’s own keys or indices.
  • Nested string leaves stay strings. JsonTree does not sniff them again.
  • Empty objects and arrays render as {} / [] leaves, not as blank fold rows.
  • This is inspect-only. There is no selection and no drag.

Styling

.ph-json-tree is the stable root hook. Consumer class values are additive, and style applies to that root. Styling uses vanilla CSS with semantic state attributes. The root remains a tree, nested lists remain group elements, and expandable rows retain their ARIA state and button controls. Top-level entries and collapsed-by-default nested containers behave as before. The shared .ph-json-value primitive maps data-kind to the active Theme’s syntax colors for both scalar roots and recursive leaves. Internal row selectors and DOM hierarchy are not customization APIs. Internal part selectors and --_ph-* properties remain implementation details.

Documentation is licensed under CC BY 4.0. Phoundry names and marks remain reserved.