Storybook

Storybook logo

Storybook 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.

How to Use Storybook in an Nx Repo

Add the Storybook plugin

yarn add --dev @nrwl/storybook

Generating Storybook Configuration

You can generate Storybook configuration for an individual project with this command:

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

If there's no .storybook folder at the root of the workspace, one is created.

<workspace name>/
├── .storybook/
│   ├── main.js
│   ├── tsconfig.json
│   └── webpack.config.js
├── apps/
├── libs/
├── nx.json
├── package.json
├── README.md
└── etc...

Also, a project-specific .storybook folder is added in the root of the project.

<project root>/
├── .storybook/
│   ├── main.js
│   ├── config.js
│   ├── tsconfig.json
│   └── webpack.config.js
├── src/
├── README.md
├── tsconfig.json
└── etc...

Running Storybook

Serve Storybook using this command:

nx run project-name:storybook

Run Cypress Tests Against a Storybook Instance

Both storybook-configuration generator gives the option to set up an e2e Cypress app that is configured to run against the project's Storybook instance.

To launch Storybook and run the Cypress tests against the iframe inside of Storybook:

nx run project-name-e2e:e2e

The url that Cypress points to should look like this:

'/iframe.html?id=buttoncomponent--primary&args=text:Click+me!;padding;style:default'

  • buttoncomponent is a lowercase version of the Title in the *.stories.ts file.
  • primary is the name of an individual story.
  • style=default sets the style arg to a value of default.

Changing args in the url query parameters allows your Cypress tests to test different configurations of your component. You can read the documentation for more information.

Example Files

*.stories.tsx file

1import { Story, Meta } from '@storybook/react';
2import { Button, ButtonProps } from './button';
3
4export default {
5  component: Button,
6  title: 'Button',
7} as Meta;
8
9const Template: Story<ButtonProps> = (args) => <Button {...args} />;
10
11export const Primary = Template.bind({});
12Primary.args = {
13  text: 'Click me!',
14  padding: 0,
15  style: 'default',
16};

Cypress *.spec.ts file

1describe('shared-ui', () => {
2  beforeEach(() =>
3    cy.visit(
4      '/iframe.html?id=buttoncomponent--primary&args=text:Click+me!;padding;style:default'
5    )
6  );
7
8  it('should render the component', () => {
9    cy.get('storybook-trial-button').should('exist');
10  });
11});

Using Addons

To register an addon for all storybook instances in your workspace:

  1. In /.storybook/main.js, in the addons array of the module.exports object, add the new addon:

    1module.exports = {
    2stories: [...],
    3...,
    4addons: [..., '@storybook/addon-essentials'],
    5};
    
  2. If a decorator is required, in each project's <project-path>/.storybook/preview.js, you can export an array called decorators.

    1import someDecorator from 'some-storybook-addon';
    2export const decorators = [someDecorator];

-- OR --

To register an addon for a single storybook instance, go to that project's .storybook folder:

  1. In main.js, in the addons array of the module.exports object, add the new addon:

    1module.exports = {
    2stories: [...],
    3...,
    4addons: [..., '@storybook/addon-essentials'],
    5};
    
  2. If a decorator is required, in preview.js you can export an array called decorators.

    1import someDecorator from 'some-storybook-addon';
    2export const decorators = [someDecorator];

More Information

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

From knobs to controls

Storybook v6 moves from "knobs" to args and controls when it comes to defining and manipulating your storybook component properties. More can be found on the official Storybook docs.

From Nx v12.5 and on, the @nrwl/storybook package will be using @storybook/addon-controls instead of @storybook/addon-knobs to generate stories.

For new Nx workspaces

  • Generators will generate your Storybook configuration files and your Stories using Controls/args instead of knobs
  • The storybook-configuration generator will install the @storybook/addon-essentials package, part of which is @storybook/addon-controls. This includes some more "essential" Storybook features (eg. docs). You can disable features you do not need anytime in your main.js.
  • Cypress e2e tests will be generated, using the args URL to set args in the controls.

For existing Nx workspaces

  • If you nx migrate to the latest version, your package.json will be updated to include the @storybook/addon-essentials package. The @storybook/addon-essentials addon will be added in your addons array in your root main.js file. You will need to run npm/yarn install to have it installed.
  • If you install manually the latest version of @nrwl/storybook, @nrwl/workspace and @nrwl/angular or @nrwl/react, you will need to manually do yarn add -D @storybook/addon-essentials. You will also need to add the addon manually in your addons array in your root main.js file.
  • All the stories you generate from that moment on will be using controls/args
  • Your existing stories will not be touched and will still work

Upgrading to Storybook 6 (and Nx versions >10.1.x)

Nx now comes with Storybook version 6. Chances are, if you used Nx version 10.1.x or older with Storybook, you are using Storybook version 5.3 with configuration files of Storybook version 5.2.

Nx version 10.2.x will continue to support Storybook version 5.2.x, however newer versions of Nx will only support Storybook version 6 (and on).

When you are running the Nx workspace migration script, your Storybook instances and configurations across your apps and libraries will NOT be migrated automatically. We chose not to migrate your Storybook instances and configurations across your apps and libraries automatically, since there a number of breaking changes that Storybook introduced in versions 5.3 and 6.0, and making decisions on what to migrate automatically would risk the integrity of your code.

Instead, when you choose to migrate from Nx versions <10.1.x to Nx versions >10.2.x (using the Nx migration script - nx migrate) we will keep your Storybook packages and Storybook instances and configurations intact. We suggest that you do the migration on your own, using the guide below, with all the references to the official Storybook migration guides. Look at the use cases below, and follow the one that matches your case.

Use cases:

Use case 1: Create an Nx workspace from scratch using the latest version of Nx

If you are creating an Nx workspace using the latest version of Nx, the latest version of Storybook (version 6) will be used as well. You do not need to do anything.

Use case 2: I already have an Nx workspace that does NOT use Storybook and I want to migrate to the latest Nx

If you already have an Nx workspace with a previous version of Nx that does NOT use Storybook, and you migrate to the latest Nx using the migrate scripts provided by Nx, and then, after the migration to the latest Nx, you choose to add Storybook, the latest version of Storybook will be used. You do not need to do anything.

Use case 3: I already have an Nx workspace with Storybook and I want to migrate to the latest Nx

In that case, when you run the Nx migration scripts, the scripts will ignore the Storybook packages, the Storybook configuration files, the Storybook instances in your apps and libraries, and all the generated stories. If you continue to add Storybook configurations and Storybook instances to new libraries and applications, then the version of Storybook that you already have will be used (most probably, if you have not changed anything manually, that version will be 5.3.9 using, however, the configuration files of 5.2). You will have to do the upgrade to the latest Storybook on your own, manually. After that, Nx will use that version, and configure all new Storybook instances using the new version.

Upgrading to Storybook 6 using the Nx migration generator

Some info about the generator

The @nrwl/react:storybook-migrate-defaults-5-to-6 generator will not exactly do a migration. It will perform the following actions:

  • It will generate new Storybook configuration files using the new (>6.x) Storybook way. The way it will do that is, it will look into workspace.json and it will find all the projects that have a Storybook configuration. Using the configFolder path provided there, it will go and generate new Storybook instances in all these paths. Finally, it will generate a new Storybook instance at the root directory.

  • If you choose to keepOld, then it will add all your existing Storybook configuration files into another folder labeled .old_storybook.

  • It will update all the Storybook-related (@storybook/*) packages in your package.json.

How to use the generator

That way, you can have working Storybook instances for all your projects just by running

nx g @nrwl/react:storybook-migrate-defaults-5-to-6

What if I had made changes to the defaults?

In case you had made customizations to the default Storybook configurations, you can then manually change each of your Storybook instance configuration files using the official Storybook 6 Migration Guide to make sure you use the new syntax. Your old configuration files are available to you to use as a reference.

Please check out this official Storybook 6 Migration Guide article, as well as the detailed guides here.

What if I am not ready to change everything at once?

The generator gives you the option to migrate one project at a time. You can provide the --name=PROJECT_NAME flag, and then the generator will only generate new files for the specified project.

Please note that this option will NOT update all the Storybook-related (@storybook/*) packages in your package.json, or the root Storybook folder. The reason is that if you want to do the migration gradually, one project at a time, you want your old, existing, projects, to still work. That way, you will still be able to run your old, non-migrated Storybook projects. However, you will not be able to run any migrated Storbook projects. Once you have migrated all your Storybook projects, you can run nx g @nrwl/react:storybook-migrate-defaults-5-to-6 once again, and the generator will take care of updating all the Storybook-related (@storybook/*) packages in your package.json and it will also generate the new Storybook files for the root Storybook directory.

General tip:

Commit any changes you have locally. We would suggest that you start the migration with a clean git history, in case anything goes wrong.

Upgrading to Storybook 6 manually

There is really no great reason for doing the migration completely manually. The @nrwl/react:storybook-migrate-defaults-5-to-6 generator will take care of Steps 1, 2 and 3. What you will need to do after running the generator is that you have to manually migrate any custom changes you had done to the default Storybook configuration files that were automatically generated by Nx when you first used Nx Storybook. To do the manual migration you should use the official Storybook 6 Migration Guide article, as well as the detailed guides here.

However, if you still want to do everything manually, these are the steps you should follow:

Step 0:

Commit any changes you have locally. We would suggest that you start the migration with a clean git history, in case anything goes wrong.

Step 1: Changing the configuration files from version 5.2 to 5.3

The most noticeable change in Storybook versions newer than 5.2 is that the configuration files have changed names and content. Quoting from the official Storybook migration guide:

  • presets.js has been renamed to main.js. main.js is the main point of configuration for storybook.
  • config.js has been renamed to preview.js. preview.js configures the "preview" iframe that renders your components.
  • addons.js has been renamed to manager.js. manager.js configures Storybook's "manager" UI that wraps the preview, and also configures addons panel.

Please follow the official Storybook version 5.2.x to 5.3.x migration guide to change your files accordingly.

Step 2: Going from version 5.3 to 6.0

Please check out this official Storybook 6 Migration Guide article, as well as the detailed guides here.

  • One big change in Storybook version 6 is that it has built-in Typescript support. This means that you can remove Typescript configurations from your configuration files.
  • Please also check that your stories match any differences in syntax introduced in versions 5.3 and 6.0.

Step 3: Upgrade all @storybook/* packages in your project

Check your package.json file for all @storybook packages. Install the latest versions of these, using yarn:

For example:

yarn add --dev @storybook/react@latest

Step 4: Check that everything works as expected

Check that everything works as expected. If you are still having trouble, you can submit you issue in the GitHub Nx repo. We wish you luck!

Sample files for manual upgrade

If you have not changed the content of the files which the storybook-configuration generator produced, you can use the following samples to migrate to Storybook 6:

Configuring the root ./storybook directory

  • In the root ./storybook directory, create a new file named main.js with the following content:
1module.exports = {
2  stories: [],
3  addons: ['@storybook/addon-essentials'],
4};
  • If you have any addons in the addons.js file, add them in the addons array in the main.js file. If you are using the default generated files without any changes, you should not have any addons. You can now delete the addons.js file.

  • The other two files remain unchanged.

Configuring the Storybook instances across apps and libraries - the library-specific ./storybook directories

  • In the library ./storybook directory, create a new file named main.js with the following content:
1const lib_main_module = require('../../.storybook/main');
2
3lib_main_module.stories.push('../src/lib/**/*.stories.mdx');
4lib_main_module.stories.push('../src/lib/**/*.stories.@(js|jsx|ts|tsx)');
5module.exports = lib_main_module;

Please take extra care making sure that the path to the root ./storybook directory provided in the first line is correct.

  • If you have any addons in the addons.js file, add them in the addons array in the main.js file. You can add any addons in the addons module array using the following syntax:
1lib_main_module.addons.push('<YOUR_ADDON_HERE>');

After you add any addons in the main.js file, you can safely delete the addons.js file. If you are using the default generated files without any changes, your addons.js file should be empty (but an import line, referencing the root addons.js file).

  • Rename the file config.js to preview.js and remove the last line where your stories paths are configured. Now, the contents of the preview.js file will look like this:
1import { addDecorator } from '@storybook/react';
2
3addDecorator(<YourDecorator>);
  • Modify the contents of webpack.config.js. Remove the following lines, which are the TypeScript configuration, which is not needed by Storybook any more:
1config.resolve.extensions.push('.ts', '.tsx');
2config.module.rules.push({
3  test: /\.(ts|tsx)$/,
4  loader: require.resolve('babel-loader'),
5  options: {
6    presets: [
7      '@babel/preset-env',
8      '@babel/preset-react',
9      '@babel/preset-typescript',
10    ],
11  },
12});

Check final folder structure

Your folder structure should now look like this:

<workspace name>/
├── .storybook/
│   ├── main.js
│   ├── tsconfig.json
│   └── webpack.config.js
├── apps/
├── libs/
│    └── <library name>/
│       ├── .storybook/
│       │   ├── main.js
│       │   ├── tsconfig.json
│       │   └── webpack.config.js
│       ├── src/
│       ├── README.md
│       ├── tsconfig.json
│       └── etc...
├── nx.json
├── package.json
├── README.md
└── etc...