Storybook is an isolated environment for developing and documenting UI components. It lets you build, test, and document components outside your main app.
How it works step-by-step:
.stories.tsx files next to your components.Installation:
1npx storybook@latest init
Complete story example with args, decorators, and documentation:
1// Button.stories.tsx2import type { Meta, StoryObj } from "@storybook/react";3import { fn } from "@storybook/test";4import { Button } from "./Button";56const meta: Meta<typeof Button> = {7 title: "UI/Button",8 component: Button,9 tags: ["autodocs"],10 parameters: {11 layout: "centered",12 docs: {13 description: {14 component: "A versatile button component with multiple variants and sizes."15 }16 }17 },18 argTypes: {19 variant: {20 control: "select",21 options: ["primary", "secondary", "ghost", "danger", "outline"],22 description: "Visual style of the button"23 },24 size: {25 control: "select",26 options: ["sm", "md", "lg", "icon"],27 description: "Button size"28 },29 isLoading: { control: "boolean" },30 disabled: { control: "boolean" },31 fullWidth: { control: "boolean" }32 },33 args: {34 onClick: fn()35 }36};37export default meta;38type Story = StoryObj<typeof meta>;3940export const Primary: Story = {41 args: {42 variant: "primary",43 children: "Primary Button"44 }45};4647export const Secondary: Story = {48 args: {49 variant: "secondary",50 children: "Secondary"51 }52};5354export const Loading: Story = {55 args: {56 isLoading: true,57 children: "Loading..."58 }59};6061export const Disabled: Story = {62 args: {63 disabled: true,64 children: "Cannot click"65 }66};6768export const AllVariants: Story = {69 render: () => (70 <div className="flex flex-wrap gap-2 items-center">71 <Button variant="primary">Primary</Button>72 <Button variant="secondary">Secondary</Button>73 <Button variant="ghost">Ghost</Button>74 <Button variant="danger">Danger</Button>75 <Button variant="outline">Outline</Button>76 </div>77 )78};7980// Story with dark mode decorator81export const DarkMode: Story = {82 parameters: {83 backgrounds: { default: "dark" }84 },85 decorators: [86 (Story) => (87 <div className="dark bg-gray-900 p-4">88 <Story />89 </div>90 )91 ]92};
Launch: npm run storybook — opens the UI on port 6006.
Configuration options:
tags: ["autodocs"] — auto-generates documentation from argTypes.parameters.layout: "centered" — centers the story in the canvas.decorators — wrapper components for context (theme providers, etc.).args — default props that can be overridden in the controls panel.Performance considerations:
parameters.docs.source to control how source code is displayed.Integration with testing:
Benefits: Isolated development, interactive documentation, visual testing, component library documentation.