@gfazioli/mantine-json-tree
A Mantine extension component that renders interactive JSON trees with syntax highlighting, collapsible nodes, copy-to-clipboard, and configurable expansion depth.
What's new
- Dark mode support with automatic color adaptation
- Line numbers display (
showLineNumbers) - Path tooltip on hover (
showPathOnHover) - Max height with scrollable container (
maxHeight) - Controlled expand/collapse state (
expanded,onExpandedChange) onExpandandonCollapsecallbacks for individual node toggling- Keyboard copy:
Ctrl+C/Cmd+Ccopies the focused node whenwithCopyToClipboardis enabled - Responsive
sizeprop via Mantine breakpoint objects (CSS-native, no re-renders) - New
lineNumberstyle selector with--json-tree-color-line-numberCSS variable
Installation
After installation import package styles at the root of your application:
You can import styles within a layer @layer mantine-json-tree by importing @gfazioli/mantine-json-tree/styles.layer.css file.
Usage
The JsonTree is interactive JSON tree viewer component built with Mantine's Tree component. Features collapsible nodes, syntax highlighting with type-specific colors, copy-to-clipboard functionality, item count badges, configurable expansion depth, and smooth animations. Perfect for debugging API responses, exploring complex data structures, and developer tools.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[...]3
- wife:
null - onClick:
[Function: onClick] - address:{...}3
- action:{...}2
- projects:[...]2
Syntax Highlighting values
Below is an example of syntax highlighting for different JSON value types: strings, numbers, booleans, nulls, objects, and arrays. Each type is displayed in a distinct color for better readability.
"Hello, World!"
42
true
false
null
- {
- key1:
"value1" - key2:
123 - key3:
false - key4:
null
- [
- 0:
"string" - 1:
456 - 2:
true - 3:
null - 4:{
- nestedKey:
"nestedValue"
Special Value Types
JsonTree supports modern JavaScript types beyond standard JSON primitives. These include Date objects, special numeric values (NaN, Infinity), BigInt for large integers, Symbols, RegExp patterns, and ES6 collections like Map and Set. Each type is displayed with distinct syntax highlighting and formatting:
- Date: Displayed in ISO 8601 format (e.g.,
2024-01-15T10:30:00.000Z) - NaN / Infinity: Special numeric values shown with their standard JavaScript representation
- BigInt: Large integers displayed with the
nsuffix (e.g.,9007199254740991n) - Symbol: Unique identifiers shown with their description (e.g.,
Symbol(unique)) - RegExp: Regular expressions displayed with their pattern and flags (e.g.,
/pattern/gi) - Map: Key-value collections that are expandable to show their entries
- Set: Unique value collections that are expandable to show their elements
- React Elements: React components displayed with their component name (e.g.,
<Loader />)
All colors can be customized using CSS variables for each type.
- {
- reactLoader:
<@mantine/core/Loader /> - reactButton:
<button /> - htmlDiv:
<div /> - createdAt:
2024-01-15T10:30:00.000Z - lastModified:
2024-06-20T14:45:30.000Z - notANumber:
NaN - positiveInfinity:
Infinity - negativeInfinity:
-Infinity - bigInteger:
9007199254740991n - userId:
123456789n - globalSymbol:
Symbol(app.config) - registryKey:
Symbol(app.registry) - emailPattern:
/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/g - phonePattern:
/\d{3}-\d{3}-\d{4}/ - userMap:{
- [0] user1:{...}2
- [1] user2:{...}2
- [2] 123:
"numeric key example"
- tags:{
- 0:
"javascript" - 1:
"typescript" - 2:
"react"
- uniqueNumbers:{
- 0:
1 - 1:
2 - 2:
3 - 3:
4 - 4:
5
- metadata:{
- timestamp:
2024-12-25T00:00:00.000Z - categories:{...}2
- config:{...}2
Indent Guides
Display vertical lines to visualize nesting levels, similar to Visual Studio Code. The guides use 5 distinct colors that cycle for deeper nesting levels.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[
- 0:
"html" - 1:
"css" - 2:
"js"
- wife:
null - onClick:
[Function: onClick] - address:{
- street:
"123 Main St" - city:
"Anytown" - zip:
"12345"
- action:{
- type:
"click" - payload:
undefined
- projects:[
- 0:{
- name:
"Project A" - status:
"completed"
- 1:{
- name:
"Project B" - status:
"in progress"
Line Numbers
Display line numbers alongside each node, similar to code editors. This is useful for referencing specific parts of the JSON structure.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[
- 0:
"html" - 1:
"css" - 2:
"js"
- wife:
null - onClick:
[Function: onClick] - address:{
- street:
"123 Main St" - city:
"Anytown" - zip:
"12345"
- action:{
- type:
"click" - payload:
undefined
- projects:[
- 0:{
- name:
"Project A" - status:
"completed"
- 1:{
- name:
"Project B" - status:
"in progress"
Sticky Header
You may enable sticky headers for better context when scrolling through large JSON structures. When enabled, the header of each expanded node remains visible at the top of the scrollable area as you navigate through its child elements. You can also set an offset to accommodate fixed headers in your layout.
Icons
You can customize the expand/collapse icons used in the JsonTree component by providing your own icons via the expandControlIcon and collapseControlIcon props. The default icons are simple chevrons, but you can replace them with any React component or icon of your choice.
Note: If only the
expandControlIconis provided, the component will automatically use it for both expand and collapse states, by rotating (90deg) it accordingly. If only thecollapseControlIconis provided, the default expand icon will be used. Of course, you can provide both icons for complete customization.
Only Expand Icons
- {...}
Only Collapse Icons
- {...}
Both Expand and Collapse Icons
- {...}
Function Display
By default, functions in JSON data are displayed as strings (e.g., [Function: name]). You can control how functions are handled using the displayFunctions prop:
as-string(default): Display functions as formatted strings showing their namehide: Completely omit functions from the treeas-object: Treat functions as objects and display their properties
as-string- {
- name:
"UserProfile" - age:
25 - onClick:
[Function: onClick] - calculate:
[Function: calculate] - methods:{
- fetchData:
[Function: fetchData] - process:
[Function: process]
- data:[
- 0:
1 - 1:
2 - 2:
3
- isActive:
true
hide- {
- name:
"UserProfile" - age:
25 - methods:
[object Object] - data:[
- 0:
1 - 1:
2 - 2:
3
- isActive:
true
as-object- {
- name:
"UserProfile" - age:
25 - onClick:{
- length:
0 - name:
"onClick" - prototype:
[object Object]
- calculate:{
- length:
2 - name:
"calculate"
- methods:{
- fetchData:{...}
- process:{...}
- data:[
- 0:
1 - 1:
2 - 2:
3
- isActive:
true
Callbacks
Use the onNodeClick callback to handle clicks on any node in the tree, and the onCopy callback to react when a value is copied to clipboard. Both callbacks provide the relevant data for integration with your application logic.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[...]
- wife:
null - onClick:
[Function: onClick] - address:{...}
- action:{...}
- projects:[...]
Keyboard Navigation
JsonTree inherits full keyboard navigation from Mantine's Tree component:
- Arrow Up/Down: Navigate between nodes
- Arrow Right: Expand a collapsed node or move to first child
- Arrow Left: Collapse an expanded node or move to parent
- Space: Toggle node expansion
- Ctrl+C / Cmd+C: Copy the focused node's value to clipboard (when
withCopyToClipboardis enabled)
Path Tooltip
Set showPathOnHover to display the full JSON path in a tooltip when hovering over any node. This is useful for identifying the exact path to a value in deeply nested structures. You can customize the tooltip behavior with the tooltipProps prop, which accepts all Tooltip props except label and children.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[
- 0:
"html" - 1:
"css" - 2:
"js"
- wife:
null - onClick:
[Function: onClick] - address:{
- street:
"123 Main St" - city:
"Anytown" - zip:
"12345"
- action:{
- type:
"click" - payload:
undefined
- projects:[
- 0:{
- name:
"Project A" - status:
"completed"
- 1:{
- name:
"Project B" - status:
"in progress"
Max Height
Use the maxHeight prop to limit the tree height and enable scrolling. This is useful for embedding the tree in layouts with limited vertical space.
Controlled Expand State
Use the expanded and onExpandedChange props for controlled expand/collapse state. The onExpand and onCollapse callbacks fire for individual node toggling:
Responsive size
The size prop supports responsive values using Mantine breakpoint objects. This allows you to set different font sizes based on the viewport width, using CSS media queries (no JavaScript re-renders):
Style the component with classNames
You can style the JsonTree component using the classNames prop to target specific inner elements. This allows for granular customization of the component's appearance.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[...]3
- wife:
null - onClick:
[Function: onClick] - address:{...}3
- action:{...}2
- projects:[...]2
Styles API
JsonTree supports Styles API, you can add styles to any inner element of the component with classNames prop. Follow Styles API documentation to learn more.
You can customize the appearance of the JsonTree component using the Styles API. This allows you to modify styles for various parts of the component, such as keys, values, brackets, and more.
- {
- name:
"John Doe" - age:
30 - isAdmin:
false - courses:[...]3
- wife:
null - onClick:
[Function: onClick] - address:{...}3
- action:{...}2
- projects:[...]2
Component Styles API
Hover over selectors to highlight corresponding elements