Skip to content

Commit d15431b

Browse files
crisbetommalerba
authored andcommitted
fix(common): account for async hammer loader when checking whether hammer is defined (#12933)
Since HammerJS can now be loaded asynchronously, we have to account for it in our sanity checks.
1 parent 38e492f commit d15431b

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

src/lib/core/common-behaviors/common-module.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {NgModule, InjectionToken, Optional, Inject, isDevMode} from '@angular/core';
10+
import {HammerLoader, HAMMER_LOADER} from '@angular/platform-browser';
1011
import {BidiModule} from '@angular/cdk/bidi';
1112

1213

@@ -44,7 +45,10 @@ export class MatCommonModule {
4445
/** Reference to the global 'window' object. */
4546
private _window = typeof window === 'object' && window ? window : null;
4647

47-
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean) {
48+
constructor(
49+
@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean,
50+
@Optional() @Inject(HAMMER_LOADER) private _hammerLoader?: HammerLoader) {
51+
4852
if (this._areChecksEnabled() && !this._hasDoneGlobalChecks) {
4953
this._checkDoctypeIsDefined();
5054
this._checkThemeIsPresent();
@@ -74,27 +78,29 @@ export class MatCommonModule {
7478
private _checkThemeIsPresent(): void {
7579
// We need to assert that the `body` is defined, because these checks run very early
7680
// and the `body` won't be defined if the consumer put their scripts in the `head`.
77-
if (this._document && this._document.body && typeof getComputedStyle === 'function') {
78-
const testElement = this._document.createElement('div');
79-
80-
testElement.classList.add('mat-theme-loaded-marker');
81-
this._document.body.appendChild(testElement);
82-
83-
const computedStyle = getComputedStyle(testElement);
84-
85-
// In some situations the computed style of the test element can be null. For example in
86-
// Firefox, the computed style is null if an application is running inside of a hidden iframe.
87-
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=548397
88-
if (computedStyle && computedStyle.display !== 'none') {
89-
console.warn(
90-
'Could not find Angular Material core theme. Most Material ' +
91-
'components may not work as expected. For more info refer ' +
92-
'to the theming guide: https://material.angular.io/guide/theming'
93-
);
94-
}
95-
96-
this._document.body.removeChild(testElement);
81+
if (!this._document || !this._document.body || typeof getComputedStyle !== 'function') {
82+
return;
83+
}
84+
85+
const testElement = this._document.createElement('div');
86+
87+
testElement.classList.add('mat-theme-loaded-marker');
88+
this._document.body.appendChild(testElement);
89+
90+
const computedStyle = getComputedStyle(testElement);
91+
92+
// In some situations the computed style of the test element can be null. For example in
93+
// Firefox, the computed style is null if an application is running inside of a hidden iframe.
94+
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=548397
95+
if (computedStyle && computedStyle.display !== 'none') {
96+
console.warn(
97+
'Could not find Angular Material core theme. Most Material ' +
98+
'components may not work as expected. For more info refer ' +
99+
'to the theming guide: https://material.angular.io/guide/theming'
100+
);
97101
}
102+
103+
this._document.body.removeChild(testElement);
98104
}
99105

100106
/** Checks whether HammerJS is available. */
@@ -103,7 +109,7 @@ export class MatCommonModule {
103109
return;
104110
}
105111

106-
if (this._areChecksEnabled() && !this._window['Hammer']) {
112+
if (this._areChecksEnabled() && !this._window['Hammer'] && !this._hammerLoader) {
107113
console.warn(
108114
'Could not find HammerJS. Certain Angular Material components may not work correctly.');
109115
}

0 commit comments

Comments
 (0)