Docs
Storybook Docs

参数

Watch a video tutorial

参数是一组关于故事的静态命名元数据,通常用于控制 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 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

我们可以使用 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-webpack5, vue3-vite)
import type { Meta, StoryObj } from '@storybook/your-framework';
 
import { Button } from './Button';
 
const meta: Meta<typeof Button> = {
  component: Button,
};
export default meta;
 
type Story = StoryObj<typeof Button>;
 
export const OnDark: Story = {
  // 👇 Story-level parameters
  parameters: {
    backgrounds: {
      default: 'dark',
    },
  },
};

组件参数

¥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-webpack5, vue3-vite)
import type { Meta, StoryObj } from '@storybook/your-framework';
 
import { Button } from './Button';
 
const meta: Meta<typeof Button> = {
  component: Button,
  // 👇 Meta-level parameters
  parameters: {
    backgrounds: {
      default: 'dark',
    },
  },
};
export default meta;
 
type Story = StoryObj<typeof Button>;
 
export const Basic: Story = {};

全局参数

¥Global parameters

我们还可以通过 .storybook/preview.js 文件的 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.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3)
import { Preview } from '@storybook/your-renderer';
 
const preview: Preview = {
  parameters: {
    backgrounds: {
      values: [
        { name: 'light', value: '#fff' },
        { 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, 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.

如果你正在定义依赖于参数的 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.