Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

json-viewer

An element that shows a JavaScript value as a tree you can expand, collapse, search and copy from. Its colors and spacing come from --json-viewer-* custom properties, which you point at Vanilla Breeze tokens with a few lines of CSS.

At a glance

Package@profpowell/json-viewer 0.1.0
Elements<json-viewer>
Renders inShadow DOM
Sourcegithub.com/ProfPowell/json-viewer
Documentationprofpowell.github.io/json-viewer
LicenceMIT
In Vanilla BreezeNot bundled. Load it yourself.

Install

From a CDN. There is no stylesheet to load.

Or from npm:

Demo

An order record shown as a tree, two levels open, with item counts. The viewer's colors, radius, font and spacing are mapped to Vanilla Breeze tokens in the page's CSS.

Usage

Write the JSON inside the element. The viewer reads its own text content when it has no data or src.

Set the data property from JavaScript. This accepts any value, including ones JSON cannot hold: Map, Set, Date, BigInt and objects that refer to themselves. Set it after the element is defined, as here.

Load JSON from a URL with src. The viewer fetches it and shows the result, or an error object if the fetch fails.

Reference

Attributes

AttributeValuesDefaultPurpose
dataJSON stringNoneThe value to show. If the string is not valid JSON it is shown as a plain string. Use the data property for values JSON cannot hold.
srcURLNoneFetched as JSON when the element is added to the page and whenever the attribute changes.
expandedWhole number, true, false1How many levels start open. true or an empty value opens every level; false opens none.
modetree, rawtreeraw shows indented JSON text instead of the tree. The toolbar's Raw button switches this attribute.
show-typesBooleanOffAdds a type label, such as string or array, after each value.
show-sizesBooleanOffShows the number of items in each array and object.
show-indicesBooleanOffShows the position number before each array item.
indentWhole number, in ch units2Indentation of each tree level, and the spacing of raw mode and copied JSON.

Methods and properties

A path names one node: users[0].name for objects and arrays, cache@0 for the first entry of a Map or Set, and $ for the root. In a pattern, * matches one segment and ** matches any number.

MemberPurpose
dataThe value to show. Setting it removes the data attribute and renders again.
expand(matcher), collapse(matcher)Open or close the branches that match. The matcher is a path, a pattern with * or **, a RegExp, or a function that receives a path and returns true or false.
expandAll(), collapseAll()Open or close every branch.
search(query, options)Finds the query in keys and values, opens and scrolls to the first match, and returns an object with next(), prev(), current and count. Options: regex and caseSensitive, both off by default.
entryAt(path)Returns { key, value } for a path. For a Map entry the key is the original key object, not a string.
copy(text)Writes the text to the clipboard. With no argument it copies the whole value as JSON, writing each BigInt as a string ending in n.

Events

Both events bubble and cross the shadow boundary, so you can listen on any ancestor.

EventDetailFired when
json-viewer:keyclick{ path, key, value }A key is activated. The viewer also shows the key's path in a bar under the toolbar.
json-viewer:valueclick{ path, value, type }A value is clicked.

CSS custom properties

PropertyBuilt-in defaultDrives
--json-viewer-surfacelight-dark(#ffffff, #1c1917)Background of the viewer, toolbar and inputs; text on pressed buttons.
--json-viewer-textlight-dark(#1c1917, #f4f4f5)Main text.
--json-viewer-text-mutedlight-dark(#71717a, #a1a1aa)Disclosure arrows, sizes, type labels, indices, the match count.
--json-viewer-borderlight-dark(#e7e5e4, #3f3f46)Outer border, dividers, button borders, the guide line beside open branches.
--json-viewer-accentlight-dark(#0f766e, #2dd4bf)Hover and focus borders, pressed buttons, search match highlight.
--json-viewer-radius0.5remCorner radius. Buttons use 60% of it.
--json-viewer-font-monoui-monospace, then a list of monospace fontsFont of the whole viewer.
--json-viewer-space-1 to --json-viewer-space-40.25rem, 0.5rem, 0.75rem, 1remGaps and padding.
--json-viewer-keylight-dark(#7e22ce, #c084fc)Object keys.
--json-viewer-stringlight-dark(#15803d, #4ade80)Strings, dates and regular expressions.
--json-viewer-numberlight-dark(#1d4ed8, #60a5fa)Numbers and BigInt values.
--json-viewer-booleanlight-dark(#a16207, #fbbf24)Booleans and symbols.
--json-viewer-nulllight-dark(#71717a, #a1a1aa)null.
--json-viewer-punctlight-dark(#52525b, #a1a1aa)Brackets, braces and colons.
--max-heightnoneMaximum height of the tree area. Beyond it the tree scrolls and the toolbar stays in place.

The shadow tree also exposes five parts for ::part() styling: toolbar, search, path-bar, path and body.

The json-viewer documentation has the full detail.

With Vanilla Breeze

Each value in the viewer is resolved in three steps:

  1. The public hook, for example --json-viewer-surface.
  2. A fallback named --vb-*, for example --vb-surface.
  3. A built-in value, written with light-dark() for colors.

The package README calls step 2 the Vanilla Breeze fallback. In version 0.1.0 the literal names it reads are --vb-surface, --vb-text, --vb-text-muted, --vb-border, --vb-accent, --vb-radius, --vb-font-mono, --vb-space-1 to --vb-space-4, and --vb-json-key, --vb-json-string, --vb-json-number, --vb-json-boolean, --vb-json-null, --vb-json-punct.

Vanilla Breeze does not define any of those names. Its tokens are --color-*, --size-*, --font-* and --radius-*. So on a Vanilla Breeze page step 2 never matches, and the viewer shows its built-in colors unless you set step 1 yourself. This table gives the mapping:

Set this hookTo this VB tokenDrives
--json-viewer-surface--color-surfaceBackground.
--json-viewer-text--color-textMain text.
--json-viewer-text-muted--color-text-mutedSecondary text.
--json-viewer-border--color-borderBorders and dividers.
--json-viewer-accent--color-interactiveFocus, pressed and match colors.
--json-viewer-radius--radius-mCorners.
--json-viewer-font-mono--font-monoFont.
--json-viewer-space-1 to -4--size-2xs, --size-xs, --size-s, --size-mGaps and padding. These four tokens have the same default sizes as the viewer's built-in values.

Put the mapping in page CSS. A rule on json-viewer covers every instance; a rule on an id or other selector covers one.

The color-scheme: inherit line matters when the page forces a mode with data-mode. The viewer sets color-scheme: light dark on itself, which makes every light-dark() value inside it follow the operating system preference. That includes its built-in value colors (keys, strings, numbers) and any Vanilla Breeze token whose value is a light-dark() pair. Inheriting the scheme from the page keeps them in step with the mode the page is showing.

Accessibility

↑