参加直播会话:周四,美国东部时间上午 11 点,Storybook 9 发布及 AMA 问答

Storybook 插件,用于显示组件的 README (支持 React 和 Vue)

在 Github 上查看

Storybook README 插件

注意:本 README 仅适用于版本 ^5.0.0。对于旧版本,请参阅 README_V4.md

所有旧版 api 在 ^5.0.0 及更高版本中应能正常工作。但 Vue 用户需要更改其导入路径,因为 Vue 命令已移至单独的文件夹。


Storybook README addon

本插件兼容以下框架

在线演示

特性

  • 自动生成 props 表(仅限 React)
  • 100% Markdown 支持
  • 代码高亮
  • 可以将文档添加到插件面板或 Story 周围
  • 接受多个 README(对 高阶组件 (hoc component) 有用 - 添加组件本身及其原始组件的 README)
  • 看起来像 Github 的 README
  • 支持 Vue 组件的 <docs/> 标签 (example-vue/components/MyButton/MyButton.vue)。

此外,它非常有用,因为大多数项目和组件都已经有 README.md 文件。现在可以轻松地将它们添加到您的 Storybook 中。

如果安装了 Storybook Info 插件,Story 会使用 .addWithInfo 方法添加。

安装

npm install --save-dev storybook-readme

yarn add --dev storybook-readme

React Storybook 的 Webpack 配置

无需操作 :)

但是如果你在使用带有 React 的 Typescript,你需要在你的 webpack 配置中添加 markdown-loader

{
  test: /\.md$/,
  use: [
    {
      loader: 'markdown-loader',
    }
  ]
}

Vue Storybook 的 Webpack 配置

仅适用于使用 <docs> 标签进行文档编写的 单文件组件

module.exports = storybookBaseConfig => {
  storybookBaseConfig.module.rules.push({
    resourceQuery: /blockType=docs/,
    use: ['storybook-readme/vue/docs-loader', 'html-loader', 'markdown-loader'],
  });
};

在 Vue 模块内部定义 <docs> 标签

<docs>
Docs inside vue module 
</docs>

<template>
  <button class="button">
    <slot></slot>
  </button>
</template>

使用它在 Story 中定义文档

import MyButton from '../components/MyButton/MyButton.vue';

storiesOf('Vue <docs>', module).addParameters({
  readme: {
    content: MyButton.__docs,
  },
});

设置

.storybook/addons.js 中注册插件

import 'storybook-readme/register';

注意:可以设置插件面板的标题

import registerWithPanelTitle from 'storybook-readme/registerWithPanelTitle';

registerWithPanelTitle('Docs');

.storybook/config.js 中添加装饰器

import { addDecorator, configure } from '@storybook/react';
import { addReadme } from 'storybook-readme';

// for Vue storybook
import { addReadme } from 'storybook-readme/vue'; // <---- vue subpackage

// for HTML storybook
import { addReadme } from 'storybook-readme/html'; // <---- html subpackage

addDecorator(addReadme);

function loadStories() {
  require('../stories/index.js');
}

configure(loadStories, module);

注意:对于 HTML Storybook,仅侧边栏文档可用。

用法

希望这非常简单。

import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';
import ButtonReadme from '../components/Button/README.md';

storiesOf('Buttons', module)
  .addDecorator(withKnobs)
  .addParameters({
    readme: {
      // Show readme before story
      content: ButtonReadme,
      // Show readme at the addons panel
      sidebar: ButtonReadme,
    },
  })
  .add('Button', () => <Button />);

可以覆盖 Story 的文档

import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';
import ButtonReadme from '../components/Button/README.md';

storiesOf('Buttons', module)
  .addDecorator(withKnobs)
  .addParameters({
    readme: {
      content: ButtonReadme,
      sidebar: ButtonReadme,
    },
  })
  .add('Button', () => <Button />)
  .add('Button', () => <Button />)
  .add('Button', () => <Button />, {
    readme: {
      // override docs
      content: CustomButtonReadme,
      sidebar: CustomButtonReadme,
    },
  });

完整选项列表

将应用于一系列 Story。

.addParameters({
    readme: {
      /**
       * Accepts string (markdown) or array of strings
       * string | Array<string>
       */
      content: Readme,

      /**
       * Accepts string (markdown) or array of strings
       * string | Array<string>
       */
      sidebar: Readme,

      /**
       * Enable / disable code highlighting for sidebar. true by default
       */
      highlightSidebar: true,

      /**
       * Enable / disable code highlighting for content. true by default
       */
      highlightContent: true,

      /**
       * Override theme values
       *
       * All theme values https://github.com/tuchk4/storybook-readme/blob/master/packages/storybook-readme/src/styles/githubMarkdownCss.js#L436

       */
      theme: {},

      /**
       * Prism code theme
       * Full list of theme https://github.com/PrismJS/prism-themes
       * To be used with storybook-readme, naming of the code theme should be used in one of these styles. https://github.com/tuchk4/storybook-readme/tree/master/packages/storybook-readme/src/styles/prismCodeThemes
       */
      codeTheme: 'github',

      /**
       * You can include prop tables locally here. See `propTables` example
       * for more information
       */
      includePropTables: [YourImportedReactComponent],

      /**
       * Wrapper for story. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      StoryPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for header docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      HeaderPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for footer docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      FooterPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for content and sidebar docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      DocPreview: ({ children}) => <div>{children}</div>
    },
  })

全局配置

将应用于所有 Story。注意:全局配置仅适用于内容文档(围绕 Story 的文档)。

import { addParameters } from '@storybook/react'; // or @storybook/vue for vuejs
import { configureReadme } from 'storybook-readme';

configureReadme({
  /**
   * Wrapper for story. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  StoryPreview: ({ children }) => <div>{children}</div>,

  /**
   * Wrapper for content and sidebar docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  DocPreview: ({ children }) => (
    <div style={{ background: '#000' }}> {children}</div>
  ),

  /**
   * Wrapper for header docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  HeaderPreview: ({ children }) => (
    <div style={{ background: 'red' }}>{children}</div>
  ),

  /**
   * Wrapper for footer docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  FooterPreview: ({ children }) => <div>{children}</div>,

  /**
   * Header docs in markdown format
   */
  header: '',

  /**
   * Footer docs in markdown format
   */
  footer: '',
});

addParameters({
  readme: {
    // You can set a code theme globally.
    codeTheme: 'github',

    // You can exclude prop tables globally here. See `propTables` example
    // for more information
    excludePropTables: [YourImportedReactComponent],
  },
});

Readme 占位符

  • <!-- STORY --> Story 的占位符
  • <!-- SOURCE --> Story 源代码的占位符
  • <!-- STORY_SOURCE --> Story 源代码的占位符
  • <!-- PROPS --> props 表的占位符。props 解析存在一些问题。详情请参阅 issue#137
  • <!-- STORY HIDE START -->, <!-- STORY HIDE END --> 这些标签之间的内容不会显示在 Story 中
Button variants could be imported separately.

\`\`\`js import { OutlinedButton, ContainedButton, TextButton } from 'Button'; \`\`\`

<!-- PROPS -->

Example:

<!-- STORY -->
<!-- SOURCE -->

<!-- STORY HIDE START -->

content here won't be shown in stories

<!-- STORY HIDE END -->

Some docs after story

表情符号

在冒号之间使用短代码将表情符号插入文档。例如

这是一个火箭 :rocket

这是一个火箭 :rocket

所有短代码列表可以在 EmojipediaGist/rxaviers 找到

  • :rocket
  • :grinning
  • :monkey

欢迎随时提出新特性建议或报告 Bug :)

v4 版本的 API。

旧版 API 的完整文档在 README_V4.md

TL;DR

接受 README 或 README 数组,格式为 Markdown。当开发高阶组件并希望在原始组件文档之外添加多个 README 时,使用多个 README 非常有用。

withReadme

在插件面板中渲染 README。

import { withReadme } from 'storybook-readme';
import withReadme from 'storybook-readme/with-readme';

// as HigherOrder Component
storiesOf('Button', module).add(
  'Default',
  withReadme(ButtonReadme, () => <Button />),
);
// as Decorator
storiesOf('Button', module)
  .addDecorator(withReadme(ButtonReadme))
  .add('Default', () => <Button />);

withDocs

在 Story 周围渲染 README。

import { withDocs } from 'storybook-readme';
import withDocs from 'storybook-readme/with-docs';

// as HigherOrder Component
storiesOf('Button', module).add(
  'Default',
  withDocs(ButtonReadme, () => <Button />),
);
// as Decorator
storiesOf('Button', module)
  .addDecorator(withDocs(ButtonReadme))
  .add('Default', () => <Button />);

doc

在主框架中渲染 README,不包含 Story。

import { doc } from 'storybook-readme';

storiesOf('Button', module).add('Default', () => doc(ButtonReadme));