Svelte <Inspect {value} />

Usage & Conditional Rendering

Install svelte-inspect-value with your favorite package manager.

A common use case for a component like this is to only render it during development.
If you are using SvelteKit, you can conditionally render Inspect using the dev variable exported from '$app/environment'.
If you are not using SvelteKit or Vite, esm-env is a good alternative for checking conditional environment variables with different bundlers and runtimes.

example
<script>
  import {dev} from '$app/environment'
  import Inspect from 'svelte-inspect-value'

  let value = $state({
    id: undefined,
    firstName: 'Bob',
    lastName: 'Alice',
    email: 'bob@alice.lol',
    introduction: `The name is Alice.

    Bob Alice.`,
    birthDate: new Date(),
    website: new URL('https://alicebob.website/?ref=abcdefg#about'),
    age: -42,
    emailVerified: true,
    interests: ['radio', 'tv', 'internet', 'kayaks'],
  })
</script>


{#if dev} <!-- only render during development -->
  <Inspect {value} name="demo" />
{/if}

<!-- or use prop (some light weight initialization code will run) -->
<Inspect {value} renderIf={dev} />
Result:
:
obj 10 entries
id :
undefined
firstName :
str 'Bob' 3 chars
lastName :
str 'Alice' 5 chars
email :
str 'bob@alice.lol' 13 chars
age :
num -42
emailVerified :
bool true

Inspect.Values

The basic Inspect component is a "single value"-inspector, meaning it can only have one "root" node. If you have multiple values you want to inspect, you can pack the values into an object and array and pass it to the value-prop. Another solution is passing the values as props to Inspect.Values:

example
<script lang="ts">
  import Inspect from 'svelte-inspect-value'

  let number = $state(1)
  let isEven = $derived(number % 2 === 0)
  let doubled = $derived(number * 2)
</script>

<input type="number" bind:value={number}>

<Inspect.Values {number} {isEven} {doubled}  />
Result:
number :
num 1
isEven :
bool false
doubled :
num 2

Inspect.Values does not accept any configuration props since any value passed as a prop will simply be inspected. If you want to change the behavior of Inspect.Values you can use global options or define a pre-configured version of the component with Inspect.Values.withOptions or the configured-function:

example
<script>
 import Inspect, {configured} from 'svelte-inspect-value'
 import data from './data.js'

  const InspectVals = Inspect.Values.withOptions(() => ({ expandLevel: 0 }))

  // elementAttributes will be to applied to outermost Inspect div
  const DarkInspect = configured(() => ({ 
    theme: 'dark',
    elementAttributes: { style: 'max-width: 500px' } 
  }))

  // create another variation that will inherit from the previous one 
  const DarkBorderless = DarkInspect.withOptions(() => ({ borderless: true }))
</script>

<InspectVals msg="i have been configured" {data} />

<!-- Override initial expandLevel using ".Expand[1-10]"  -->
<DarkInspect.Expand0 msg="me too" {data} />

<!-- spread properties of data as individiual values -->
<DarkBorderless.Expand1 msg="i inherit options from DarkInspect" {...data} />

Inspect.Values and configured variants can all have expandLevel set by using "Expand" properties from 0 to 10 (default 1.)

Result:
msg :
str 'i have been configured' 22 chars
msg :
str 'me too' 6 chars
msg :
str 'i inherit options from DarkInspect' 34 chars
a :
num 1
b :
num 2
c :
num 3
:
obj 3 entries
a :
num 1
b :
num 2
c :
num 3

Chainable inline configuration

A final method to configuring Inspect.Values that will override global options and withOptions-variations is "chainable inline configuration":

example
...

Global Options

svelte-inspect-value exports a utility function to set a "global" config for every instance of the Inspect-component in or under the component where the function is called (it sets context). Alternatively, you can use the InspectOptionsProvider-component.

Passing a function returning a reactive object to the function will update the components if any property of the object is changed.
You can try this now if you change any options in the configurator at the bottom of your screen! (hover it)

+layout.svelte
...
Result:
str 'no long strings in t…' 44 chars

Options set with props, withOptions or configured will override any global options

Props

NameDescriptionDefault
valuerequired. value to inspect. can be any javascript valuen/a
namename of outer value. displayed as keyundefined
OptionsThe following props can also be set using setGlobalInspectOptions or InspectOptionsProvider
stringCollapseset a max display length for string values. 0 means full string will be displayed0
showLengthdisplay length of arrays or strings and number of nested entries in objects / maps etctrue
showTypesdisplay type labels before values e.g. "string" / "number." Mainly affects basic primitive typestrue
showPreviewdisplay preview of nested values of object, array, map, set etc.true
previewDepthhow deeply nested items should be previewed before simply showing types1
previewEntrieshow many nested items should be previewed3
showToolsdisplay row of utility-"tools" when hovering an entrytrue
noanimatedisable animations / transitionsfalse
embedMediaembed images and audio if a string value is a path or url that ends with an image or audio file extensionfalse
themeset color theme class
available built-in themes: 'inspect','drak','stereo','dark','light','cotton-candy'
'inspect'
expandAllinitially expand all nodes. can be a performance hitch with a lot of entriesfalse
expandLeveldefault level of initially expanded nested nodes1
expandPathsarray of paths (string) to initially expanded nodes[]
borderlessremove background color, border and paddingfalse
quotesquote type for string values. 'single', 'double' or 'none''single'
renderIffunction or value. render condition for Inspect. if value or return value of function is truthy, Inspect will render.true
customComponentscustom components for values.
object with type as keyname and tuple of component and optional prop modification function.
extended documentation here
{}
parseJsonif enabled, attempt to parse strings that start with '{' or '[' and display the parsed value if it was valid JSONfalse
storesif enabled, treat any object with a subscribe function as a store or observable and show the last value emitted when subscribed totrue
onCopycustom callback run when clicking copy tool-button.
if this option is set without a canCopy-function, the copy button will be shown for all values.
this overrides the default copy-button behavior.
undefined
canCopycustom predicate that determines if copy-button should be displayed for a valueundefined
onLogcustom callback run when clicking log tool-button.
this overrides the default log-button behavior.
undefined
global options