Set up Storybook for React Projects
This guide will walk you through setting up Storybook for React projects in your Nx 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>
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:
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:
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