如何添加说明文档
文档内容基于 Storybook 8.6 版本的特性和功能编写,所提供示例代码为 React 或 Typescript内容。
每一段文字,都是与时间对话的桥梁,承载着智慧与经验的传承。
在Storybook
中, 有两种方式可以为文档添加丰富的图文介绍:
- 使用
Autodocs
自动生成的基础模板,通过Meta配置的parameters.docs
属性,为组件添加介绍内容; - 使用
MDX
为每个组件创建自定义的文档页面。
Autodocs
Autodocs
是Storybook
的一个功能,它通过读取Meta
中parameters.docs
属性内容自动生成组件文档。属性内容请参考:
附:parameters.docs属性介绍。
文件目录
components/
├─ Button/
│ ├─ Button.tsx
│ ├─ Button.stories.tsx
组件代码
Button.tsx组件代码如下:
type ButtonProps = {
/** 按钮文字 */
label: string;
/** 按钮是否禁用 */
disabled?: boolean;
/** 按钮大小 */
size?: 'small' | 'medium' | 'large';
};
const Button = ({ label, disabled, size }: ButtonProps) => (
<button disabled={disabled} className={`btn-${size}`}>
{label}
</button>
);
export default Button;
Story代码
Button.stories.tsx内容如下:
import type { Meta, StoryObj } from '@storybook/react';
import Button from './Button';
import type ButtonProps from './Button';
export default {
title: 'Components/Button',
component: Button,
tags: ['autodocs'],
parameters: {
docs: {
subtitle: '按钮组件使用文档',
description: {
component: '自定义按钮组件,支持多种大小和样式切换,具体效果请选择下方Props参数进行查看。',
},
},
}
} as Meta<ButtonProps>;
type Story = StoryObj<typeof ButtonProps>;
export const Primary: Story = {};
运行效果
Autodocs
使用起来会比较方便,但是对于一些复杂的组件,Autodocs
生成的文档内容可能不够完善,这时候就可以使用MDX
为每个组件创建自定义的文档页面。
MDX
MDX
是Storybook
的一个功能,它允许我们在文档中使用Markdown
和JSX
,从而可以更灵 活地创建组件文档。通过MDX
,我们可以在文档中嵌入组件、添加交互式示例、使用自定义布局等。
文件目录
components/
├─ Button/
│ ├─ Button.tsx
│ ├─ Button.stories.tsx
│ ├─ Button.mdx
Story代码
Button.stories.tsx内容如下:
import type { Meta, StoryObj } from '@storybook/react';
import Button from './Button';
import type ButtonProps from './Button';
export default {
title: 'Components/Button',
component: Button
} as Meta<ButtonProps>;
export type Story = StoryObj<ButtonProps>;
export const Primary: Story = {
name:'Primary',
args: {
children: 'Small Button',
size: 'small',
},
}
export const Medium: Story = {
name:'Medium',
args: {
children: 'Medium Button',
size: 'medium',
},
};
export const Large: Story = {
args: {
children: 'Large Button',
size: 'large',
},
}
详细文档
在Button.mdx中添加如下内容:
import { ArgTypes, Canvas, Meta } from '@storybook/addon-docs';
import Button, { Primary,Medium, Large} from './index.stories';
<Meta of={Button} />
# Button 组件文档
为了帮助开发者更好地使用`Button`组件,我们提供了详细的文档说明。`Button`组件是一个简单的按钮,为了兼容多尺寸设备,我们提供了多种尺寸的按钮。
+ `Small`:适用于小型操作,通常用于表单或小型界面。
+ `Medium`:适用于PC操作,通常用于PC项目。
+ `Large`:适用于大屏设备,通常用于大屏项目。
## Small 尺寸
`Small`尺寸的按钮适用于小型操作,通常用于表单或小型界面。
<Canvas of={Primary} />
## Medium 尺寸
`Medium`尺寸的按钮适用于PC操作,通常用于PC项目。
<Canvas of={Medium}/>
## Large 尺寸
`Large`尺寸的按钮适用于大屏设备,通常用于大屏项目。
<Canvas of={Large}/>
## Props
<ArgTypes />
Meta 块用于定义文档在侧边栏中的位置。在这个例子中,它被放置在 Checkbox 的故事旁边。默认情况下,文档的侧边栏节点标题为 “Docs”,但可以通过传递 name 属性进行自定义(例如:<Meta of={CheckboxStories} name="Info"/> )。如果你希望将文档节点放置在导航层级中的任意位置,可以使用 title 属性(例如:<Meta title="path/to/node"/> )。
运行效果:
在MDX
中,我们可以使用Canvas
组件来嵌入故事(Story),并通过of
属性指定要渲染的故事。我们还可以使用ArgTypes
组件来展示组件的参数(Props)表格。同时,Markdown支持我们为文档添加更丰富的内容,如表格、图片、视频等。
参考资料
附:
parameters.docs
属性介绍:
属性名 | 类型 | 说明 |
---|---|---|
argTypes | ArgTypesBlockParameters | ArgTypes部分配置,仅包含exclude、include、of、sort |
canvas | CanvasBlockParameters | 画布配置 |
controls | ControlsBlockParameters | 控制面板配置 |
description | DescriptionBlockParameters | Story描述 |
disable | boolean | 移除插件面板并禁用插件的行为 |
page | unknown | 用Storybook 使用的默认文档页面模板替换为你自己的模板 |
source | SourceBlockParameters | 配置在文档页面中显示时的
|