Storybook is configured via a folder called
.storybook, which contains various configuration files.
Note that you can change the folder that Storybook uses by setting the
-c flag to your
The main configuration file is
main.js|ts. This file controls the Storybook server's behavior, so you must restart Storybook’s process when you change it. It contains the following:
main.js|ts configuration file is a preset and, as such, has a powerful interface, but the key fields within it are:
stories- an array of globs that indicates the location of your story files, relative to
addons- a list of the addons you are using.
webpackFinal- custom Webpack configuration.
babel- custom babel configuration.
framework- framework specific configurations to help the loading and building process.
docs- auto-generated documentation configuration.
See all the available fields below if you need further customization.
Additionally, you can also provide additional feature flags to your Storybook configuration. Below is an abridged list of available features that are currently available.
|Configures Storybook to load stories on demand, rather than during boot up. |
|Generates a |
By default, Storybook will load stories from your project based on a glob (pattern matching string) in
.storybook/main.js that matches all files in your project with extension
.stories.*. The intention is you colocate a story file with the component it documents.
If you want to use a different naming convention, you can alter the glob using the syntax supported by picomatch.
For example, if you wanted to pull both
.js files from the
my-project/src/components directory, you could write:
Additionally, you can customize your Storybook configuration to load your stories based on a configuration object. For example, if you wanted to load your stories from a
packages directory, you could adjust your
stories configuration field into the following:
When Storybook starts, it will look for any file containing the
stories extension inside the
packages/stories directory and generate the titles for your stories.
You can also simplify your Storybook configuration and load the stories based on a directory. For example, if you want to load all the stories inside a
packages/MyStories, you can adjust the configuration as such:
You can also adjust your Storybook configuration and implement your custom logic for loading your stories. For example, suppose you were working on a project that includes a particular pattern that the conventional ways of loading stories could not solve, in that case, you could adjust your configuration as follows:
As your Storybook grows, it gets challenging to load all of your stories performantly, slowing down the loading times and yielding a large bundle. Out of the box, Storybook loads your stories on demand rather than during boot-up to improve the performance of your Storybook. If you need to load all of your stories during boot-up, you can disable this feature by setting the
storyStoreV7 feature flag to
false in your configuration as follows:
This feature is experimental, and it has some limitations on what you can and cannot do in your stories files. If you plan to use it, you'll need to take into consideration the following limitations:
- CSF formats from version 1 to version 3 are supported. The
storiesOfconstruct is not.
storySortfunctions are allowed based on a restricted API.
If you would like, you can also write your Storybook configuration using TypeScript. To get started, add a
.babelrc file inside your project and include the following Babel presets:
This babel config will be used to process your stories, as well as your config files.
Alternatively, you can install
ts-node in your project, which will be used to process your config files without the need for a
.storybook/main.ts and restart your Storybook.
You can also use Storybook's TypeScript types to ensure you are using valid options and get autocompletion in your editor. Below is an abridged Storybook configuration with TypeScript types and additional information about each configuration element.
See the vite builder TypeScript documentation if using
|The array of globs that indicates the location of your story files, relative to |
|Sets a list of directories of static files to be loaded by Storybook |
|Sets the list of addons loaded by Storybook |
|Configures how Storybook handles TypeScript files |
|Configures Storybook based on a set of framework-specific settings |
|Configures Storybook's internal features.|
|Configures Storybook's auto-generated documentation|
|Enables Storybook's additional features|
See table below for a list of available features
|Configures Storybook composition |
|Configures Storybook's logs in the browser terminal. Useful for debugging |
|Customize Storybook's Webpack setup |
|Customize Storybook's Vite setup when using the vite builder |
|Defines custom Storybook environment variables. |
To control the way stories are rendered and add global decorators and parameters, create a
.storybook/preview.js file. This is loaded in the Canvas tab, the “preview” iframe that renders your components in isolation. Use
preview.js file can be an ES module and export the following keys:
decorators- an array of global decorators
parameters- an object of global parameters
globalTypes- definition of globalTypes
If you’re looking to change how to order your stories, read about sorting stories.
To control the behavior of Storybook’s UI (the “manager”), you can create a