Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming #4125

Closed
patrikx3 opened this issue Apr 17, 2017 · 36 comments · Fixed by #4178
Assignees
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@patrikx3
Copy link

Hi! Ciao! How Are You???

Bug, feature request, or proposal:

Could not find Angular Material core theme, although all core @include mat-core(); is included. I got 6 themes, all working except the warning.

What is the expected behavior?

No warning.

What is the current behavior?

Warning.

What are the steps to reproduce?

git clone https://github.com/patrikx3/corifeus-app-web-pages.git
cd corifeus-app-web-pages
yarn install
grunt run

See.

What is the use-case or motivation for changing an existing behavior?

Themes.

Which versions of Angular, Material, OS, browsers are affected?

Current.

Is there anything else we should know?

Nope.

@fxck
Copy link
Contributor

fxck commented Apr 18, 2017

Yeah, I'm getting this as well.

@crisbeto
Copy link
Member

We have a marker style that we check for on load. When are you loading your theme?

@patrikx3
Copy link
Author

It is dynamic, based on a cookie.

@patrikx3
Copy link
Author

but i think the mat-core is loading right away.
it is in a css. so instant i think.

@crisbeto
Copy link
Member

I can see you loading the mixins in a few places, but I couldn't find any instances of the mat-core or angular-material-theme mixins being included anywhere.

@patrikx3
Copy link
Author

@patrikx3
Copy link
Author

@crisbeto
Copy link
Member

Alright, then it's definitely an issue with Material checking too early. We probably can't anticipate when styles are loaded without a major performance hit, but we could add a flag to disable the warnings for advanced cases like this. What do you think @jelbourn?

@willshowell
Copy link
Contributor

Being able to disable the warning might help with #4056 too

@jelbourn
Copy link
Member

@crisbeto what if we just delay the check for a few seconds? That way it will still be visible to developers by the time they open the dev tools but should be enough time to load styles for initial view.

@crisbeto
Copy link
Member

We could, but that can still be race condition-ey. We can do something like a 5s delay if we really want to be safe.

@jelbourn
Copy link
Member

I'd be okay with that as a simple workaround

@crisbeto crisbeto self-assigned this Apr 18, 2017
@Torqode
Copy link

Torqode commented Apr 18, 2017

It seems like the warning itself could be removed in favor of a clearer set of requirements in the documentation.

I may be missing some other reason for its existence, but it appears that the compatibility module is just checking to make sure you've put @include mat-core(); in your style. Couldn't that requirement be made more obvious (e.g., bold) in the docs, rendering the warning redundant?

Personally, the idea of tweaking timers to do css checks feels a bit hacky.

@crisbeto
Copy link
Member

crisbeto commented Apr 18, 2017

We do have info about it in the docs @Torqode, however people tend to miss it, which ends up looking like it works in some cases, but breaks once you start using the overlay-based components. I'm not sure that making it bolder will solve the issue.

@kara kara added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Apr 18, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 20, 2017
Delays the sanity checks from the `CompatibilityModule` by 5 seconds, in order to give the user's base styles a better chance to load.

Fixes angular#4125.
crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 20, 2017
Delays the sanity checks from the `CompatibilityModule` by 5 seconds, in order to give the user's base styles a better chance to load.

Fixes angular#4125.
crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 21, 2017
Delays the sanity checks from the `CompatibilityModule` by 5 seconds, in order to give the user's base styles a better chance to load.

Fixes angular#4125.
crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 26, 2017
Allows users to disable the Material sanity checks, by providing a value for the `MATERIAL_SANITY_CHECKS` token:

```ts
@NgModule({
  providers: [
    {provide: MATERIAL_SANITY_CHECKS, useValue: false}
  ]

  // other config
});

```

Fixes angular#4125.
mmalerba pushed a commit that referenced this issue Apr 27, 2017
Allows users to disable the Material sanity checks, by providing a value for the `MATERIAL_SANITY_CHECKS` token:

```ts
@NgModule({
  providers: [
    {provide: MATERIAL_SANITY_CHECKS, useValue: false}
  ]

  // other config
});

```

Fixes #4125.
@MikeInSwitzerland
Copy link

This issue tied me in knots, but eventually, I realised that what Angular's own Materials tutorial doesn't tell us is that we must include this line in the "styles.css" file, to make the controls appear normally (certainly in Windows 7).

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

Trying to include that file using an "include", even in the index.html file, failed miserably for me. It could never locate this .css file.. even if I manually copied it into the same folder as index.html.

@zhanglongqi
Copy link

@MikeInSwitzerland It works for me. Thanks.

@ghost
Copy link

ghost commented Jul 26, 2017

How can I override the theme colors using scss and still include it before modules try to load and I get the Could not find Angular Material core theme error?

@ghost
Copy link

ghost commented Jul 26, 2017

@crisbeto GREAT! Your documentation does describe each directive such as mat-core and angular-material-theme. I GET THAT!!

Now, where do I refer to theme.scss so that it is compiled and included in the page before receiving the error Could not find Angular Material core theme. ?

Let me explain what we need answered in this thread. Lets say I have some basic css and the material theme color instructions all contained in theme.scss. Follow me so far?
Do I put theme.scss in tsconfig? E.g.:

{
    "compilerOptions": {
        "styles": {
             "./theme.scss"
        }
    }
}

NOPE! That's not going to work!

Where do I put theme.scss so that it is compiled? Maybe angular-cli.json?

{
    "apps": [
        {
            styles: [
                "./theme.scss"
            ]
        }
    ]
}

NO, that doesn't work because it doesn't compile scss, it just inserts css into the head of index.html.
Error: Could not find Angular Material core theme

In my app.component.ts?
https://github.com/ng-seed/universal/blob/master/src/client/app/app.component.ts

// external styles
import '../assets/sass/layout.scss';

NO! That only works with awesome-typescript-loader and webpack, doesn't work with angular-cli?

How about using encapsulation: ViewEncapsulation.None in app.component.ts?

@Component({
    selector: 'bc-app',
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: [TranslateService, LayoutService],
    templateUrl: './app.component.html',
    styleUrls: ['./theme.scss', './app.component.scss'],
    encapsulation: ViewEncapsulation.None,
})

HEY THAT WORKS! Except we still get this error message even though everything looks correct.

Any ideas? Anyone? Does every theme have to start with a precompiled color in order to resolve this error? Do you start with a precompiled theme and then override the theme meaning it injects multiple themes in to your page?

What is the most up to date method for doing this and why isn't it made clear on this page?
https://material.angular.io/guide/theming

@willshowell Provides a great example of a use case with linking multiple .scss files together:
#6063 (comment)

@p3x-robot
Copy link

I don't get it. I still get the error.

@Component({
    selector: 'cory-web-material-test-layout',
    templateUrl: './layout.html',
    encapsulation: ViewEncapsulation.None
})
@import '@angular/material/theming';
@include mat-core();

No matter I do I get the warning, and with electron, it doesn't load the SCSS. :(

In the web it works. Ok, warning.
But, in Electron, something is fishy.

@ghost
Copy link

ghost commented Jul 30, 2017

@patrikx3 I am not sure which loader supports importing SCSS directly in your typescript files but it's not the default angular one. Try using styleUrls: [] and moving your import code in to that, make sure you have a SCSS loader setup in web-config.

@p3x-robot
Copy link

Ciao!

How are you?
Thanks for looking at it. What do you mean not default angular? I tried AOT, JIT and the compiler with JIT at once, in the web works, though same warning though in dev.

I did as you said:

application.ts

@Component({
    selector: 'cory-home-app',
    template: `
    <div style="padding: 5px;">
        <router-outlet></router-outlet>
    </div>
  `,
    styleUrls: [
        './application.scss'
    ],
    encapsulation: ViewEncapsulation.None,
})
export class Application {
}

application.scss

@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
@import url('https://fonts.googleapis.com/css?family=Roboto');
@import '~@angular/material/theming';
@include mat-core();

It is a Material issue.

<md-icon class="mat-icon" role="img" aria-hidden="true">keyboard_arrow_right</md-icon>

it does not add for even the material classess

<md-icon class="mat-icon material-icons" role="img" aria-hidden="true">keyboard_arrow_right</md-icon>

Angular is working. The error is only in material issue, all Angular functions work.

@ghost
Copy link

ghost commented Jul 31, 2017 via email

@p3x-robot
Copy link

Ahh, if you have some something weird with Electron and Angular Material, make sure if you use ipcRenderer from Electron, make sure use it in the NgZone.run!!!!
Now it is fine, perfect. 1 month sadness!!!

Thanks guys so much!

@sharpevo
Copy link

It seems that none of the above solutions works for me. After tons of tests with rm -rf node_modules and npm install , the result is really weird.

angular-cli.json src/styles result
".../indigo-pink.css" ".../indigo-pink.css" fail
".../indigo-pink.css" ".../deeppurple-amber.css" fail
".../deeppurple-amber.css" ".../deeppurple-amber.css" fail
".../deeppurple-amber.css" ".../indigo-pink.css" success

Finally, the config is:

# angular-cli.json

      "styles": [
        "styles.css",
        "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
      ],

# src/styles.css

     @import "~@angular/material/prebuilt-themes/indigo-pink.css"

and the dependencies in package.json are:

  "dependencies": {
    "@angular/animations": "^4.3.5",
    "@angular/cdk": "^2.0.0-beta.8",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/flex-layout": "^2.0.0-beta.8",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.8",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "chart.js": "^2.5.0",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "material-design-icons": "^3.0.1",
    "ng2-charts": "^1.5.0",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.8.4"
  },

@siebmanb
Copy link

@sharpevo thanks a lot for your help. I had the issue when running tests using karma. Your fix does not work for this use case, however adding the theme file in karma.conf.js as follows fixes the issue

files: [
            {pattern: './src/test.ts', watched: false},
            'node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css'
        ],

@vincecampanale
Copy link

For newcomers, @siebmanb's solution also worked for me. Note that the absolute path should be used (e.g. node_modules/@angular/material/... instead of ../../node_modules/@angular/material/...). Thanks @siebmanb !!

@Vadorequest
Copy link

Okay, my mistake was the following:

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

I put this line in src/app/app.component.css instead of src/styles.css.

@leoncc
Copy link

leoncc commented Nov 11, 2017

Just to mention the file to edit where styles.css is configured is .angular-cli.json, not angular-cli.json. A hidden file on Unix OSes.

In src/styles.css, either this :
@import "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";

or this:
@import "~@angular/material/prebuilt-themes/deeppurple-amber.css";

worked for me on @angular/cdk 5.0.0-rc0.

@subramn
Copy link

subramn commented Nov 26, 2017

Still i get this error with 5.0.0-rc.1. all components are getting displayed but the warning in the console keeps coming.

Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming

I followed all the "Get started" instructions. is there anything i am missing. I am using webpack instead of systemjs.

any thoughts

@briosheje
Copy link

briosheje commented Nov 27, 2017

Experiencing the same in 5.0.0-rc.1

Tried a tons of stuff, but nothing seems to allow my app to see the css file. Whatever, I'm likely downgrading to rc.0 if it somehow works for some strange reasons :P

Update: the error still appears in rc0.

@crisbeto
Copy link
Member

crisbeto commented Nov 27, 2017

That warning is a sanity check that won't be done in production mode. If you want to disable it in development mode, you can use the MATERIAL_SANITY_CHECKS token:

import {MATERIAL_SANITY_CHECKS} from '@angular/material/core';

@NgModule({
  providers: [{provide: MATERIAL_SANITY_CHECKS, useValue: false}]
})
export class YourModule {}

@briosheje
Copy link

I've found a solution for electron.

Avoid including the file in the .scss files, include it in your vendor instead.

In a nutshell, include the file in a .ts file, ideally the vendor.ts file. This will properly include everything.

To make things clearer, in my case I had a app.style.scss file with:

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

I've removed this line and added in my vendor.ts file:

import '@angular/material/prebuilt-themes/deeppurple-amber.css';.

Also, I'm using webpack. The relevant part for CSS / scss is:

           {
                test: /\.scss$/,
                exclude: /node_modules/,
                loaders: ['to-string-loader','raw-loader','sass-loader']
            },

@rhanb
Copy link

rhanb commented Feb 7, 2018

I found a quick fix to avoid this issue:

themes.scss

@import '~@angular/material/theming';
@include mat-core();
// all custom themes imports below
@import './matooma-theme';
@import './yellow-mars-theme';

// all custom themes includes
// Include the alternative theme styles inside of a block with a CSS class. You can make this
// CSS class whatever you want. In this example, any component inside of an element with
// `.unicorn-dark-theme` will be affected by this alternate dark theme instead of the default theme.
.matooma-theme {
  @include angular-material-theme($matooma-theme);
}

.yellow-mars-theme {
  @include angular-material-theme($yellow-mars-theme);
}

style.scss

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

@import "./style/themes";

Just add a default angular material import in your style.scss. Not the best solution but works, no warning and no transparency on modals.

@aryaroudi
Copy link

if I don´t want to use the material thems and use my own, how can I hide this message?

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.