> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Using Storybook

[Storybook](https://storybook.js.org/) is a tool specifically designed for component debugging. It provides:

- A rich variety of debugging capabilities
- Integration with some testing tools
- Reusable documentation content
- Sharing capabilities
- Workflow automation

## Principle

Rsbuild provides [Storybook Rsbuild](https://storybook.rsbuild.rs/index.html), which allows any Rsbuild project to use this tool for building Storybook.

In Modern.js v3 projects, `@modern-js/storybook-addon` uses the application config resolver and merges the shared and `client` configurations into the Storybook preview. Storybook Rsbuild manages the preview entry, output paths and HMR client.

## Installation


```sh [npm]
npm install storybook@~10.6.7 storybook-react-rsbuild@~3.6.3 storybook-builder-rsbuild@~3.6.3 @rsbuild/core@^2.3.2 @modern-js/storybook-addon -D
```

```sh [yarn]
yarn add storybook@~10.6.7 storybook-react-rsbuild@~3.6.3 storybook-builder-rsbuild@~3.6.3 @rsbuild/core@^2.3.2 @modern-js/storybook-addon -D
```

```sh [pnpm]
pnpm add storybook@~10.6.7 storybook-react-rsbuild@~3.6.3 storybook-builder-rsbuild@~3.6.3 @rsbuild/core@^2.3.2 @modern-js/storybook-addon -D
```

```sh [bun]
bun add storybook@~10.6.7 storybook-react-rsbuild@~3.6.3 storybook-builder-rsbuild@~3.6.3 @rsbuild/core@^2.3.2 @modern-js/storybook-addon -D
```

```sh [deno]
deno add npm:storybook@~10.6.7 npm:storybook-react-rsbuild@~3.6.3 npm:storybook-builder-rsbuild@~3.6.3 npm:@rsbuild/core@^2.3.2 npm:@modern-js/storybook-addon -D
```

Use the same `@rsbuild/core` version as your project’s `@modern-js/app-tools`. Projects on older Modern.js versions can continue using [storybook-addon-modernjs](https://storybook.rsbuild.rs/guide/integrations/modernjs).

## Configure `.storybook/main.ts`

```ts
import type { StorybookConfig } from 'storybook-react-rsbuild';

const config: StorybookConfig = {
  stories: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  addons: ['@modern-js/storybook-addon'],
  framework: 'storybook-react-rsbuild',
};
export default config;
```

## BFF Integrated Calls in Storybook

When you want to call BFF APIs directly in Storybook components (for example `/api/**`), start the BFF service and configure a proxy in Storybook's dev server to avoid CORS issues.

### 1. Add scripts

Add the following scripts to `package.json` to start the BFF server and Storybook separately. The `BFF_PROXY` environment variable ensures the proxy is only enabled during Storybook build:

```json
{
  "scripts": {
    "dev:api": "modern dev --api-only",
    "storybook": "BFF_PROXY=1 storybook dev -p 6006 --no-open"
  }
}
```

### 2. Configure proxy

Add the following config in `modern.config.ts` to proxy requests to the BFF service only during Storybook build:

```ts
export default applyBaseConfig({
  dev: process.env.BFF_PROXY
    ? {
        server: {
          proxy: {
            '/api': 'http://localhost:8080',
          },
        },
      }
    : {},
});
```

Proxy path and port follow this logic:

- If `dev.server.proxy` is configured, use that configuration
- If not configured, the proxy path falls back to `bff.prefix`, otherwise `/api`
- If not configured, the target port falls back to `server.port`, otherwise `8080`
- The final target is `http://localhost:<port>`

### 3. Start

```bash
pnpm dev:api
pnpm storybook
```

Now you can request the path configured in `bff.prefix`; if not configured, use `/api/**`.

## Example

You can check out the [example](https://github.com/rstackjs/storybook-rsbuild/tree/main/sandboxes/modernjs-react) to learn how to use Storybook in Modern.js.
