npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@kaifronsdal/svelte-json-viewer

v0.1.0

Published

A performant JSON viewer component for Svelte with collapsible blocks and string truncation

Readme

Svelte JSON Viewer

A performant JSON viewer component for Svelte 5 with collapsible blocks, string truncation, inline short containers, and theming support.

Features

  • 🎯 Collapsible Objects & Arrays: Click to expand/collapse nested structures
  • ✂️ String Truncation: Long strings are automatically truncated with expandable ellipsis
  • 📦 Inline Short Containers: Smart inline rendering for short objects and arrays
  • 🎨 Theming Support: Light and dark themes with customizable CSS variables
  • 🔍 Type-aware Rendering: Different colors for strings, numbers, booleans, null, etc.
  • 📊 Metadata Display: Shows object key counts and array lengths
  • 🎛️ Configurable: Customizable indentation, collapse behavior, and display options
  • Performance: Built with Svelte 5 reactivity and optimized for large JSON structures
  • 🎪 Hover Effects: Visual feedback on hover
  • 🗂️ Modular Architecture: Well-structured components for maintainability

Installation

npm install svelte-json-viewer

Basic Usage

<script>
  import { JsonViewer } from 'svelte-json-viewer';
  
  const data = {
    name: "John Doe",
    age: 30,
    hobbies: ["reading", "coding"],
    address: {
      city: "New York",
      zipCode: "10001"
    }
  };
</script>

<JsonViewer value={data} />

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | value | JsonValue | - | The JSON data to display | | theme | 'light' \| 'dark' | 'light' | Theme for the viewer | | collapsed | boolean \| number | false | Control collapse behavior. If boolean, applies to all levels. If number, collapses levels >= that number | | collapseStringsAfterLength | number | 50 | Truncate strings longer than this length | | indentWidth | number | 12 | Indentation width in pixels | | sortKeys | boolean | false | Sort object keys alphabetically | | displayDataTypes | boolean | false | Show data types next to values | | displayObjectSize | boolean | true | Show object/array size metadata | | displayArrayKey | boolean | false | Show array indices as keys | | inlineShortContainers | boolean \| number | false | Render short containers inline. If boolean true, uses 40 char limit. If number, uses that as the character limit |

Examples

Custom Theme and Collapse Behavior

<JsonViewer 
  value={data} 
  theme="dark"
  collapsed={2}
  collapseStringsAfterLength={30}
  sortKeys={true}
  displayDataTypes={true}
/>

Inline Short Containers

The inlineShortContainers feature renders short objects and arrays on a single line while preserving full expand/collapse functionality:

<script>
  const data = {
    coordinates: { lat: 40.7128, lng: -74.0060 },
    tags: ["javascript", "svelte", "json"],
    user: { name: "Alice", age: 25 },
    longArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] // won't be inlined
  };
</script>

<JsonViewer 
  value={data}
  inlineShortContainers={true}  // Uses 40 character limit
/>

<!-- Or with custom character limit -->
<JsonViewer 
  value={data}
  inlineShortContainers={60}  // Custom 60 character limit
/>

This will render short containers like:

  • {lat: 40.7128, lng: -74.0060} instead of multi-line format
  • ["javascript", "svelte", "json"] instead of each item on separate lines
  • Maintains syntax highlighting with proper colors for strings, numbers, etc.
  • Containers can still be expanded to full view when clicked

Minimal Configuration

<JsonViewer 
  value={data} 
  displayObjectSize={false}
  indentWidth={8}
/>

Theming

The component supports extensive theming through CSS custom properties:

.my-json-viewer {
  --json-viewer-bg: #ffffff;
  --json-viewer-string-color: #d73a49;
  --json-viewer-number-color: #005cc5;
  --json-viewer-boolean-color: #d73a49;
  --json-viewer-null-color: #6f42c1;
  --json-viewer-property-color: #032f62;
  --json-viewer-brace-color: #24292e;
  --json-viewer-bracket-color: #24292e;
  --json-viewer-comma-color: #24292e;
  --json-viewer-colon-color: #24292e;
  --json-viewer-meta-color: #6a737d;
  --json-viewer-ellipsis-color: #6a737d;
  --json-viewer-hover-bg: #f6f8fa;
  --json-viewer-border-color: #e1e4e8;
  --json-viewer-indent-guide-color: #e1e4e8;
  --json-viewer-expand-icon-color: #586069;
  --json-viewer-expand-icon-hover-color: #0366d6;
}

Development

# Install dependencies
npm install

# Start development server
npm run dev

# Build library
npm run build

# Run tests
npm run test

License

MIT