For AI agents: the complete documentation index is available at https://rslib.rs/llms.txt, the full documentation bundle is available at https://rslib.rs/llms-full.txt, and this page is available as Markdown at https://rslib.rs/guide/advanced/storybook.md.
close
  • English
  • Use Storybook

    Storybook is a powerful tool for developing UI components in isolation for React, Vue, and other frameworks. It enables you to build and test components independently, which can accelerate both development and testing.

    storybook-rsbuild is the Rsbuild powered Storybook builder, and provided the framework integration for React, Vue3 and vanilla JavaScript. The coherent Rsbuild system could make Storybook use an unified configuration with Rslib.

    Storybook Rsbuild version map

    The table below shows how Storybook Rsbuild matches Storybook versions. If you are on Storybook v10 or earlier, follow the Storybook migration guide and the Storybook Rsbuild migration guide to upgrade to v11.

    StorybookStorybook Rsbuild
    v11^4.0.0
    v10^3.0.0
    v9^2.0.0
    v8^1.0.0

    Getting started

    Setup a Rslib project

    This is the prerequisite for setting up Storybook. You need to have a Rslib project with components that you want to showcase in Storybook, check out Solution to setup a Rslib project.

    Tip

    You can create a new project with Storybook already wired up via create-rslib, without manual setup.

    Add Storybook to project

    To set up Storybook in an existing Rslib project, first install the Storybook framework package for your project. See the Storybook Rsbuild guide for installation instructions.

    Version requirements
    • Storybook Rsbuild 4 requires Node.js 22.12 or later.
    • The React and Vue 3 frameworks in Storybook 11 require typescript as a peer dependency even in JavaScript projects. Both frameworks support TypeScript 5.9 and 6.x for server-side docgen, with React also supporting 7.1 and later (7.0 lacks the required compiler API).

    Using React as an example, at this step you need to:

    1. Install the dependencies for Storybook Rsbuild React framework. The essential ones include

      • storybook: The Storybook core.
      • @storybook/react: React renderer for Storybook.
      • @storybook/addon-docs: Documentation tooling for Storybook.
      • @storybook/addon-onboarding: Interactive onboarding experience for Storybook.
      • @rsbuild/core: Storybook builder.
      • storybook-addon-rslib: This addon will make Storybook Rsbuild could derive Rsbuild configuration from Rslib config file. The addon will automatically read the Rslib configuration and apply it to Storybook Rsbuild, ensuring that the configuration is unified. You can check the storybook-addon-rslib documentation for available options.
      • typescript: A required peer dependency for Storybook 11's server-side docgen, including in JavaScript projects.
      npm
      yarn
      pnpm
      bun
      npm add storybook @storybook/react @storybook/addon-docs @storybook/addon-onboarding storybook-addon-rslib @rsbuild/core typescript@6 -D

      Dependencies vary by framework. This React example uses storybook-react-rsbuild as the framework integration.

      React
      Vue
      npm
      yarn
      pnpm
      bun
      deno
      npm add storybook-react-rsbuild -D
    2. Configure the Storybook configuration file .storybook/main.js, specify the stories and addons, and set the framework with corresponding framework integration.

      .storybook/main.js
      export default {
        stories: [
          '../stories/**/*.mdx',
          '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)',
        ],
        addons: [
          '@storybook/addon-docs',
          '@storybook/addon-onboarding',
          'storybook-addon-rslib',
        ],
        framework: 'storybook-react-rsbuild', // storybook-react-rsbuild for example
      };
    3. Add a simple story to the stories directory. For example, create a Button.stories.js file with the following content:

      stories/Button.stories.js
      import { Button } from '../src/Button';
      
      const meta = {
        title: 'Example/Button',
        component: Button,
      };
      
      export default meta;
      
      export const Primary = {
        args: {
          primary: true,
          label: 'Button',
        },
      };
    Tip

    If Storybook has trouble resolving packages in a monorepo, use getAbsolutePath('storybook-addon-rslib') in the Storybook config to resolve the addon. See the Storybook FAQ.

    Storybook 11 requires the node-modules linker when using Yarn. In .yarnrc.yml, set nodeLinker: node-modules and reinstall dependencies.

    There you go, you can start and build Storybook with the following commands:

    npx storybook dev   # development mode
    npx storybook build # build static files

    Open the URL printed by storybook dev or pass --open to launch the browser automatically.

    Check out more details in the Storybook Rsbuild documentation and the Storybook documentation.

    Example