To develop, test, and document UI components in isolation from the main application.
- Building a Design System.
- Visual testing.
- Collaborating with designers.
npx storybook@latest initA story captures a specific state of a component.
Button.stories.tsx:
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
component: Button,
title: 'Components/Button',
};
export default meta;
type Story = StoryObj<typeof Button>;
export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};
export const Secondary: Story = {
args: {
label: 'Button',
},
};- Controls: Edit props dynamically in the UI.
- Actions: Log events (clicks).
- Docs: Auto-generate documentation from comments (JSDoc).
Use Interaction Testing (play function) to simulate user behavior within Storybook.
- Maintenance: Stories must be updated when component props change.
- Context: Components relying on global providers (Redux, Router) need decorators in
.storybook/preview.jsto work.
A living style guide where every component state is cataloged and interactive.