Skip to content

Commit 82550af

Browse files
committed
fix(material/checkbox): set token values on the element where theme is @included (#27114)
* fix(material/checkbox): set token values on the element where theme is @included For example: ``` .my-el { @include mat.checkbox-theme(...); } ``` should emit the token values on `.my-el`, not `.my-el .mat-mdc-checkbox` * add a helper mixin for current-selector-or-root (cherry picked from commit 6610b6d)
1 parent 6dd2c60 commit 82550af

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/material/checkbox/_checkbox-theme.scss

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use 'sass:map';
22
@use '@material/checkbox/checkbox-theme' as mdc-checkbox-theme;
33
@use '@material/form-field' as mdc-form-field;
4+
@use '../core/style/sass-utils';
45
@use '../core/theming/theming';
56
@use '../core/typography/typography';
67
@use '../core/mdc-helpers/mdc-helpers';
@@ -12,9 +13,11 @@
1213
$warn: map.get($config, warn);
1314
$foreground: map.get($config, foreground);
1415

15-
.mat-mdc-checkbox {
16+
@include sass-utils.current-selector-or-root() {
1617
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($config));
18+
}
1719

20+
.mat-mdc-checkbox {
1821
&.mat-primary {
1922
$primary-config: map.merge($config, (accent: $primary));
2023
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-color-tokens($primary-config));
@@ -40,9 +43,12 @@
4043
@mixin typography($config-or-theme) {
4144
$config: typography.private-typography-to-2018-config(
4245
theming.get-typography-config($config-or-theme));
43-
.mat-mdc-checkbox {
46+
47+
@include sass-utils.current-selector-or-root() {
4448
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-typography-tokens($config));
49+
}
4550

51+
.mat-mdc-checkbox {
4652
@include mdc-helpers.using-mdc-typography($config) {
4753
// TODO(mmalerba): Switch to static-styles, theme-styles, and theme once they're available.
4854
@include mdc-form-field.core-styles($query: mdc-helpers.$mdc-typography-styles-query);
@@ -53,7 +59,7 @@
5359
@mixin density($config-or-theme) {
5460
$density-scale: theming.get-density-config($config-or-theme);
5561

56-
.mat-mdc-checkbox {
62+
@include sass-utils.current-selector-or-root() {
5763
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-density-tokens($density-scale));
5864
}
5965

src/material/checkbox/checkbox.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
}
143143
}
144144

145-
.mat-mdc-checkbox {
145+
html {
146146
// Add default values for MDC checkbox tokens that aren't outputted by the theming API.
147147
@include mdc-checkbox-theme.theme(tokens-mdc-checkbox.get-unthemable-tokens());
148148
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Include content under the current selector (&) or the document root if there is no current
2+
// selector.
3+
@mixin current-selector-or-root($root: html) {
4+
@at-root #{& or $root} {
5+
@content;
6+
}
7+
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,15 @@ describe('theming api', () => {
210210
return;
211211
}
212212
node.selectors.forEach(selector => {
213-
// Only check selectors that match the specified base selector.
214-
if (baseSelector && !baseSelectorRegex.test(selector)) {
215-
return;
213+
if (baseSelector && selector === baseSelector) {
214+
// Styles emitted directly to the baseSelector are emitted to html
215+
// when there is no baseSelector.
216+
selector = 'html';
217+
} else {
218+
// Only check selectors that match the specified base selector.
219+
if (baseSelector && !baseSelectorRegex.test(selector)) {
220+
return;
221+
}
216222
}
217223
selector = selector.replace(baseSelectorRegex, '');
218224
const matchingRule = knownDensitySelectors.get(selector);

0 commit comments

Comments
 (0)