react-styleguidist是一個基于JSDOC可以幫助react項目快速建構項目文檔的一個插件。
一、簡單入門
1.1 環境準備
準備一個新鮮的react項目(非必需)
npx create-react-app react-app
添加react-styleguidist
npm install
在package.json添加啟動腳本
"scripts": {
...
"styleguide": "styleguidist server",
"styleguide:build": "styleguidist build"
}
1.2 寫一個button元件
在
src/components/Button
下建立兩個檔案:index.js、index.css
// index.js
import React from 'react';
import PropTypes from 'prop-types';
import './Button.css';
/**
* The only true button.
*/
export default function Button({ color, size, onClick, disabled, children }) {
const styles = {
color,
fontSize: Button.sizes[size],
};
return (
<button className="button" style={styles} onClick={onClick} disabled={disabled}>
{children}
</button>
);
}
Button.propTypes = {
/** Button label */
children: PropTypes.node.isRequired,
/** The color for the button */
color: PropTypes.string,
/** The size of the button */
size: PropTypes.oneOf(['small', 'normal', 'large']),
/** Disable button */
disabled: PropTypes.bool,
/** Gets called when the user clicks on the button */
onClick: PropTypes.func,
};
Button.defaultProps = {
color: '#333',
size: 'normal',
onClick: event => {
// eslint-disable-next-line no-console
console.log('You have clicked me!', event.target);
},
};
Button.sizes = {
small: '10px',
normal: '14px',
large: '18px',
};
// index.css
.button {
padding: 0.5em 1.5em;
color: #666;
background-color: #fff;
border: 1px solid currentColor;
border-radius: 0.3em;
text-align: center;
vertical-align: middle;
cursor: pointer;
}
.button[disabled] {
opacity: 0.35;
cursor: default;
}
.button + .button {
margin-left: 8px;
}
.checks {
background-image: linear-gradient(45deg, #f5f5f5 25%, transparent 25%),
linear-gradient(-45deg, #f5f5f5 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #f5f5f5 75%),
linear-gradient(-45deg, transparent 75%, #f5f5f5 75%);
background-size: 16px 16px;
background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
}
1.3 生成文檔
運作
yarn styleguide
就可以啟動文檔伺服器了。打開
localhost:6060
:

styleguide文檔
1.4 總結
styleguide會預設為
src/components/**/*.js
的js檔案生成文檔。通過生成的
styleguide
文檔我們可以看出,styleguide讀取了
注解
、
Button.propTypes
和
Button.defaultProps
為我們生成了元件文檔,并且将propTypes的注解放到description中,可謂是非常貼心了。
注意:styleguide讀取的是注解,不是注釋語句
二、Documenting components
經過第一節,styleguide已經幫助我們生成了元件的使用文檔。隻有一些props的文檔顯然是不夠的,除了JSDoc styleguide還支援markdown來生成我們自定義的文檔。在
src/components/Button
檔案夾下建立
Readme.md
:
// Readme.md
React component example:
```js
<Button size="large">Push Me</Button>
```
通過props配置Code wrapper樣式:
```js { "props": { "className": "checks" } }
<Button>I’m transparent!</Button>
```
disable `view code` 按鈕:
```jsx noeditor
<Button>Push Me</Button>
```
`static` modifier支援js代碼高亮:
```jsx static
import React from 'react';
```
其他語言高亮:
```html
<Button size="large">Push Me</Button>
```
_支援所有的_ [Markdown](http://daringfireball.net/projects/markdown/) _文法_.
重新整理
localhost:6060
就可以看到我們組建的樣例和自定義文檔了。而且,我們還可以點選
VIEW CODE
來實時更新example代碼,這樣就可以面向文檔程式設計了。更多的styleguide的知識可在官方文檔按需學習,高階用法請參考Cookbook。
。
三、配置
我們之前啟動都styleguide server都是使用了styleguide的預設配置,styleguide預設會讀取項目根目錄下的
styleguide.config.js
來替換預設配置,當然也可以使用
--config
來指定配置檔案。官方文檔--配置。
四、TS
styleguide也支援了Typescript,隻需要添加
react-docgen
插件就可以了。
安裝插件
npm i -D react-docgen-typescript
配置插件,在
styleguide.config.js
中添加:
module.exports = {
...
resolver: require('react-docgen').resolver.findAllComponentDefinitions,
propsParser: require('react-docgen-typescript').parse,
webpackConfig: require('./config/webpack.config')
}
差別于普通的react,ts的props是通過
interface
來聲明的。是以在ts中就不需要寫propTypes了。
作者:zhiqiangx