Join live session: Top 8 Storybook myths holding your team back
Docs
Storybook Docs

Parameters

Parameters are a set of static, named metadata about a story, typically used to control the behavior of Storybook features and addons.

For example, let’s customize the backgrounds addon via a parameter. We’ll use parameters.backgrounds to define which backgrounds appear in the backgrounds toolbar when a story is selected.

Story parameters

We can set a parameter for a single story with the parameters key on a CSF export:

Button.stories.js|ts|jsx|tsx
import { Button } from './Button';
 
export default {
  /* πŸ‘‡ The title prop is optional.
  * See https://storybook.js.org/docs/6/configure#configure-story-loading
  * to learn how to generate automatic titles
  */
  title: 'Button',
  component: Button,
};
 
const Template = (args) => ({
  // πŸ‘ˆ Your template goes here
});
 
export const Primary = Template.bind({});
Primary.args = {
  primary: true,
  label: 'Button',
};
Primary.parameters = {
  backgrounds: {
    values: [
      { name: 'red', value: '#f00' },
      { name: 'green', value: '#0f0' },
      { name: 'blue', value: '#00f' },
    ],
  },
};

Component parameters

We can set the parameters for all stories of a component using the parameters key on the default CSF export:

Button.stories.ts|tsx
import React from 'react';
 
import { ComponentMeta } from '@storybook/react';
 
import { Button } from './Button';
 
export default {
  /* πŸ‘‡ The title prop is optional.
  * See https://storybook.js.org/docs/6/configure#configure-story-loading
  * to learn how to generate automatic titles
  */
  title: 'Button',
  component: Button,
  //πŸ‘‡ Creates specific parameters for the story
  parameters: {
    backgrounds: {
      values: [
        { name: 'red', value: '#f00' },
        { name: 'green', value: '#0f0' },
        { name: 'blue', value: '#00f' },
      ],
    },
  },
} as ComponentMeta<typeof Button>;

Global parameters

We can also set the parameters for all stories via the parameters export of your .storybook/preview.js file (this is the file where you configure all stories):

.storybook/preview.js
export const parameters = {
  backgrounds: {
    values: [
      { name: 'red', value: '#f00' },
      { name: 'green', value: '#0f0' },
    ],
  },
};

Setting a global parameter is a common way to configure addons. With backgrounds, you configure the list of backgrounds that every story can render in.

Rules of parameter inheritance

The way the global, component and story parameters are combined is:

  • More specific parameters take precedence (so a story parameter overwrites a component parameter which overwrites a global parameter).
  • Parameters are merged so keys are only ever overwritten, never dropped.

The merging of parameters is important. It means it is possible to override a single specific sub-parameter on a per-story basis but still retain the majority of the parameters defined globally.

If you are defining an API that relies on parameters (e.g. an addon) it is a good idea to take this behavior into account.