> **Version 8** — **React** / **TypeScript**
> Also available:
- `?renderer=svelte` for svelte
- `?language=js` for JavaScript
- `?codeOnly=true` for code snippets only
- other versions: Version 10.4 (latest) (`/docs/get-started/frameworks/svelte-vite.md`), Version 10.5 (alpha) (`/docs/10.5/get-started/frameworks/svelte-vite.md`), Version 9 (`/docs/9/get-started/frameworks/svelte-vite.md`)

# Storybook for Svelte & Vite

Storybook for Svelte & Vite is a [framework](https://storybook.js.org/docs/8/contribute/framework.md) that makes it easy to develop and test UI components in isolation for applications using [Svelte](https://svelte.dev/) built with [Vite](https://vitejs.dev/).

## Requirements

* Svelte ≥ 4.0
* Vite ≥ 4.0
* Storybook ≥ 8.0

## Getting started

### In a project without Storybook

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

```shell
npm create storybook@latest
```

```shell
pnpm create storybook@latest
```

```shell
yarn create storybook@latest
```

[More on getting started with Storybook.](https://storybook.js.org/docs/8/get-started/install.md)

### 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:

```shell
npx storybook@latest upgrade
```

```shell
pnpm dlx storybook@latest upgrade
```

```shell
yarn dlx storybook@latest upgrade
```

#### Automatic migration

When running the `upgrade` command above, you should get a prompt asking you to migrate to `@storybook/svelte-vite`, 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:

## Writing native Svelte stories

Storybook provides a Svelte [addon](https://storybook.js.org/addons/@storybook/addon-svelte-csf) maintained by the community, enabling you to write stories for your Svelte components using the template syntax.

  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).

### Setup

If you initialized your project with the Svelte framework, the addon has already been installed and configured for you. However, if you're [migrating](#automatic-migration) from a previous version, you'll need to take additional steps to enable this feature.

Run the following command to install the addon.

  The CLI's [`add`](https://storybook.js.org/docs/8/api/cli-options.md#add) command automates the addon's installation and setup. To install it manually, see our [documentation](https://storybook.js.org/docs/8/get-started/addons/install-addons.md#manual-installation) on how to install addons.

Update your Storybook configuration file (i.e., `.storybook/main.js|ts`) to enable support for this format.

### Configure

By default, the Svelte [addon](https://storybook.js.org/addons/@storybook/addon-svelte-csf) 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. <br/> `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](https://github.com/storybookjs/addon-svelte-csf/blob/next/README.md#legacy-api).

### Upgrade to Svelte CSF addon v5

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.

#### Simplified story API

If you are using the `Meta` component or the `meta` named export to define the story's metadata (e.g., [parameters](https://storybook.js.org/docs/8/writing-stories/parameters.md)), 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.

#### Story templates

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.

  If you need support for the `Template` component, the addon provides a feature flag for backward compatibility. For more information, see the [configuration options](#configure).

#### Story slots to snippets

With Svelte's slot deprecation and the introduction of reusable [`snippets`](https://svelte.dev/docs/svelte/v5-migration-guide#Snippets-instead-of-slots), 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. This feature extends the built-in `children` support in the `Story` component, allowing you to create dynamic stories without losing reactivity.

```svelte title="MyComponent.stories.svelte"
<script>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import MyComponent from './MyComponent.svelte';

  const { Story } = defineMeta({
    component: MyComponent,
  });
</script>

  {#snippet children(args)}
    Reactive component
  {/snippet}

```

#### Tags support

If you enabled automatic documentation generation with the `autodocs` story property, you must replace it with [`tags`](https://storybook.js.org/docs/8/writing-stories/tags.md). This property allows you to categorize and filter stories based on specific criteria and generate documentation based on the tags applied to the stories.

## API

### Options

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

The available options are:

#### `builder`

Type: `Record<string, any>`

Configure options for the [framework's builder](https://storybook.js.org/docs/8/api/main-config/main-config-framework.md#optionsbuilder). For this framework, available options can be found in the [Vite builder docs](https://storybook.js.org/docs/8/builders/vite.md).