Storybook is a great tool for documenting a component library, as it allows you to visually develop and test individual components in isolation. Storybook is an essential tool for developers building scalable and reusable component libraries, as it improves the efficiency and quality of UI development. Here some advantage using Storybook for your projects :

Setup

Here’s how you can set it up to document your component library:

  1. Install Storybook: If you haven't installed Storybook yet, you can do so by running the following command inside your project directory ( using npm ):

    npx storybook@latest init
    
  2. Create a Story for Each Component: For each component in your library, create a corresponding story file (with .stories.js or .stories.jsx extensions). These stories will showcase the different states and variations of your component. In this example, we are using Button component to create a stories. As you can see, there some stories is created. A story represent of 1 state of component.

    // src/components/atoms/Button/Button.stories.jsx
    
    import Button from '.';
    
    export default {
      component: Button,
      argTypes: {
        className: { control: 'text' },
      },
      tags: ['autodocs'],
      parameters: {
        layout: 'centered',
        backgrounds: {
          default: 'dark',
        },
      },
    };
    
    export const Primary = {
      args: {
        children: 'Primary Button',
        variant: 'primary',
      },
    };
    
    export const Secondary = {
      args: {
        children: 'Secondary Button',
        variant: 'secondary',
      },
    };
    
    export const LoadingState = {
      args: {
        children: 'Loading Button',
        variant: 'primary',
        isLoading: true,
      },
    };
    
    export const DisabledState = {
      args: {
        children: 'Disabled Button',
        variant: 'primary',
        isDisabled: true,
      },
    };
    	
    

    To load the styling for the components ( especially if you using Tailwind CSS to styling ), don’t forget to import it on .storybook/preview.js file.

    import './../src/styles/globals.css';
    
    /** @type { import('@storybook/react').Preview } */
    const preview = {
      parameters: {
        controls: {
          matchers: {
            color: /(background|color)$/i,
            date: /Date$/i,
          },
        },
      },
    };
    
    export default preview;
    
    

Stories configuration such a args, parameters and etc can be defined at the story, component and global level. It is a JSON serializable object composed of string keys with matching valid value types that can be passed into a component for your framework, just like you passing a props when using the component on your project. Here’s how to define at each level :

  1. Story Level

    These args will only apply to the story for which they are attached

    // src/components/atoms/Button/Button.stories.jsx
    
    import { Button } from './Button';
     
    export default {
      component: Button,
    };
     
    export const Primary = {
      args: {
        label: 'Primary Button',
        primary: true,
      },
    };
    
  2. Component Level

    This way will apply to all the component's stories unless you overwrite them.

    // src/components/atoms/Button/Button.stories.jsx
    
    import { Button } from './Button';
     
    export default {
      component: Button,
      args: {
        //👇 Now all Button stories will be primary.
        primary: true,
      },
    };
    
    export const Primary = {
      args: {
        label: 'Primary Button',
      },
    };
    
  3. Global Level

    This way will apply to every component's stories unless you overwrite them. To do so, define the args property in the default export of preview.js:

    // .storybook/preview.js
    
    export default {
      // The default value of the theme arg for all stories
      args: { theme: 'light' },
    };
    

args

A story is a component with a set of arguments that define how the component should render. “Args” are Storybook’s mechanism for defining those arguments in a single JavaScript object. Args can be used to dynamically change props, slots, styles, inputs, etc. Read more the args documentation.

parameters

Parameters are a set of static, named metadata about a story, typically used to control the behavior of Storybook features and addons.

Storybook only accepts a few parameters directly, here are some parameters that most likely we used on our projects.