|
9 | 9 | } from '@angular/core';
|
10 | 10 | import {DOCUMENT} from '@angular/platform-browser';
|
11 | 11 |
|
| 12 | +/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */ |
| 13 | +let hasDoneGlobalChecks = false; |
12 | 14 |
|
13 | 15 | export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibility-mode');
|
14 | 16 |
|
@@ -170,14 +172,41 @@ export class CompatibilityModule {
|
170 | 172 | };
|
171 | 173 | }
|
172 | 174 |
|
173 |
| - constructor(@Optional() @Inject(DOCUMENT) document: any) { |
174 |
| - if (isDevMode() && typeof document && !document.doctype) { |
| 175 | + constructor(@Optional() @Inject(DOCUMENT) private _document: any) { |
| 176 | + if (!hasDoneGlobalChecks && isDevMode()) { |
| 177 | + this._checkDoctype(); |
| 178 | + this._checkTheme(); |
| 179 | + hasDoneGlobalChecks = true; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private _checkDoctype(): void { |
| 184 | + if (this._document && !this._document.doctype) { |
175 | 185 | console.warn(
|
176 | 186 | 'Current document does not have a doctype. This may cause ' +
|
177 | 187 | 'some Angular Material components not to behave as expected.'
|
178 | 188 | );
|
179 | 189 | }
|
180 | 190 | }
|
| 191 | + |
| 192 | + private _checkTheme(): void { |
| 193 | + if (this._document) { |
| 194 | + const 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://material.angular.io/guide/theming' |
| 204 | + ); |
| 205 | + } |
| 206 | + |
| 207 | + this._document.body.removeChild(testElement); |
| 208 | + } |
| 209 | + } |
181 | 210 | }
|
182 | 211 |
|
183 | 212 |
|
|
0 commit comments