Docs
Storybook Docs
You are viewing documentation for a previous version of StorybookSwitch to latest version

Configure Storybook

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 storybook dev and storybook build CLI commands.

Configure your Storybook project

Storybook's main configuration (i.e., the main.js|ts) defines your Storybook project's behavior, including the location of your stories, the addons you use, feature flags and other project-specific settings. This file should be in the .storybook folder in your project's root directory. You can author this file in either JavaScript or TypeScript. Listed below are the available options and examples of how to use them.

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  // Required
  framework: '@storybook/your-framework',
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  // Optional
  addons: ['@storybook/addon-essentials'],
  docs: {
    autodocs: 'tag',
  },
  staticDirs: ['../public'],
};
 
export default config;

This configuration file is a preset and, as such, has a powerful interface, which can be further customized. Read our documentation on writing presets to learn more.

Configuration elementDescription
storiesThe array of globs that indicates the location of your story files, relative to main.js
staticDirsSets a list of directories of static files to be loaded by Storybook
staticDirs: ['../public']
addonsSets the list of addons loaded by Storybook
addons: ['@storybook/addon-essentials']
typescriptConfigures how Storybook handles TypeScript files
typescript: { check: false, checkOptions: {} }
frameworkConfigures Storybook based on a set of framework-specific settings
framework: { name: '@storybook/svelte-vite', options:{} }
coreConfigures Storybook's internal features
core: { disableTelemetry: true, }
docsConfigures Storybook's auto-generated documentation
docs: { autodocs: 'tag' }
featuresEnables Storybook's additional features
See table below for a list of available features features: { storyStoreV7: true }
refsConfigures Storybook composition
refs:{ example: { title: 'ExampleStorybook', url:'https://your-url.com' } }
logLevelConfigures Storybook's logs in the browser terminal. Useful for debugging
logLevel: 'debug'
webpackFinalCustomize Storybook's Webpack setup
webpackFinal: async (config:any) => { return config; }
viteFinalCustomize Storybook's Vite setup when using the vite builder
viteFinal: async (config: Vite.InlineConfig, options: Options) => { return config; }
envDefines custom Storybook environment variables.
env: (config) => ({...config, EXAMPLE_VAR: 'Example var' }),
buildOptimizes Storybook's production build for performance by excluding specific features from the bundle. Useful when decreased build times are a priority.
build:ย { test: {} }

Feature flags

Additionally, you can also provide additional feature flags to your Storybook configuration. Below is an abridged list of available features that are currently available.

Configuration elementDescription
storyStoreV7Configures Storybook to load stories on demand, rather than during boot up (defaults to true as of v7.0)
features: { storyStoreV7: true }
buildStoriesJsonGenerates index.json and stories.json files to help story loading with the on-demand mode (defaults to true when storyStoreV7 is true)
features: { buildStoriesJson: true }
legacyMdx1Enables support for MDX version 1 as a fallback. Requires @storybook/mdx1-csf
features: { legacyMdx1: true }

Configure story loading

By default, Storybook will load stories from your project based on a glob (pattern matching string) in .storybook/main.js|ts that matches all files in your project with extension .stories.*. The intention is for you to colocate a story file along with the component it documents.

โ€ข
โ””โ”€โ”€ components
    โ”œโ”€โ”€ Button.js
    โ””โ”€โ”€ Button.stories.js

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 .md and .js files from the my-project/src/components directory, you could write:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  stories: ['../my-project/src/components/*.@(js|md)'],
};
 
export default config;

With a configuration object

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/components directory, you could adjust your stories configuration field into the following:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  stories: [
    {
      // ๐Ÿ‘‡ Sets the directory containing your stories
      directory: '../packages/components',
      // ๐Ÿ‘‡ Storybook will load all files that match this glob
      files: '*.stories.*',
      // ๐Ÿ‘‡ Used when generating automatic titles for your stories
      titlePrefix: 'MyComponents',
    },
  ],
};
 
export default config;

When Storybook starts, it will look for any file containing the stories extension inside the packages/components directory and generate the titles for your stories.

With a directory

You can also simplify your Storybook configuration and load the stories using a directory. For example, if you want to load all the stories inside a packages/MyStories, you can adjust the configuration as such:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  // ๐Ÿ‘‡ Storybook will load all existing stories within the MyStories folder
  stories: ['../packages/MyStories'],
};
 
export default config;

With a custom implementation

You can also adjust your Storybook configuration and implement custom logic to load 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:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
import type { StoriesEntry } from '@storybook/types';
 
async function findStories(): Promise<StoriesEntry[]> {
  // your custom logic returns a list of files
}
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  stories: async (list: StoriesEntry[]) => [
    ...list,
    // ๐Ÿ‘‡ Add your found stories to the existing list of story files
    ...(await findStories()),
  ],
};
 
export default config;

On-demand story loading

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:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  features: {
    storyStoreV7: false, // ๐Ÿ‘ˆ Opt out of on-demand story loading
  },
};
 
export default config;

Known limitations

Because of the way stories are currently indexed in Storybook, loading stories on demand with storyStoreV7 has a couple of minor limitations at the moment:

  • CSF formats from version 1 to version 3 are supported. The storiesOf construct is not.
  • CustomstorySort functions are allowed based on a restricted API.

Configure story rendering

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 UI, the โ€œpreviewโ€ iframe that renders your components in isolation. Use preview.js for global code (such as CSS imports or JavaScript mocks) that applies to all stories.

The preview.js file can be an ES module and export the following keys:

If youโ€™re looking to change how to order your stories, read about sorting stories.

Configure Storybookโ€™s UI

To control the behavior of Storybookโ€™s UI (the โ€œmanagerโ€), you can create a .storybook/manager.js file.

This file does not have a specific API but is the place to set UI options and to configure Storybookโ€™s theme.