[Day12] Storybook - Writing Docs

DocsPage

DocsPage 是由 Storybook Docs 所提供的页面,无需任何的设定自动就会从 Stories 和元件中的 props, emitsslots 甚至是注解 (没错就是注解) 中解析聚合出基本的预设文档。

Storybook Docs 包含在 Essential Addons 中,所以用 CLI 建立的专案都会预设含有 Storybook Docs。

https://ithelp.ithome.com.tw/upload/images/20210927/20113487OAl2S2l2Dl.png

https://ithelp.ithome.com.tw/upload/images/20210927/20113487Sw94WBQv7C.png

ArgsTable

如果有看过上一篇 Storybook - Controls 的朋友一定会注意到, DocsPage 中的 ArgsTable 和 Controls 面板非常相似,因为事情上他们背後是使用相同的运作原理,所以 argsType 的设定会同时在 Controls 面板中以及 DocsPage 出现。

Customizing

ArgsTable 的内容预设是从元件以及 Story 中自动推断出来的,但 Storybook 也提供我们一些属性可以自己定义内容来让文件更丰富。

栏位 说明
name 属性名称
type.required 是否为必填属性,是的话会出现红色星号
description 属性说明
table.category 属性的分类
table.subcategory 属性的子分类
table.type.summary 属性类型的简短描述
table.type.detail 属性类型的描述
table.defaultValue.summary 属性预设值的简短描述
table.defaultValue.detail 属性预设值的描述

control | 查看完整列表

其中我特别挑 category 和 subcategory 出来说明,我们可以将类似的 args 分门别类,甚至细到可以再分一层子类别。

import MyButton from './Button.vue'

export default {
  title: 'Example/Button',
  component: MyButton,
	argTypes: {
	  backgroundColor: {
	    control: 'color',
	    table: {
	      category: 'Colors'
	    }
	  },
	  primary: {
	    table: {
	      category: 'Colors'
	    }
	  },
	  label: {
	    table: {
	      category: 'Text',
	      subcategory: 'Label'
	    }
	  },
	  size: {
	    table: {
	      category: 'Text',
	      subcategory: 'Size'
	    },
	    control: {
	      type: 'select',
	      options: ['small', 'medium', 'large']
	    }
	  },
	  click: {
	    table: {
	      category: 'Events'
	    }
	  }
	}
}

https://ithelp.ithome.com.tw/upload/images/20210927/20113487lh56BnTRKA.png

Description

我们可以为整份 DocsPage 写上补充说明

https://ithelp.ithome.com.tw/upload/images/20210927/20113487WXc2ykbPjQ.png

import MyButton from './Button.vue'

export default {
  title: 'Example/Button',
  component: MyButton,
  parameters: {
    docs: {
      description: {
        component: 'This is Button\'s description.',
      }
    }
  },
  ...
}

Subcomponents parameter

有些元件可能会在包裹着子元件,这时如果要想要将他们一起显示在同一个 DocsPage 中,可以透过subcomponents 属性。

// src/stories/Header.stories.js

import MyHeader from './Header.vue';
import MyButton from './Button.vue';

export default {
  title: 'Example/Header',
  component: MyHeader,
  subcomponents: { MyButton }
};

https://i.imgur.com/5rqiugG.gif

参考资料


今天的分享就到这边,如果大家对我分享的内容有兴趣欢迎点击追踪 & 订阅系列文章,如果对内容有任何疑问,或是文章内容有错误,都非常欢迎留言讨论或指教的!

明天要来分享的是 Storybook 主题的第六篇 MDX,那我们明天见!


<<:  13 出牌倒数要怎麽做才好?

>>:  ASP.NET MVC 从入门到放弃(Day22)-MVC新增资料介绍

[DAY 25] 估计学生战力

如同前几天所说 「估计战力」是可以依照想看的不同能力去做的 例如我们把题目先依照章节进行分类 再去看...

【DB】B tree B+ tree

从今天开始不讲 Leetcode 了除非有想到什麽还没点到。 後面要提一下对於其他知识点的准备, 毕...

[DAY 27]推特推送到discord频道

FF14里面有个系统叫时尚品监 每个礼拜都会有个主题穿搭风格,如果参加有超过80以上都会拿到还不错的...

Day28-机器学习(2) KNN

KNN简单说明 为一种监督学习的方法,其原理就好像物以类聚一样,相同的东西会聚在一起 我们可以设定一...

day30 : 写不完所有东西的最後一天

30天的最後一天,写到最後几天才发现有一些想分享的没有篇幅能写入了,所以今天我认为分享的内容偏实用的...