Set Up Storybook for Component-Driven Development
Intermediate12 min
Install and configure Storybook to develop, document, and visually test React components in isolation.
Prerequisites
- -React or Next.js project
- -Node.js 18+
Steps
1
Initialize Storybook
Run the Storybook initializer which auto-detects your framework and sets everything up.
$ npx storybook@latest init
Storybook auto-detects React, Next.js, Vite, or Webpack and installs the correct addons.
2
Start Storybook dev server
Launch the Storybook UI in the browser to view and interact with components.
$ pnpm storybook
3
Create a story for a component
Write a .stories.tsx file next to your component with different variants.
$ cat > src/components/Button.stories.tsx << 'EOF'
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
title: 'UI/Button',
component: Button,
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof Button>;
export const Primary: Story = { args: { variant: 'primary', children: 'Click me' } };
export const Secondary: Story = { args: { variant: 'secondary', children: 'Cancel' } };
export const Disabled: Story = { args: { disabled: true, children: 'Disabled' } };
EOF
4
Build static Storybook for deployment
Generate a static site that can be deployed to any hosting provider.
$ pnpm build-storybook
Deploy the storybook-static/ directory to Vercel, Netlify, or Chromatic for team review.
Full Script
FAQ
Discussion
Loading comments...