docs/get-started/frameworks/svelte-vite.mdx
Storybook for Svelte & Vite is a framework that makes it easy to develop and test UI components in isolation for applications using Svelte built with Vite.
To install Storybook in an existing Svelte 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: 'Svelte', range: '≥ 5', 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).
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 Svelte 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 offers zero-config support for Storybook's Svelte 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="svelte-vite-install.md" />Then, update your .storybook/main.js|ts to change the framework property:
You can pass an options object for additional configuration if needed:
{/* prettier-ignore-start */ }
<CodeSnippets path="svelte-vite-framework-options.md" />The available options are:
builderType: Record<string, any>
Configure options for the framework's builder. For this framework, 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.