-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
feat(theming): log a warning if core theme isn't loaded #2846
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {NgModule, isDevMode} from '@angular/core'; | ||
|
||
/** Whether the theme presence has already been checked. */ | ||
let hasBeenChecked = false; | ||
|
||
/** | ||
* Module that verifies that the user has loaded the core theming file, | ||
* without which most Material module won't work as expected. | ||
* | ||
* Note on testing methodology: A more efficient way to check for the theme | ||
* would be to loop through the `document.styleSheets`, however most browsers | ||
* don't expose the stylesheet rules, if the file was loaded from another domain. | ||
* This would trigger false positives if the theme is being loaded from a CDN. | ||
* | ||
* @docs-private | ||
*/ | ||
@NgModule() | ||
export class MdThemeCheckModule { | ||
constructor() { | ||
if (hasBeenChecked || typeof document === 'undefined' || !isDevMode()) { | ||
return; | ||
} | ||
|
||
let testElement = document.createElement('div'); | ||
|
||
testElement.classList.add('md-theme-loaded-marker'); | ||
document.body.appendChild(testElement); | ||
|
||
if (getComputedStyle(testElement).display !== 'none') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if instead we did something like .mat-theme-load-test {
width: 500px;
} Then we create two elements (position absolute, min heights/width 1px), append them to body, and add the class to one of them then see if they have the same/different offsetWidth? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that it may end up being slower. Both |
||
console.warn( | ||
'Could not find Angular Material core theme. Most Material ' + | ||
'components may not work as expected. For more info refer ' + | ||
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md' | ||
); | ||
} | ||
|
||
document.body.removeChild(testElement); | ||
hasBeenChecked = true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we roll this functionality into
CompatibilityModule
so we don't have to add another module on every components?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reasoning for not adding it there is that, as far as I understand, it won't be a permanent module, but rather something that's there for a while until people have migrated completely to M2.