|
1 | 1 | # json-tree-view-vue3 |
2 | 2 |
|
3 | | -[](https://www.npmjs.com/package/json-tree-view-vue3) [](https://github.com/ellerbrock/typescript-badges/)  [](https://opensource.org/licenses/MIT) [](https://github.com/prettier/prettier) |
| 3 | +[](https://www.npmjs.com/package/json-tree-view-vue3) |
| 4 | +[](https://github.com/seijikohara/json-tree-view-vue3/actions/workflows/npm-ci.yml) |
| 5 | +[](https://github.com/seijikohara/json-tree-view-vue3/actions/workflows/playwright.yml) |
| 6 | +[](https://www.typescriptlang.org/) |
| 7 | +[](https://bundlephobia.com/package/json-tree-view-vue3) |
| 8 | +[](https://opensource.org/licenses/MIT) |
4 | 9 |
|
5 | | -A Vue3 component that displays JSON in a collapsible tree. |
6 | | -Inspired by [vue-json-component](https://www.npmjs.com/package/vue-json-component) and [vue-json-tree-view](https://www.npmjs.com/package/vue-json-tree-view) to work with Vue3 and TypeScript. |
| 10 | +A Vue 3 component for rendering JSON data as an interactive, collapsible tree structure. |
7 | 11 |
|
8 | | -## Example |
| 12 | +Inspired by [vue-json-component](https://www.npmjs.com/package/vue-json-component) and [vue-json-tree-view](https://www.npmjs.com/package/vue-json-tree-view), this library provides native Vue 3 and TypeScript support. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install json-tree-view-vue3 |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
9 | 21 |
|
10 | 22 | ```vue |
11 | 23 | <script setup lang="ts"> |
12 | 24 | import { JsonTreeView } from "json-tree-view-vue3"; |
13 | | -import 'json-tree-view-vue3/dist/style.css' |
| 25 | +import "json-tree-view-vue3/dist/style.css"; |
14 | 26 |
|
15 | | -const json = `{"string":"text","number":123,"boolean":true,"null":null,"array":["A","B","C"],"object":{"prop1":"value1","nestedObject":{"prop2":"value2"}}}` |
| 27 | +const json = `{"string":"text","number":123,"boolean":true,"null":null,"array":["A","B","C"],"object":{"prop1":"value1","nestedObject":{"prop2":"value2"}}}`; |
16 | 28 | </script> |
17 | 29 |
|
18 | 30 | <template> |
19 | 31 | <JsonTreeView :json="json" :maxDepth="3" /> |
20 | 32 | </template> |
21 | 33 | ``` |
22 | 34 |
|
23 | | -<img width="296" alt="image" src="https://github.com/seijikohara/json-tree-view-vue3/assets/9543980/0d4d74bc-367d-4fd1-a47a-edfb857a6478"> |
| 35 | +### Example Output |
| 36 | + |
| 37 | +<img width="296" alt="JSON tree view example" src="https://github.com/seijikohara/json-tree-view-vue3/assets/9543980/0d4d74bc-367d-4fd1-a47a-edfb857a6478"> |
| 38 | + |
| 39 | +## API Reference |
| 40 | + |
| 41 | +### Props |
| 42 | + |
| 43 | +| Property | Type | Required | Default | Description | |
| 44 | +|---------------|----------|----------|-----------|------------------------------------------------------------------| |
| 45 | +| `json` | `string` | Yes | - | A valid JSON string to be rendered as a tree structure | |
| 46 | +| `rootKey` | `string` | No | `"/"` | The label displayed for the root node of the tree | |
| 47 | +| `maxDepth` | `number` | No | `1` | The initial depth level to which the tree will be expanded | |
| 48 | +| `colorScheme` | `string` | No | `"light"` | Visual theme of the component. Accepts `"light"` or `"dark"` | |
| 49 | + |
| 50 | +### Events |
| 51 | + |
| 52 | +#### `selected` |
| 53 | + |
| 54 | +Emitted when a user selects a value in the tree. |
| 55 | + |
| 56 | +**Payload Type:** |
| 57 | +```typescript |
| 58 | +{ |
| 59 | + key: string; // The key of the selected node |
| 60 | + value: PrimitiveTypes; // The value of the selected node (string, number, boolean, or null) |
| 61 | + path: string; // The full path to the selected node |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Styling and Customization |
| 66 | + |
| 67 | +The component uses CSS custom properties (variables) for theming, allowing you to customize colors and dimensions to match your application's design system. |
| 68 | + |
| 69 | +### Available CSS Variables |
| 70 | + |
| 71 | +```css |
| 72 | +/* Color palette */ |
| 73 | +--jtv-key-color: oklch(0.55 0.15 240); /* Object/Array key color */ |
| 74 | +--jtv-valueKey-color: oklch(0.25 0.05 210); /* Primitive value key color */ |
| 75 | +--jtv-string-color: oklch(0.6 0.12 230); /* String value color */ |
| 76 | +--jtv-number-color: oklch(0.65 0.1 180); /* Number value color */ |
| 77 | +--jtv-boolean-color: oklch(0.55 0.15 40); /* Boolean value color */ |
| 78 | +--jtv-null-color: oklch(0.55 0.12 280); /* Null value color */ |
| 79 | + |
| 80 | +/* UI colors */ |
| 81 | +--jtv-arrow-color: oklch(0.3 0 0); /* Expand/collapse arrow color */ |
| 82 | +--jtv-hover-color: oklch(0 0 0 / 0.1); /* Hover background color */ |
| 83 | + |
| 84 | +/* Dimensions */ |
| 85 | +--jtv-arrow-size: 6px; /* Size of the expand/collapse arrow */ |
| 86 | +--jtv-spacing-unit: 4px; /* Base spacing unit */ |
| 87 | +``` |
| 88 | + |
| 89 | +### Custom Styling Example |
| 90 | + |
| 91 | +You can override these variables to customize the appearance: |
| 92 | + |
| 93 | +```vue |
| 94 | +<style> |
| 95 | +.json-tree-view { |
| 96 | + /* Custom color scheme */ |
| 97 | + --jtv-key-color: #2563eb; |
| 98 | + --jtv-valueKey-color: #1e293b; |
| 99 | + --jtv-string-color: #059669; |
| 100 | + --jtv-number-color: #dc2626; |
| 101 | + --jtv-boolean-color: #7c3aed; |
| 102 | + --jtv-null-color: #64748b; |
| 103 | +
|
| 104 | + /* Custom dimensions */ |
| 105 | + --jtv-arrow-size: 8px; |
| 106 | + --jtv-spacing-unit: 6px; |
| 107 | +} |
| 108 | +</style> |
| 109 | +
|
| 110 | +<template> |
| 111 | + <div class="json-tree-view"> |
| 112 | + <JsonTreeView :json="json" /> |
| 113 | + </div> |
| 114 | +</template> |
| 115 | +``` |
24 | 116 |
|
25 | | -## Props |
| 117 | +### Dark Mode |
26 | 118 |
|
27 | | -| Props | Required | Param Type | Default value | Description | |
28 | | -|-------------|----------|------------|---------------|-------------------------------------------------------| |
29 | | -| json | true | string | | JSON string to display the tree | |
30 | | -| rootKey | false | string | "/" | Top root-level name | |
31 | | -| maxDepth | false | number | 1 | The depth of the tree that will be open when rendered | |
32 | | -| colorScheme | false | string | "light" | "light" or "dark" can be used. | |
| 119 | +The component includes built-in dark mode support through the `colorScheme` prop. Alternatively, you can customize the dark theme by overriding the CSS variables within your own dark mode class. |
33 | 120 |
|
34 | | -## Events |
| 121 | +## License |
35 | 122 |
|
36 | | -- **selected**(event: `{key: string, value: PrimitiveTypes, path: string}`] |
| 123 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments