File size: 7,720 Bytes
b5ea024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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](https://zerodevx.github.io/json-pretty-print/H4sIAAAAAAAAA7WaXXOkNhaG7_MrtK692FQaAgIE8tU4tmfjxJ7MepykUklqS4Do1g4NHT5sd1L73_foC2g3bXs83krF0wLpICHpOe854tcvEPoL_kfo6N8iPzpGR8SLKWFBESakIJnvJYkfZn4YHS10NVHl_B4qeqa87E27mMShz1InS7PQCUmWOkkUJg4hHityFjCSpoON9iTrxC2Hdl3Tc3M1ZSWrMnnx6O94QXDiJtS22Iis6xt1b9V1m-Ovv96ULOOrusxd0X0d4PsA27psKesFoSnyLT-ty7qRbdOmvqtsvYqtlcHLuqkY-lDWbLi15DBK1aLga1Zyez2r1xtWbeWN9z9c31ycntg7UE2U8noprbXS2JtN3XQiYy60Gsaxqiv10K989A_q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Ift89lBKa1NNQHsQpTlkdLQBiwrCh7wmEPpMJnEBniqZO3wioK1Jcg0ZBiVZx28IND-1cYCspJmpOmaMBwW8k6zY8QZa4Q6nfhrQoPMHXu50eDjPm6pbVWN3lOOTBwob9GvB_mHyTCmpDe8HjCs4a3-gHjWcH1q7LrrRD5RxiIS-DgdsvSFq2HWsrRgSMS76F7QaPxNR8Yd6oA4j1ERq_zPrPc3nJ5NBPO4nYgf2sgd-wj_2yXH0SL7YiWOZE_Y8GzlPPUUkz1rCgNpDnX2N_wjmJxmS_eSx9Nljyayz8YJe06_hJ74XDbyZCp3DYqy7p1O9c47iWiyXLegtVj3nQHDOUZyuOIhaBqDqW_5sX7ELt4m7iF7kLr74_X8qHdwptSYAAA/).

## Usage

Install the package:

```
$ npm i @zerodevx/svelte-json-view
```

### Svelte

If you're using this in a Svelte app:

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

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

<JsonView {json} />
```

### Vanilla JS

For other applications with a bundler:

```js
// 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:

```html
<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.

| Name                         | Default      | Description                                   |
| ---------------------------- | ------------ | --------------------------------------------- |
| --jsonPaddingLeft            | 1rem         | Amount of left padding to apply at each depth |
| --jsonBorderLeft             | 1px dotted   | Style applied to left border for each depth   |
| --jsonBracketColor           | currentcolor | Color for brackets `[`, `{`, `}` and `]`      |
| --jsonBracketHoverBackground | #e5e7eb      | Hover background for brackets                 |
| --jsonSeparatorColor         | currentcolor | Color for separators `:` and `,`              |
| --jsonKeyColor               | currentcolor | Color for keys                                |
| --jsonValColor               | #9ca3af      | Default color for values                      |
| --jsonValStringColor         | #059669      | Color for `string` values                     |
| --jsonValNumberColor         | #d97706      | Color for `number` values                     |
| --jsonValBooleanColor        | #2563eb      | Color 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.

<!-- prettier-ignore -->
```html
<div class="wrap">
  <JsonView {json} />
</div>

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

## Props

| Name  | Type   | Default   | Description                      |
| ----- | ------ | --------- | -------------------------------- |
| json  | Object | undefined | Un-stringified object to display |
| depth | Number | Infinity  | Initial expansion depth          |

## Development

Library is packaged via [SvelteKit](https://kit.svelte.dev/docs/packaging). Standard Github
[contribution workflow](https://docs.github.com/en/get-started/quickstart/contributing-to-projects)
applies.

### Tests

End-to-end testing via [Playwright](https://github.com/microsoft/playwright). To run tests
headlessly:

```
$ npm run test
```

## Changelog

Please refer to the [releases](https://github.com/zerodevx/svelte-json-view/releases) page.

## License

ISC