Brandon Roberts headshot rounded

Brandon Roberts

Notes to my future self

TwitterGitHubBlueskyYouTube
Using Storybook with Angular and Vite 🎨

Using Storybook with Angular and Vite 🎨

September 26, 2024 - 3 min read

Storybook is a frontend workshop for building UI components and pages in isolation.

By default, Angular and Storybook uses Webpack to build and serve the Storybook application. Angular has already switched over to using Vite as a development server, so you may be interested in using Vite for other parts of your development workflow.

This post guides you through the process of switching to building and serving your Storybook with Angular using Vite. This process can be applied to any Angular project using Storybook.

Setting up Storybook

If you don't have Storybook setup already, run the following command to initialize Storybook for your project:

npx storybook@latest init --type angular

This installs the necessary Storybook dependencies, and sets up a Storybook with a few example components.

Follow the provided prompts, and commit your changes.

Installing the Storybook and Vite Integration

Next, install the AnalogJS Storybook Angular integration using your preferred package manager:

npm install @analogjs/storybook-angular --save-dev

Configuring Storybook

Update the .storybook/main.ts file to use the @analogjs/storybook-angular package.

import { StorybookConfig } from '@analogjs/storybook-angular';

const config: StorybookConfig = {
  // other config, addons, etc.
  framework: {
    name: "@analogjs/storybook-angular",
    options: {},
  },
  docs: {},
};

export default config;

Remove the existing webpackFinal config function if present.

Update the angular.json or project.json storybook targets to use the @analogjs/storybook-angular builders:

    "storybook": {
      "builder": "@analogjs/storybook-angular:start-storybook",
    },
    "build-storybook": {
      "builder": "@analogjs/storybook-angular:build-storybook"
    }

Remove any webpack-specific config options.

If necessary, add the /storybook-static folder to your .gitignore file.

Setting Up Global Styles

To register global styles, add them to the @analogjs/storybook-angular builder options in the angular.json or project.json.

    "storybook": {
      "builder": "@analogjs/storybook-angular:start-storybook",
      "options": {
        // ... other options
        "styles": [
          "src/styles.css"
        ]
      }
    },
    "build-storybook": {
      "builder": "@analogjs/storybook-angular:build-storybook",
      "options": {
        // ... other options
        "styles": [
          "src/styles.css"
        ]
      }
    }

Running Storybook

Use the existing scripts to run your storybook in development.

npm run storybook

Building Storybook

Run the storybook commands for building the storybook.

npm run build-storybook

Storybook is a great way to build and iterate on your components in isolation before integrating them into your application.

If you enjoyed this post, click the :heart: so other people will see it. Follow me on X and subscribe to my YouTube Channel for more content!