Docs
Storybook Docs

参数

参数是一组关于故事的静态命名元数据,通常用于控制 Storybook 功能和插件的行为。

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

可用参数列在 参数 API 参考 中。

¥Available parameters are listed in the parameters API reference.

例如,让我们通过参数自定义背景功能。我们将使用 parameters.backgrounds 来定义选择故事时背景工具栏中显示的背景。

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

故事参数

¥Story parameters

我们可以使用 CSF 导出中的 parameters 键为单个故事设置参数:

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

Button.stories.ts|tsx
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';
 
import { Button } from './Button';
 
const meta = {
  component: Button,
} satisfies Meta<typeof Button>;
 
export default meta;
type Story = StoryObj<typeof meta>;
 
export const Primary: Story = {
  // 👇 Story-level parameters
  parameters: {
    backgrounds: {
      options: {
        red: { name: 'Red', value: '#f00' },
        green: { name: 'Green', value: '#0f0' },
        blue: { name: 'Blue', value: '#00f' },
      },
    },
  },
};

组件参数

¥Component parameters

我们可以使用默认 CSF 导出中的 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
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { Meta } from '@storybook/your-framework';
 
import { Button } from './Button';
 
const meta = {
  component: Button,
  //👇 Creates specific parameters at the component level
  parameters: {
    backgrounds: {
      options: {},
    },
  },
} satisfies Meta<typeof Button>;
 
export default meta;

全局参数

¥Global parameters

我们还可以通过 .storybook/preview.js|ts 文件的 parameters 导出(这是你配置所有故事的文件)为所有故事设置参数:

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

.storybook/preview.ts
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { Preview } from '@storybook/your-framework';
 
const preview: Preview = {
  parameters: {
    backgrounds: {
      options: {
        light: { name: 'Light', value: '#fff' },
        dark: { name: 'Dark', value: '#333' },
      },
    },
  },
};
 
export default preview;

设置全局参数是配置插件的常用方法。使用背景,你可以配置每个故事可以渲染的背景列表。

¥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 and never dropped.

参数的合并很重要。这意味着可以基于每个故事覆盖单个特定的子参数,同时保留大多数全局定义的参数。

¥The merging of parameters is important. This means it is possible to override a single specific sub-parameter on a per-story basis while retaining most of the parameters defined globally.

如果你正在定义一个依赖于参数的 API(例如 addon),最好考虑到这种行为。

¥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.