storybook让组件自动形成好看的文档~

简介: storybook让组件自动形成好看的文档~

storybook让组件自动形成好看的文档~


storybook 是什么

storybook 是一个 JS 库,可以在已有项目,在无需修改业务逻辑的情況下,给组件自动形成文档,可很好的展示属性和功能。

示例

怎么安装

在已有的项目里,一行命令安装

npx storybook init

此时项目里增加了一些文件:

  • .storybook文件夹,里面是配置
  • src/stories,展示组件,我们的重点
  • package.json会多两个命令,
  • "storybook": "start-storybook -p 6006 -s public",
  • "build-storybook": "build-storybook -s public"

先运行起来,看看

npm run story

开始写 story

第一步,像平时一样,写组件。

比如这边,来个Button.jsx

import PropTypes from 'prop-types';
const Button = ({ label, backgroundColor = 'red', onClick }) => {
  return (
    <button style={{ backgroundColor, color: '#fff' }} onClick={onClick}>
      {label}
    </button>
  );
};
Button.propTypes = {
  label: PropTypes.string,
  backgroundColor: PropTypes.string,
  onClick: PropTypes.func,
};
export default Button;

第二步,新建Button.stores.jsx

import Button from './Button';
export default {
  title: 'Button', // 标题
  component: Button, // 哪个组件
};
// 组件展示的例子
export const Red = () => <Button label="red" backgroundColor="red" />;
export const Blue = () => <Button label="blue" backgroundColor="blue" />;

title有路径的话,自动在左侧菜单形成文件夹

此时,重新访问网址

网络异常,图片无法展示
|
网络异常,图片无法展示
|
为增强可读性,我们应该为组件编写 propTypes

增加 - Controls 动态修改属性显示

展示的两个例子,并不能修改,其实可以升级,在页面随时编辑,随时显示。 其实很简单,将上面的代码微改造下

import Button from './Button';
export default {
  title: 'Button', // 标题
  component: Button, // 哪个组件
};
// 先写模版
const Template = (args) => <Button {...args} />;
// 写例子,例子的模版都是一样的,只是属性不一样
// Template.bind({}) is a standard JavaScript technique for making a copy of a function. We copy the Template so each exported story can set its own properties on it.
export const Red = Template.bind({});
Red.args = {
  label: 'red',
  backgroundColor: 'red',
};
export const Blue = Template.bind({});
Blue.args = {
  label: 'blue',
  backgroundColor: 'blue',
};

网络异常,图片无法展示
|

增加 - Actions 事件用法

上面都是属性,现在增加事件onClick

const Button = ({ label, backgroundColor = 'red', onClick }) => {
  return (
    <button style={{ backgroundColor, color: '#fff' }} onClick={onClick}>
      {label}
    </button>
  );
};
Button.propTypes = {
  label: PropTypes.string,
  backgroundColor: PropTypes.string,
  onClick: PropTypes.func,
};
export default Button;

看下配置文件,发现当属性以on开头的话,会自动识别为action如果不以on开头的话,需要额外说明。

const Button = ({ label, backgroundColor = 'red', handleClick }) => {
  return (
    <button style={{ backgroundColor, color: '#fff' }} onClick={handleClick}>
      {label}
    </button>
  );
};
Button.propTypes = {
  label: PropTypes.string,
  backgroundColor: PropTypes.string,
  handleClick: PropTypes.func,
};
export default Button;

想要示例里,按钮能绑定事件的话

export default {
  title: 'Button', // 标题
  component: Button, // 哪个组件
  argTypes: { handleClick: { action: 'handleClick' } },
};

argTypes 的具体文档

增加 - children 的处理

这边简单增加一个布局组件,体验下children的处理。

import PropTypes from 'prop-types';
const Stack = ({ children, direction = 'row', spacing = 2 }) => (
  <div
    style={{ display: 'flex', gap: spacing + 'px', flexDirection: direction }}
  >
      {children} 
  </div>
);
Stack.propTypes = {
  spacing: PropTypes.number,
  direction: PropTypes.oneOf(['row', 'column']),
};
export default Stack;

感觉还是很普通的。 来个文档:

import Stack from './Stack';
export default {
  title: 'Stack', // 标题
  component: Stack, // 哪个组件
  argTypes: {
    countOfChild: { type: 'number', defaultValue: 3 },
  },
};
// 先写模版
const style = {
  display: 'flex',
  width: '50px',
  height: '50px',
  alignItems: 'center',
  justifyContent: 'center',
  backgroundColor: '#f69',
  color: '#fff',
};
const Template = ({ countOfChild, ...args }) => (
  <Stack {...args}>
    {new Array(countOfChild).fill('').map((_, index) => (
      <div style={style} key={index}>
        {index + 1}
      </div>
    ))}
  </Stack>
);
export const Horizontal = Template.bind({});
Horizontal.args = {
  direction: 'row',
  spacing: 10,
};
export const Vertical = Template.bind({});
Vertical.args = {
  direction: 'column',
  spacing: 10,
};

argTypes 的属性,只是添加到示例里面,来方便示例的展示,跟真实的组件属性没有关系。

网络异常,图片无法展示
|
网络异常,图片无法展示
|

Vue 类项目添加 storybook

上面步骤基本一致,唯一不同的就是组件的写法,不一致。 Button.vue:

<template>
   <button
    type="button"
    @click="onClick"
    :style="{ backgroundColor, color: '#fff' }"
  >
       {{ label }}  
  </button>
</template>
<script>
export default {
  name: 'Button',
  props: {
    label: {
      type: String,
      required: true,
    },
    backgroundColor: {
      type: String,
      default: '#f69',
    },
  },
  methods: {
    onClick() {
      this.$emit('onClick');
    },
  },
};
</script>

Button.stories.js:

import Button from './Button';
// More on default export: https://storybook.js.org/docs/vue/writing-stories/introduction#default-export
export default {
  title: 'Button',
  component: Button
};
// More on component templates: https://storybook.js.org/docs/vue/writing-stories/introduction#using-args
const Template = (args, { argTypes }) => ({
  props: Object.keys(argTypes),
  components: { Button },
  template: '<Button @onClick="onClick" v-bind="$props" />'
});
export const Red = Template.bind({});
Red.args = {
  label: 'Red',
  backgroundColor: 'red'
};
export const Blue = Template.bind({});
Blue.args = {
  label: 'Blue',
  backgroundColor: 'blue'
};

Stories 部署访问

先运行命令:

# npm run build-storybook
yarn build-storybook

根目录下会多一个文件夹storybook-static,部署即可,本地可以进到目录,然后anywhere 9000即可访问

其他借鉴的例子

  • vue 系列
  • react 系列点左上角的 logo,可以看到源码。 抛砖引玉,实践中,会慢慢熟练 storybook,希望能帮助大家,感谢 ❤️~

引用

目录
相关文章
|
前端开发 JavaScript API
vue3-ts-storybook:理解storybook、实践 / 前端组件库
vue3-ts-storybook:理解storybook、实践 / 前端组件库
1084 0
|
算法 Linux API
【Linux系统编程】Linux下删除文件的 API方式以及文件删除机制差异
【Linux系统编程】Linux下删除文件的 API方式以及文件删除机制差异
696 0
|
UED
element el-cascader动态加载数据 (多级联动,落地方案)
最近需要用到element ui的这个插件做地区的四级联动,但是碰了一些问题: 官网的说明太泛泛然,不易看懂 网上的教程乱七八糟,代码一堆一堆的 看这篇就对了!!!
2828 0
element el-cascader动态加载数据 (多级联动,落地方案)
|
2月前
|
设计模式 人工智能 JSON
Agent Skill规范、构建与设计模式
文章从 Skill 的规范格式、三层渐进式加载机制、模型驱动触发逻辑出发,深入解析 Skill-Creator 的工程化开发范式。(文章内容基于作者个人技术实践与独立思考,旨在分享经验,仅代表个人观点。)
2973 3
Agent Skill规范、构建与设计模式
|
4月前
|
弹性计算 Ubuntu Linux
阿里云环境怎么使用代理ip
本文详解阿里云ECS上代理IP的两种核心用法:一是ECS通过外部代理访问网络(支持HTTP/HTTPS/SOCKS5及认证配置),二是ECS自建代理服务器(Squid或Dante),涵盖Linux/Windows环境变量设置、SDK代理调用、安全组开放、服务启停等完整操作步骤,兼顾安全与合规。
|
前端开发
如何在React Router中进行嵌套路由配置?
如何在React Router中进行嵌套路由配置?
717 57
|
存储 JavaScript API
Vue3实现图片懒加载及自定义懒加载指令
Vue3实现图片懒加载及自定义懒加载指令
1665 1
|
小程序 JavaScript 前端开发
7.9K star!跨平台开发从未如此简单,这个开源框架让APP开发效率飙升!
Lynx 是一个革命性的跨平台开发框架,使用 TypeScript 开发即可同时构建 iOS、Android 和 Web 应用。通过创新的布局引擎和原生渲染技术,让开发者用一套代码实现三端同屏效果,大大提升整体的开发效率!
953 0
|
设计模式 算法
工厂模式与策略模式的区别
【8月更文挑战第22天】
468 2
工厂模式与策略模式的区别
|
前端开发
React给antd中TreeSelect组件左侧加自定义图标icon
本文介绍了如何在React中为Ant Design的TreeSelect组件的每个树节点添加自定义图标,并解决了因缺少key属性而导致的警告问题,展示了如何通过递归函数处理treeData数据并为每个节点添加图标。
947 2
React给antd中TreeSelect组件左侧加自定义图标icon