Android App

TechLogic
As per official site, Storybook is a user interface development environment and playground for UI components which enables developers to create components independently and showcase components interactively in an isolated development environment.
As official site of storybook says, Storybook is an open source tool for developing UI components in isolation for React, Vue, and Angular, React-Native. It makes building stunning UIs organised and efficient. So, we can implement a story book with any component.
For React, we just need to do or follow some setup kind of things as mentioned below:
a) Setup with React project
You may follow official doc to use quick start guide to setup your project for Storybook. But, here we just go with manual setup for better understanding:
Assuming we already have reactreact-dom@babel/core, and babel-loader in our dependencies i.e. in package.json file. If not then we need this dependencies for storybook as storybook have peer dependencies on this modules.
For adding @storybook/react to react project we need to run following command:
  1. npm install @storybook/react --save-dev
In order to start the storybook we need to add task in our package.json . This will execute as command npm run storybook to start on a random open port in dev-mode.
  1. {
  2. "scripts": {
  3. "storybook": "start-storybook"
  4. }
  5. }
If we want to add any specific port for storybook then we just need to mention in above script of storybook along with ‘start-storybook’.
e,g `start-storybook -p 6006`. This will start storybook on port 6006
b) Configuration of story book with React
Now, lets create a new directory called .storybook in our project root. Then add config file named as ‘config.js’ file inside this directory. This config file will help to tell Storybook where to find stories.
  1. import { configure } from '@storybook/react';
  2. import './addons'; // This file will contain all addon if we need
  3. import { setDefaults } from '@storybook/addon-info';
  4. import { setOptions } from '@storybook/addon-options';
  5.  
  6. // addon-info
  7. setDefaults({ styles: { header: { h1: { color: 'red' } } } });
  8. // addon-info
  9. setOptions({ name: 'My Story Book', addonPanelInRight: true, sortStoriesByKind: true });
  10. //load all stories
  11. function loadStories() {
  12. require('../stories/index.js');
  13. // require('../stories/commonComponents/index.js');
  14. // require('../stories/sampleComponents/index.js');
  15. // We can add here all stories which we need
  16. }
  17. configure(loadStories, module);
Or we can also add context module to find and load all files which ending with `.stories.js` in config file:
A context module contains references to all modules in that directory that can be required with a request matching the regular expression. The context module contains a map which translates requests to module ids.
  1. function loadStories() {
  2. // Here we find all files in the stories folder and descending folders ending with `.stories.js`
  3. const req = require.context('../stories', true, /\.stories\.js$/);
  4. req.keys().forEach(filename => req(filename));
  5. }
Its totally depend on us to decide where to place stories, we can co-locate them with source files, or place them in an other directory.
c) Write (sample) story for Button component to execute story book with React
Now lets create a ../stories/index.js file and write our story as mentioned below:
  1. import React from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { action } from '@storybook/addon-actions';
  4. import { Button } from './button'; // button component
  5.  
  6. const props = { className: 'my-default-button', onClick: action('onClick') };
  7. const customProps = { className: 'my-custom-button', onClick: action('onClick') };
  8. const redButtonProps = { className: 'my-red-button', onClick: action('onClick') };
  9.  
  10. storiesOf('Button', module)
  11. .add('normal text Button', () => (
  12. <Button {...props}> Click Me! </Button>
  13. ))
  14. .add('with custom class', () => (
  15. <Button {...customProps}> (Custom Button)Click Me!</Button>
  16. ))
  17. .add('with custom left icon', () => (
  18. <Button {...{...customProps, leftIcon: 'icon-eye'}}> Click Me or Icon!</Button>
  19. ))
  20. .add('with red border class', () => (
  21. <Button {...redButtonProps}> I'm RED, Click Me!</Button>
  22. ));
  23.  
  24. // Each story is a single state of our component. We will see following four buttons in storybook:
  25.  
  26. // Button
  27. // ├── normal text Button
  28. // ├── with custom class
  29. // ├── with custom left icon
  30. // └── with red border class
d) Run our Storybook
Now we can finally run and test our stories with npm run storybook
  1. we expect to start storybook and see following output on a http:localhost:xxxx --->
  2. Button
  3. ├── normal text Button
  4. ├── with custom class
  5. ├── with custom left icon
  6. └── with red border class
Thanks!





Post a Comment