Storybook is an open-source tool for developing UI (user interface) components in isolation.
This means developers can create UI components in an environment separate from the application without worrying about flaky data, unfinished APIs, or business logic. This allows you to ship components with confidence.
Storybook has integrations with most front-end frameworks including React, Vue, and Angular.
In this article, you will be introduced to using Storybook for your projects.
There are several use cases for adopting Storybook:
Several teams rely on Storybook to do the heavy lifting of composing a component library and also as a building block for their design systems. Companies like Salesforce, Artsy, GOV.UK, and GitHub use Storybook to build and distribute UI components that impact millions of people.
A Storybook is a combination of different stories for different components. Stories are functions that return something that can be rendered to the screen. A story can contain a single state of one component or can be seen as a visual representation of a component.
Building UI components with Storybook means you have all the components in your application isolated so that they function regardless of the connection between them, and can be tested as separate UI components.
When building with Storybook there are several ways to structure and organize stories within your application.
Here is an example of a directory origination strategy with stories.js
files inside a component directory:
.
└── src/
└── components/
└── button
└── button.js
└── button.stories.js
Here is an example of a second directory organization strategy with stories
directory outside the src
directory:
.
└── src
└── components
└── button.js
└── stories
└── button.stories.js
Here is an example of a third directory organization strategy with stories
subdirectory in a component directory:
.
└── src/
└── components/
└── button
└── button.js
└── stories
└── button.stories.js
Following any of these methods is a matter of choice. It’s up to you to pick what works best for you and your team.
Storybook supports almost all the frontend frameworks available and in order for you to integrate with either of these frameworks a guide has been written by the storybook team that you can follow to setup storybook for individual frameworks and they include:
Support for other frameworks is made possible with community addons:
Addons extend the features and behavior of Storybook.
An example is the @storybook/addon-actions
addon, this helps with logging the data received by event handlers in Storybook.
There is a suite of essential addons that are pre-installed. And many third-party addons provided by the community.
A list of all addons curated by Storybook team can be found on this addons page.
Storybook can be used alongside the project you are developing because it gives a great developer experience with features, like hot-reloading via Webpack HMR. But you can also extend this further by deploying the Storybook as a static site on its own. This will enable you to showcase your components to everyone as a style guide reference.
This will require you to configure your Storybook using this script:
{
"scripts": {
"build-storybook": "build-storybook -c .storybook -o .out"
}
}
Now when you run npm run build-storybook
or yarn build-storybook
this will build the Storybook configured in the storybook
directory into a static web app and place it inside the directory called out
. Then, you can deploy that directory using services like GitHub Pages, Netlify, or Now.
In this article, you learned what Storybook is all about and when it is useful to take advantage of when building web applications.
Storybook works for a lot of use cases and integrating it into your workflow will enable you to build comprehensive UI components for your applications in isolation.
Continue your learning with a tutorial on how to build an interactive UI component using React and Storybook.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!