Storybook for Web components with Vite
Storybook for Web components & Vite is a framework that makes it easy to develop and test UI components in isolation for applications using Web components built with Vite.
Install
To install Storybook in an existing project, run this command in your project's root directory:
npm create storybook@latestThe installer detects Web components projects via dependencies such as Lit. If your project uses vanilla Web components, detection fails, and you need to explicitly pass a project type. Use --type web_components to install this framework, or --type html for the plain HTML framework.
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.
Requirements
Vite โฅ 5
Run Storybook
To run Storybook for a particular project, run the following:
npm run storybookTo build Storybook, run:
npm run build-storybookYou will find the output in the configured outputDir (default is storybook-static).
Configure
Component documentation
Storybook reads your custom elements and their JSDoc comments, a step called docgen, to generate controls and documentation, and to improve skill output. New projects do not need any docgen configuration.
This framework supports three docgen engines:
| Docgen engine | Runs | Status |
|---|---|---|
@wc-toolkit/cem-generator, on your source code | Server-side | Recommended, default |
Your own Custom Elements Manifest, set with customElementsManifest | Server-side | Recommended if your library already ships one |
Your own manifest, passed to setCustomElementsManifest() in your preview file | Browser-side | Deprecated |
The server-side engines need the docgenServer feature, which is on by default. Server-side docgen is necessary for agent skills to contain docgen information. If you turn server-side docgen off, and do not pass a custom elements manifest, control generation will also be disabled.
Writing stories
In your story file, set the component property to the element's tag name and import the module that defines the element. Storybook then finds the element's attributes, properties, events, slots, CSS custom properties, CSS parts, and CSS states. It uses them to create controls and automatic documentation, and it shows your JSDoc comments as descriptions.
By default, Storybook renders the element from the story's args:
import preview from '../.storybook/preview';
import './lit-slots-and-css';
const meta = preview.meta({
component: 'lit-slots-and-css',
});
export const Basic = meta.story({
args: {
heading: 'Hello',
'default-slot': 'Body <b>text</b>',
'actions-slot': '<button>Confirm</button>',
'panel-part': 'color: rebeccapurple;',
'--slot-panel-color': 'teal',
},
});To listen to an event, set its arg to a function:
import preview from '../.storybook/preview';
import './lit-events';
const meta = preview.meta({
component: 'lit-events',
});
export const WithListener = meta.story({
args: {
value: 'changed',
'my-change-event': () => {},
},
});Each arg key tells Storybook which part of the element to set. Keys for events, slots, CSS parts, and CSS states end in a suffix, so they do not conflict with attribute names:
| Element API | Arg key | Example | Result |
|---|---|---|---|
| Attribute | The attribute name | heading: 'Hello' | heading="Hello". true adds the attribute, false omits it. |
| Property | The property name | items: ['a', 'b'] | element.items = ['a', 'b'] |
| Event | <name>-event | 'my-change-event': () => {} | A listener for the my-change event |
| Method | <name>-method | Listed in the docs table only. Nothing is rendered. | |
| Slot | <name>-slot, or default-slot for the default slot | 'actions-slot': '<button>Confirm</button>' | Child content with slot="actions" |
| CSS part | <name>-part | 'panel-part': 'color: rebeccapurple;' | A ::part(panel) rule |
| CSS state | <name>-state | 'active-state': 'opacity: 0.5;' | A :state(active) rule |
| CSS custom property | --<name> | '--panel-color': 'teal' | style="--panel-color: teal" |
To write the markup yourself, add a render function that returns a Lit html template:
import preview from '../.storybook/preview';
import { html } from 'lit';
import './lit-slots-and-css';
const meta = preview.meta({
component: 'lit-slots-and-css',
});
export const CustomMarkup = meta.story({
render: (args) =>
html`<lit-slots-and-css heading=${args.heading}>
<p>Default slot content</p>
<button slot="actions">Confirm</button>
</lit-slots-and-css>`,
args: {
heading: 'Hello',
},
});Storybook finds your elements by following the imports in your story files and preview file. If it cannot find or read an element, the Controls table shows a message, as will agents using the Storybook's AI tools. The build does not fail.
A story file with no component defined gets no automatic controls. The Button, Header, and Page example stories that Storybook adds to a new project work this way: they render through a template function, and the Code panel shows the story's source code.
Code snippets
Autodocs and the Code panel show HTML that Storybook generates from your story. For a story that uses args, the snippet is one element with its attributes and slot content, plus a <style> block when needed. For example, the Basic story above becomes:
<style>
lit-slots-and-css::part(panel) {
color: rebeccapurple;
}
</style>
<lit-slots-and-css heading="Hello" style="--slot-panel-color: teal;">
Body <b>text</b>
<button slot="actions">Confirm</button>
</lit-slots-and-css>For a story with a render function, the snippet is the Lit template with the arg values filled in. If the story has event args, the snippet also includes a <script> that adds the listeners.
Some things cannot be shown as HTML, such as a Lit directive or a property that has no matching attribute. In those cases, Storybook shows a warning above the snippet. To write the snippet yourself, set parameters.docs.source.code. To hide it, set that parameter to null. For all options, see the Source block API.
Custom Elements Manifest
A Custom Elements Manifest is optional. If your project already has one, you can tell Storybook where to find it. Storybook then reads the elements in the manifest from the manifest instead of from your source code:
import type { StorybookConfig } from '@storybook/web-components-vite';
const config: StorybookConfig = {
framework: {
name: '@storybook/web-components-vite',
options: {
customElementsManifest: '../custom-elements.json',
},
},
};
export default config;If you do not set this option, Storybook uses the customElements field in your package.json, if it exists. This option replaces the runtime setCustomElementsManifest() call, which is deprecated and will be removed in Storybook 12.
If you generate the manifest with @custom-elements-manifest/analyzer, enable the plugin for your library, such as --litelement for Lit. Without it, the manifest does not include attributes that you declare with decorators.
FAQ
How do I use Stencil with static Storybook builds?
If you register Stencil components with defineCustomElements() from the generated loader, enable Stencil's import injection so Vite can include the lazy-loaded component entry files in the static Storybook build.
For Stencil 4.26 and later:
import type { Config } from '@stencil/core';
export const config: Config = {
extras: {
enableImportInjection: true,
},
};For older Stencil versions, use experimentalImportInjection: true instead.
How do I manually install the Web Components framework?
First, install the framework:
npm install --save-dev @storybook/web-components-viteThen, update your .storybook/main.js|ts to change the framework property:
import type { StorybookConfig } from '@storybook/web-components-vite';
const config: StorybookConfig = {
// ...
framework: '@storybook/web-components-vite', // ๐ Add this
};
export default config;API
Options
You can pass an options object for additional configuration if needed:
import type { StorybookConfig } from '@storybook/web-components-vite';
const config: StorybookConfig = {
framework: {
name: '@storybook/web-components-vite',
options: {
customElementsManifest: '../custom-elements.json',
},
},
};
export default config;The available options are:
customElementsManifest
Type: string | string[]
Default: The customElements field in your package.json. If that field does not exist, Storybook reads all elements from your source code.
The path to one or more Custom Elements Manifest files, relative to the Storybook configuration directory. Storybook reads the elements in these files from the manifest. It reads all other elements from your source code.
builder
Type: Record<string, any>
Configure options for the framework's builder. For this framework, available options can be found in the Vite builder docs.
