2.1.1 • Published 4 years ago

@power-elements/json-viewer v2.1.1

Weekly downloads
19
License
ISC
Repository
github
Last release
4 years ago

json-viewer

Custom Element that shows a JavaScript object's properties as syntax-highlighted JSON.

The element will respect prefers-color-scheme by default, but if you use the CSS Custom Properties listed below, you should customize both light and dark themes.

❤️ Proudly uses open-wc tools and recommendations.

Examples

import '/path/to/json-viewer.js';
const viewer = document.createElement('json-viewer');
      viewer.allowlist = ['foo', 'bar'];
      viewer.object = {
        foo: 'foo',
        bar: 'bar',
        baz: 'baz',
      };
<script type="module" src="/path/to/json-viewer.js"></script>

<json-viewer allowlist="meenie,minie">
  <script type="application/json">
    {
      "eenie": 1,
      "meenie": true,
      "minie": [{ "mo": "catch a tiger by the toe" }]
    }
  </script>
</json-viewer>

Properties

PropertyAttributeTypeDescription
allowlistallowliststring[]User-defined allowlist of top-level keys for the object.Optional for plain objects,Required when setting object to a non-serializable object (e.g. an HTMLElement)Property is an Array of stringsAttribute is a comma-separated string
errorErrorJSON.parse error
objectobjectstring\|objectJavaScript Object to displaySetting this property will override <script type="application/json"> children

Events

EventTypeDescription
json-parse-errorCustomEvent<any>when JSON parse fails

Slots

NameDescription
JSON scripts or JSON strings appended as text nodes will be parsed and displayed

CSS Shadow Parts

PartDescription
booleanboolean property values
codethe wrapping <code> element
keyproperty keys
nullnull property values
numbernumber property values
stringstring property values

CSS Custom Properties

PropertyDescription
--json-viewer-backgroundColor for generic text. Light #212121, Dark white
--json-viewer-boolean-colorColor for booleans. Light #f76707, Dark #22b8cf
--json-viewer-colorColor for generic text. Light white, Dark #212121
--json-viewer-key-colorColor for keys. Light #f76707, Dark #ff922b
--json-viewer-null-colorColor for nulls. Light #e03131, Dark #ff6b6b
--json-viewer-number-colorColor for numbers. Light #0ca678, Dark #51cf66
--json-viewer-string-colorColor for strings. Light #0c8599, Dark #22b8cf