storybookに階層構造を追加する
背景
デフォルトでは1階層しかパスが切れない。
例えばAtomsのButton、MoleculesのFormがあったとして、理想的には以下のように整理したい。
- Atoms
- Button
- with text
- with some emoji
- Button
- Molecules
- Form
- ContactForm
- MailAddressForm
- Form
しかしデフォルトでは以下のようにするしかない。
- Button
- with text
- with some emoji
- Form
- ContactForm
- MailAddressForm
@storybook/addon-chaptersを使うと実現できるらしい。おれは以下の本を読んでこのアドオンの存在を知った。
Atomic Design ~堅牢で使いやすいUIを効率良く設計する | 五藤 佑典 | コンピュータ・IT | Kindleストア | Amazon
公式のaddon galleryでも紹介されている。
https://storybook.js.org/addons/addon-gallery/#chapters
階層構造と読んでいたがChapterと言うようだ。
@storybook/addon-chapters
install
npmを見る。
@storybook/addon-chapters - npm
storiesOf('Atoms', module) .addChapter('Button', chapter=>chapter .add('with text', () => <Button>with text</Button>) .add('with som emoji', () => <Button>with some emoji</Button>) ); storiesOf('Molecules', module) .addChapter('Form', chapter=>chapter .add( ...
というような感じで階層構造を追加出来た。
storybook-directory-chapters
components以下のdirectory構造をそのままstorybookにできるnpmパッケージもあったので試してみたが動かないようだ。 storybook-directory-chapters - npm
設定で module.exports
を使っているがimport
文も使っておりEJSとCJSの仕様がごちゃまぜになってる。module.exports
をexports default
に書き換えたが今度はcontext(...) is not a function
というエラーが出てしまい解消できなかった。