|
| 1 | +# Vue 3 JSON Viewer |
| 2 | + |
| 3 | +A modern, sleek Vue 3 component for viewing, editing, and manipulating JSON data with two view modes: Tree Mode and Text Mode. |
| 4 | + |
| 5 | +## ✨ Features |
| 6 | + |
| 7 | +- 🌳 **Tree Mode**: Hierarchical display with expandable/collapsible nodes |
| 8 | +- 📝 **Text Mode**: Raw JSON text display with syntax highlighting |
| 9 | +- ✏️ **Inline Editing**: Edit values directly in the tree view |
| 10 | +- ➕ **Add/Remove Nodes**: Add new key-value pairs or remove existing ones |
| 11 | +- 🔍 **Search & Filter**: Search across all JSON keys and values |
| 12 | +- 📋 **Copy to Clipboard**: Easy copying of JSON content |
| 13 | +- 💾 **Download JSON**: Save current JSON to file |
| 14 | +- 🎨 **Dark/Light Theme**: Support for both themes |
| 15 | +- 📱 **Responsive Design**: Works on desktop and mobile devices |
| 16 | +- ⌨️ **Keyboard Navigation**: Full keyboard support |
| 17 | +- ♿ **Accessibility**: Screen reader support with proper ARIA labels |
| 18 | + |
| 19 | +## 🚀 Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install vue3-json-viewer |
| 23 | +``` |
| 24 | + |
| 25 | +Or with yarn: |
| 26 | + |
| 27 | +```bash |
| 28 | +yarn add vue3-json-viewer |
| 29 | +``` |
| 30 | + |
| 31 | +## 📖 Usage |
| 32 | + |
| 33 | +### Basic Usage |
| 34 | + |
| 35 | +```vue |
| 36 | +<template> |
| 37 | + <div> |
| 38 | + <JsonViewer |
| 39 | + v-model:data="jsonData" |
| 40 | + :editable="true" |
| 41 | + theme="light" |
| 42 | + default-mode="tree" |
| 43 | + @update:data="handleDataUpdate" |
| 44 | + /> |
| 45 | + </div> |
| 46 | +</template> |
| 47 | +
|
| 48 | +<script setup lang="ts"> |
| 49 | +import { ref } from 'vue' |
| 50 | +import JsonViewer from 'vue3-json-viewer' |
| 51 | +
|
| 52 | +const jsonData = ref({ |
| 53 | + name: "John Doe", |
| 54 | + age: 30, |
| 55 | + active: true, |
| 56 | + hobbies: ["reading", "coding"], |
| 57 | + address: { |
| 58 | + street: "123 Main St", |
| 59 | + city: "Anytown" |
| 60 | + } |
| 61 | +}) |
| 62 | +
|
| 63 | +const handleDataUpdate = (newData: any) => { |
| 64 | + console.log('Data updated:', newData) |
| 65 | +} |
| 66 | +</script> |
| 67 | +``` |
| 68 | + |
| 69 | +### Advanced Usage |
| 70 | + |
| 71 | +```vue |
| 72 | +<template> |
| 73 | + <JsonViewer |
| 74 | + v-model:data="complexData" |
| 75 | + :editable="true" |
| 76 | + :theme="currentTheme" |
| 77 | + default-mode="tree" |
| 78 | + :show-line-numbers="true" |
| 79 | + :max-depth="3" |
| 80 | + @update:data="handleDataUpdate" |
| 81 | + @node-click="handleNodeClick" |
| 82 | + @node-expand="handleNodeExpand" |
| 83 | + @node-collapse="handleNodeCollapse" |
| 84 | + @edit-start="handleEditStart" |
| 85 | + @edit-save="handleEditSave" |
| 86 | + @edit-cancel="handleEditCancel" |
| 87 | + /> |
| 88 | +</template> |
| 89 | +
|
| 90 | +<script setup lang="ts"> |
| 91 | +import { ref } from 'vue' |
| 92 | +import JsonViewer from 'vue3-json-viewer' |
| 93 | +
|
| 94 | +const complexData = ref({ |
| 95 | + // Your complex JSON data |
| 96 | +}) |
| 97 | +
|
| 98 | +const currentTheme = ref<'light' | 'dark'>('light') |
| 99 | +
|
| 100 | +// Event handlers |
| 101 | +const handleDataUpdate = (newData: any) => { |
| 102 | + console.log('Data updated:', newData) |
| 103 | +} |
| 104 | +
|
| 105 | +const handleNodeClick = (node: any) => { |
| 106 | + console.log('Node clicked:', node) |
| 107 | +} |
| 108 | +
|
| 109 | +const handleNodeExpand = (node: any) => { |
| 110 | + console.log('Node expanded:', node) |
| 111 | +} |
| 112 | +
|
| 113 | +const handleNodeCollapse = (node: any) => { |
| 114 | + console.log('Node collapsed:', node) |
| 115 | +} |
| 116 | +
|
| 117 | +const handleEditStart = () => { |
| 118 | + console.log('Edit mode started') |
| 119 | +} |
| 120 | +
|
| 121 | +const handleEditSave = (data: any) => { |
| 122 | + console.log('Changes saved:', data) |
| 123 | +} |
| 124 | +
|
| 125 | +const handleEditCancel = () => { |
| 126 | + console.log('Edit cancelled') |
| 127 | +} |
| 128 | +</script> |
| 129 | +``` |
| 130 | + |
| 131 | +## 📋 Props |
| 132 | + |
| 133 | +| Prop | Type | Default | Description | |
| 134 | +|------|------|---------|-------------| |
| 135 | +| `data` | `any` | `{}` | JSON data to display | |
| 136 | +| `editable` | `boolean` | `true` | Enable editing capabilities | |
| 137 | +| `theme` | `'light' \| 'dark'` | `'light'` | Theme preference | |
| 138 | +| `defaultMode` | `'tree' \| 'text'` | `'tree'` | Default view mode | |
| 139 | +| `showLineNumbers` | `boolean` | `false` | Show line numbers in text mode | |
| 140 | +| `maxDepth` | `number` | `3` | Maximum depth to expand by default | |
| 141 | + |
| 142 | +## 🎭 Events |
| 143 | + |
| 144 | +| Event | Payload | Description | |
| 145 | +|-------|---------|-------------| |
| 146 | +| `update:data` | `newData: any` | Data changed event | |
| 147 | +| `node-click` | `node: JsonNode` | Node clicked event | |
| 148 | +| `node-expand` | `node: JsonNode` | Node expanded event | |
| 149 | +| `node-collapse` | `node: JsonNode` | Node collapsed event | |
| 150 | +| `edit-start` | - | Edit mode started | |
| 151 | +| `edit-save` | `data: any` | Changes saved | |
| 152 | +| `edit-cancel` | - | Edit mode cancelled | |
| 153 | + |
| 154 | +## 🏗️ Data Structure |
| 155 | + |
| 156 | +```typescript |
| 157 | +interface JsonNode { |
| 158 | + key: string; |
| 159 | + value: any; |
| 160 | + type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null'; |
| 161 | + path: string[]; |
| 162 | + level: number; |
| 163 | + expanded?: boolean; |
| 164 | + children?: JsonNode[]; |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +## 🎨 Styling |
| 169 | + |
| 170 | +The component uses CSS custom properties for theming. You can customize the appearance by overriding these variables: |
| 171 | + |
| 172 | +```css |
| 173 | +.json-viewer { |
| 174 | + --bg-primary: #ffffff; |
| 175 | + --bg-secondary: #f8fafc; |
| 176 | + --bg-tertiary: #f1f5f9; |
| 177 | + --text-primary: #1e293b; |
| 178 | + --text-secondary: #64748b; |
| 179 | + --border-color: #e2e8f0; |
| 180 | + --accent-color: #3b82f6; |
| 181 | + --hover-color: #f1f5f9; |
| 182 | + --success-color: #10b981; |
| 183 | + --error-color: #ef4444; |
| 184 | + --warning-color: #f59e0b; |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## 🛠️ Development |
| 189 | + |
| 190 | +### Setup |
| 191 | + |
| 192 | +```bash |
| 193 | +# Clone the repository |
| 194 | +git clone https://github.com/jeevan-lal/vue3-json-viewer.git |
| 195 | + |
| 196 | +# Install dependencies |
| 197 | +npm install |
| 198 | + |
| 199 | +# Start development server |
| 200 | +npm run dev |
| 201 | +``` |
| 202 | + |
| 203 | +### Build |
| 204 | + |
| 205 | +```bash |
| 206 | +# Build for production |
| 207 | +npm run build |
| 208 | + |
| 209 | +# Preview production build |
| 210 | +npm run preview |
| 211 | +``` |
| 212 | + |
| 213 | +### Lint |
| 214 | + |
| 215 | +```bash |
| 216 | +# Run ESLint |
| 217 | +npm run lint |
| 218 | + |
| 219 | +# Type check |
| 220 | +npm run type-check |
| 221 | +``` |
| 222 | + |
| 223 | +## 🧪 Testing |
| 224 | + |
| 225 | +Run the test suite: |
| 226 | + |
| 227 | +```bash |
| 228 | +npm run test |
| 229 | +``` |
| 230 | + |
| 231 | +## 📱 Browser Support |
| 232 | + |
| 233 | +- Chrome ≥ 87 |
| 234 | +- Firefox ≥ 78 |
| 235 | +- Safari ≥ 14 |
| 236 | +- Edge ≥ 88 |
| 237 | + |
| 238 | +## 🤝 Contributing |
| 239 | + |
| 240 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 241 | + |
| 242 | +1. Fork the repository |
| 243 | +2. Create your feature branch (`git checkout -b feature/AmazingFeature`) |
| 244 | +3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) |
| 245 | +4. Push to the branch (`git push origin feature/AmazingFeature`) |
| 246 | +5. Open a Pull Request |
| 247 | + |
| 248 | +## 📄 License |
| 249 | + |
| 250 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 251 | + |
| 252 | +## 🙏 Acknowledgments |
| 253 | + |
| 254 | +- Icons provided by [Heroicons](https://heroicons.com/) |
| 255 | +- Font families: [Inter](https://rsms.me/inter/) and [Fira Code](https://github.com/tonsky/FiraCode) |
| 256 | +- Built with [Vue 3](https://vuejs.org/) and [Vite](https://vitejs.dev/) |
| 257 | + |
| 258 | +## 📞 Support |
| 259 | + |
| 260 | +If you have any questions or need help, please: |
| 261 | + |
| 262 | +1. Check the [documentation](#-usage) |
| 263 | +2. Search [existing issues](https://github.com/jeevan-lal/vue3-json-viewer/issues) |
| 264 | +3. Create a [new issue](https://github.com/jeevan-lal/vue3-json-viewer/issues/new) |
| 265 | + |
| 266 | +--- |
| 267 | + |
| 268 | +Made with ❤️ and Vue 3 |
0 commit comments