Storybook
A UI development tool to develop and test components isolated.
Installation
To install Storybook you will only need to run:
npm i --save-dev @storybook/react @types/storybook__react awesome-typescript-loader react-docgen-typescript-webpack-pluginSee their official web page for more.
Plugins
In Storybook we have something called addons.
Recommended plugins
actions: can be used to display data received by event handlers.
info: will show additional information for stories.
knobs: allow you to edit React props dynamically using the Storybook UI.
jsx: shows the JSX of the component.
All of them can be installed by running:
npm i --save-dev @storybook/addon-actions @types/storybook__addon-actions
npm i --save-dev @storybook/addon-info @types/storybook__addon-info
npm i --save-dev @storybook/addon-knobs @types/storybook__addon-knobs
npm i --save-dev storybook-addon-jsxConfiguration
We will proceed to create a config folder only for Storybook in the root of our project. It will look like this:
addons.js file will import all the addons we need for Storybook:
The config.js file will contain all the configuration. Is the one responsible to load all the stories:
Finally, we will need to set our webpack.config.js file. Because we are using TypeScript, our info addon will need to import a plugin called react-docgen-typescript-webpack-plugin:
JSX addon does not have types. TypeScript will complain once you add it to a component.
To solve this, create a new dile inside your project in the following path src/storybook.d.ts:
Component example
In our application we have a button component:
We don't need to add a decorator here for knobs because we already set this as default in our config.js file:
However, if we want to use the JSX addon, we will need to add a decorator in our story called addWithJSX:
Run Storybook
We can add a command in our package.json: