Set up Storybook for React Projects

This guide will walk you through setting up Storybook for React projects in your Nx workspace.

Set up Storybook in your workspace

You first need to set up Storybook for your Nx workspace, if you haven't already. You can read the Storybook plugin overview guide to get started.

Generate Storybook Configuration for a React project

You can generate Storybook configuration for an individual React project by using the @nx/react:storybook-configuration generator, like this:

โฏ

nx g @nx/react:storybook-configuration project-name

Auto-generate Stories

The @nx/react:storybook-configuration generator has the option to automatically generate *.stories.ts|tsx files for each component declared in the library. The stories will be generated using Component Story Format 3 (CSF3).

1<some-folder>/ 2โ”œโ”€โ”€ my-component.tsx 3โ””โ”€โ”€ my-component.stories.tsx 4

If you add more components to your project, and want to generate stories for all your (new) components at any point, you can use the @nx/react:stories generator:

โฏ

nx g @nx/react:stories --project=<project-name>

Example

Let's take for a example a library in your workspace, under libs/feature/ui, called feature-ui. This library contains a component, called my-button.

The command to generate stories for that library would be:

โฏ

nx g @nx/react:stories --project=feature-ui

and the result would be the following:

1<workspace name>/ 2โ”œโ”€โ”€ apps/ 3โ”œโ”€โ”€ libs/ 4โ”‚ โ”œโ”€โ”€ feature/ 5โ”‚ โ”‚ โ”œโ”€โ”€ ui/ 6| | | โ”œโ”€โ”€ .storybook/ 7| | | โ”œโ”€โ”€ src/ 8| | | | โ”œโ”€โ”€lib 9| | | | | โ”œโ”€โ”€my-button 10| | | | | | โ”œโ”€โ”€ my-button.tsx 11| | | | | | โ”œโ”€โ”€ my-button.stories.tsx 12| | | | | | โ””โ”€โ”€ etc... 13| | | | | โ””โ”€โ”€ etc... 14| | | โ”œโ”€โ”€ README.md 15| | | โ”œโ”€โ”€ tsconfig.json 16| | | โ””โ”€โ”€ etc... 17| | โ””โ”€โ”€ etc... 18| โ””โ”€โ”€ etc... 19โ”œโ”€โ”€ nx.json 20โ”œโ”€โ”€ package.json 21โ”œโ”€โ”€ README.md 22โ””โ”€โ”€ etc... 23

Example Files

Let's take for a example a library in your workspace, under libs/feature/ui, called feature-ui with a component, called my-button.

Let's say that your component code looks like this:

libs/feature/ui/src/lib/my-button/my-button.tsx
1export interface MyButtonProps { 2 text: string; 3 padding: number; 4 disabled: boolean; 5} 6 7export function MyButton(props: MyButtonProps) { 8 return ( 9 <button disabled={props.disabled} style={{ padding: props.padding }}> 10 {props.text} 11 </button> 12 ); 13} 14 15export default MyButton; 16

Story file

The @nx/react:storybook-configuration generator would generate a Story file that looks like this:

libs/feature/ui/src/lib/my-button/my-button.stories.tsx
1import type { Meta, StoryObj } from '@storybook/react'; 2import { MyButton } from './my-button'; 3import { within } from '@storybook/testing-library'; 4import { expect } from '@storybook/jest'; 5 6const meta: Meta<typeof MyButton> = { 7 component: MyButton, 8 title: 'MyButton', 9}; 10export default meta; 11type Story = StoryObj<typeof MyButton>; 12 13export const Primary = { 14 args: { 15 text: '', 16 padding: 0, 17 disabled: false, 18 }, 19}; 20 21export const Heading: Story = { 22 args: { 23 text: '', 24 padding: 0, 25 disabled: false, 26 }, 27 play: async ({ canvasElement }) => { 28 const canvas = within(canvasElement); 29 expect(canvas.getByText(/Welcome to MyButton!/gi)).toBeTruthy(); 30 }, 31}; 32

Notice the interaction test on the second story, inside the play function. This just tests if the default text that appears on generated components exists in the rendered component. You can edit this test to suit your needs. You can read more about interaction tests here.

More Documentation

You can find all Storybook-related Nx topics here.

For more on using Storybook, see the official Storybook documentation.

Migration Scenarios

Here's more information on common migration scenarios for Storybook with Nx. For Storybook specific migrations that are not automatically handled by Nx please refer to the official Storybook page

Older migration scenarios