Skip to content

Commit f8bd658

Browse files
authored
feat(material/theming): add ability to use sys variables (#28898)
* feat(material-experimental/theming): add ability to use sys variables * fixup! feat(material-experimental/theming): add ability to use sys variables
1 parent 971f83f commit f8bd658

File tree

7 files changed

+258
-17
lines changed

7 files changed

+258
-17
lines changed

src/dev-app/theme-m3.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ mat.$theme-legacy-inspection-api-compatibility: false;
1212
theme-type: $type,
1313
primary: mat.$azure-palette,
1414
tertiary: mat.$blue-palette,
15+
use-system-variables: true,
1516
),
17+
typography: (use-system-variables: true),
1618
density: (
1719
scale: $density
1820
),
@@ -33,6 +35,8 @@ $dark-theme: create-theme($type: dark);
3335
// Include the default theme styles.
3436
html {
3537
@include mat.all-component-themes($light-theme);
38+
@include mat.system-level-colors($light-theme);
39+
@include mat.system-level-typography($light-theme);
3640
// TODO(mmalerba): Support M3 for experimental components.
3741
// @include matx.column-resize-theme($light-theme);
3842
// @include matx.popover-edit-theme($light-theme);
@@ -54,6 +58,7 @@ html {
5458
.demo-unicorn-dark-theme {
5559
// Include the dark theme color styles.
5660
@include mat.all-component-colors($dark-theme);
61+
@include mat.system-level-colors($dark-theme);
5762
// TODO(mmalerba): Support M3 for experimental components.
5863
// @include matx.column-resize-color($dark-theme);
5964
// @include matx.popover-edit-color($dark-theme);

src/material/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@forward './core/typography/typography' show typography-hierarchy;
1919
@forward './core/typography/typography-utils' show font-shorthand;
2020
@forward './core/tokens/m2' show m2-tokens-from-theme;
21+
@forward './core/tokens/m3-tokens' show system-level-colors, system-level-typography;
2122

2223
// Private/Internal
2324
@forward './core/density/private/all-density' show all-component-densities;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@
5858
/// A version of the Sass `color.change` function that is safe ot use with CSS variables.
5959
@function safe-color-change($color, $args...) {
6060
$args: meta.keywords($args);
61-
@return if(meta.type-of($color) == 'color', color.change($color, $args...), $color);
61+
@if (meta.type-of($color) == 'color') {
62+
@return color.change($color, $args...);
63+
}
64+
@else if ($color != null and map.get($args, alpha) != null) {
65+
@return #{color(from #{$color} srgb r g b / #{map.get($args, alpha)})};
66+
}
67+
@return $color;
6268
}
6369

6470
/// Gets the given arguments as a map of keywords and validates that only supported arguments were

src/material/core/theming/_config-validation.scss

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
@if $err {
9090
@return (#{'$config should be a color configuration object. Got:'} $config);
9191
}
92-
$allowed: (theme-type, primary, tertiary);
92+
$allowed: (theme-type, primary, tertiary, use-system-variables);
9393
$err: validation.validate-allowed-values(map.keys($config or ()), $allowed...);
9494
@if $err {
9595
@return (
@@ -128,7 +128,14 @@
128128
@if $err {
129129
@return (#{'$config should be a typography configuration object. Got:'} $config);
130130
}
131-
$allowed: (brand-family, plain-family, bold-weight, medium-weight, regular-weight);
131+
$allowed: (
132+
brand-family,
133+
plain-family,
134+
bold-weight,
135+
medium-weight,
136+
regular-weight,
137+
use-system-variables
138+
);
132139
$err: validation.validate-allowed-values(map.keys($config or ()), $allowed...);
133140
@if $err {
134141
@return (

src/material/core/theming/_definition.scss

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +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;
4344

4445
@return (
4546
$internals: (
@@ -54,7 +55,7 @@ $theme-version: 1;
5455
error: map.get($primary, error),
5556
),
5657
color-tokens: m3-tokens.generate-color-tokens(
57-
$type, $primary, $tertiary, map.get($primary, error))
58+
$type, $primary, $tertiary, map.get($primary, error), $use-sys-vars)
5859
)
5960
);
6061
}
@@ -73,12 +74,20 @@ $theme-version: 1;
7374
$bold: map.get($config, bold-weight) or 700;
7475
$medium: map.get($config, medium-weight) or 500;
7576
$regular: map.get($config, regular-weight) or 400;
77+
$use-sys-vars: map.get($config, use-system-variables) or false;
7678

7779
@return (
7880
$internals: (
7981
theme-version: $theme-version,
82+
font-definition: (
83+
plain: $plain,
84+
brand: $brand,
85+
bold: $bold,
86+
medium: $medium,
87+
regular: $regular,
88+
),
8089
typography-tokens: m3-tokens.generate-typography-tokens(
81-
$brand, $plain, $bold, $medium, $regular)
90+
$brand, $plain, $bold, $medium, $regular, $use-sys-vars)
8291
)
8392
);
8493
}
@@ -93,6 +102,7 @@ $theme-version: 1;
93102
}
94103

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

97107
@return (
98108
$internals: (

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe('theming definition api', () => {
8383
'theme-type',
8484
'palettes',
8585
'color-tokens',
86+
'font-definition',
8687
'typography-tokens',
8788
'density-scale',
8889
'density-tokens',
@@ -279,7 +280,11 @@ describe('theming definition api', () => {
279280
}
280281
`);
281282
const vars = getRootVars(css);
282-
expect(vars['keys'].split(', ')).toEqual(['theme-version', 'typography-tokens']);
283+
expect(vars['keys'].split(', ')).toEqual([
284+
'theme-version',
285+
'font-definition',
286+
'typography-tokens',
287+
]);
283288
});
284289
});
285290

0 commit comments

Comments
 (0)