Skip to content
Search
Search the documentation
Live Examples
GitHub
2 min read

Live Examples

Real interactive components rendered inside the docs.

A real, running component

The button below is a real Svelte component running as part of this app. It is not a screenshot, and not a sandboxed iframe. Click it, then open the source:

Single source of truth

The rendered component and the source panel above both come from one file, counter.svelte. It is imported twice: once as a component (rendered) and once as ?source (highlighted at build time by the docsmith() plugin from svelte-docsmith/vite), so the demo and its code can never drift.

Examples that can't rot

The example in your docs is the same component your users import. When the component changes, the rendered demo and its shown source both change with it, so it can never decay into a screenshot of something that used to work.

See Writing pages for the import pattern to copy.

Coding agents see the same code. In a page’s agent markdown, which feeds llms-full.txt and the .md pages, each LiveExample is written out as its source file in a svelte code block.

Props playground

Pass a props import as controls and the example grows a panel of controls, one for each prop whose type allows it. The children snippet then receives the current values, so you decide where they go:

New
Controls
variant
href
external
<Badge>…</Badge>
			<script>
	import { Badge, LiveExample } from 'svelte-docsmith';
	import badgeProps from '$lib/badge.svelte?props';
</script>

<LiveExample controls={badgeProps}>
	{#snippet children(props)}
		<Badge {...props}>New</Badge>
	{/snippet}
</LiveExample>
		

The controls come from each prop’s TypeScript type:

Prop typeControl
booleana switch
a union of string or number literalsbuttons for up to four choices, a select beyond
stringa text field
numbera number field
anything else (snippets, callbacks, …)no control

A prop starts at its default when that default is a literal like 'primary', false or 3. A prop without a literal default can be left unset, so the component applies its own fallback. @deprecated props get no control.

Use args to start somewhere else, or to fill a required prop that has no default. A prop set in args still gets its control, and Reset returns to these values:

			<LiveExample controls={badgeProps} args={{ variant: 'success', href: '/docs' }}>
	{#snippet children(props)}
		<Badge {...props}>Docs</Badge>
	{/snippet}
</LiveExample>
		

Under the controls, a usage line such as <Badge variant="success" external>…</Badge> shows the props that differ from their defaults, in the order the component declares them, ready to copy.

Experimental

controls and args are outside the 1.0 stability promise for now. A control doesn’t follow changes the component makes to a $bindable() prop itself.

API reference

LiveExample
source string
Pre-highlighted source HTML for the source panel. Pass a ?source import of the same file.
controls ExtractedProps
A props import (import props from './badge.svelte?props'): shows a control for each prop whose type allows one, and passes the current values to children.
Fields
name required string
Component name derived from the file name, e.g. FileTree.
typeParameters required string[]
Type parameter names of a generic component, e.g. ['T'].
since string
The component's @since, from its `<!--
props required ExtractedProp[]
Declared props, in declaration order.
Fields
name required string
type string
Resolved type, e.g. 'a' | 'b'. Absent when the props are untyped.
default string
Source text of the default value, e.g. 'default'.
required required boolean
bindable required boolean
Declared with $bindable().
deprecated string
Present when tagged @deprecated: the tag's text as inline HTML, or ''.
description string
JSDoc description rendered to inline HTML.
control PropControl
How a props playground edits this prop, from its type. Absent when the type has no control (snippets, callbacks, objects, generics) or the prop is deprecated.
Fields
kind: 'boolean'
kind required 'boolean'
kind: 'text'
kind required 'text'
kind: 'number'
kind required 'number'
kind: 'options'
kind required 'options'
options required (string | number)[]
literalDefault stringnumberboolean
The default as a value, when its source text is a literal ('a', false, -1).
since string
@since text: the release the prop first appeared in.
fields ExtractedFieldGroup[]
What the prop's type opens into when it is one of the library's own object types: one group for an object or a list of them, one per object in a union.
Fields
label string
fields required ExtractedField[]
Fields
name required string
type string
default string
The @default tag's text.
required required boolean
deprecated string
Present when tagged @deprecated: the tag's text as inline HTML, or ''.
description string
JSDoc description rendered to inline HTML.
since string
fields ExtractedFieldGroup[]
inherited required InheritedAttributes[]
What the component also accepts by spreading its remaining props.
Fields
kind required 'element''type'
element: native attributes of an HTML element (label is the tag name). type: every prop of some other type (label is that type, as written).
label required string
args PlaygroundValues
Starting values for the controls, over the component's own defaults. Fills a required prop, or starts the example in a chosen state.
children required Snippet<[PlaygroundValues]>
The live, rendered example. With controls it receives the current values: {#snippet children(props)}<Badge {...props} />{/snippet}.

Was this page helpful?