This PR removes the `/nx-api` pages from `nx-dev`. They are already redirected from `/nx-api` to either `/technologies` or `/reference/core-api` URLs. e.g. `/nx-api/nx` goes to `/reference/core-api/nx` and `/nx-api/react` goes to `/technologies/react/api` **Changes**: - Remove old `nx-api.json` from being generated in `scripts/documentation/generators/generate-manifests.ts` -- this was used to generate the sitemap - Remove `pages/nx-api` from Next.js app since we don't need them - Remove workaround from link checker `scripts/documentation/internal-link-checker.ts` -- the angular rspack/rsbuild and other workarounds are gone now that they are proper docs in `map.json` - Update Powerpack/Remote Cache reference docs to exclude API documents (since they are duplicated in the Intro page) -- `nx-dev/models-document/src/lib/mappings.ts` - All content in `docs` have been updated with new URL structure **Note:** Redirects are already handled, and Claude Code was used to verify the updated `docs/` URLs (see report below). The twelve 404s links were updated by hand. ## Verification Report https://gist.github.com/jaysoo/c7863fe7e091cb77929d1976165c357a
152 lines
5.1 KiB
Markdown
152 lines
5.1 KiB
Markdown
---
|
|
title: Set up Storybook for React Projects
|
|
description: This guide explains how to set up Storybook for React projects in your Nx workspace.
|
|
---
|
|
|
|
# Set up Storybook for React Projects
|
|
|
|
This guide will walk you through setting up [Storybook](https://storybook.js.org) for React projects in your Nx workspace.
|
|
|
|
{% callout type="warning" title="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](/technologies/test-tools/storybook/api) to get started.
|
|
{% /callout %}
|
|
|
|
## 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](/technologies/react/api/generators/storybook-configuration), like this:
|
|
|
|
```shell
|
|
nx g @nx/react:storybook-configuration project-name
|
|
```
|
|
|
|
## Auto-generate Stories
|
|
|
|
The [`@nx/react:storybook-configuration` generator](/technologies/react/api/generators/storybook-configuration) 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)](https://storybook.js.org/blog/storybook-csf3-is-here/).
|
|
|
|
```text
|
|
<some-folder>/
|
|
├── my-component.tsx
|
|
└── my-component.stories.tsx
|
|
```
|
|
|
|
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](/technologies/react/api/generators/stories):
|
|
|
|
```shell
|
|
nx g @nx/react:stories --project=<project-name>
|
|
```
|
|
|
|
{% callout type="note" title="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:
|
|
|
|
```shell
|
|
nx g @nx/react:stories --project=feature-ui
|
|
```
|
|
|
|
and the result would be the following:
|
|
|
|
```text
|
|
<workspace name>/
|
|
├── apps/
|
|
├── libs/
|
|
│ ├── feature/
|
|
│ │ ├── ui/
|
|
| | | ├── .storybook/
|
|
| | | ├── src/
|
|
| | | | ├──lib
|
|
| | | | | ├──my-button
|
|
| | | | | | ├── my-button.tsx
|
|
| | | | | | ├── my-button.stories.tsx
|
|
| | | | | | └── etc...
|
|
| | | | | └── etc...
|
|
| | | ├── README.md
|
|
| | | ├── tsconfig.json
|
|
| | | └── etc...
|
|
| | └── etc...
|
|
| └── etc...
|
|
├── nx.json
|
|
├── package.json
|
|
├── README.md
|
|
└── etc...
|
|
```
|
|
|
|
{% /callout %}
|
|
|
|
## 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:
|
|
|
|
```typescript {% fileName="libs/feature/ui/src/lib/my-button/my-button.tsx" %}
|
|
export interface MyButtonProps {
|
|
text: string;
|
|
padding: number;
|
|
disabled: boolean;
|
|
}
|
|
|
|
export function MyButton(props: MyButtonProps) {
|
|
return (
|
|
<button disabled={props.disabled} style={{ padding: props.padding }}>
|
|
{props.text}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
export default MyButton;
|
|
```
|
|
|
|
### Story file
|
|
|
|
The [`@nx/react:storybook-configuration` generator](/technologies/react/api/generators/storybook-configuration) would generate a Story file that looks like this:
|
|
|
|
```typescript {% fileName="libs/feature/ui/src/lib/my-button/my-button.stories.tsx" %}
|
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
import { MyButton } from './my-button';
|
|
import { within } from '@storybook/testing-library';
|
|
import { expect } from '@storybook/jest';
|
|
|
|
const meta: Meta<typeof MyButton> = {
|
|
component: MyButton,
|
|
title: 'MyButton',
|
|
};
|
|
export default meta;
|
|
type Story = StoryObj<typeof MyButton>;
|
|
|
|
export const Primary = {
|
|
args: {
|
|
text: '',
|
|
padding: 0,
|
|
disabled: false,
|
|
},
|
|
};
|
|
|
|
export const Heading: Story = {
|
|
args: {
|
|
text: '',
|
|
padding: 0,
|
|
disabled: false,
|
|
},
|
|
play: async ({ canvasElement }) => {
|
|
const canvas = within(canvasElement);
|
|
expect(canvas.getByText(/Welcome to MyButton!/gi)).toBeTruthy();
|
|
},
|
|
};
|
|
```
|
|
|
|
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](https://storybook.js.org/docs/react/writing-tests/interaction-testing).
|
|
|
|
## More Documentation
|
|
|
|
You can find all Storybook-related Nx topics [here](/technologies/test-tools/storybook/introduction).
|
|
|
|
For more on using Storybook, see the [official Storybook documentation](https://storybook.js.org/docs/react/get-started/introduction).
|
|
|
|
### 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](https://storybook.js.org/)
|
|
|
|
- [Set up Storybook version 7](/technologies/test-tools/storybook/recipes/storybook-7-setup)
|
|
- [Migrate to Storybook version 7](/technologies/test-tools/storybook/api/generators/migrate-7)
|