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 workspaceYou 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