Skip to content

Commit f1a51f9

Browse files
committed
refactor(multiple): convert components to theme inspection API
Converts: input, list, radio, slide-toggle, slider. See related PR #27688 for more context.
1 parent 24202a8 commit f1a51f9

File tree

22 files changed

+728
-802
lines changed

22 files changed

+728
-802
lines changed

src/material-experimental/popover-edit/_popover-edit-theme.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@
148148
}
149149

150150
@mixin typography($config-or-theme) {
151-
$config: mat.private-typography-to-2014-config(
151+
$config: mat.private-typography-to-2018-config(
152152
mat.get-typography-config($config-or-theme));
153153
[mat-edit-title] {
154-
@include mat.typography-level($config, title);
154+
@include mat.typography-level($config, headline-6);
155155
}
156156
}
157157

src/material/checkbox/_checkbox-theme.scss

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
@use '../core/mdc-helpers/mdc-helpers';
99
@use '../core/tokens/m2/mdc/checkbox' as tokens-mdc-checkbox;
1010

11-
@mixin base($config-or-theme) {
12-
@if inspection.get-theme-version($config-or-theme) == 1 {
13-
@include _theme-from-tokens(inspection.get-theme-tokens($config-or-theme, base));
11+
@mixin base($theme) {
12+
@if inspection.get-theme-version($theme) == 1 {
13+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, base));
1414
}
1515
@else {
1616
@include sass-utils.current-selector-or-root() {
@@ -19,75 +19,64 @@
1919
}
2020
}
2121

22-
@mixin color($config-or-theme) {
23-
$config: theming.get-color-config($config-or-theme);
24-
25-
@if inspection.get-theme-version($config-or-theme) == 1 {
26-
@include _theme-from-tokens(inspection.get-theme-tokens($config-or-theme, color));
22+
@mixin color($theme) {
23+
@if inspection.get-theme-version($theme) == 1 {
24+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, color));
2725
}
2826
@else {
29-
$primary: map.get($config, primary);
30-
$warn: map.get($config, warn);
31-
$foreground: map.get($config, foreground);
32-
3327
@include sass-utils.current-selector-or-root() {
34-
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($config));
28+
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($theme));
3529
}
3630

3731
.mat-mdc-checkbox {
3832
&.mat-primary {
39-
$primary-config: map.merge($config, (accent: $primary));
40-
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($primary-config));
33+
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($theme, primary));
4134
}
4235

4336
&.mat-warn {
44-
$warn-config: map.merge($config, (accent: $warn));
45-
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($warn-config));
37+
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($theme, warn));
4638
}
4739

48-
@include mdc-helpers.using-mdc-theme($config) {
40+
@include mdc-helpers.using-mdc-theme($theme) {
4941
// TODO(mmalerba): Switch to static-styles, theme-styles, and theme once they're available.
5042
@include mdc-form-field.core-styles($query: mdc-helpers.$mdc-theme-styles-query);
5143
}
5244

5345
&.mat-mdc-checkbox-disabled label {
5446
// MDC should set the disabled color on the label, but doesn't, so we do it here instead.
55-
color: theming.get-color-from-palette($foreground, disabled-text);
47+
color: inspection.get-theme-color($theme, foreground, disabled-text);
5648
}
5749
}
5850
}
5951
}
6052

61-
@mixin typography($config-or-theme) {
62-
$config: typography.private-typography-to-2018-config(
63-
theming.get-typography-config($config-or-theme));
64-
65-
@if inspection.get-theme-version($config-or-theme) == 1 {
66-
@include _theme-from-tokens(inspection.get-theme-tokens($config-or-theme, typography));
53+
@mixin typography($theme) {
54+
@if inspection.get-theme-version($theme) == 1 {
55+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, typography));
6756
}
6857
@else {
6958
@include sass-utils.current-selector-or-root() {
70-
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-typography-tokens($config));
59+
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-typography-tokens($theme));
7160
}
7261

7362
.mat-mdc-checkbox {
74-
@include mdc-helpers.using-mdc-typography($config) {
63+
@include mdc-helpers.using-mdc-typography($theme) {
7564
// TODO(mmalerba): Switch to static-styles, theme-styles, and theme once they're available.
7665
@include mdc-form-field.core-styles($query: mdc-helpers.$mdc-typography-styles-query);
7766
}
7867
}
7968
}
8069
}
8170

82-
@mixin density($config-or-theme) {
83-
$density-scale: theming.get-density-config($config-or-theme);
71+
@mixin density($theme) {
72+
$density-scale: inspection.get-theme-density($theme);
8473

85-
@if inspection.get-theme-version($config-or-theme) == 1 {
86-
@include _theme-from-tokens(inspection.get-theme-tokens($config-or-theme, density));
74+
@if inspection.get-theme-version($theme) == 1 {
75+
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
8776
}
8877
@else {
8978
@include sass-utils.current-selector-or-root() {
90-
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-density-tokens($density-scale));
79+
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-density-tokens($theme));
9180
}
9281

9382
@include mdc-helpers.if-touch-targets-unsupported($density-scale) {
@@ -98,27 +87,21 @@
9887
}
9988
}
10089

101-
@mixin theme($theme-or-color-config) {
102-
$theme: theming.private-legacy-get-theme($theme-or-color-config);
103-
90+
@mixin theme($theme) {
10491
@include theming.private-check-duplicate-theme-styles($theme, 'mat-checkbox') {
10592
@if inspection.get-theme-version($theme) == 1 {
10693
@include _theme-from-tokens(inspection.get-theme-tokens($theme));
10794
}
10895
@else {
109-
$color: theming.get-color-config($theme);
110-
$density: theming.get-density-config($theme);
111-
$typography: theming.get-typography-config($theme);
112-
11396
@include base($theme);
114-
@if $color != null {
115-
@include color($color);
97+
@if inspection.theme-has($theme, color) {
98+
@include color($theme);
11699
}
117-
@if $density != null {
118-
@include density($density);
100+
@if inspection.theme-has($theme, density) {
101+
@include density($theme);
119102
}
120-
@if $typography != null {
121-
@include typography($typography);
103+
@if inspection.theme-has($theme, typography) {
104+
@include typography($theme);
122105
}
123106
}
124107
}

src/material/core/mdc-helpers/_mdc-helpers.scss

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// "theming", "typography", "core".
33

44
@use '../theming/theming';
5+
@use '../theming/inspection';
56
@use '../typography/typography';
67
@use '../typography/typography-utils';
78
@use '@material/feature-targeting' as mdc-feature-targeting;
@@ -36,7 +37,7 @@ $mat-typography-mdc-level-mappings: (
3637
);
3738

3839
// Converts an Angular Material typography level config to an MDC one.
39-
@function typography-level-config-to-mdc($mat-config, $mat-level) {
40+
@function typography-level-config-to-mdc($theme, $mat-level) {
4041
$mdc-level: map.get($mat-typography-mdc-level-mappings, $mat-level);
4142

4243
$result-with-nulls: map.merge(
@@ -49,11 +50,11 @@ $mat-typography-mdc-level-mappings: (
4950
)),
5051
if($mat-level,
5152
(
52-
font-size: typography-utils.font-size($mat-config, $mat-level),
53-
line-height: typography-utils.line-height($mat-config, $mat-level),
54-
font-weight: typography-utils.font-weight($mat-config, $mat-level),
55-
letter-spacing: typography-utils.letter-spacing($mat-config, $mat-level),
56-
font-family: typography-utils.font-family($mat-config, $mat-level),
53+
font-size: inspection.get-theme-typography($theme, $mat-level, font-size),
54+
line-height: inspection.get-theme-typography($theme, $mat-level, line-height),
55+
font-weight: inspection.get-theme-typography($theme, $mat-level, font-weight),
56+
letter-spacing: inspection.get-theme-typography($theme, $mat-level, letter-spacing),
57+
font-family: inspection.get-theme-typography($theme, $mat-level, font-family),
5758
// Angular Material doesn't use text-transform, so disable it.
5859
text-transform: none,
5960
),
@@ -71,13 +72,13 @@ $mat-typography-mdc-level-mappings: (
7172
}
7273

7374
// Converts an Angular Material typography config to an MDC one.
74-
@function typography-config-to-mdc($mat-config) {
75+
@function typography-config-to-mdc($theme) {
7576
$mdc-config: ();
7677

7778
@each $mat-level, $mdc-level in $mat-typography-mdc-level-mappings {
7879
$mdc-config: map.merge(
7980
$mdc-config,
80-
($mdc-level: typography-level-config-to-mdc($mat-config, $mat-level)));
81+
($mdc-level: typography-level-config-to-mdc($theme, $mat-level)));
8182
}
8283

8384
@return $mdc-config;
@@ -100,12 +101,11 @@ $mat-typography-mdc-level-mappings: (
100101

101102
// Configures MDC's global variables to reflect the given theme, applies the given styles,
102103
// then resets the global variables to prevent unintended side effects.
103-
@mixin using-mdc-theme($config) {
104-
$primary: theming.get-color-from-palette(map.get($config, primary));
105-
$accent: theming.get-color-from-palette(map.get($config, accent));
106-
$warn: theming.get-color-from-palette(map.get($config, warn));
107-
$background-palette: map.get($config, background);
108-
$is-dark: map.get($config, is-dark);
104+
@mixin using-mdc-theme($theme) {
105+
$primary: inspection.get-theme-color($theme, primary);
106+
$accent: inspection.get-theme-color($theme, accent);
107+
$warn: inspection.get-theme-color($theme, warn);
108+
$is-dark: inspection.get-theme-type($theme) == dark;
109109

110110
// Save the original values.
111111
$orig-primary: mdc-theme-color.$primary;
@@ -126,8 +126,8 @@ $mat-typography-mdc-level-mappings: (
126126
mdc-theme-color.$secondary: $accent;
127127
mdc-theme-color.$on-secondary:
128128
if(variable-safe-contrast-tone(mdc-theme-color.$secondary, $is-dark) == 'dark', #000, #fff);
129-
mdc-theme-color.$background: theming.get-color-from-palette($background-palette, background);
130-
mdc-theme-color.$surface: theming.get-color-from-palette($background-palette, card);
129+
mdc-theme-color.$background: inspection.get-theme-color($theme, background, background);
130+
mdc-theme-color.$surface: inspection.get-theme-color($theme, background, card);
131131
mdc-theme-color.$on-surface:
132132
if(variable-safe-contrast-tone(mdc-theme-color.$surface, $is-dark) == 'dark', #000, #fff);
133133
mdc-theme-color.$error: $warn;
@@ -193,13 +193,13 @@ $mat-typography-mdc-level-mappings: (
193193

194194
// Configures MDC's global variables to reflect the given typography config,
195195
// applies the given styles, then resets the global variables to prevent unintended side effects.
196-
@mixin using-mdc-typography($config) {
196+
@mixin using-mdc-typography($theme) {
197197
// Save the original values.
198198
$orig-mdc-typography-styles: mdc-typography.$styles;
199199

200200
// Set new values based on the given Angular Material typography configuration.
201-
@if $config {
202-
mdc-typography.$styles: typography-config-to-mdc($config);
201+
@if inspection.theme-has($theme, typography) {
202+
mdc-typography.$styles: typography-config-to-mdc($theme);
203203
}
204204

205205
// Apply given rules.

src/material/core/theming/_m2-inspection.scss

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
@use 'sass:map';
33
@use 'sass:meta';
44
@use './theming';
5-
@use '../typography/typography-utils';
6-
@use '../typography/typography';
5+
@use '../typography/property-getters' as typography-getters;
6+
@use '../typography/versioning' as typography-versioning;
77

88
/// Key used to access the Angular Material theme internals.
9-
$_internals: _mat-theming-internals-do-not-access;
9+
$_internals: _mat-theming-internals-do-not-accesst;
1010

1111
/// Keys that indicate a config object is a color config.
1212
$_color-keys: (
@@ -139,29 +139,30 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
139139
@if not theme-has($theme, typography) {
140140
@error 'Typography information is not available on this theme.';
141141
}
142-
$typography: typography.private-typography-to-2018-config(theming.get-typography-config($theme));
142+
$typography: typography-versioning.private-typography-to-2018-config(
143+
theming.get-typography-config($theme));
143144
@if $property == font {
144-
$font-weight: typography-utils.font-weight($typography, $typescale);
145-
$font-size: typography-utils.font-size($typography, $typescale);
146-
$line-height: typography-utils.line-height($typography, $typescale);
147-
$font-family: typography-utils.font-family($typography, $typescale);
145+
$font-weight: typography-getters.font-weight($typography, $typescale);
146+
$font-size: typography-getters.font-size($typography, $typescale);
147+
$line-height: typography-getters.line-height($typography, $typescale);
148+
$font-family: typography-getters.font-family($typography, $typescale);
148149
@return ($font-weight list.slash($font-size, $line-height) $font-family);
149150
}
150151
@else if $property == font-family {
151-
$result: typography-utils.font-family($typography, $typescale);
152-
@return $result or typography-utils.font-family($typography);
152+
$result: typography-getters.font-family($typography, $typescale);
153+
@return $result or typography-getters.font-family($typography);
153154
}
154155
@else if $property == font-size {
155-
@return typography-utils.font-size($typography, $typescale);
156+
@return typography-getters.font-size($typography, $typescale);
156157
}
157158
@else if $property == font-weight {
158-
@return typography-utils.font-weight($typography, $typescale);
159+
@return typography-getters.font-weight($typography, $typescale);
159160
}
160161
@else if $property == line-height {
161-
@return typography-utils.line-height($typography, $typescale);
162+
@return typography-getters.line-height($typography, $typescale);
162163
}
163164
@else if $property == letter-spacing {
164-
@return typography-utils.letter-spacing($typography, $typescale);
165+
@return typography-getters.letter-spacing($typography, $typescale);
165166
}
166167
@else {
167168
@error #{'Valid typography properties are: #{$_typography-properties}. Got:'} $property;

src/material/core/tokens/m2/mat/_radio.scss

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use 'sass:map';
22
@use '../../token-utils';
33
@use '../../../theming/theming';
4+
@use '../../../theming/inspection';
45
@use '../../../style/sass-utils';
56

67
// The prefix used to generate the fully qualified name for tokens in this file.
@@ -13,25 +14,23 @@ $prefix: (mat, radio);
1314
}
1415

1516
// Tokens that can be configured through Angular Material's color theming API.
16-
@function get-color-tokens($config) {
17-
$foreground: map.get($config, foreground);
18-
$is-dark: map.get($config, is-dark);
19-
$accent: map.get($config, accent);
17+
@function get-color-tokens($theme, $palette-name: accent) {
18+
$is-dark: inspection.get-theme-type($theme) == dark;
2019

2120
@return (
2221
ripple-color: if($is-dark, #fff, #000),
23-
checked-ripple-color: theming.get-color-from-palette($accent, default),
24-
disabled-label-color: theming.get-color-from-palette($foreground, disabled-text),
22+
checked-ripple-color: inspection.get-theme-color($theme, $palette-name, default),
23+
disabled-label-color: inspection.get-theme-color($theme, foreground, disabled-text),
2524
);
2625
}
2726

2827
// Tokens that can be configured through Angular Material's typography theming API.
29-
@function get-typography-tokens($config) {
28+
@function get-typography-tokens($theme) {
3029
@return ();
3130
}
3231

3332
// Tokens that can be configured through Angular Material's density theming API.
34-
@function get-density-tokens($config) {
33+
@function get-density-tokens($theme) {
3534
@return ();
3635
}
3736

src/material/core/tokens/m2/mat/_slide-toggle.scss

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use '../../token-utils';
22
@use '../../../style/sass-utils';
3+
@use '../../../theming/inspection';
34
@use '../../../typography/typography-utils';
45

56
// The prefix used to generate the fully qualified name for tokens in this file.
@@ -12,31 +13,23 @@ $prefix: (mat, slide-toggle);
1213
}
1314

1415
// Tokens that can be configured through Angular Material's color theming API.
15-
@function get-color-tokens($config) {
16+
@function get-color-tokens($theme) {
1617
@return ();
1718
}
1819

1920
// Tokens that can be configured through Angular Material's typography theming API.
20-
@function get-typography-tokens($config) {
21-
// TODO(amysorto): The earlier implementation of the slide-toggle used MDC's APIs to create the
22-
// typography tokens. As a result, we unintentionally allowed `null` typography configs to be
23-
// passed in. Since there a lot of apps that now depend on this pattern, we need this temporary
24-
// fallback.
25-
@if ($config == null) {
26-
$config: mdc-helpers.private-fallback-typography-from-mdc();
27-
}
28-
21+
@function get-typography-tokens($theme) {
2922
@return (
30-
label-text-font: typography-utils.font-family($config),
31-
label-text-size: typography-utils.font-size($config, body-2),
32-
label-text-tracking: typography-utils.letter-spacing($config, body-2),
33-
label-text-line-height: typography-utils.line-height($config, body-2),
34-
label-text-weight: typography-utils.font-weight($config, body-2),
23+
label-text-font: inspection.get-theme-typography($theme, body-2, font-family),
24+
label-text-size: inspection.get-theme-typography($theme, body-2, font-size),
25+
label-text-tracking: inspection.get-theme-typography($theme, body-2, letter-spacing),
26+
label-text-line-height: inspection.get-theme-typography($theme, body-2, line-height),
27+
label-text-weight: inspection.get-theme-typography($theme, body-2, font-weight),
3528
);
3629
}
3730

3831
// Tokens that can be configured through Angular Material's density theming API.
39-
@function get-density-tokens($config) {
32+
@function get-density-tokens($theme) {
4033
@return ();
4134
}
4235

0 commit comments

Comments
 (0)