skip to Main Content

How do we configure Angular Themes for Storybook such that the Storybook rendered components can load all the classes provided by the Angular theme?

There’s a Styling and CSS "Hint" in the storybook documentation, but no example at how to go about loading the compiled global Angular SASS style sheets.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out it's really simple. To import styles.scss just add this to ./storybook/preview.js:

    import '!style-loader!css-loader!sass-loader!../src/styles.scss';
    

  2. This answer is Nx specific but should be adaptable.

    If you’re using a singular storybook-host

    Example libs/storybook-host/project.json

    {
      "name": "storybook-host",
      "$schema": "../../node_modules/nx/schemas/project-schema.json",
      "projectType": "library",
      "sourceRoot": "libs/storybook-host/src",
      "prefix": "yourOrg",
      "targets": {
        "storybook": {
          "executor": "@storybook/angular:start-storybook",
          "options": {
            "port": 4400,
            "configDir": "libs/storybook-host/.storybook",
            "browserTarget": "storybook-host:build-storybook",
            "compodoc": false,
            "styles": [
              {
                "input": "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
              }
            ]
          },
          "configurations": {
            "ci": {
              "quiet": true
            }
          }
        },
        "build-storybook": {
          "executor": "@storybook/angular:build-storybook",
          "outputs": ["{options.outputDir}"],
          "options": {
            "outputDir": "dist/storybook/storybook-host",
            "configDir": "libs/storybook-host/.storybook",
            "browserTarget": "storybook-host:build-storybook",
            "compodoc": false,
            "styles": [
              {
                "input": "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
              }
            ]
          },
          "configurations": {
            "ci": {
              "quiet": true
            }
          }
        },
        "test-storybook": {
          "executor": "nx:run-commands",
          "options": {
            "command": "test-storybook -c libs/storybook-host/.storybook --url=http://localhost:4400"
          }
        }
      },
      "tags": ["type:storybook"],
      "implicitDependencies": [
        // ui library names
      ]
    }
    

    With preview.html

    <link
      href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet"
    />
    

    A hazardous guess at what angular.json will look like (it’s been awhile since I’ve messed around with these files).

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
      "version": 1,
      "newProjectRoot": "projects",
      "projects": {
        "my-project": {
          "projectType": "application",
          "schematics": {
            "@schematics/angular:component": {
              "style": "scss"
            }
          },
          "root": "",
          "sourceRoot": "src",
          "prefix": "app",
          "architect": {
            // ...
            "extract-i18n": {
              "builder": "@angular-devkit/build-angular:extract-i18n",
              "options": {
                "browserTarget": "my-project:build"
              }
            },
            // ...
            // starting from here
            "build-storybook": {
              "builder": "@storybook/angular:build-storybook",
              "options": {
                "outputPath": "dist/storybook"
                "configDir": ".storybook",
                "compodoc": false,
                "styles": [
                  {
                    "input": "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
                  }
                ]
              },
              // similar for storybook, test-storybook
            }
          }
        }
      },
      "defaultProject": "my-project"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search