StoryBook
Sun April 3, 2022 10:05 AM
安装
在项目根目录下执行。
1 | npx sb init |
需要在已存在的项目中使用。
Storybook needs to be installed into a project that is already setup with a framework. It will not work on an empty project.
运行
1 | npm run storybook |
开始
Component Story Format (CSF)
所有故事和组件都需要使用 ES 模块化,每个组件故事需由
default export和 至少一个named exports。默认导出包括
component、title、decorator和parameters等。其中
title为必选项(需全局唯一),其他为可选项,但是component是推荐选项,因为addon界面能根据组件属性自动生成表单控件。1
2
3
4
5
6
7
8
9
10// MyComponent.story.js
import MyComponent from './MyComponent';
export default {
title: 'Path/To/MyComponent',
component: MyComponent,
decorators: [ ... ],
parameters: { ... },
...
}每个具名导出默认表示一个故事。
为故事函数输入参数
1
2
3
4
5
6// Button.stories.js
export const Text = ({ label, onClick }) => <Button label={label} onClick={onClick} />;
Text.args = {
label: 'Hello',
onClick: action('clicked'),
};
故事写在哪里
挨着你的组件
1 | Button.js | ts |
如何去编写故事
1 | // Button.stories.js |
有什么问题?
对于较少的故事集,无伤大雅。当故事多起来后,就会变得冗余。
使用 args
1 | // Button.stories.js |