Skip to content

Commit 0bb5610

Browse files
authored
fix(material/theming): restrict css color usage behind a flag (#28944)
* fix(material/theming): restrict css color usage behind a flag * fixup! fix(material/theming): restrict css color usage behind a flag * fixup! fix(material/theming): restrict css color usage behind a flag
1 parent 7cd3f02 commit 0bb5610

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/material/core/style/_sass-utils.scss

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
@use 'sass:map';
44
@use 'sass:meta';
55

6+
/// Whether our theming API is using --sys- variables for color tokens.
7+
$use-system-color-variables: false;
8+
9+
/// Whether our theming API is using --sys- variables for typography tokens.
10+
$use-system-typography-variables: false;
11+
612
/// Include content under the current selector (&) or the document root if there is no current
713
/// selector.
814
/// @param {String} $root [html] The default root selector to use when there is no current selector.
@@ -61,8 +67,9 @@
6167
@if (meta.type-of($color) == 'color') {
6268
@return color.change($color, $args...);
6369
}
64-
@else if ($color != null and map.get($args, alpha) != null) {
65-
@return #{color(from #{$color} srgb r g b / #{map.get($args, alpha)})};
70+
@else if ($color != null and map.get($args, alpha) != null and $use-system-color-variables) {
71+
$opacity: map.get($args, alpha);
72+
@return #{color-mix(in srgb, #{$color} #{($opacity * 100) + '%'}, transparent)};
6673
}
6774
@return $color;
6875
}

src/material/core/theming/_definition.scss

+4-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $theme-version: 1;
4040
$type: map.get($config, theme-type) or light;
4141
$primary: map.get($config, primary) or palettes.$violet-palette;
4242
$tertiary: map.get($config, tertiary) or $primary;
43-
$use-sys-vars: map.get($config, use-system-variables) or false;
43+
sass-utils.$use-system-color-variables: map.get($config, use-system-variables) or false;
4444

4545
@return (
4646
$internals: (
@@ -55,7 +55,7 @@ $theme-version: 1;
5555
error: map.get($primary, error),
5656
),
5757
color-tokens: m3-tokens.generate-color-tokens(
58-
$type, $primary, $tertiary, map.get($primary, error), $use-sys-vars)
58+
$type, $primary, $tertiary, map.get($primary, error))
5959
)
6060
);
6161
}
@@ -74,7 +74,7 @@ $theme-version: 1;
7474
$bold: map.get($config, bold-weight) or 700;
7575
$medium: map.get($config, medium-weight) or 500;
7676
$regular: map.get($config, regular-weight) or 400;
77-
$use-sys-vars: map.get($config, use-system-variables) or false;
77+
sass-utils.$use-system-typography-variables: map.get($config, use-system-variables) or false;
7878

7979
@return (
8080
$internals: (
@@ -87,7 +87,7 @@ $theme-version: 1;
8787
regular: $regular,
8888
),
8989
typography-tokens: m3-tokens.generate-typography-tokens(
90-
$brand, $plain, $bold, $medium, $regular, $use-sys-vars)
90+
$brand, $plain, $bold, $medium, $regular)
9191
)
9292
);
9393
}
@@ -102,7 +102,6 @@ $theme-version: 1;
102102
}
103103

104104
$density-scale: map.get($config, scale) or 0;
105-
$use-sys-vars: map.get($config, use-system-variables) or false;
106105

107106
@return (
108107
$internals: (

src/material/core/tokens/_m3-tokens.scss

+10-9
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@
170170
}
171171
@else if($color != null) {
172172
$result: map.remove($result, $opacity-key);
173-
$result: map.set($result, $color-key, #{color(from #{$color} srgb r g b / #{$opacity})});
173+
$combined-color: #{color-mix(in srgb, #{$color} #{($opacity * 100) + '%'}, transparent)};
174+
$result: map.set($result, $color-key, $combined-color);
174175
}
175176
}
176177

@@ -1029,8 +1030,8 @@
10291030
}
10301031
}
10311032

1032-
@function _get-sys-color($type, $use-sys-vars, $ref) {
1033-
@if $use-sys-vars {
1033+
@function _get-sys-color($type, $ref) {
1034+
@if (sass-utils.$use-system-color-variables) {
10341035
@return (
10351036
'background': var(--sys-background),
10361037
'error': var(--sys-error),
@@ -1089,8 +1090,8 @@
10891090
mdc-tokens.md-sys-color-values-light($ref));
10901091
}
10911092

1092-
@function _get-sys-typeface($use-sys-vars, $ref) {
1093-
@if ($use-sys-vars) {
1093+
@function _get-sys-typeface($ref) {
1094+
@if (sass-utils.$use-system-typography-variables) {
10941095
@return (
10951096
'body-large': var(--sys-body-large),
10961097
'body-large-font': var(--sys-body-large-font),
@@ -1195,12 +1196,12 @@
11951196
/// @param {Map} $tertiary The tertiary palette
11961197
/// @param {Map} $error The error palette
11971198
/// @return {Map} A map of namespaced color tokens
1198-
@function generate-color-tokens($type, $primary, $tertiary, $error, $use-sys-vars) {
1199+
@function generate-color-tokens($type, $primary, $tertiary, $error) {
11991200
$ref: (
12001201
md-ref-palette: _generate-ref-palette-tokens($primary, $tertiary, $error)
12011202
);
12021203

1203-
$sys-color: _get-sys-color($type, $use-sys-vars, $ref);
1204+
$sys-color: _get-sys-color($type, $ref);
12041205

12051206
@return _generate-tokens(map.merge($ref, (
12061207
md-sys-color: $sys-color,
@@ -1222,11 +1223,11 @@
12221223
/// @param {String|Number} $medium The medium font-weight
12231224
/// @param {String|Number} $regular The regular font-weight
12241225
/// @return {Map} A map of namespaced typography tokens
1225-
@function generate-typography-tokens($brand, $plain, $bold, $medium, $regular, $use-sys-vars) {
1226+
@function generate-typography-tokens($brand, $plain, $bold, $medium, $regular) {
12261227
$ref: (
12271228
md-ref-typeface: _generate-ref-typeface-tokens($brand, $plain, $bold, $medium, $regular)
12281229
);
1229-
$sys-typeface: _get-sys-typeface($use-sys-vars, $ref);
1230+
$sys-typeface: _get-sys-typeface($ref);
12301231
@return _generate-tokens((
12311232
md-sys-typescale: $sys-typeface
12321233
));

0 commit comments

Comments
 (0)