Socket
Socket
Sign inDemoInstall

@zerodevx/svelte-json-view

Package Overview
Dependencies
21
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @zerodevx/svelte-json-view

View JSON data prettily


Version published
Weekly downloads
2.7K
decreased by-7.68%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

svelte-json-view

View JSON data prettily.

Display JSON data in a tree-like expandable view. Use as a Svelte component or in Vanilla JS.

Here's a demo.

Usage

Install the package:

$ npm i @zerodevx/svelte-json-view

Svelte

If you're using this in a Svelte app:

<script>
  import { JsonView } from '@zerodevx/svelte-json-view'

  const json = { foo: 'bar' }
</script>

<JsonView {json} />

Vanilla JS

For other applications with a bundler:

// Import the compiled code from `/dist`
import { JsonView } from '@zerodevx/svelte-json-view/dist'

const app = new JsonView({
  target: document.body   // node to render into
})

app.$set({
  json: { foo: 'bar' },   // object to prettify
  ...                     // any other props
})

Or load via CDN:

<head>
  ...
  <!-- Load `JsonView` from CDN -->
  <script defer src="https://cdn.jsdelivr.net/npm/@zerodevx/svelte-json-view@1"></script>
  <!-- Register the view -->
  <script type="module">
    const app = new JsonView({
      target: document.querySelector('#target'), // node to render into
      props: {
        json: { foo: 'bar' }, // object to prettify
        ...                   // any other props
      }
    })
  </script>
</head>
<body>
  <div id="target"></div>
</body>

Theming

In general, use CSS variables.

NameDefaultDescription
--jsonPaddingLeft1remAmount of left padding to apply at each depth
--jsonBorderLeft1px dottedStyle applied to left border for each depth
--jsonBracketColorcurrentcolorColor for brackets [, {, } and ]
--jsonBracketHoverBackground#e5e7ebHover background for brackets
--jsonSeparatorColorcurrentcolorColor for separators : and ,
--jsonKeyColorcurrentcolorColor for keys
--jsonValColor#9ca3afDefault color for values
--jsonValStringColor#059669Color for string values
--jsonValNumberColor#d97706Color for number values
--jsonValBooleanColor#2563ebColor for boolean values

It's recommended to wrap the component to apply your own font family (a monospaced font is recommended) and also to apply custom CSS var overrides.

<div class="wrap">
  <JsonView {json} />
</div>

<style>
  .wrap {
    font-family: monospace;
    font-size: 14px;
    --jsonBorderLeft: 2px dashed red;
    --jsonValColor: blue;
    ...
  }
</style>

Props

NameTypeDefaultDescription
jsonObjectundefinedUn-stringified object to display
depthNumberInfinityInitial expansion depth

Development

Library is packaged via SvelteKit. Standard Github contribution workflow applies.

Tests

End-to-end testing via Playwright. To run tests headlessly:

$ npm run test

Changelog

Please refer to the releases page.

License

ISC

Keywords

FAQs

Last updated on 18 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc