Storybook for SvelteKit

Storybook for SvelteKit is a framework that makes it easy to develop and test UI components in isolation for SvelteKit applications. It includes:

  • 🪄 Zero config
  • 🧩 Easily mock many Kit modules
  • 🔗 Automatic link handling
  • 💫 and more!

Storybook for SvelteKit is only supported in Svelte projects.

Requirements

  • SvelteKit ≥ 1.0
  • Storybook ≥ 8.0

Getting started

In a project without Storybook

Follow the prompts after running this command in your Sveltekit project's root directory:

More on getting started with Storybook.

In a project with Storybook

This framework is designed to work with Storybook 7+. If you’re not already using v7, upgrade with this command:

Automatic migration

When running the upgrade command above, you should get a prompt asking you to migrate to @storybook/sveltekit, which should handle everything for you. In case that auto-migration does not work for your project, refer to the manual migration below.

Manual migration

First, install the framework:

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-vite
  • @storybook/svelte-webpack5
  • storybook-builder-vite
  • @storybook/builder-vite

Supported features

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:

ModuleStatusNote
$app/environment✅ Supportedversion is always empty in Storybook.
$app/forms⚠️ ExperimentalSee How to mock.
$app/navigation⚠️ ExperimentalSee How to mock.
$app/paths✅ SupportedRequires SvelteKit 1.4.0 or newer.
$app/stores⚠️ ExperimentalSee How to mock.
$env/dynamic/public🚧 Partially supportedOnly 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 supportedThis is a server-side feature, and Storybook renders all components on the client.
$env/static/private⛔ Not supportedThis is a server-side feature, and Storybook renders all components on the client.
$service-worker⛔ Not supportedThis is a service worker feature, which does not apply to Storybook.

How to mock

To mock a SvelteKit import you can define it within parameters.sveltekit_experimental:

MyComponent.stories.svelte
Loading...

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:

MyComponent.stories.svelte
Loading...

See the API reference for more information.

Writing native Svelte stories

Storybook provides a Svelte addon maintained by the community, enabling you to write stories for your Svelte components using the template syntax. You'll need to take some additional steps to enable this feature.

Run the following command to install the addon.

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.

API

Parameters

This framework contributes the following parameters to Storybook, under the sveltekit_experimental namespace:

forms

Type: { enhance: () => void }

Provides mocks for the $app/forms module.

forms.enhance

Type: () => void

A callback that will be called when a form with use:enhance is submitted.

hrefs

Type: 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.

Type: See SvelteKit docs

Provides mocks for the $app/navigation module.

Type: 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.

Type: 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.

Type: 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.

Type: 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.

Type: 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.

Type: See SvelteKit docs

An object that will be passed to the afterNavigate function, which will be invoked when the onMount event fires.

stores

Type: See SvelteKit docs

Provides mocks for the $app/stores module.

stores.navigating

Type: See SvelteKit docs

A partial version of the navigating store.

stores.page

Type: See SvelteKit docs

A partial version of the page store.

stores.updated

Type: boolean

A boolean representing the value of updated (you can also access updated.check() which will be a no-op).

Options

You can pass an options object for additional configuration if needed:

.storybook/main.js
Loading...

The available options are:

builder

Type: Record<string, any>

Configure options for the framework's builder. For Sveltekit, available options can be found in the Vite builder docs.

Troubleshooting

Error when starting Storybook

When starting Storybook after upgrading to v7.0, it may quit with the following error:

Loading...

This can occur when manually upgrading from 6.5 to 7.0. To resolve it, you'll need to remove the svelteOptions property in .storybook/main.js, as that is not supported (and no longer necessary) in Storybook 7+ with SvelteKit.

Was this page helpful?

Markdown accepted ([link text](url), _italic_, **bold**, etc). Your anonymous feedback will be posted publicly on GitHub.

✍️ Edit on GitHub – PRs welcome!
Storybook
Join the community
6,587 developers and counting
WhyWhy StorybookComponent-driven UI
DocsGuidesTutorialsChangelogTelemetryStatus
CommunityAddonsGet involvedBlog
ShowcaseExploreProjectsComponent glossary
Open source software
Storybook

Maintained by
Chromatic
Special thanks to Netlify and CircleCI