json-viewer-custom-element

Demo of JSON Viewer custom element

by Anatolii Chakkaev

HTML

<script type="module" src="https://unpkg.com/@_1602/[email protected]/dist/json-viewer.es.js"></script>
<span class="theme-toggle"></span>
<json-viewer></json-viewer>
<json-viewer value="[object Object]"></json-viewer>
<json-viewer value='[]'></json-viewer>
<json-viewer value='"hello world"'></json-viewer>
<json-viewer value='1602'></json-viewer>
<json-viewer value='{}'></json-viewer>
<json-viewer value='null'></json-viewer>
<json-viewer value='[true, false]'></json-viewer>

<json-viewer value='
        { "demo":
            { "types":
                { "boolean": [true, false]
                , "number": [0, 1, 3.14]
                , "string": ["hello world", "👻"]
                , "null": null
                , "array": [1, 2]
                , "object": { "foo": "bar" }
                }
            }
        }'>
</json-viewer>

SCSS

.light-theme {
    
  json-viewer {
    --jv-color: rgb(31, 31, 31);
    --jv-expand-bullet-color: black;
    --jv-expand-bullet-width: 14px;
    --jv-expand-bullet-height: 14px;
    --jv-key-color: rgb(142, 0, 75);
    --jv-number-value-color: rgb(8, 66, 160);
    --jv-bool-value-color: rgb(8, 66, 160);
    --jv-null-value-color: rgba(31, 31, 31, 0.38);
    --jv-string-value-color: rgb(220, 54, 46);
    --jv-focused-node-background: #eee;
    --jv-hovered-node-background: #eee;
  }
  
  .theme-toggle {
    mask-image: var(--moon);
    background-color: black;
  }

  body { background: #fff; color: black;  }
}

json-viewer {
  --background-color: transparent;
}


body {
  padding: 20px;
  background: #222;
  position: relative;
  transition: background 0.2s ease;
  color: white;
}

.theme-toggle {
  mask-image: var(--sun);
  background-color: white;
  width: 24px;
  height: 24px;
  display: block;
  position: absolute;
  top: 0;
  right: 0;
}

:root {
  --moon: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-moon"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>');
  --sun: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-sun"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>');
}

JavaScript

const tt = document.querySelector('.theme-toggle');
const html = document.querySelector('html');

tt.addEventListener('click', () => {
  if (html.className === 'light-theme') {
     html.className = '';
  } else {
     html.className = 'light-theme';
  }
});

const urls = ['https://json-schema.org/draft/2020-12/schema', 'https://protocol.automationcloud.net/schema.json', 'https://microsoftedge.github.io/Demos/json-dummy-data/512KB-min.json', 'https://microsoftedge.github.io/Demos/json-dummy-data/5MB-min.json'];

(async () => {
  for (let url of urls) {
    await loadUrl(url);
  }
})();

async function loadUrl(url) {
  const data = await (await fetch(url)).json();
  const jv = document.createElement('json-viewer');
  jv.setAttribute('value', JSON.stringify(data));
  document.body.appendChild(jv);
}