Storybook for documenting components

How to use Storybook to develop, document and test UI components in isolation

9 min

Storybook has become the standard tool for developing and documenting UI components in isolation. With over 80,000 GitHub stars and adoption by teams at Airbnb, BBC, Shopify and GitLab, it provides an environment where each component is visualised outside the application context, with all its states and variants.

Beyond being a visual gallery, Storybook serves as living documentation, a development sandbox and a testing platform. This guide covers everything from initial setup to CI integration and automated visual testing.

What is Storybook and why use it?

Storybook is an open-source tool that lets you build UI components in isolation, without spinning up the full application. Each component is documented through "stories": representations of a component in a specific state or variant.

Its core value is twofold. For developers, it provides a fast development environment to iterate on a component without depending on the rest of the application. For product and design teams, it offers a browsable catalogue of every available component with its variants and usage rules.

Initial setup

Storybook supports React, Vue, Angular, Svelte, Web Components and more. Installation takes a single command (npx storybook@latest init) that detects your framework and configures everything automatically. The result is a .storybook folder with the configuration and an independent dev server.

The main configuration lives in .storybook/main.ts, where you define which files contain stories (typically *.stories.tsx), which addons to load and framework options. The preview.ts file controls global decorators, default parameters and Storybook’s visual theme.

  • npx storybook@latest init for automatic installation
  • main.ts: story globs, addons, framework configuration
  • preview.ts: global decorators, default parameters, theme
  • Storybook launches an independent dev server (port 6006 by default)

Writing effective stories

A story is a function that returns a component rendered with a specific set of props. The Component Story Format (CSF) is the current standard: each file exports a default meta with the component configuration and named exports for each variant.

The best stories cover the component’s real use cases, not just prop variations. Include stories for states (loading, error, empty, success), viewport sizes, themes (light/dark) and combinations with other components. Each story should be understandable without reading the source code.

  • Use CSF 3 (Component Story Format): export objects instead of functions for clarity
  • Create one story per relevant variant and state
  • Name stories descriptively: Primary, WithIcon, Loading, ErrorState
  • Include default args and use argTypes to document each prop’s type

Essential addons

Addons extend Storybook with functionality beyond the core. The essentials (included by default) cover interactive controls, automatic documentation, viewports, backgrounds and actions. Beyond these, there are addons for accessibility, internationalisation, testing and design.

  • Controls: interactive panel to modify component props in real time
  • Docs: auto-generates documentation with prop tables, source code and descriptions
  • a11y: runs accessibility audits (axe-core) on each story and displays violations
  • Interactions: lets you write and run interaction tests directly within Storybook
  • Design: integrates Figma designs alongside each story to compare implementation vs design

Visual testing with Chromatic

Chromatic is the visual testing platform created by the Storybook maintainers. It captures an image of every story on each build and compares them pixel by pixel against the previous version. When it detects a visual change, it flags it for human review before approval or rejection.

This workflow is especially powerful on pull requests: Chromatic comments on the PR showing which components have changed visually, allowing designers and developers to review before merging. This catches visual regressions that slip through traditional code reviews.

  • Automatic capture of every story on each CI build
  • Pixel-by-pixel comparison between versions with clear visual diffs
  • Review integrated into GitHub/GitLab pull requests
  • Support for multiple viewports and themes in a single run

CI/CD integration

Storybook integrates into your CI pipeline in several ways. The simplest is running npx storybook build to generate a static site you can deploy anywhere. The most advanced includes visual testing with Chromatic, interaction tests with the Storybook test-runner and automated accessibility checks.

A good practice is publishing each branch’s Storybook as a preview deployment (on Vercel, Netlify or Chromatic) so designers and product managers can review changes without running anything locally.

  • Static build: npx storybook build generates a site deployable to any hosting
  • Storybook test-runner: runs all story interaction tests in CI
  • Preview deployments: publish each branch for team review
  • Smoke test: verify Storybook compiles without errors as a minimum check on each PR

Best practices and pitfalls to avoid

Storybook is only as useful as the effort you put into maintaining it. Outdated or incomplete stories erode trust and lead teams to stop consulting it. Treat stories as part of the definition of done for every component: it is not finished until its stories are up to date.

Avoid creating stories that depend on external data or APIs. Stories should be deterministic and reproducible. Use mock data and decorators to simulate contexts (providers, themes, auth states) without depending on external services.

Key Takeaways

  • Storybook lets you develop and document components in isolation, speeding up the development cycle
  • Write stories that cover real states, not just prop variations
  • Accessibility and interaction addons turn Storybook into a testing platform
  • Chromatic automates visual testing and prevents regressions on pull requests
  • Integrate Storybook into CI/CD and treat stories as part of the definition of done

Want to document your component library with Storybook?

We set up Storybook, define story conventions and integrate it into your CI pipeline so every component is always documented and tested.