Docs
Storybook Docs

插件类型

每个 Storybook 插件分为两大类,基于 UI 或预设。每种类型的插件功能都记录在此处。在创建插件时使用此作为参考。

¥Each Storybook addon is classified into two general categories, UI-based or Presets. Each type of addons feature is documented here. Use this as a reference when creating your addon.

基于 UI 插件

¥UI-based addons

基于 UI 的插件允许你使用以下元素自定义 Storybook 的 UI。

¥UI-based addons allow you to customize Storybook's UI with the following elements.

面板

¥Panels

面板插件允许你在 Storybook 的插件面板中添加自己的 UI。这是生态系统中最常见的插件类型。例如,官方的 @storybook/actions@storybook/a11y 就使用这种模式。

¥Panel addons allow you to add your own UI in Storybook's addon panel. This is the most common type of addon in the ecosystem. For example, the official @storybook/actions and @storybook/a11y use this pattern.

Storybook panel

使用此样板代码将新的 Panel 添加到 Storybook 的 UI:

¥Use this boilerplate code to add a new Panel to Storybook's UI:

addon-panel/manager.js
import React from 'react';
 
import { AddonPanel } from '@storybook/components';
 
import { useGlobals, addons, types } from '@storybook/manager-api';
 
addons.register('my/panel', () => {
  addons.add('my-panel-addon/panel', {
    title: 'Example Storybook panel',
    //👇 Sets the type of UI element in Storybook
    type: types.PANEL,
    render: ({ active }) => (
      <AddonPanel active={active}>
        <h2>I'm a panel addon in Storybook</h2>
      </AddonPanel>
    ),
  });
});

工具栏

¥Toolbars

工具栏插件允许你在 Storybook 的工具栏中添加自己的自定义工具。例如,官方的 @storybook/backgrounds@storybook/addon-outline 就使用这种模式。

¥Toolbar addons allow you to add your own custom tools in Storybook's Toolbar. For example, the official @storybook/backgrounds and the @storybook/addon-outline use this pattern.

Storybook toolbar addon

使用此样板代码将新的 button 添加到 Storybook 的工具栏:

¥Use this boilerplate code to add a new button to Storybook's Toolbar:

addon-toolbar/manager.js
import React from 'react';
 
import { addons, types } from '@storybook/manager-api';
import { IconButton } from '@storybook/components';
import { OutlineIcon } from '@storybook/icons';
 
addons.register('my-addon', () => {
  addons.add('my-addon/toolbar', {
    title: 'Example Storybook toolbar',
    //👇 Sets the type of UI element in Storybook
    type: types.TOOL,
    //👇 Shows the Toolbar UI element if the story canvas is being viewed
    match: ({ tabId, viewMode }) => !tabId && viewMode === 'story',
    render: ({ active }) => (
      <IconButton active={active} title="Show a Storybook toolbar">
        <OutlineIcon />
      </IconButton>
    ),
  });
});

match 属性允许你有条件地渲染工具栏插件 基于当前视图

¥The match property allows you to conditionally render your toolbar addon, based on the current view.


示例中使用的 icon 元素从 @storybook/components 包中加载图标。有关你可以使用的图标列表,请参阅 此处

¥The icon element used in the example loads the icons from the @storybook/components package. See here for the list of available icons that you can use.

标签

¥Tabs

标签插件允许你在 Storybook 中创建自己的自定义标签。例如,官方的 @storybook/addon-docs 就使用这种模式。

¥Tab addons allow you to create your own custom tabs in Storybook. For example, the official @storybook/addon-docs uses this pattern.

Storybook tab addon

使用此样板代码将新的 Tab 添加到 Storybook 的 UI:

¥Use this boilerplate code to add a new Tab to Storybook's UI:

addon-tab/manager.js
import React from 'react';
 
import { addons, types } from '@storybook/manager-api';
 
addons.register('my-addon', () => {
  addons.add('my-addon/tab', {
    type: types.TAB,
    title: 'Example Storybook tab',
    render: () => (
      <div>
        <h2>I'm a tabbed addon in Storybook</h2>
      </div>
    ),
  });
});

了解如何编写包含这些 UI 元素 此处 的自己的插件。

¥Learn how to write your own addon that includes these UI elements here.

预设插件

¥Preset addons

Storybook 预设插件是 babelwebpackaddons 配置的分组集合,用于集成 Storybook 和其他技术。例如官方 preset-create-react-app

¥Storybook preset addons are grouped collections of babel, webpack, and addons configurations to integrate Storybook and other technologies. For example the official preset-create-react-app.

在编写你自己的预设插件时使用此样板代码。

¥Use this boilerplate code while writing your own preset addon.

.storybook/my-preset.js
export default {
  managerWebpack: async (config, options) => {
    // Update config here
    return config;
  },
  webpackFinal: async (config, options) => {
    return config;
  },
  babel: async (config, options) => {
    return config;
  },
};

了解有关 Storybook 插件生态系统的更多信息

¥Learn more about the Storybook addon ecosystem