Skip to content

Commit b6cbba7

Browse files
authored
refactor(multiple): convert components to theme inspection API (round 2) (#27713)
* refactor(multiple): convert components to theme inspection API Converts: input, list, checkbox, radio, slide-toggle, slider. See related PR #27688 for more context. * refactor(multiple): convert components to theme inspection API Converts: button, icon-button, fab, paginator. * build: fix lint
1 parent c30e234 commit b6cbba7

34 files changed

+864
-1002
lines changed

.stylelintrc.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,24 @@
147147
"overrides": [
148148
{
149149
"files": [
150+
"**/_button-theme.scss",
150151
"**/_card-theme.scss",
152+
"**/_checkbox-theme.scss",
151153
"**/_core-theme.scss",
154+
"**/_fab-theme.scss",
152155
"**/_form-field-theme.scss",
156+
"**/_icon-button-theme.scss",
157+
"**/_input-theme.scss",
158+
"**/_list-theme.scss",
153159
"**/_optgroup-theme.scss",
154160
"**/_option-theme.scss",
161+
"**/_paginator-theme.scss",
155162
"**/_progress-bar-theme.scss",
156163
"**/_pseudo-checkbox-theme.scss",
157-
"**/_ripple-theme.scss"
164+
"**/_radio-theme.scss",
165+
"**/_ripple-theme.scss",
166+
"**/_slide-toggle-theme.scss",
167+
"**/_slider-theme.scss"
158168
],
159169
"rules": {
160170
"material/theme-mixin-api": false

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/autocomplete/autocomplete.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,13 @@ export class MatAutocomplete
250250
@Input('class')
251251
set classList(value: string | string[]) {
252252
if (value && value.length) {
253-
this._classList = coerceStringArray(value).reduce((classList, className) => {
254-
classList[className] = true;
255-
return classList;
256-
}, {} as {[key: string]: boolean});
253+
this._classList = coerceStringArray(value).reduce(
254+
(classList, className) => {
255+
classList[className] = true;
256+
return classList;
257+
},
258+
{} as {[key: string]: boolean},
259+
);
257260
} else {
258261
this._classList = {};
259262
}

src/material/button/_button-base.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
}
9191
}
9292

93-
// Changes a button theme to exclude the ripple styles.
94-
@function mat-private-button-remove-ripple($theme) {
95-
@return map.merge($theme, (
93+
// Changes a button token set to exclude the ripple styles.
94+
@function mat-private-button-remove-ripple($tokens) {
95+
@return map.merge($tokens, (
9696
focus-state-layer-color: null,
9797
focus-state-layer-opacity: null,
9898
hover-state-layer-color: null,

src/material/button/_button-theme-private.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
@use '@material/theme/theme-color' as mdc-theme-color;
44

55
@use '../core/mdc-helpers/mdc-helpers';
6+
@use '../core/theming/inspection';
67

78
@mixin _ripple-color($color) {
89
--mat-mdc-button-persistent-ripple-color: #{$color};
910
--mat-mdc-button-ripple-color: #{rgba($color, 0.1)};
1011
}
1112

12-
@mixin ripple-theme-styles($config, $is-filled) {
13-
$opacities: if(map.get($config, is-dark),
13+
@mixin ripple-theme-styles($theme, $is-filled) {
14+
$opacities: if(inspection.get-theme-type($theme) == dark,
1415
mdc-ripple-theme.$light-ink-opacities, mdc-ripple-theme.$dark-ink-opacities);
1516

1617
// Ideally these styles would be structural, but MDC bases some of the opacities on the theme.

src/material/button/_button-theme.scss

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@use 'sass:map';
21
@use '@material/button/button' as mdc-button;
32
@use '@material/button/button-theme' as mdc-button-theme;
43
@use '@material/button/button-text-theme' as mdc-button-text-theme;
@@ -10,6 +9,7 @@
109
@use './button-theme-private';
1110
@use '../core/mdc-helpers/mdc-helpers';
1211
@use '../core/theming/theming';
12+
@use '../core/theming/inspection';
1313
@use '../core/typography/typography';
1414

1515
@mixin _button-variant($color) {
@@ -38,10 +38,9 @@
3838
));
3939
}
4040

41-
@mixin color($config-or-theme) {
42-
$config: theming.get-color-config($config-or-theme);
43-
@include mdc-helpers.using-mdc-theme($config) {
44-
$is-dark: map.get($config, is-dark);
41+
@mixin color($theme) {
42+
@include mdc-helpers.using-mdc-theme($theme) {
43+
$is-dark: inspection.get-theme-type($theme) == dark;
4544
$on-surface: mdc-theme-color.prop-value(on-surface);
4645
$surface: mdc-theme-color.prop-value(surface);
4746
$disabled-ink-color: rgba($on-surface, if($is-dark, 0.5, 0.38));
@@ -174,25 +173,24 @@
174173

175174
// Ripple colors
176175
.mat-mdc-button, .mat-mdc-outlined-button {
177-
@include button-theme-private.ripple-theme-styles($config, false);
176+
@include button-theme-private.ripple-theme-styles($theme, false);
178177
}
179178

180179
.mat-mdc-raised-button, .mat-mdc-unelevated-button {
181-
@include button-theme-private.ripple-theme-styles($config, true);
180+
@include button-theme-private.ripple-theme-styles($theme, true);
182181
}
183182
}
184183
}
185184

186-
@mixin typography($config-or-theme) {
187-
$config: typography.private-typography-to-2018-config(
188-
theming.get-typography-config($config-or-theme));
189-
@include mdc-helpers.using-mdc-typography($config) {
185+
@mixin typography($theme) {
186+
@include mdc-helpers.using-mdc-typography($theme) {
190187
@include mdc-button.without-ripple($query: mdc-helpers.$mdc-typography-styles-query);
191188
}
192189
}
193190

194-
@mixin density($config-or-theme) {
195-
$density-scale: theming.get-density-config($config-or-theme);
191+
@mixin density($theme) {
192+
$density-scale: theming.clamp-density(inspection.get-theme-density($theme), -3);
193+
196194
.mat-mdc-button,
197195
.mat-mdc-raised-button,
198196
.mat-mdc-unelevated-button,
@@ -205,21 +203,16 @@
205203
}
206204
}
207205

208-
@mixin theme($theme-or-color-config) {
209-
$theme: theming.private-legacy-get-theme($theme-or-color-config);
206+
@mixin theme($theme) {
210207
@include theming.private-check-duplicate-theme-styles($theme, 'mat-button') {
211-
$color: theming.get-color-config($theme);
212-
$density: theming.get-density-config($theme);
213-
$typography: theming.get-typography-config($theme);
214-
215-
@if $color != null {
216-
@include color($color);
208+
@if inspection.theme-has($theme, color) {
209+
@include color($theme);
217210
}
218-
@if $density != null {
219-
@include density($density);
211+
@if inspection.theme-has($theme, density) {
212+
@include density($theme);
220213
}
221-
@if $typography != null {
222-
@include typography($typography);
214+
@if inspection.theme-has($theme, typography) {
215+
@include typography($theme);
223216
}
224217
}
225218
}

src/material/button/_fab-theme.scss

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@use 'sass:map';
21
@use '@material/fab/fab' as mdc-fab;
32
@use '@material/fab/fab-theme' as mdc-fab-theme;
43
@use '@material/fab/extended-fab-theme' as mdc-extended-fab-theme;
@@ -7,11 +6,12 @@
76
@use '../core/mdc-helpers/mdc-helpers';
87
@use '../core/style/sass-utils';
98
@use '../core/theming/theming';
9+
@use '../core/theming/inspection';
1010
@use '../core/tokens/m2/mdc/fab' as tokens-mdc-fab;
1111
@use '../core/tokens/m2/mdc/extended-fab' as tokens-mdc-extended-fab;
1212
@use '../core/typography/typography';
1313

14-
@mixin base($config-or-theme) {
14+
@mixin base($theme) {
1515
// Add default values for tokens not related to color, typography, or density.
1616
@include sass-utils.current-selector-or-root() {
1717
@include mdc-fab-theme.theme(tokens-mdc-fab.get-unthemable-tokens());
@@ -21,17 +21,11 @@
2121
}
2222
}
2323

24-
@mixin _fab-variant($config, $foreground, $background) {
25-
$color-config: map.merge(
26-
$config,
27-
(
28-
primary: (
29-
default: $background,
30-
default-contrast: $foreground,
31-
),
32-
)
24+
@mixin _fab-variant($foreground, $background) {
25+
$color-tokens: (
26+
container-color: $background,
27+
icon-color: $foreground
3328
);
34-
$color-tokens: tokens-mdc-fab.get-color-tokens($color-config);
3529
@include mdc-fab-theme.theme($color-tokens);
3630

3731
--mat-mdc-fab-color: #{$foreground};
@@ -41,90 +35,78 @@
4135
@return if(mdc-helpers.variable-safe-contrast-tone($color, $is-dark) == 'dark', #000, #fff);
4236
}
4337

44-
@mixin color($config-or-theme) {
45-
$config: theming.get-color-config($config-or-theme);
38+
@mixin color($theme) {
39+
$is-dark: inspection.get-theme-type($theme) == dark;
4640

47-
$is-dark: map.get($config, is-dark);
48-
$background: map.get($config, background);
49-
50-
$surface: theming.get-color-from-palette($background, card);
51-
$primary: theming.get-color-from-palette(map.get($config, primary));
52-
$accent: theming.get-color-from-palette(map.get($config, accent));
53-
$warn: theming.get-color-from-palette(map.get($config, warn));
41+
$surface: inspection.get-theme-color($theme, background, card);
42+
$primary: inspection.get-theme-color($theme, primary);
43+
$accent: inspection.get-theme-color($theme, accent);
44+
$warn: inspection.get-theme-color($theme, warn);
5445

5546
$on-surface: white-or-black($surface, $is-dark);
5647
$on-primary: white-or-black($primary, $is-dark);
5748
$on-accent: white-or-black($accent, $is-dark);
5849
$on-warn: white-or-black($warn, $is-dark);
5950

6051
$disabled: rgba($on-surface, 0.12);
61-
$on-disabled: rgba($on-surface, if(map.get($config, is-dark), 0.5, 0.38));
52+
$on-disabled: rgba($on-surface, if($is-dark, 0.5, 0.38));
6253

6354
.mat-mdc-fab,
6455
.mat-mdc-mini-fab {
6556
// TODO(wagnermaciel): The ripple-theme-styles mixin depends heavily on
6657
// being wrapped by using-mdc-theme. This workaround needs to be
6758
// revisited w/ a more holistic solution.
68-
@include mdc-helpers.using-mdc-theme($config) {
69-
@include button-theme-private.ripple-theme-styles($config, true);
59+
@include mdc-helpers.using-mdc-theme($theme) {
60+
@include button-theme-private.ripple-theme-styles($theme, true);
7061
}
7162

7263
@include button-theme-private.apply-disabled-style() {
73-
@include _fab-variant($config, $on-disabled, $disabled);
64+
@include _fab-variant($on-disabled, $disabled);
7465
}
7566

7667
&.mat-unthemed {
77-
@include _fab-variant($config, $on-surface, $surface);
68+
@include _fab-variant($on-surface, $surface);
7869
}
7970

8071
&.mat-primary {
81-
@include _fab-variant($config, $on-primary, $primary);
72+
@include _fab-variant($on-primary, $primary);
8273
}
8374

8475
&.mat-accent {
85-
@include _fab-variant($config, $on-accent, $accent);
76+
@include _fab-variant($on-accent, $accent);
8677
}
8778

8879
&.mat-warn {
89-
@include _fab-variant($config, $on-warn, $warn);
80+
@include _fab-variant($on-warn, $warn);
9081
}
9182
}
9283
}
9384

94-
@mixin typography($config-or-theme) {
95-
$config: typography.private-typography-to-2018-config(
96-
theming.get-typography-config($config-or-theme)
97-
);
98-
99-
@include mdc-helpers.using-mdc-typography($config) {
85+
@mixin typography($theme) {
86+
@include mdc-helpers.using-mdc-typography($theme) {
10087
@include mdc-fab.without-ripple($query: mdc-helpers.$mdc-typography-styles-query);
10188
}
10289

103-
$typography-tokens: tokens-mdc-extended-fab.get-typography-tokens($config);
90+
$typography-tokens: tokens-mdc-extended-fab.get-typography-tokens($theme);
10491
.mat-mdc-extended-fab {
10592
@include mdc-extended-fab-theme.theme($typography-tokens);
10693
}
10794
}
10895

109-
@mixin density($config-or-theme) {
96+
@mixin density($theme) {
11097
}
11198

112-
@mixin theme($theme-or-color-config) {
113-
$theme: theming.private-legacy-get-theme($theme-or-color-config);
99+
@mixin theme($theme) {
114100
@include theming.private-check-duplicate-theme-styles($theme, 'mat-fab') {
115-
$color: theming.get-color-config($theme);
116-
$density: theming.get-density-config($theme);
117-
$typography: theming.get-typography-config($theme);
118-
119101
@include base($theme);
120-
@if $color != null {
121-
@include color($color);
102+
@if inspection.theme-has($theme, color) {
103+
@include color($theme);
122104
}
123-
@if $density != null {
124-
@include density($density);
105+
@if inspection.theme-has($theme, density) {
106+
@include density($theme);
125107
}
126-
@if $typography != null {
127-
@include typography($typography);
108+
@if inspection.theme-has($theme, typography) {
109+
@include typography($theme);
128110
}
129111
}
130112
}

0 commit comments

Comments
 (0)