Skip to content

Commit dabb967

Browse files
authored
fix(material-experimental/theming): set up core theme and app tokens (#28431)
* Sets up the `core` theme for tokens. * Adds the app-level tokens.
1 parent 08c97ab commit dabb967

File tree

5 files changed

+106
-49
lines changed

5 files changed

+106
-49
lines changed

src/dev-app/theme-m3.scss

+3-11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ html {
9696
@include mat.card-theme($light-theme);
9797
@include mat.checkbox-theme($light-theme);
9898
@include mat.chips-theme($light-theme);
99+
@include mat.core-theme($light-theme);
99100
@include mat.datepicker-theme($light-theme);
100101
@include mat.dialog-theme($light-theme);
101102
@include mat.divider-theme($light-theme);
@@ -108,14 +109,10 @@ html {
108109
@include mat.input-theme($light-theme);
109110
@include mat.list-theme($light-theme);
110111
@include mat.menu-theme($light-theme);
111-
@include mat.optgroup-theme($light-theme);
112-
@include mat.option-theme($light-theme);
113112
@include mat.paginator-theme($light-theme);
114-
@include mat.pseudo-checkbox-theme($light-theme);
115113
@include mat.progress-bar-theme($light-theme);
116114
@include mat.progress-spinner-theme($light-theme);
117115
@include mat.radio-theme($light-theme);
118-
@include mat.ripple-theme($light-theme);
119116
@include mat.select-theme($light-theme);
120117
@include mat.sidenav-theme($light-theme);
121118
@include mat.slide-toggle-theme($light-theme);
@@ -146,6 +143,7 @@ html {
146143
@include mat.card-color($dark-theme);
147144
@include mat.checkbox-color($dark-theme);
148145
@include mat.chips-color($dark-theme);
146+
@include mat.core-color($dark-theme);
149147
@include mat.datepicker-color($dark-theme);
150148
@include mat.dialog-color($dark-theme);
151149
@include mat.divider-color($dark-theme);
@@ -158,14 +156,10 @@ html {
158156
@include mat.input-color($dark-theme);
159157
@include mat.list-color($dark-theme);
160158
@include mat.menu-color($dark-theme);
161-
@include mat.optgroup-color($dark-theme);
162-
@include mat.option-color($dark-theme);
163159
@include mat.paginator-color($dark-theme);
164-
@include mat.pseudo-checkbox-color($dark-theme);
165160
@include mat.progress-bar-color($dark-theme);
166161
@include mat.progress-spinner-color($dark-theme);
167162
@include mat.radio-color($dark-theme);
168-
@include mat.ripple-color($dark-theme);
169163
@include mat.select-color($dark-theme);
170164
@include mat.sidenav-color($dark-theme);
171165
@include mat.slide-toggle-color($dark-theme);
@@ -195,6 +189,7 @@ html {
195189
@include mat.card-density($scale-theme);
196190
@include mat.checkbox-density($scale-theme);
197191
@include mat.chips-density($scale-theme);
192+
@include mat.core-density($scale-theme);
198193
@include mat.datepicker-density($scale-theme);
199194
@include mat.dialog-density($scale-theme);
200195
@include mat.divider-density($scale-theme);
@@ -207,10 +202,7 @@ html {
207202
@include mat.input-density($scale-theme);
208203
@include mat.list-density($scale-theme);
209204
@include mat.menu-density($scale-theme);
210-
@include mat.optgroup-density($scale-theme);
211-
@include mat.option-density($scale-theme);
212205
@include mat.paginator-density($scale-theme);
213-
@include mat.pseudo-checkbox-density($scale-theme);
214206
@include mat.progress-bar-density($scale-theme);
215207
@include mat.progress-spinner-density($scale-theme);
216208
@include mat.radio-density($scale-theme);

src/material-experimental/theming/_custom-tokens.scss

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
);
3232
}
3333

34+
/// Generates custom tokens for the app.
35+
/// @param {Map} $systems The MDC system tokens
36+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
37+
/// @return {Map} A set of custom tokens for the app
38+
@function app($systems, $exclude-hardcoded) {
39+
@return (
40+
background-color: map.get($systems, md-sys-color, background),
41+
text-color: map.get($systems, md-sys-color, on-background),
42+
);
43+
}
44+
3445
/// Generates custom tokens for the mat-badge.
3546
/// @param {Map} $systems The MDC system tokens
3647
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values

src/material-experimental/theming/_m3-tokens.scss

+5
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@
670670
671671
// Choose values for our made up tokens based on MDC system tokens or sensible hardcoded
672672
// values.
673+
_namespace-tokens(
674+
(mat, app),
675+
custom-tokens.app($systems, $exclude-hardcoded),
676+
$token-slots
677+
),
673678
_namespace-tokens(
674679
(mdc, tab-indicator),
675680
custom-tokens.tab-indicator($systems, $exclude-hardcoded),

src/material/core/_core-theme.scss

+85-38
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,51 @@
1010
@use './typography/typography';
1111
@use './tokens/token-utils';
1212
@use './tokens/m2/mat/app' as tokens-mat-app;
13+
@use './tokens/m2/mat/ripple' as tokens-mat-ripple;
14+
@use './tokens/m2/mat/option' as tokens-mat-option;
15+
@use './tokens/m2/mat/full-pseudo-checkbox' as tokens-mat-full-pseudo-checkbox;
16+
@use './tokens/m2/mat/minimal-pseudo-checkbox' as tokens-mat-minimal-pseudo-checkbox;
1317

1418
@mixin base($theme) {
15-
@include ripple-theme.base($theme);
16-
@include option-theme.base($theme);
17-
@include optgroup-theme.base($theme);
18-
@include pseudo-checkbox-theme.base($theme);
19-
// TODO(mmalerba): Move additional core base tokens here
19+
@if inspection.get-theme-version($theme) == 1 {
20+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, base));
21+
}
22+
@else {
23+
@include ripple-theme.base($theme);
24+
@include option-theme.base($theme);
25+
@include optgroup-theme.base($theme);
26+
@include pseudo-checkbox-theme.base($theme);
27+
}
2028
}
2129

2230
@mixin color($theme) {
23-
@include ripple-theme.color($theme);
24-
@include option-theme.color($theme);
25-
@include optgroup-theme.color($theme);
26-
@include pseudo-checkbox-theme.color($theme);
27-
@include sass-utils.current-selector-or-root() {
28-
@include token-utils.create-token-values(tokens-mat-app.$prefix,
29-
tokens-mat-app.get-color-tokens($theme));
31+
@if inspection.get-theme-version($theme) == 1 {
32+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, color));
3033
}
34+
@else {
35+
@include ripple-theme.color($theme);
36+
@include option-theme.color($theme);
37+
@include optgroup-theme.color($theme);
38+
@include pseudo-checkbox-theme.color($theme);
39+
@include sass-utils.current-selector-or-root() {
40+
@include token-utils.create-token-values(tokens-mat-app.$prefix,
41+
tokens-mat-app.get-color-tokens($theme));
42+
}
3143

32-
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
33-
// `mat-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element is
34-
// elevated.
35-
@for $zValue from 0 through 24 {
36-
$selector: elevation.$prefix + $zValue;
37-
// We need the `mat-mdc-elevation-specific`, because some MDC mixins
38-
// come with elevation baked in and we don't have a way of removing it.
39-
.#{$selector}, .mat-mdc-elevation-specific.#{$selector} {
40-
@include private.private-theme-elevation($zValue, $theme);
44+
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
45+
// `mat-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element is
46+
// elevated.
47+
@for $zValue from 0 through 24 {
48+
$selector: elevation.$prefix + $zValue;
49+
// We need the `mat-mdc-elevation-specific`, because some MDC mixins
50+
// come with elevation baked in and we don't have a way of removing it.
51+
.#{$selector}, .mat-mdc-elevation-specific.#{$selector} {
52+
@include private.private-theme-elevation($zValue, $theme);
53+
}
4154
}
4255
}
4356

57+
// TODO(crisbeto): move this into the base.
4458
// Marker that is used to determine whether the user has added a theme to their page.
4559
@at-root {
4660
.mat-theme-loaded-marker {
@@ -50,34 +64,67 @@
5064
}
5165

5266
@mixin typography($theme) {
53-
@include option-theme.typography($theme);
54-
@include optgroup-theme.typography($theme);
55-
@include pseudo-checkbox-theme.typography($theme);
56-
@include ripple-theme.typography($theme);
67+
@if inspection.get-theme-version($theme) == 1 {
68+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, typography));
69+
}
70+
@else {
71+
@include option-theme.typography($theme);
72+
@include optgroup-theme.typography($theme);
73+
@include pseudo-checkbox-theme.typography($theme);
74+
@include ripple-theme.typography($theme);
75+
}
5776
}
5877

5978
@mixin density($theme) {
60-
@include option-theme.density($theme);
61-
@include optgroup-theme.density($theme);
62-
@include pseudo-checkbox-theme.density($theme);
63-
@include ripple-theme.density($theme);
79+
@if inspection.get-theme-version($theme) == 1 {
80+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
81+
}
82+
@else {
83+
@include option-theme.density($theme);
84+
@include optgroup-theme.density($theme);
85+
@include pseudo-checkbox-theme.density($theme);
86+
@include ripple-theme.density($theme);
87+
}
6488
}
6589

6690
// Mixin that renders all of the core styles that depend on the theme.
67-
@mixin theme($theme) {
91+
@mixin theme($theme, $options...) {
6892
// Wrap the sub-theme includes in the duplicate theme styles mixin. This ensures that
6993
// there won't be multiple warnings. e.g. if `mat-core-theme` reports a warning, then
7094
// the imported themes (such as `mat-ripple-theme`) should not report again.
7195
@include theming.private-check-duplicate-theme-styles($theme, 'mat-core') {
72-
@include base($theme);
73-
@if inspection.theme-has($theme, color) {
74-
@include color($theme);
75-
}
76-
@if inspection.theme-has($theme, density) {
77-
@include density($theme);
96+
@if inspection.get-theme-version($theme) == 1 {
97+
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
7898
}
79-
@if inspection.theme-has($theme, typography) {
80-
@include typography($theme);
99+
@else {
100+
@include base($theme);
101+
@if inspection.theme-has($theme, color) {
102+
@include color($theme);
103+
}
104+
@if inspection.theme-has($theme, density) {
105+
@include density($theme);
106+
}
107+
@if inspection.theme-has($theme, typography) {
108+
@include typography($theme);
109+
}
81110
}
82111
}
83112
}
113+
114+
@mixin _theme-from-tokens($tokens, $options...) {
115+
$mat-app-tokens: token-utils.get-tokens-for($tokens, tokens-mat-app.$prefix, $options...);
116+
$mat-ripple-tokens: token-utils.get-tokens-for($tokens, tokens-mat-ripple.$prefix, $options...);
117+
$mat-option-tokens: token-utils.get-tokens-for($tokens, tokens-mat-option.$prefix, $options...);
118+
$mat-full-pseudo-checkbox-tokens: token-utils.get-tokens-for($tokens,
119+
tokens-mat-full-pseudo-checkbox.$prefix, $options...);
120+
$mat-minimal-pseudo-checkbox-tokens: token-utils.get-tokens-for($tokens,
121+
tokens-mat-minimal-pseudo-checkbox.$prefix, $options...);
122+
123+
@include token-utils.create-token-values(tokens-mat-app.$prefix, $mat-app-tokens);
124+
@include token-utils.create-token-values(tokens-mat-ripple.$prefix, $mat-ripple-tokens);
125+
@include token-utils.create-token-values(tokens-mat-option.$prefix, $mat-option-tokens);
126+
@include token-utils.create-token-values(tokens-mat-full-pseudo-checkbox.$prefix,
127+
$mat-full-pseudo-checkbox-tokens);
128+
@include token-utils.create-token-values(tokens-mat-minimal-pseudo-checkbox.$prefix,
129+
$mat-minimal-pseudo-checkbox-tokens);
130+
}

src/material/core/tokens/m2/_index.scss

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use 'sass:map';
22
@use 'sass:meta';
33
@use '../../style/sass-utils';
4+
@use './mat/app' as tokens-mat-app;
45
@use './mat/autocomplete' as tokens-mat-autocomplete;
56
@use './mat/badge' as tokens-mat-badge;
67
@use './mat/text-button' as tokens-mat-text-button;
@@ -107,6 +108,7 @@
107108
/// )
108109
@function m2-tokens-from-theme($theme) {
109110
@return sass-utils.deep-merge-all(
111+
_get-tokens-for-module($theme, tokens-mat-app),
110112
_get-tokens-for-module($theme, tokens-mat-autocomplete),
111113
_get-tokens-for-module($theme, tokens-mat-badge),
112114
_get-tokens-for-module($theme, tokens-mat-bottom-sheet),

0 commit comments

Comments
 (0)