Search code examples
angularangular-materialcodesandbox

CodeSandbox and Angular Material 13


I'm trying to get an Angular Material instance working with the latest (13.x) version, using CodeSandbox (and by extension the CLI). I am receiving the dreaded, "Could not find angular material Core Theme" error. Note that I have tried importing via the usual routes

angular-cli.json: "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css"],

styles.css:

@import "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";
@import "~@angular/material/prebuilt-themes/deeppurple-amber.css";

Note that the first approach doesn't "fail", but doesn't help at all with theming. The second approach yields a different error:

postcss-import: /src/@angular/material/prebuilt-themes/indigo-pink.css:1:1: Unknown word

Sample project is at https://codesandbox.io/s/idle-test-r3rqp3. Any tips or pointers are appreciated.

Joe Allan


Solution

  • The config file should be called angular.json in angular 13. Here's a stackblitz with angular 13 and material set up: https://stackblitz.com/edit/angular-ivy-w8wwx5?file=angular.json

    and here's the config file:

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
      "version": 1,
      "newProjectRoot": "projects",
      "projects": {
        "demo": {
          "root": "",
          "sourceRoot": "src",
          "projectType": "application",
          "prefix": "app",
          "schematics": {},
          "architect": {
            "build": {
              "builder": "@angular-devkit/build-angular:browser",
              "options": {
                "outputPath": "dist/demo",
                "index": "src/index.html",
                "main": "src/main.ts",
                "polyfills": "src/polyfills.ts",
                "tsConfig": "src/tsconfig.app.json",
                "assets": ["src/favicon.ico", "src/assets"],
                "styles": [
                  "src/styles.css",
                  "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css"
                ],
                "scripts": []
              },
              "configurations": {
                "production": {
                  "fileReplacements": [
                    {
                      "replace": "src/environments/environment.ts",
                      "with": "src/environments/environment.prod.ts"
                    }
                  ],
                  "optimization": true,
                  "outputHashing": "all",
                  "sourceMap": false,
                  "extractCss": true,
                  "namedChunks": false,
                  "aot": true,
                  "extractLicenses": true,
                  "vendorChunk": false,
                  "buildOptimizer": true
                }
              }
            },
            "serve": {
              "builder": "@angular-devkit/build-angular:dev-server",
              "options": {
                "browserTarget": "demo:build"
              },
              "configurations": {
                "production": {
                  "browserTarget": "demo:build:production"
                }
              }
            },
            "extract-i18n": {
              "builder": "@angular-devkit/build-angular:extract-i18n",
              "options": {
                "browserTarget": "demo:build"
              }
            },
            "test": {
              "builder": "@angular-devkit/build-angular:karma",
              "options": {
                "main": "src/test.ts",
                "polyfills": "src/polyfills.ts",
                "tsConfig": "src/tsconfig.spec.json",
                "karmaConfig": "src/karma.conf.js",
                "styles": [
                  "styles.css",
                  "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css"
                ],
                "scripts": [],
                "assets": ["src/favicon.ico", "src/assets"]
              }
            },
            "lint": {
              "builder": "@angular-devkit/build-angular:tslint",
              "options": {
                "tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
                "exclude": ["**/node_modules/**"]
              }
            }
          }
        }
      },
      "defaultProject": "demo"
    }