storybook-addon-code
一個 Storybook 擴充功能,可以在 Storybook 中為您的故事在 Storybook 面板中展示程式碼範例。
開始使用
npm i --save-dev storybook-addon-code
使用方法
在您的 Storybook 設定中建立一個名為 addons.js
的檔案。
將以下內容新增至其中
import * as CodeAddon from '../src/register';
CodeAddon.setTabs(
[{ label: 'Sass', type: 'sass' }, {label: 'TypeScript', type: 'typescript'}]
);
setTab 函式接受一個類似 {label: 'Sass', type:'sass'} 的物件,或者如果您想要有多個分頁,您可以傳遞一個包含多個物件的陣列。標籤將會顯示在 Storybook 面板中。
然後像這樣編寫您的故事
import { storiesOf } from '@storybook/react';
import withCode from 'storybook-addon-code';
import Button from './Button';
const styleFile = require('raw-loader!./style.scss');
const typescriptFile = require('./test.tsx');
storiesOf('Button', module)
.addDecorator(withCode(typescriptFile, 'typescript'))
.addDecorator(withCode(styleFile, 'sass'))
.add('with text', () =>
<Button onClick={action('clicked')}>Hello Button</Button>
)
withCode 函式可用的格式清單
- clike (withCode(YourCFile, 'clike'))
- css (withCode(YourCssFile, 'css'))
- html (withCode(YourHtmlFile, 'html'))
- js | javascript (withCode(YourJavascriptFile, 'js'))
- markup (withCode(YourMarkupFile, 'js'))
- mathml (withCode(YourMatHmlFile, 'mathml'))
- sass (withCode(YourSassFile, 'sass'))
- svg (withCode(YourSvgFile, 'svg'))
- ts (withCode(YourTsFile, 'ts'))
- typescript (withCode(YourTypescriptFile, 'typescript'))
- xml (withCode(YourXmlFile, 'xml'))
請查看 這個範例 故事,以了解更多關於
withCode
API 的資訊