Skip to main content

Publishing Storybook to Chromatic

By Asllan Makaj ·

Creating reusable and efficient components is key to building successful applications. One of the most effective ways to test and showcase these components is through the use of Storybook, a powerful tool for developing UI components in isolation. Storybook allows developers to isolate and test individual components in a variety of scenarios, making it an essential tool in any developer's toolkit. However, once you've created your Storybook, how do you share it with others? That's where Chromatic comes in. In this blog post, we'll explore how to publish your Storybook to Chromatic, and why it's a valuable addition to your development workflow.

To get started with publishing your Storybook to Chromatic , the first step is to create a new project in Chromatic. Once you've signed up for a free account, you can easily create a new project and choose the repository source code where your Storybook lives. This will allow Chromatic to track changes to your components over time and provide you with valuable insights into how your UI is evolving. Once you've added your project to Chromatic, you'll be ready to start publishing your Storybook and collaborating with other developers.

Chromatic Projects page for the Shko-Online organization with the Add project button
Chromatic 'Choose project to add' list showing GitHub repositories such as LookupObjects-Mock

After adding your project to Chromatic, the next step is to install the Chromatic in your project. To install the chromatic package, simply run the following command in your project directory:

terminal
npm install --save-dev chromatic

Now that you have installed the Chromatic, you can start publishing your Storybook to Chromatic. To do this, simply run the following command in your project directory:

terminal
npx chromatic --project-token=<your-project-token>

Replace <your-project-token> with the project token provided by Chromatic when you added your project. This command will build your Storybook and publish it to Chromatic, where you can view it in the Chromatic dashboard.

Once your Storybook is published, you can start collaborating with other developers by sharing the link to your Chromatic project. You can also use Chromatic to track changes to your components over time, and ensure that your UI remains consistent across your applications.

To automate the process of publishing your Storybook every time you make changes, you can set up a GitHub Action

To set up a a GitHub Action for your repository, create a new file called.github/workflows/storybook.yml in the root directory of your repository.

storybook.yml
name: Deploy Storybook to Chromatic

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

# Allow one concurrent deployment
concurrency:
  group: "chromatic"
  cancel-in-progress: true

jobs:
  # Build job
  build:
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with: 
          fetch-depth: 0
      - name: Setup NodeJs
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'npm'
      - name: Install Dependencies
        run: npm install
      - name: Publish to Chromatic
        uses: chromaui/action@v1
        # Chromatic GitHub Action options
        with:
          # 👇 Chromatic projectToken, refer to the manage page to obtain it.
          projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}

Then set up the required secrets GitHub repository. Go to your repository's settings and add the following secrets:

GitHub repository settings adding a new Actions secret named CHROMATIC_PROJECT_TOKEN

Once you've set up the GitHub Action and added the required secrets, you're ready to start publishing your Storybook to Chromatic automatically every time you make changes to your project. So push your latest changes to your repository

Now that you've set up the GitHub Action, committed the changes to your repository, and triggered the action, you can go to the Actions section in your GitHub repository to view the progress of the action.

Once the action has completed successfully, you can go to Chromatic to view the latest version of your Storybook. You should see that the changes you made have been published automatically.

Chromatic library for LookupObjects-Mock showing the published MultiSelect, MultiTable and WebAPI control stories