仕組み
The value is flattened into a list of nodes once, and only the open ones render, as 28px monospace rows. Each level indents 18px; indent guides are one repeating gradient per row, not one element per level. Strings, numbers, booleans and null take chart colours mixed toward the text colour, so they stay readable in both themes. A folded branch shows a small badge with its size. Search marks hits with a warm highlight and a dot in the gutter and opens every branch that holds one, without overriding branches the user toggled. Hovering a row fades in a “Copy path” chip that writes a JavaScript-style path, and turns into a check for 1.4 seconds.
向いている場面
- API consoles, webhook logs and debug panels where people inspect a payload.
- Settings or records stored as JSON that people need to read, not edit.
向かない場面
- Editing JSON: use a code editor with validation.
- Very large documents (tens of thousands of nodes): page or virtualise them.
使い方
import { JsonViewer } from './JsonViewer.jsx'
export function ResponsePanel({ body, query }) {
return (
<JsonViewer
data={body}
rootLabel="response"
label="Response body"
search={query}
defaultExpandDepth={2}
onCopyPath={(path) => console.info('copied', path)}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
data | unknown | — | Any JSON-compatible value. |
rootLabel | string | '$' | Name of the root, and the start of every copied path (response.files[0].title). |
defaultExpandDepth | number | 1 | Levels open at first. The user's own toggles win afterwards. |
search | string | '' | Case-insensitive text to mark in keys and values; branches holding a match open, and a count appears above the tree. |
guides | boolean | true | A hairline per indent level. |
onCopyPath | (path: string, value) => void | — | Called when a path is copied; the path is also written to the clipboard. |
label / labels | string / { copy, copied, keys(n), items(n), matches(n), announceCopied(path) } | 'JSON' | Accessible name of the tree and every built-in string. |
アクセシビリティ
- The tree uses role tree and treeitem with aria-level, aria-setsize, aria-posinset and aria-expanded; one row is in the Tab order.
- Up and Down move between rows, Right opens a branch or enters it, Left closes it or goes to the parent, Home and End jump, Enter or Space toggles.
- C copies the focused row's path and a polite live region announces it; the mouse chip is hidden from assistive technology to avoid a duplicate control.
- The match count is a polite live region. Types are told apart by quotes and literals as well as colour.
- Reduced motion removes the chevron turn and the chip slide.
モーション
Chevron: 220ms turn on the reveal curve. Copy chip: 160ms fade with a 220ms spring slide.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
JsonViewer.jsxjson-viewer.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





