Storybook Docs

Setup Storybook

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, or a .stories.mdx file to go along with it. It might look something like this:

import React from 'react';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { YourComponent } from './YourComponent';
//๐Ÿ‘‡ This default export determines where your story goes in the story list
export default {
  /* ๐Ÿ‘‡ The title prop is optional.
  * See
  * to learn how to generate automatic titles
  title: 'YourComponent',
  component: YourComponent,
} as ComponentMeta<typeof YourComponent>;
//๐Ÿ‘‡ We create a โ€œtemplateโ€ of how args map to rendering
const Template: ComponentStory<typeof YourComponent> = (args) => <YourComponent {...args} />;
export const FirstStory = Template.bind({});
FirstStory.args = {
  /*๐Ÿ‘‡ The args you need here will depend on your component */

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.

Configure Storybook for your stack

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.

Build configuration like Webpack and Babel

If you see errors on the CLI when you run the yarn storybook command, you likely need to make changes to Storybookโ€™s build configuration. Here are some things to try:

  • Presets bundle common configurations for various technologies into Storybook. In particular, presets exist for Create React App, SCSS and Ant Design.
  • Specify a custom Babel configuration for Storybook. Storybook automatically tries to use your projectโ€™s config if it can.
  • Adjust the Webpack configuration that Storybook uses. Try patching in your own configuration if needed.
Runtime configuration

If Storybook builds but you see an error immediately when connecting to it in the browser, in that case, 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.

Component context

If a particular story has a problem rendering, often it means your component expects a specific environment is available to the component.

A common frontend pattern is for components to assume that they render in a specific โ€œcontextโ€ with parent components higher up the rendering hierarchy (for instance, theme providers).

Use decorators to โ€œwrapโ€ every story in the necessary context providers. The .storybook/preview.js file allows you to customize how components render in Canvas, the preview iframe. See how you can wrap every component rendered in Storybook with Styled Components ThemeProvider, Vue's Fontawesome, or with an Angular theme provider component in the example below.

import React from 'react';
import { ThemeProvider } from 'styled-components';
export const decorators = [
  (Story) => (
    <ThemeProvider theme="default">
      <Story />

Render component styles

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:

CSS-in-JS like styled-components and Emotion

If you are using CSS-in-JS, chances are your styles are working because theyโ€™re generated in JavaScript and served alongside each component. Theme users may need to add a decorator to .storybook/preview.js, see above.

@import CSS into components

Storybook allows you to import CSS files in your components directly. But in some cases you may need to tweak its Webpack configuration. Angular components require a special import.

Global imported styles

If you have global imported styles, create a file called .storybook/preview.js and import the styles there. They will be added by Storybook automatically for all stories.

Add external CSS or webfonts in the <head>

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.

Load fonts or images from a local directory

If you're referencing fonts or images from a local directory, you'll need to configure the Storybook script to serve the static files.

Load assets and resources

If you want to link to static files in your project or stories (e.g., /fonts/XYZ.woff), use the -s path/to/folder flag to specify a static folder to serve from when you start up Storybook. To do so, edit the storybook and build-storybook scripts in package.json.

We recommend serving external resources and assets requested in your components statically with Storybook. It ensures that assets are always available to your stories.