Storybook for Web components & Webpack

Storybook for Web components & Webpack is a framework that makes it easy to develop and test UI components in isolation for applications using Web components built with Webpack.

Storybook for Web components & Webpack is only supported in Web components projects.

Requirements

  • Webpack ≥ 5.0
  • Storybook ≥ 8.0

Getting started

In a project without Storybook

Follow the prompts after running this command in your Web components 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/web-components-webpack5, 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:

Next, install and register your appropriate compiler addon, depending on whether you're using SWC (recommended) or Babel:

or

More details can be found in the Webpack builder docs.

Finally, update your .storybook/main.js|ts to change the framework property:

Hot Module Reloading (HMR)

Web components are registered on global registry, which only accepts a given name/class once. That can lead to errors when using classical HMR. While there are ideas on how to achieve HMR with a static registry, there is no proven solution yet. Therefore, the best approach for now is to do full page reloads while developing. You can ensure those page reloads happen quickly by defining your stories as specific states of components (which we would recommend regardless).

Set up es6/7 dependencies

By default, Storybook only works with precompiled ES5 code. Because most web components themselves and their libs are distributed as ES2017, you will need to manually mark those packages as "needs transpilation".

For example, if you have a library called my-library which is in ES2017, then you can configure your project like so:

.storybook/main.js
Loading...

By default, the following folders are transpiled:

  • src/*.js
  • packages/*/src/*.js
  • node_modules/lit-html/*.js
  • node_modules/lit-element/*.js
  • node_modules/@open-wc/*.js
  • node_modules/@polymer/*.js
  • node_modules/@vaadin/*.js

Note that the src folder is also included. This provides some extra configuration to allow for import.meta and some other features.

If you use a folder for your components/stories other than src, you will need to use the configuration example above to have it properly transpiled.

API

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 this framework, available options can be found in the Webpack builder docs.

Troubleshooting

Error while developing with HMR

While developing a component, you might encounter this error:

Loading...

This error occurs because the component is already registered in the global registry. See the limitations of HMR with web components for more information.

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,598 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