Skip to content

Commit 068f416

Browse files
committed
feat(theming): log a warning if core theme isn't loaded
* Checks the user's loaded stylesheets and logs a warning if the Material core theme isn't loaded. * Fixes a wrong `typeof` check when determining the doctype. Fixes #2828. Note: I originally went with looping through the `document.styleSheets` to check whether the selector is defined, however I had to switch back to `getComputedStyle`, because browsers don't expose the `document.styleSheets`, if the CSS file is being loaded from another domain. This would've caused the warning to be logged if the user loads over a CDN.
1 parent a25e7bb commit 068f416

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/lib/core/_core.scss

+5
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@
3636
$background: map-get($theme, background);
3737
background-color: mat-color($background, background);
3838
}
39+
40+
// Marker that is used to determine whether the user has added a theme to their page.
41+
.mat-theme-loaded-marker {
42+
display: none;
43+
}
3944
}

src/lib/core/compatibility/compatibility.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
} from '@angular/core';
1010
import {DOCUMENT} from '@angular/platform-browser';
1111

12+
/** Flag for whether we've checked that the theme is loaded. */
13+
let hasCheckedThemePresence = false;
1214

1315
export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibility-mode');
1416

@@ -170,14 +172,41 @@ export class CompatibilityModule {
170172
};
171173
}
172174

173-
constructor(@Optional() @Inject(DOCUMENT) document: any) {
174-
if (isDevMode() && typeof document && !document.doctype) {
175+
constructor(@Optional() @Inject(DOCUMENT) private _document: any) {
176+
this._checkDoctype();
177+
this._checkTheme();
178+
}
179+
180+
private _checkDoctype(): void {
181+
if (isDevMode() && this._document && !this._document.doctype) {
175182
console.warn(
176183
'Current document does not have a doctype. This may cause ' +
177184
'some Angular Material components not to behave as expected.'
178185
);
179186
}
180187
}
188+
189+
private _checkTheme(): void {
190+
if (hasCheckedThemePresence || !this._document || !isDevMode()) {
191+
return;
192+
}
193+
194+
let testElement = this._document.createElement('div');
195+
196+
testElement.classList.add('mat-theme-loaded-marker');
197+
this._document.body.appendChild(testElement);
198+
199+
if (getComputedStyle(testElement).display !== 'none') {
200+
console.warn(
201+
'Could not find Angular Material core theme. Most Material ' +
202+
'components may not work as expected. For more info refer ' +
203+
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md'
204+
);
205+
}
206+
207+
this._document.body.removeChild(testElement);
208+
hasCheckedThemePresence = true;
209+
}
181210
}
182211

183212

0 commit comments

Comments
 (0)