All components (283)
JSON viewer
Data · JsonViewer · .amset-jsonviewer
What it is and when to use it
A JSON to read: it opens and closes by levels, the values come out in the colour of their type and the whole thing can be copied.
When to use it
- Showing the response of an API, an audit record or the content of a message to whoever reviews it.
valueis any value: an object, a list or a single value. Dates,bigint,undefinedand circular references come out fine.defaultOpenDepthsays how many levels start open (1 by default; withInfinity, all of them),maxItemshow many entries are drawn before "N more" andsortKeyssorts the keys.- Pressing a key copies its path (
customer.address.city,lines[0].sku). - "Expand all" and "Collapse all" open or fold every level at once.
- The top bar shows the path of the key under the pointer or with focus (
customer.address.city), without having to copy it.
When not to
- Code that is written or copied with its lines:
CodeBlock. - Data that is edited: a form.
- Data compared in columns:
Table.
Keyboard: each level is a browser <details>: Tab moves from level to level and Enter or Space opens and closes it. The page finder (Ctrl+F) opens the level where it finds the text.
Accessibility: it is a group named after label; each level says whether it is open or closed and how many keys or items it has. Colour is not the only thing that tells a type apart: strings carry quotes and empty values are written out (null, undefined).
Fine-tuning: --amset-jsonviewer-font, --amset-jsonviewer-indent and the colours --amset-jsonviewer-key, -string, -number and -keyword.
What it looks like
{8 keys}
customer: {3 keys}
address: {2 keys}
lines: [2 items]
0: {3 keys}
1: {3 keys}
<JsonViewer label="Order response" value={pedido} />Props
Import it with import { JsonViewer } from '@amset/ui'. Required props are marked.
| Prop | Type | What it does |
|---|---|---|
labelrequired | string | What this JSON is: "Order response". Required: it is the name of the region. |
valuerequired | unknown | What is shown: an object, a list or a single value. |
className | string | Merged with the library classes; it does not replace them. |
defaultOpenDepth | number | How many levels start open. 1 by default. With `Infinity`, all of them. |
maxItems | number | How many entries of each object or list are drawn before saying "N more". 100 by default. |
sortKeys | boolean | Sorts the keys of the objects alphabetically. |
withoutCopy | boolean | Removes the copy button. |
It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.
CSS variables
These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.
| Variable | Default |
|---|---|
--amset-jsonviewer-font | var(--amset-text-sm) |
--amset-jsonviewer-indent | 1.25rem |
--amset-jsonviewer-radius | var(--amset-radius) |
--amset-jsonviewer-key | var(--amset-accent-text) |
--amset-jsonviewer-string | var(--amset-success) |
--amset-jsonviewer-number | var(--amset-warning) |
--amset-jsonviewer-keyword | var(--amset-secondary-text) |
How to change it
.amset-jsonviewer {
--amset-jsonviewer-font: ...;
--amset-jsonviewer-indent: ...;
}In the test bench
Every state and variant, to try one at a time, on its page at storybook.amsetui.com:
