Now that you’ve learned what stories are and how to browse them, let’s demo working on one of your components.
Pick a simple component from your project, like a Button, and write a
.stories.js file to go along with it. It might look something like this:
Go to your Storybook to view the rendered component. It’s OK if it looks a bit unusual right now.
Depending on your technology stack, you also might need to configure the Storybook environment further.
Storybook comes with a permissive default configuration. It attempts to customize itself to fit your setup. But it’s not foolproof.
Your project may have additional requirements before components can be rendered in isolation. This warrants customizing configuration further. There are three broad categories of configuration you might need.
If you see errors on the CLI when you run the
yarn storybook command. It’s likely you need to make changes to Storybook’s build configuration. Here are some things to try:
If Storybook builds but you see an error immediately when connecting to it in the browser, then chances are one of your input files is not compiling/transpiling correctly to be interpreted by the browser. Storybook supports modern browsers and IE11, but you may need to check the Babel and webpack settings (see above) to ensure your component code works correctly.
If a particular story has a problem rendering, often it means your component expects a certain environment is available to the component.
A common frontend pattern is for components to assume that they render in a certain “context” with parent components higher up the rendering hierarchy (for instance theme providers)
Use decorators to “wrap” every story in the necessary context providers.
.storybook/preview.js allows you to customize how components render in Canvas, the preview iframe. In this decorator example, we wrap every component rendered in Storybook with
Storybook isn’t opinionated about how you generate or load CSS. It renders whatever DOM elements you provide. But sometimes things won’t “look right” out of the box.
You may have to configure your CSS tooling for Storybook’s rendering environment. Here are some tips on what could help:
Theme users may need to add a decorator to
.storybook/preview.js, see above.
If you have global imported styles, create a file called
.storybook/preview.js and import the styles there. The styles will be added by Storybook automatically for all stories.
Alternatively, if you want to inject a CSS link tag to the
<head> directly (or some other resource like a webfont link), you can use
.storybook/preview-head.html to add arbitrary HTML.
If you're referencing fonts or images from a local directory, you'll need to configure the Storybook script to serve the static files.
If you want to link to static files in your project or stories (e.g.
/fonts/XYZ.woff), use the
-s path/to/folder to specify a static folder to serve from when you start up Storybook. To do so, edit the
build-storybook scripts in
We recommend serving external resources and assets requested in your components statically with Storybook. This ensures that assets are always available to your stories.