self-taught
blog
Storybook is an open source tool for building UI components and pages in isolation. This means that you can create something similar to a UI library, deploy it and import it to use in different projects.
I think it’s a great tool for company that has their own products, where they have similar design system across their apps. It is good because storybook not only help the process of creating component easier but it also help to manage and document the component.
But even if you just want to create a reusable component within your project, it’s worth the try because to my own experience, using an existing UI library isn’t ideal, the component layout adjustment is not always easy and most of the time, we install the whole library but use only a few components.
Normally, if you just create a component, you’ll only need a .js and a stylesheet. Then, to see if how the component look like, you need to import it in your page to see changes.
However, with Storybook, after installing and run, a page (i’ll call it workshop) will run locally on a certain port which will help you see how your component looks like and all the changes while you’re developing it. That’s what the .stories.js
does.
A
*.stories.js
file defines all the stories for a component. Each story has a corresponding sidebar item. When you click on a story, it renders in the Canvas an isolated preview iframe.
① The Button
and Header
from the side menu below are the component that were created as an example from strorybook, these are called stories.
② Inside each component are variants , just like you see on Material UI or Bootstrap.
③ is how your component looks like.
And, the Controls
and Actions
tabs are for you to experiment your component. The properies in Controls
section can be adjusted as you wish.
As mentioned earlier, the Controls and Actions tab from the “workshop” can be adjusted. Check below, FYR, all the propTypes that were included in the main component will appear in the Controlstab. These propTypes are necessary for controlling the props that was tranfer from parent component because we don’t want to break anything.
Last part is default value for those props, if nothing was passed to this Button component, these values will be used.
In “export default” object:
If you want to change the way it appears on “workshop” page, change control type of the argTypes, like this. Now you can see, the size display as a select dropdown.
Last but not least in the story file is the variant.
This is also not obligatory, but one of the main reason to use storybook is to help document all the component you created, which is easy for someone else in your team to read and understand.
Variant is a copy of the main component with some predefined arguments. Once you declared, it’ll show up on the sidebar.
4. Writting documents using .mdx
If you want to create a document for you component, you can use .mdx file, the file allows you to write markdown syntax together with you component in the same file.
Storybook has dedicated a whole page for this, you can check it out.
https://storybook.js.org/docs/react/writing-docs/mdx
It’s not difficult to set up basic storybook to your project, just install it and you’re good to go, unless you’re want other advanced setup.
npx -p @storybook/cli sb init
This CLI will detect project and does installation on it’s own.
Here’s what the step looks like:
Step 1: Install Storybook on existing project.
Step 2: Delete uneccessary components and stories from storybook example or keep it FYR.
Step 3: Create you component.
Step 4: Create story to render your component on “workshop” page.
Step 5: Adjust your component and see the change on “workshop” page.
I didn’t want to create a tutorial on how to create something with storybook because there’re plenty of them and the storybook’s document itself is very clear and well written. The point is to understand what storybook does and why it’s worth the try. I hope this help you get the main idea of Storybook so you can decide whether to use it or not.