Skip to content

Commit 5f14690

Browse files
authored
fix(material/core): use Platform to check whether to run theming check (#26477)
Uses the `Platform` service to figure out whether to run the theming check, instead of relying on the presence of `getComputedStyle`, because it might have been stubbed out. Fixes #26468.
1 parent 434ec52 commit 5f14690

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
import {HighContrastModeDetector} from '@angular/cdk/a11y';
1010
import {BidiModule} from '@angular/cdk/bidi';
11-
import {Inject, InjectionToken, NgModule, Optional} from '@angular/core';
11+
import {inject, Inject, InjectionToken, NgModule, Optional} from '@angular/core';
1212
import {VERSION as CDK_VERSION} from '@angular/cdk';
1313
import {DOCUMENT} from '@angular/common';
14-
import {_isTestEnvironment} from '@angular/cdk/platform';
14+
import {Platform, _isTestEnvironment} from '@angular/cdk/platform';
1515
import {VERSION} from '../version';
1616

1717
/** @docs-private */
@@ -65,12 +65,15 @@ export class MatCommonModule {
6565
this._hasDoneGlobalChecks = true;
6666

6767
if (typeof ngDevMode === 'undefined' || ngDevMode) {
68+
// Inject in here so the reference to `Platform` can be removed in production mode.
69+
const platform = inject(Platform, {optional: true});
70+
6871
if (this._checkIsEnabled('doctype')) {
6972
_checkDoctypeIsDefined(this._document);
7073
}
7174

7275
if (this._checkIsEnabled('theme')) {
73-
_checkThemeIsPresent(this._document);
76+
_checkThemeIsPresent(this._document, !!platform?.isBrowser);
7477
}
7578

7679
if (this._checkIsEnabled('version')) {
@@ -105,10 +108,10 @@ function _checkDoctypeIsDefined(doc: Document): void {
105108
}
106109

107110
/** Checks that a theme has been included. */
108-
function _checkThemeIsPresent(doc: Document): void {
111+
function _checkThemeIsPresent(doc: Document, isBrowser: boolean): void {
109112
// We need to assert that the `body` is defined, because these checks run very early
110113
// and the `body` won't be defined if the consumer put their scripts in the `head`.
111-
if (!doc.body || typeof getComputedStyle !== 'function') {
114+
if (!doc.body || !isBrowser) {
112115
return;
113116
}
114117

0 commit comments

Comments
 (0)