Skip to content

Commit 233c8a3

Browse files
committed
fix(material/core): M3 themes not inserting loaded marker
Fixes that all M3 themes were causing a "no theme has been loaded" warning to be logged, because they weren't inserting the loaded marker. Note: it's tempting to create the marker as a token, but we can't do it because tokens are output under a selector, whereas we want the marker to always be at the top level since we detect it by creating an element and inserting it into the `body`. Fixes angular#29115.
1 parent 1214b4e commit 233c8a3

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/material/core/_core-theme.scss

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
@use './tokens/m2/mat/full-pseudo-checkbox' as tokens-mat-full-pseudo-checkbox;
1717
@use './tokens/m2/mat/minimal-pseudo-checkbox' as tokens-mat-minimal-pseudo-checkbox;
1818

19+
$_has-inserted-loaded-marker: false;
20+
21+
@mixin _theme-loaded-marker {
22+
@if not $_has-inserted-loaded-marker {
23+
$_has-inserted-loaded-marker: true !global;
24+
25+
// Marker that is used to determine whether the user has added a theme to their page.
26+
// Needs to be generated at the root, because themes may be nested inside classes.
27+
@at-root {
28+
.mat-theme-loaded-marker {
29+
display: none;
30+
}
31+
}
32+
}
33+
}
34+
1935
@mixin base($theme) {
2036
@if inspection.get-theme-version($theme) == 1 {
2137
@include _theme-from-tokens(inspection.get-theme-tokens($theme, base));
@@ -26,6 +42,9 @@
2642
@include optgroup-theme.base($theme);
2743
@include pseudo-checkbox-theme.base($theme);
2844
}
45+
46+
// The marker is a concrete style no matter which Material version we're targeting.
47+
@include _theme-loaded-marker;
2948
}
3049

3150
@mixin color($theme) {
@@ -54,14 +73,6 @@
5473
}
5574
}
5675
}
57-
58-
// TODO(crisbeto): move this into the base.
59-
// Marker that is used to determine whether the user has added a theme to their page.
60-
@at-root {
61-
.mat-theme-loaded-marker {
62-
display: none;
63-
}
64-
}
6576
}
6677

6778
@mixin typography($theme) {
@@ -127,6 +138,9 @@
127138
}
128139
}
129140
}
141+
142+
// The marker is a concrete style no matter which Material version we're targeting.
143+
@include _theme-loaded-marker;
130144
}
131145

132146
@mixin _theme-from-tokens($tokens, $options...) {

src/material/core/theming/tests/m3-theme.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {parse} from 'postcss';
1+
import {parse, Rule} from 'postcss';
22
import {compileString} from 'sass';
33
import {runfiles} from '@bazel/runfiles';
44
import * as path from 'path';
@@ -68,14 +68,18 @@ describe('M3 theme', () => {
6868
root.walkRules(rule => {
6969
selectors.push(rule.selector);
7070
});
71-
expect(selectors).toEqual(['html']);
71+
expect(selectors).toEqual(['html', '.mat-theme-loaded-marker']);
7272
});
7373

7474
it('should only emit CSS variables', () => {
7575
const root = parse(transpile(`html { @include mat.all-component-themes($theme); }`));
7676
const nonVarProps: string[] = [];
7777
root.walkDecls(decl => {
78-
if (!decl.prop.startsWith('--')) {
78+
if (
79+
!decl.prop.startsWith('--') &&
80+
// Skip the theme loaded marker since it can't be a variable.
81+
(decl.parent as Rule).selector !== '.mat-theme-loaded-marker'
82+
) {
7983
nonVarProps.push(decl.prop);
8084
}
8185
});

src/material/core/theming/tests/theming-inspection-api.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,14 @@ describe('theming inspection api', () => {
472472
div {
473473
@include mat.all-component-themes($theme);
474474
}`);
475-
expect(css).toBe('');
475+
expect(css).toBe(
476+
[
477+
// The marker is always included.
478+
`.mat-theme-loaded-marker {`,
479+
` display: none;`,
480+
`}`,
481+
].join('\n'),
482+
);
476483
});
477484
});
478485
});

0 commit comments

Comments
 (0)