Skip to content
Cover image for "Getting to Know Storybook Framework for Developing and Testing UI Components" - Blog post by Muhammad Rivki about Tutorial

Getting to Know Storybook Framework for Developing and Testing UI Components

Written on β€’ 3 min read

What is Storybook Why use Storybook Installing Storybook in your frontend repository Adding UI components to Storybook Running Storybook Advantages of using Storybook in your frontend repository Conclusion

What is Storybook

Storybook is an open-source framework used to develop and test UI components separately from the main application. Using Storybook, we can easily develop, test, and display UI components without running the main application.

Storybook can be used with various frontend technologies like React, Vue, Angular, etc. Additionally, Storybook also supports several key features like:

  • Automatic documentation of UI components
  • Preview UI components on different devices and screen sizes
  • Integration with various tools like Jest, Enzyme, etc.
  • Support for various addons that can add new features to Storybook.

Storybook is very useful for frontend development teams in developing and testing UI components effectively and in a structured way.

Why Use Storybook

There are several reasons for using Storybook in frontend application development, including:

  1. Separate UI component development and testing from the main application. With Storybook, we can develop and test UI components separately from the main application. This is very useful for reducing dependencies between parts of the application and speeding up the development process.

  2. Automatic documentation of UI components. Storybook provides a feature to automatically display documentation for UI components. You can add descriptions, usage examples, and more in the .stories.tsx file, which will be displayed on the Storybook documentation page.

  3. Preview UI components on different devices and screen sizes. With Storybook, you can preview UI components on different devices and screen sizes. This is very useful to ensure the developed UI components display well on various devices and screen sizes.

Installing Storybook in Your Frontend Repository

To install Storybook in your frontend repository, first run the following command:

npx storybook init

This command will install all required dependencies and generate files needed to run Storybook.

Next, you can add UI components to Storybook by adding .stories.tsx files to the stories folder created by the init command. Each .stories.tsx file will become a story that displays the UI component within it.

Example .stories.tsx file:

import React from "react";
import { Button } from "./Button";

export default {
  title: "Button",
  component: Button,
};

export const Text = () => <Button>Hello Button</Button>;
export const Emoji = () => (
  <Button>
    <span role="img" aria-label="so cool">
      πŸ˜€ 😎 πŸ‘ πŸ’―
    </span>
  </Button>
);

After adding UI components to Storybook, you can run Storybook by typing npm run storybook or yarn storybook in the terminal. Storybook will run a server on localhost and display the default Storybook page in your browser.

Adding UI Components to Storybook

Blog