# <json-viewer> Element
[įŽäŊ䏿](./README.md) | English
> đ A lightweight, modern Web Component for JSON visualization and interaction.
## Features
- đ **Web Component**: Native, framework-agnostic
- đ¨ **Theme**: Light & dark mode
- đĻ **Boxed**: Optional border and padding
- đ **Copyable**: One-click copy JSON
- đ **Sort**: Key sorting support
- đ **Expand Depth**: Control initial expand level
- đ§Š **Custom Copy Button**: Slot for custom copy button
- đ§Ŧ **Type Highlight**: Colorful type highlighting
- đ ī¸ **Custom Events**: Listen for copy/toggle events
## Usage
### Install
```bash
npm install json-viewer-element
```
### Import
#### As a module
```js
import 'json-viewer-element'
```
#### UMD (CDN)
```html
```
### Basic Example
Set value by script:
```html
```
Set value by attribute:
```html
```
Use in Vue framework:
Vue 2/3 Options API:
```vue
```
Vue 3 composition API:
```vue
```
> [!TIP]
>
> [Skipping Component Resolution](https://vuejs.org/guide/extras/web-components.html#skipping-component-resolution)
>
> To let Vue know that certain elements should be treated as custom elements and skip component resolution, we can specify the [`compilerOptions.isCustomElement` option](https://vuejs.org/api/application.html#app-config-compileroptions).
```js
// vite.config.js
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
export default {
plugins: [
vue({
template: {
compilerOptions: {
// treat all tags with a dash as custom elements
isCustomElement: tag => tag.includes('-')
}
}
}),
vueJsx({
// treat all tags with a dash as custom elements
isCustomElement: tag => tag.includes('-')
}),
]
}
```
If you're using ESLint with Vue, you may need to configure it to ignore the custom element:
```js
// eslint.config.js
export default {
rules: {
'vue/component-name-in-template-casing': [
'warn',
'PascalCase',
{
registeredComponentsOnly: false,
ignores: ['/^icon-/', 'json-viewer'],
},
],
},
}
```
## Props
> [!TIP]
> When using with frameworks like Vue, you should pass value and copyable props as strings.
| Prop | Type | Default | Description |
| :----------- | :----------------------------------------- | :------ | :---------------------------------------------------------- |
| value | object / array / string / number / boolean | null | JSON data |
| expand-depth | number | 1 | Initial expand depth |
| copyable | boolean / CopyableOptions | false | Enable copy button or custom copy button config (see below) |
| sort | boolean | false | Whether to sort object keys |
| boxed | boolean | false | Whether to show border and padding |
| theme | 'light' / 'dark' | 'light' | Theme |
| parse | boolean | true | Whether to parse string value as JSON |
### CopyableOptions
| Prop | Type | Default | Description |
| :--------- | :--------------- | :------ | :------------------------------- |
| copyText | string | Copy | Text shown on the copy button |
| copiedText | string | Copied | Text shown after successful copy |
| timeout | number | 2000 | How long to show copiedText (ms) |
| align | 'left' / 'right' | right | Copy button alignment |
## Events
| Event | Description |
| :----------- | :----------------------- |
| copy-success | Fired after copy success |
| copy-error | Fired after copy failure |
| toggle | Node expand/collapse |
## Slots
Custom copy button:
```html
```
## License
[MIT](https://opensource.org/licenses/MIT)
Copyright (c) 2025-present [Lruihao](https://github.com/Lruihao)