Storybook and Component Documentation

Develop, document and test components in isolation with Storybook stories, controls, docs and visual and interaction tests.

Intermediate · about 3 hours · difficulty 2/5

Learn first: Component Libraries: shadcn/ui and Radix, Component Testing with Testing Library

Storybook is a workshop for UI components. You write stories, each showing a component in one state (default, loading, error, long text, right-to-left), and Storybook renders them in an isolated app with controls to tweak props live.

Why teams use it

  • Build in isolation: work on a component's hard states without clicking through the app to reach them.
  • Living documentation: autodocs pages list props, variants and usage examples for designers and other developers.
  • Review: product and design can see changes in a deployed Storybook before merging.
  • Testing: stories double as test cases.

Writing stories

Component Story Format: a default export describes the component, and named exports are stories with args. Use decorators to wrap stories in providers (theme, router, query client), and MSW handlers to mock data. Cover edge cases, not just the happy path.

Testing with stories

  • Interaction tests with play functions click and type inside a story and assert results, powered by Testing Library and Vitest.
  • Accessibility checks with the a11y addon run axe on every story.
  • Visual regression testing with Chromatic or Playwright screenshots catches unintended style changes.

Alternatives

Ladle and Histoire are lighter options; many small teams simply document components in MDX or a docs site. Storybook matters most when a component library is shared across teams or apps.

Good enough to move on

You can add Storybook to a project, write stories covering the states of five components, add an interaction test and an accessibility check, and publish it for review.

Key concepts

  • Stories and args
  • Controls and autodocs
  • Decorators and mocked data
  • Interaction tests with play functions
  • Accessibility addon
  • Visual regression testing
  • Publishing Storybook for review

Mini project

Add Storybook to your component library, write stories for every state of Button, Input, Dialog, Toast and DataTable, add a play-function test for the Dialog and run the a11y addon on all stories.

Check yourself

How does writing a story for a component's error state save time compared with testing it inside the full app?

Free resources