生态产品多个设计器现已发布候选版本

查看生态总览
Skip to content

JsonViewer ​

Data display component. This page is the API reference, generated from the source code. Usage guidance and live examples are on the Chinese page.

Anatomy ​

Parts in bold are required.

data-scope="json-viewer": root · tree · item · item-key · item-value · branch · branch-control · branch-trigger · branch-indicator · branch-text · branch-summary · branch-content · preview · text · token · empty · mark · toolbar · expand-trigger · wrap-trigger · copy-trigger · view-trigger · status

API reference ​

Descriptions of individual props, events and slots are written in Chinese and appear on the Chinese page.

Artifacts ​

LayerValue
Custom element<xh-json-viewer>
Vue componentsXhJsonViewerRoot
ComposableuseJsonViewer
State machinejsonViewerMachine
Skin@xihan-ui/styles/json-viewer.css

Props ​

PropTypeRequired
valueunknown
viewJsonViewerView
defaultViewJsonViewerView
sourcestring
wrapboolean
defaultWrapboolean
toolsboolean | readonly JsonViewerTool[]
variantControlVariant
expandedValuestring[]
defaultExpandedValuestring[]
defaultExpandedDepthnumber
maxStringLengthnumber
maxItemsnumber
sortKeysboolean
loopboolean
dirDirection
sizeSize
searchstring
translationsPartial<JsonViewerTranslations>
onExpandedValueChange(details: JsonViewerExpandedValueChangeDetails) => void
onViewChange(details: JsonViewerViewChangeDetails) => void
onWrapChange(details: JsonViewerWrapChangeDetails) => void
onCopyError(details: JsonViewerCopyErrorDetails) => void

Events ​

Custom elements put the payload in detail; Vue emits an event of the same name.

EventPayload
expanded-value-changeJsonViewerExpandedValueChangeDetails
view-changeJsonViewerViewChangeDetails
wrap-changeJsonViewerWrapChangeDetails
copy-errorJsonViewerCopyErrorDetails

Slots ​

Only slots with a payload are listed.

Vue componentSlotPayload
XhJsonViewerRootempty—
XhJsonViewerRoottoolbarJsonViewerToolbarSlotProps

React adapter props ​

Only the props each component declares itself: DOM attributes inherited from ComponentPropsWithRef are left out, and root props already listed in the Props table are not repeated. The Vue counterpart is the slot table above.

React componentPropTypeRequired
XhJsonViewerRootemptyReactNode
XhJsonViewerRoottoolbarSlotChildren<JsonViewerToolbarSlotProps>

State ​

Public state is written to data-state.

PartValues
branch'open' | 'closed'
branch-control'open' | 'closed'
branch-trigger'open' | 'closed'
branch-indicator'open' | 'closed'
branch-text'open' | 'closed'
branch-summary'open' | 'closed'
branch-content'open' | 'closed'
preview'open' | 'closed'
expand-trigger'open' | 'closed'
wrap-trigger'on' | 'off'
copy-trigger'idle' | 'copying' | 'copied'
status'idle' | 'copying' | 'copied'

The following names are internal to the state machine.

States: idle · copying · copied

Events: EXPANDED.SET · BRANCH.EXPAND · BRANCH.COLLAPSE · BRANCH.TOGGLE · NODE.FOCUS · VIEWER.BLUR · PRESS.START · PRESS.END · MATCH.SET · SEARCH.SYNC · VIEW.SET · WRAP.SET · COPY.TRIGGER · COPY.SUCCESS · COPY.ERROR · after.timeout · TOOL.PRESS.START · TOOL.PRESS.END

connect API ​

getXxxProps() returns the host attributes of the matching part.

MemberType
visibleNodesreadonly JsonViewerNode[]
expandedValuestring[]
focusedValuestring | null
isFocusWithinboolean
isExpanded(value: string) => boolean
previewText(node: JsonViewerNode) => string
branchSummary(node: JsonViewerNode) => string
valueText(node: JsonViewerNode) => string
setExpandedValue(next: string[]) => void
expand(value: string) => void
collapse(value: string) => void
toggle(value: string) => void
viewJsonViewerView
wrapboolean
toolsreadonly JsonViewerTool[]
copyStatusJsonViewerCopyStatus
copiedboolean
announcementstring
setView(view: JsonViewerView) => void
setWrap(wrap: boolean) => void
expandAll() => void
collapseAll() => void
allExpandedboolean
copy() => void
isEmptyboolean
emptyTextstring
textstring
textSegmentsreadonly JsonViewerTextSegment[]
searchMatchesreadonly string[]
activeMatchstring | null
nextMatch() => void
prevMatch() => void
setActiveMatch(value: string | null) => void
keySegments(node: JsonViewerNode) => readonly HighlightSegment[]
valueSegments(node: JsonViewerNode) => readonly HighlightSegment[]
getRootProps() => T['element']
getTreeProps() => T['element']
getTextProps() => T['element']
getTokenProps(props: JsonViewerTokenProps) => T['element']
getItemProps(props: JsonViewerNodeProps) => T['element']
getItemKeyProps(props: JsonViewerNodeProps) => T['element']
getItemValueProps(props: JsonViewerNodeProps) => T['element']
getBranchProps(props: JsonViewerNodeProps) => T['element']
getBranchControlProps(props: JsonViewerNodeProps) => T['element']
getBranchTriggerProps(props: JsonViewerNodeProps) => T['element']
getBranchIndicatorProps(props: JsonViewerNodeProps) => T['element']
getBranchTextProps(props: JsonViewerNodeProps) => T['element']
getBranchSummaryProps(props: JsonViewerNodeProps) => T['element']
getBranchContentProps(props: JsonViewerNodeProps) => T['element']
getPreviewProps(props: JsonViewerNodeProps) => T['element']
getEmptyProps() => T['element']
getMarkProps() => T['element']
getToolbarProps() => T['element']
getExpandTriggerProps() => T['button']
getWrapTriggerProps() => T['button']
getCopyTriggerProps() => T['button']
getViewTriggerProps() => T['button']
getStatusProps() => T['element']

Accessibility ​

Keyboard ​

Specification: W3C APG. Each row is a stable behavior id that the conformance tests cover; the conditions and effects are described on the Chinese page.

KeysBehavior
Tab / Shift+Tabjson-viewer.kbd.tab
ArrowDownjson-viewer.kbd.next
ArrowUpjson-viewer.kbd.prev
Homejson-viewer.kbd.first
Endjson-viewer.kbd.last
ArrowRightjson-viewer.kbd.expand
ArrowLeftjson-viewer.kbd.collapse
Enter / Spacejson-viewer.kbd.toggle
Enter / Spacejson-viewer.kbd.press
*json-viewer.kbd.expand-siblings
Enter / Spacejson-viewer.kbd.tool

ARIA ​

These attributes are produced by connect.

PartAttributeValue
treearia-labellabel.tree
treerole'tree'
itemaria-levelnode?.level
itemaria-posinsetnode?.posInSet
itemaria-setsizenode?.setSize
itemrole'treeitem'
brancharia-expanded'true' | 'false'
brancharia-levelnode?.level
brancharia-posinsetnode?.posInSet
brancharia-setsizenode?.setSize
branchrole'treeitem'
branch-triggeraria-hidden'true'
branch-indicatoraria-hidden'true'
branch-textaria-hidden'true'
branch-contentrole'group'
previewaria-hidden'true'
textaria-labellabel.text
textrole'region'
toolbararia-labellabel.toolbar
toolbarrole'group'
wrap-triggeraria-pressed'true' | 'false'
statusaria-atomic'true'
statusaria-live'polite'
statusrole'status'

Style reference ​

Skin ​

@xihan-ui/styles/json-viewer.css is written against [data-scope="json-viewer"][data-part="root"] part selectors; the published build uses the mount class .xh-scope-json-viewer in place of data-scope (same specificity) and sits in the xihan.components layer. Put overrides in xihan.overrides; the part selectors work as usual.

Data attributes ​

Produced by connect; valueless attributes are omitted while their condition does not hold.

PartAttributeValue
rootdata-sizeprops.size
rootdata-variantprops.variant
rootdata-viewcontext.get('view')
itemdata-circular'' (present only while the condition holds)
itemdata-current'' (present only while the condition holds)
itemdata-highlighted'' (present only while the condition holds)
itemdata-match'' (present only while the condition holds)
itemdata-truncated'' (present only while the condition holds)
itemdata-value-typenode?.type
item-keydata-circular'' (present only while the condition holds)
item-keydata-current'' (present only while the condition holds)
item-keydata-highlighted'' (present only while the condition holds)
item-keydata-match'' (present only while the condition holds)
item-keydata-truncated'' (present only while the condition holds)
item-keydata-value-typenode?.type
item-valuedata-circular'' (present only while the condition holds)
item-valuedata-current'' (present only while the condition holds)
item-valuedata-highlighted'' (present only while the condition holds)
item-valuedata-match'' (present only while the condition holds)
item-valuedata-truncated'' (present only while the condition holds)
item-valuedata-value-typenode?.type
branchdata-circular'' (present only while the condition holds)
branchdata-current'' (present only while the condition holds)
branchdata-highlighted'' (present only while the condition holds)
branchdata-match'' (present only while the condition holds)
branchdata-state'open' | 'closed'
branchdata-truncated'' (present only while the condition holds)
branchdata-value-typenode?.type
branch-controldata-circular'' (present only while the condition holds)
branch-controldata-current'' (present only while the condition holds)
branch-controldata-highlighted'' (present only while the condition holds)
branch-controldata-match'' (present only while the condition holds)
branch-controldata-pressed'' (present only while the condition holds)
branch-controldata-state'open' | 'closed'
branch-controldata-truncated'' (present only while the condition holds)
branch-controldata-value-typenode?.type
branch-triggerdata-circular'' (present only while the condition holds)
branch-triggerdata-current'' (present only while the condition holds)
branch-triggerdata-highlighted'' (present only while the condition holds)
branch-triggerdata-match'' (present only while the condition holds)
branch-triggerdata-state'open' | 'closed'
branch-triggerdata-truncated'' (present only while the condition holds)
branch-triggerdata-value-typenode?.type
branch-indicatordata-circular'' (present only while the condition holds)
branch-indicatordata-current'' (present only while the condition holds)
branch-indicatordata-highlighted'' (present only while the condition holds)
branch-indicatordata-match'' (present only while the condition holds)
branch-indicatordata-state'open' | 'closed'
branch-indicatordata-truncated'' (present only while the condition holds)
branch-indicatordata-value-typenode?.type
branch-textdata-circular'' (present only while the condition holds)
branch-textdata-current'' (present only while the condition holds)
branch-textdata-highlighted'' (present only while the condition holds)
branch-textdata-match'' (present only while the condition holds)
branch-textdata-state'open' | 'closed'
branch-textdata-truncated'' (present only while the condition holds)
branch-textdata-value-typenode?.type
branch-summarydata-circular'' (present only while the condition holds)
branch-summarydata-current'' (present only while the condition holds)
branch-summarydata-highlighted'' (present only while the condition holds)
branch-summarydata-match'' (present only while the condition holds)
branch-summarydata-state'open' | 'closed'
branch-summarydata-truncated'' (present only while the condition holds)
branch-summarydata-value-typenode?.type
branch-contentdata-circular'' (present only while the condition holds)
branch-contentdata-current'' (present only while the condition holds)
branch-contentdata-highlighted'' (present only while the condition holds)
branch-contentdata-match'' (present only while the condition holds)
branch-contentdata-state'open' | 'closed'
branch-contentdata-truncated'' (present only while the condition holds)
branch-contentdata-value-typenode?.type
previewdata-circular'' (present only while the condition holds)
previewdata-current'' (present only while the condition holds)
previewdata-highlighted'' (present only while the condition holds)
previewdata-match'' (present only while the condition holds)
previewdata-state'open' | 'closed'
previewdata-truncated'' (present only while the condition holds)
previewdata-value-typenode?.type
textdata-wrap'' (present only while the condition holds)
tokendata-kindprops.kind
toolbardata-viewcontext.get('view')
expand-triggerdata-state'open' | 'closed'
expand-triggerdata-xh-action-control''
expand-triggerdata-xh-action-display'always'
expand-triggerdata-xh-action-profile'icon'
expand-triggerdata-xh-action-size'md' | 'sm'
expand-triggerdata-xh-action-variant'ghost'
wrap-triggerdata-state'on' | 'off'
wrap-triggerdata-xh-action-control''
wrap-triggerdata-xh-action-display'always'
wrap-triggerdata-xh-action-profile'icon'
wrap-triggerdata-xh-action-size'md' | 'sm'
wrap-triggerdata-xh-action-variant'ghost'
copy-triggerdata-copied'' (present only while the condition holds)
copy-triggerdata-state'idle' | 'copying' | 'copied'
copy-triggerdata-xh-action-control''
copy-triggerdata-xh-action-display'always'
copy-triggerdata-xh-action-profile'icon'
copy-triggerdata-xh-action-size'md' | 'sm'
copy-triggerdata-xh-action-variant'ghost'
view-triggerdata-viewcontext.get('view')
view-triggerdata-xh-action-control''
view-triggerdata-xh-action-display'always'
view-triggerdata-xh-action-profile'icon'
view-triggerdata-xh-action-size'md' | 'sm'
view-triggerdata-xh-action-variant'ghost'
statusdata-state'idle' | 'copying' | 'copied'

CSS variables ​

The public override slots, generated from what the standalone skin actually consumes.

VariablePartCSS propertyStateDefault from
--xh-json-viewer-bgempty
root
text
tree
backgrounddefault
is([data-scope='json-viewer'][data-part='tree'], [data-scope='json-viewer'][data-part='text'], [data-scope='json-viewer'][data-part='empty'])
variant=subtle
--xh-bg-subtle
--xh-bg-surface
--xh-json-viewer-boolean-fgitem-value
token
colorkind=boolean
value-type=boolean
--xh-syntax-keyword
--xh-json-viewer-borderempty
text
tree
borderdefault--xh-border-default
--xh-json-viewer-empty-fgemptycolordefault--xh-fg-muted
--xh-json-viewer-empty-gapemptygapdefault--xh-space-2
--xh-json-viewer-empty-pxemptypadding-inlinedefault--xh-space-4
--xh-json-viewer-empty-pyemptypadding-blockdefault--xh-space-6
--xh-json-viewer-fgrootcolordefault--xh-fg-default
--xh-json-viewer-fontroot
text
font-familydefault--xh-font-family-mono
--xh-json-viewer-font-sizerootfont-sizedefault--xh-_json-viewer-font-size
--xh-json-viewer-icon-sizeroot--xh-icon-sizedefault
size=lg
size=sm
--xh-glyph-size-lg
--xh-glyph-size-md
--xh-glyph-size-sm
--xh-json-viewer-indentbranch-contentpadding-inline-startdefault--xh-_json-viewer-indent
--xh-json-viewer-indicator-fgbranch-triggercolordefault--xh-fg-subtle
--xh-json-viewer-indicator-sizebranch-trigger--xh-icon-size
inline-size
default--xh-control-indicator-size
--xh-json-viewer-key-fgbranch-text
item-key
token
colordefault
kind=key
--xh-fg-brand-strong
--xh-json-viewer-key-font-weightbranch-text
item-key
token
font-weightdefault
kind=key
--xh-font-weight-medium
--xh-json-viewer-mark-bgmarkbackgrounddefault--xh-bg-brand-subtle
--xh-json-viewer-mark-bg-currentbranch
branch-control
branch-text
item
item-key
item-value
mark
backgroundcurrent--xh-bg-brand
--xh-json-viewer-mark-fgmarkcolordefault--xh-fg-brand-strong
--xh-json-viewer-mark-fg-currentbranch
branch-control
branch-text
item
item-key
item-value
mark
colorcurrent--xh-fg-on-brand
--xh-json-viewer-mark-radiusmarkborder-radiusdefault--xh-shape-inset
--xh-json-viewer-max-htext
tree
max-block-sizedefault--xh-viewport-max-h
--xh-json-viewer-null-fgitem-value
token
colorkind=null
value-type=null
--xh-fg-subtle
--xh-json-viewer-number-fgitem-value
token
colorkind=number
value-type=number
--xh-syntax-number
--xh-json-viewer-preview-fgpreviewcolordefault--xh-fg-muted
--xh-json-viewer-preview-font-sizepreviewfont-sizedefault--xh-text-caption-size
--xh-json-viewer-punctuation-fgbranch-text
item-key
item-value
token
colordefault
kind=punctuation
value-type=array
value-type=object
--xh-fg-subtle
--xh-json-viewer-pxtext
tree
padding-inlinedefault--xh-space-2
--xh-json-viewer-pytext
tree
padding-blockdefault--xh-space-2
--xh-json-viewer-radiusempty
text
tree
border-radiusdefault--xh-shape-surface
--xh-json-viewer-row-bg-activebranch-controlbackgrounddisabled
is(:active, [data-pressed])
not([data-disabled])
pressed
--xh-bg-subtle-hover
--xh-json-viewer-row-bg-hoverbranch-control
item
backgroundhighlighted
is(:hover, [data-highlighted])
--xh-bg-subtle
--xh-json-viewer-row-gapbranch-control
item
gapdefault--xh-space-1
--xh-json-viewer-row-pxbranch-control
item
padding-inlinedefault--xh-space-1
--xh-json-viewer-row-pybranch-control
item
padding-blockdefault--xh-_json-viewer-row-py
--xh-json-viewer-row-radiusbranch-control
item
border-radiusdefault--xh-shape-inset
--xh-json-viewer-shadowempty
text
tree
box-shadowdefaultnone
--xh-json-viewer-string-fgitem-value
token
colorkind=string
value-type=string
--xh-syntax-string
--xh-json-viewer-text-fgtextcolordefault--xh-fg-default
--xh-json-viewer-tool-fgstatus
toolbar
colorwhere(:not([data-part='status'])--xh-fg-muted
--xh-json-viewer-tool-fg-copiedcopy-triggercolorcopied
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-fg-success
--xh-json-viewer-tool-icon-sizestatus
toolbar
--xh-icon-sizewhere(:not([data-part='status'])--xh-_action-profile-glyph-size
--xh-json-viewer-toolbar-gaptoolbargapdefault--xh-space-0_5
--xh-json-viewer-toolbar-mbtoolbarmargin-block-enddefault--xh-space-1

Released under The MIT License