Docs
Storybook Docs

stories

(Required)

Parent: main.js|ts configuration

Type:

| (string | StoriesSpecifier)[]
| async (list: (string | StoriesSpecifier)[]) => (string | StoriesSpecifier)[]

Configures Storybook to load stories from the specified locations. The intention is for you to colocate a story file along with the component it documents:

โ€ข
โ””โ”€โ”€ components
    โ”œโ”€โ”€ Button.ts
    โ””โ”€โ”€ Button.stories.ts
.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/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
};
 
export default config;

If you want to use a different naming convention, you can alter the glob using the syntax supported by picomatch.

Keep in mind that some addons may assume Storybook's default naming convention.

With an array of globs

Storybook will load stories from your project as found by this array of globs (pattern matching strings).

Stories are loaded in the order they are defined in the array. This allows you to control the order in which stories are displayed in the sidebar:

.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/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
};
 
export default config;

With a configuration object

Additionally, you can customize your Storybook configuration to load your stories based on a configuration object. This object is of the type StoriesSpecifier, defined below.

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.

StoriesSpecifier

Type:

{
  directory: string;
  files?: string;
  titlePrefix?: string;
}

StoriesSpecifier.directory

(Required)

Type: string

Where to start looking for story files, relative to the root of your project.

StoriesSpecifier.files

Type: string

Default: '**/*.@(mdx|stories.@(js|jsx|mjs|ts|tsx))'

A glob, relative to StoriesSpecifier.directory (with no leading ./), that matches the filenames to load.

StoriesSpecifier.titlePrefix

Type: string

Default: ''

When auto-titling, prefix used when generating the title for your stories.

With a custom implementation

๐Ÿ’ก Storybook now statically analyzes the configuration file to improve performance. Loading stories with a custom implementation may de-optimize or break this ability.

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;