Storybook is a development environment for UI components. It allows you to browse a component library, view the different states of each component, and interactively develop and test components in isolation, away from the business logic of your main application.
Think of it as a workbench for your UI. Instead of running your entire application just to see how a button looks with a longer text label, you can view that specific state directly in Storybook.
Core Concepts
- Component-Driven Development: This is the methodology where you build your UI from the "bottom up," starting with basic components (like buttons and inputs) and progressively composing them into more complex layouts and pages. Storybook is the perfect tool for this.
- Stories: A story captures a single, rendered state of a UI component. You typically write multiple stories for each component to represent all the different states it can be in. For a Button component, you might have stories like:
- Primary
- Secondary
- Small
- Large
- Disabled
- With an Icon
Writing a Story
Stories are easy to write. You create a file ending in .stories.js or .stories.jsx.
Let's imagine we have a Button component in Button.jsx.
JavaScript
// Button.jsx
import React from 'react';
import './button.css';
export const Button = ({ primary, backgroundColor, size, label, ...props }) => {
const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
return (
<button
type="button"
className={['storybook-button', `storybook-button--${size}`, mode].join(' ')}
style={backgroundColor && { backgroundColor }}
{...props}
>
{label}
</button>
);
};
Here is the corresponding story file for it.
JavaScript
// Button.stories.jsx
import { Button } from './Button';
// This default export contains metadata about the component
export default {
title: 'Example/Button', // How the component is named and grouped in the Storybook sidebar
component: Button,
// This 'argTypes' metadata controls how the "Controls" addon works
argTypes: {
backgroundColor: { control: 'color' },
},
};
// A "template" helps you write less boilerplate for each story
const Template = (args) => <Button {...args} />;
// Each export from the file is a separate story
export const Primary = Template.bind({});
// 'args' are the props passed to the component for this specific story
Primary.args = {
primary: true,
label: 'Button',
};
export const Secondary = Template.bind({});
Secondary.args = {
label: 'Button',
};
export const Large = Template.bind({});
Large.args = {
size: 'large',
label: 'Button',
};
The Power of Storybook
- Isolation: Develop components without needing to run your app's backend or navigate through complex UI flows.
- Documentation: Storybook automatically generates a living style guide for your project. This is invaluable for designers, product managers, and other developers.
- Testing: With addons, you can run accessibility tests, visual regression tests, and even interaction tests directly within Storybook.
- Collaboration: It provides a shared environment where designers and developers can collaborate on the UI.