docs/get-started/frameworks/sveltekit.mdx
Storybook for SvelteKit is a framework that makes it easy to develop and test UI components in isolation for SvelteKit applications.
To install Storybook in an existing SvelteKit project, run this command in your project's root directory:
<CodeSnippets path="create-command.md" variant="new-users" copyEvent="CreateCommandCopy" />You can then get started writing stories, running tests and documenting your components. For more control over the installation process, refer to the installation guide.
<GetStartedVersions versions={[{ name: 'SvelteKit', range: '≥ 1.0', icon: '/images/logos/renderers/logo-svelte.svg' }, { name: 'Vite', range: '≥ 5', icon: '/images/logos/builders/vite.svg' }]} />
To run Storybook for a particular project, run the following:
<CodeSnippets path="storybook-run-dev.md" />To build Storybook, run:
<CodeSnippets path="build-storybook-production-mode.md" />You will find the output in the configured outputDir (default is storybook-static).
This section covers SvelteKit support and configuration options.
All Svelte language features are supported out of the box, as the Storybook framework uses the Svelte compiler directly. However, SvelteKit has some Kit-specific modules that aren't supported. Here's a breakdown of what will and will not work within Storybook:
| Module | Status | Note |
|---|---|---|
$app/environment | ✅ Supported | version is always empty in Storybook. |
$app/forms | ⚠️ Experimental | See How to mock. |
$app/navigation | ⚠️ Experimental | See How to mock. |
$app/paths | ✅ Supported | Requires SvelteKit 1.4.0 or newer. |
$app/state | ⚠️ Experimental | Requires SvelteKit v2.12 or newer. See How to mock. |
$app/stores | ⚠️ Experimental | See How to mock. |
$env/dynamic/public | 🚧 Partially supported | Only supported in development mode. Storybook is built as a static app with no server-side API, so it cannot dynamically serve content. |
$env/static/public | ✅ Supported | |
$lib | ✅ Supported | |
@sveltejs/kit/* | ✅ Supported | |
$env/dynamic/private | ⛔ Not supported | This is a server-side feature, and Storybook renders all components on the client. |
$env/static/private | ⛔ Not supported | This is a server-side feature, and Storybook renders all components on the client. |
$service-worker | ⛔ Not supported | This is a service worker feature, which does not apply to Storybook. |
To mock a SvelteKit import you can define it within parameters.sveltekit_experimental:
The available parameters are documented in the API section, below.
The default link-handling behavior (e.g., when clicking an <a href="..." /> element) is to log an action to the Actions panel.
You can override this by assigning an object to parameters.sveltekit_experimental.hrefs, where the keys are strings representing an href, and the values define your mock. For example:
See the API reference for more information.
Storybook provides a Svelte addon maintained by the community, enabling you to write stories for your Svelte components using the template syntax.
<Callout variant="info"> The community actively maintains the Svelte CSF addon but still lacks some features currently available in the official Storybook Svelte framework support. For more information, see the [addon's documentation](https://github.com/storybookjs/addon-svelte-csf). </Callout>If you initialized your project with the Sveltekit framework, the addon has already been installed and configured for you. However, if you're migrating from a previous version, you'll need to take additional steps to enable this feature.
Run the following command to install the addon.
<CodeSnippets path="svelte-csf-addon-install.md" /> <Callout variant="info">The CLI's add command automates the addon's installation and setup. To install it manually, see our documentation on how to install addons.
Update your Storybook configuration file (i.e., .storybook/main.js|ts) to enable support for this format.
By default, the Svelte addon addon offers zero-config support for Storybook's SvelteKit framework. However, you can extend your Storybook configuration file (i.e., .storybook/main.js|ts) and provide additional addon options. Listed below are the available options and examples of how to use them.
| Options | Description |
|---|---|
legacyTemplate | Enables support for the Template component for backward compatibility. |
options: { legacyTemplate: true } |
Enabling the legacyTemplate option can introduce a performance overhead and should be used cautiously. For more information, refer to the addon's documentation.
With the Svelte 5 release, Storybook's Svelte CSF addon has been updated to support the new features. This guide will help you migrate to the latest version of the addon. Below is an overview of the major changes in version 5.0 and the steps needed to upgrade your project.
If you are using the Meta component or the meta named export to define the story's metadata (e.g., parameters), you'll need to update your stories to use the new defineMeta function. This function returns an object with the required information, including a Story component that you must use to define your component stories.
{/* prettier-ignore-start */ }
<CodeSnippets path="svelte-csf-story-migration.md" />If you used the Template component to control how the component renders in the Storybook, this feature was replaced with built-in children support in the Story component, enabling you to compose components and define the UI structure directly in the story.
{/* prettier-ignore-start */ }
<CodeSnippets path="svelte-csf-story-custom-children.md" /> <Callout variant="info">If you need support for the Template component, the addon provides a feature flag for backward compatibility. For more information, see the configuration options.
With Svelte's slot deprecation and the introduction of reusable snippets, the addon also introduced support for this feature allowing you to extend the Story component and provide a custom snippet to provide dynamic content to your stories. Story accepts a template snippet, allowing you to create dynamic stories without losing reactivity.
<script>
import { defineMeta } from '@storybook/addon-svelte-csf';
import MyComponent from './MyComponent.svelte';
const { Story } = defineMeta({
component: MyComponent,
});
</script>
<Story name="Default" args={{ exampleProperty: true }}>
{#snippet template(args)}
<MyComponent {...args}>Reactive component</MyComponent>
{/snippet}
</Story>
If you enabled automatic documentation generation with the autodocs story property, you must replace it with tags. This property allows you to categorize and filter stories based on specific criteria and generate documentation based on the tags applied to the stories.
{/* prettier-ignore-start */ }
<CodeSnippets path="svelte-csf-addon-tags.md" />First, install the framework:
<CodeSnippets path="sveltekit-install.md" />Then, update your .storybook/main.js|ts to change the framework property:
Finally, these packages are now either obsolete or part of @storybook/sveltekit, so you no longer need to depend on them directly. You can remove them (npm uninstall, yarn remove, pnpm remove) from your project:
@storybook/svelte-vitestorybook-builder-vite@storybook/builder-viteThis framework contributes the following parameters to Storybook, under the sveltekit_experimental namespace:
formsType: { enhance: () => void }
Provides mocks for the $app/forms module.
forms.enhanceType: () => void
A callback that will be called when a form with use:enhance is submitted.
hrefsType: Record<[path: string], (to: string, event: MouseEvent) => void | { callback: (to: string, event: MouseEvent) => void, asRegex?: boolean }>
If you have an <a /> tag inside your code with the href attribute that matches one or more of the links defined (treated as regex based if the asRegex property is true) the corresponding callback will be called. If no matching hrefs are defined, an action will be logged to the Actions panel. See Mocking links for an example.
navigationType: See SvelteKit docs
Provides mocks for the $app/navigation module.
navigation.gotoType: See SvelteKit docs
A callback that will be called whenever goto is called. If no function is provided, an action will be logged to the Actions panel.
navigation.pushStateType: See SvelteKit docs
A callback that will be called whenever pushState is called. If no function is provided, an action will be logged to the Actions panel.
navigation.replaceStateType: See SvelteKit docs
A callback that will be called whenever replaceState is called. If no function is provided, an action will be logged to the Actions panel.
navigation.invalidateType: See SvelteKit docs
A callback that will be called whenever invalidate is called. If no function is provided, an action will be logged to the Actions panel.
navigation.invalidateAllType: See SvelteKit docs
A callback that will be called whenever invalidateAll is called. If no function is provided, an action will be logged to the Actions panel.
navigation.afterNavigateType: See SvelteKit docs
An object that will be passed to the afterNavigate function, which will be invoked when the onMount event fires.
storesType: See SvelteKit docs
Provides mocks for the $app/stores module.
stores.navigatingType: See SvelteKit docs
A partial version of the navigating store.
stores.pageType: See SvelteKit docs
A partial version of the page store.
stores.updatedType: boolean
A boolean representing the value of updated (you can also access updated.check() which will be a no-op).
stateType: See SvelteKit docs
Provides mocks for the $app/state module.
state.navigatingType: See SvelteKit docs
A partial version of the navigating store.
state.pageType: See SvelteKit docs
A partial version of the page store.
state.updatedType: { current: boolean }
An object representing the current value of updated. You can also access updated.check(), which will be a no-op.
You can pass an options object for additional configuration if needed:
{/* prettier-ignore-start */ }
<CodeSnippets path="sveltekit-framework-options.md" />The available options are:
builderType: Record<string, any>
Configure options for the framework's builder. For Sveltekit, available options can be found in the Vite builder docs.
docgenType: boolean
Default: true
Enables or disables automatic documentation generation for component properties. When disabled, Storybook will skip the docgen processing step during build, which can improve build performance.
<CodeSnippets path="svelte-framework-options-docgen.md" />Disabling docgen can improve build performance for large projects, but argTypes won't be inferred automatically, which will prevent features like Controls and docs from working as expected. To use those features, you will need to define argTypes manually.