{ "githubRoot": "https://github.com/nrwl/nx/blob/master", "name": "storybook", "packageName": "@nrwl/storybook", "description": "The Nx Plugin for Storybook contains executors and generators for allowing your workspace to use the powerful Storybook integration testing & documenting capabilities.", "root": "/packages/storybook", "source": "/packages/storybook/src", "documentation": [ { "id": "overview", "name": "Overview Generic", "path": "/packages/storybook", "file": "shared/guides/storybook/plugin-overview", "content": "![Storybook logo](/shared/storybook-logo.png)\n\n[Storybook](https://storybook.js.org) is a development environment for UI components. It allows you to browse a component library, view the different states of each component, and interactively develop and test components.\n\nThis guide will briefly walk you through using Storybook within an Nx workspace.\n\n## Setting Up Storybook\n\n### Add the Storybook plugin\n\n```bash\nyarn add --dev @nrwl/storybook\n```\n\n## Using Storybook\n\n### Generating Storybook Configuration\n\nYou can generate Storybook configuration for an individual project with this command:\n\n```bash\nnx g @nrwl/storybook:configuration project-name\n```\n\nYou can choose to use Storybook for one of the supported frameworks:\n\n- `@storybook/angular`\n- `@storybook/react`\n- `@storybook/react-native`\n- `@storybook/html`\n- `@storybook/web-components`\n- `@storybook/vue`\n- `@storybook/vue3`\n- `@storybook/svelte`\n\nChoosing one of these frameworks will have the following effects on your workspace:\n\n1. Nx will install all the required Storybook packages that go with it.\n\n2. Nx will generate a root `.storybook` folder and a project-level `.storybook` folder (located under `libs/your-project/.storybook` or `apps/your-project/.storybook`) containing the essential configuration files for Storybook.\n\n3. If you are working on an Angular, a React or a React Native project (and you choose `@storybook/angular`, `@storybook/react` or `@storybook/react-native`) the Nx generator will also generate stories for all the components in your project.\n\n4. Nx will create new `targets` in your project's `project.json`, called `storybook` and `build-storybook`, containing all the necessary configuration to serve and build Storybook.\n\n5. Nx will generate a new Cypress e2e app for your project (if there isn't one already) to run against the Storybook instance.\n\n### Configure your project using TypeScript\n\nYou can choose to configure your project using TypeScript instead of JavaScript. To do that, just add the `--tsConfiguration=true` flag to the above command, like this:\n\n```bash\nnx g @nrwl/storybook:configuration project-name --tsConfiguration=true\n```\n\n[Here is the Storybook documentation](https://storybook.js.org/docs/react/configure/overview#configure-your-project-with-typescript) if you want to learn more.\n\n### Running Storybook\n\nServe Storybook using this command:\n\n```bash\nnx run project-name:storybook\n```\n\nor\n\n```bash\nnx storybook project-name\n```\n\n### Building Storybook\n\nBuild Storybook using this command:\n\n```bash\nnx run project-name:build-storybook\n```\n\nor\n\n```bash\nnx build-storybook project-name\n```\n\n### Anatomy of the Storybook setup\n\nWhen running the Nx Storybook generator, it'll configure the Nx workspace to be able to run Storybook seamlessly. It'll create\n\n- a global Storybook configuration\n- a project specific Storybook configuration\n\nThe **global** Storybook configuration allows to set addon-ons or custom webpack configuration at a global level that applies to all Storybook's within the Nx workspace. You can find that folder at `.storybook/` at the root of the workspace.\n\n```treeview\n/\n├── .storybook/\n│ ├── main.js\n│ ├── tsconfig.json\n├── apps/\n├── libs/\n├── nx.json\n├── package.json\n├── README.md\n└── etc...\n```\n\nThe project-specific Storybook configuration is pretty much similar what you would have for a non-Nx setup of Storybook. There's a `.storybook` folder within the project root folder.\n\n```treeview\n/\n├── .storybook/\n│ ├── main.js\n│ ├── preview.js\n│ ├── tsconfig.json\n├── src/\n├── README.md\n├── tsconfig.json\n└── etc...\n```\n\n### Using Addons\n\nTo register a [Storybook addon](https://storybook.js.org/addons/) for all storybook instances in your workspace:\n\n1. In `/.storybook/main.js`, in the `addons` array of the `module.exports` object, add the new addon:\n ```typescript\n module.exports = {\n stories: [...],\n ...,\n addons: [..., '@storybook/addon-essentials'],\n };\n ```\n2. If a decorator is required, in each project's `/.storybook/preview.js`, you can export an array called `decorators`.\n\n ```typescript\n import someDecorator from 'some-storybook-addon';\n export const decorators = [someDecorator];\n ```\n\n**-- OR --**\n\nTo register an [addon](https://storybook.js.org/addons/) for a single storybook instance, go to that project's `.storybook` folder:\n\n1. In `main.js`, in the `addons` array of the `module.exports` object, add the new addon:\n ```typescript\n module.exports = {\n stories: [...],\n ...,\n addons: [..., '@storybook/addon-essentials'],\n };\n ```\n2. If a decorator is required, in `preview.js` you can export an array called `decorators`.\n\n ```typescript\n import someDecorator from 'some-storybook-addon';\n export const decorators = [someDecorator];\n ```\n\n## More Documentation\n\nYou can find dedicated information for React and Angular:\n\n- [Set up Storybook for Angular Projects](/storybook/overview-angular)\n- [Set up Storybook for React Projects](/storybook/overview-react)\n\nYou can find all Storybook-related Nx documentation [here](/packages#storybook).\n\nFor more on using Storybook, see the [official Storybook documentation](https://storybook.js.org/docs/basics/introduction/).\n\n### Migration Scenarios\n\nHere'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/)\n\n- [Upgrading to Storybook 6](/storybook/upgrade-storybook-v6-react)\n- [Migrate to the Nrwl React Storybook Preset](/storybook/migrate-webpack-final-react)\n" }, { "id": "overview-react", "name": "Set up Storybook for React Projects", "file": "shared/guides/storybook/plugin-react", "content": "# Set up Storybook for React Projects\n\n![Storybook logo](/shared/storybook-logo.png)\n\nThis guide will walk you through setting up [Storybook](https://storybook.js.org) for React projects in your Nx workspace.\n\n{% callout type=\"warning\" title=\"Set up Storybook in your workspace\" %}\nYou first need to set up Storybook for your Nx workspace, if you haven't already. You can read the [Storybook plugin overview guide](/packages/storybook) to get started.\n{% /callout %}\n\n## Generate Storybook Configuration for a React project\n\nYou can generate Storybook configuration for an individual React project with this command:\n\n```bash\nnx g @nrwl/react:storybook-configuration project-name\n```\n\n## Nx React Storybook Preset\n\n`@nrwl/react` ships with a Storybook preset to make sure it uses the very same configuration as your Nx React application. When you generate a Storybook configuration for a project, it'll automatically add the preset to your configuration.\n\n```typescript\nconst rootMain = require('../../../.storybook/main');\n\nmodule.exports = {\n ...rootMain,\n addons: [...rootMain.addons, '@nrwl/react/plugins/storybook'],\n ...\n};\n```\n\n## Auto-generate Stories\n\nThe `@nrwl/react:storybook-configuration` generator has the option to automatically generate `*.stories.ts` files for each component declared in the library.\n\n```treeview\n/\n├── my.component.ts\n└── my.component.stories.ts\n```\n\nYou can re-run it at a later point using the following command:\n\n```bash\nnx g @nrwl/react:stories \n```\n\n{% callout type=\"note\" title=\"Example\" %}\n\nLet'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`.\n\nThe command to generate stories for that library would be:\n\n```bash\nnx g @nrwl/react:stories feature-ui\n```\n\nand the result would be the following:\n\n```treeview\n/\n├── .storybook/\n├── apps/\n├── libs/\n│ ├── feature/\n│ │ ├── ui/\n| | | ├── .storybook/\n| | | ├── src/\n| | | | ├──lib\n| | | | | ├──my-button\n| | | | | | ├── my-button.component.ts\n| | | | | | ├── my-button.component.stories.ts\n| | | | | | └── etc...\n| | | | | └── etc...\n| | | ├── README.md\n| | | ├── tsconfig.json\n| | | └── etc...\n| | └── etc...\n| └── etc...\n├── nx.json\n├── package.json\n├── README.md\n└── etc...\n```\n\n{% /callout %}\n\n## Cypress tests for Stories\n\nThe `storybook-configuration` generator gives the option to set up an e2e Cypress app that is configured to run against the project's Storybook instance.\n\nTo launch Storybook and run the Cypress tests against the iframe inside of Storybook:\n\n```bash\nnx run project-name-e2e:e2e\n```\n\nThe url that Cypress points to should look like this:\n\n`'/iframe.html?id=buttoncomponent--primary&args=text:Click+me!;padding;style:default'`\n\n- `buttoncomponent` is a lowercase version of the `Title` in the `*.stories.ts` file.\n- `primary` is the name of an individual story.\n- `style=default` sets the `style` arg to a value of `default`.\n\nChanging args in the url query parameters allows your Cypress tests to test different configurations of your component. You can [read the documentation](https://storybook.js.org/docs/react/writing-stories/args#setting-args-through-the-url) for more information.\n\n## Example Files\n\n**\\*.stories.tsx file**\n\n```typescript\nimport { Story, Meta } from '@storybook/react';\nimport { Button, ButtonProps } from './button';\n\nexport default {\n component: Button,\n title: 'Button',\n} as Meta;\n\nconst Template: Story = (args) =>