Skip to content

Commit 20e6e92

Browse files
authored
feat(material/core): add an m2-theme API to define system vars (#31616)
1 parent 993a9a3 commit 20e6e92

File tree

11 files changed

+79
-27
lines changed

11 files changed

+79
-27
lines changed

src/material/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ sass_library(
6565
"//src/material/core/theming:_inspection",
6666
"//src/material/core/theming:_palettes",
6767
"//src/material/core/theming:core_all_theme",
68-
"//src/material/core/tokens:m3_system",
68+
"//src/material/core/tokens:system",
6969
"//src/material/core/typography",
7070
"//src/material/core/typography:all_typography",
7171
"//src/material/core/typography:utils",
@@ -140,7 +140,7 @@ ng_package(
140140
"//src/material/core/theming:_inspection",
141141
"//src/material/core/theming:_palettes",
142142
"//src/material/core/theming:core_all_theme",
143-
"//src/material/core/tokens:m3_system",
143+
"//src/material/core/tokens:system",
144144
"//src/material/core/typography",
145145
"//src/material/core/typography:all_typography",
146146
"//src/material/core/typography:utils",

src/material/_index.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
@forward './core/theming/theming' as private-* show private-clamp-density;
1818
@forward './core/typography/typography' show typography-hierarchy;
1919
@forward './core/typography/typography-utils' show font-shorthand;
20-
@forward './core/tokens/m3-system' show system-level-colors,
20+
@forward 'core/tokens/system' show system-level-colors,
2121
system-level-typography, system-level-elevation, system-level-shape,
22-
system-level-motion, system-level-state, theme, theme-overrides;
22+
system-level-motion, system-level-state, theme, theme-overrides, m2-theme;
2323

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

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $_typography-keys: (
4242
$_typography-properties: (font, font-family, line-height, font-size, letter-spacing, font-weight);
4343

4444
/// Gets the m2-config from the theme.
45-
@function _get-m2-config($theme) {
45+
@function get-m2-config($theme) {
4646
// It is possible for a user to pass a "density theme" that is just a number.
4747
@if meta.type-of($theme) != 'map' {
4848
@return $theme;
@@ -89,7 +89,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
8989
/// @param {Map} $theme The theme
9090
/// @return {String} The type of theme (either `light` or `dark`).
9191
@function get-theme-type($theme) {
92-
$theme: _get-m2-config($theme);
92+
$theme: get-m2-config($theme);
9393
@if not theme-has($theme, color) {
9494
@error 'Color information is not available on this theme.';
9595
}
@@ -112,7 +112,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
112112
/// @param {Number} $opacity The alpha channel value for the color.
113113
/// @return {Color} The requested theme color.
114114
@function get-theme-color($theme, $palette-name, $args...) {
115-
$theme: _get-m2-config($theme);
115+
$theme: get-m2-config($theme);
116116
@if not theme-has($theme, color) {
117117
@error 'Color information is not available on this theme.';
118118
}
@@ -137,7 +137,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
137137
/// (font, font-family, font-size, font-weight, line-height, or letter-spacing).
138138
/// @return {*} The value of the requested font property.
139139
@function get-theme-typography($theme, $typescale, $property) {
140-
$theme: _get-m2-config($theme);
140+
$theme: get-m2-config($theme);
141141
@if not theme-has($theme, typography) {
142142
@error 'Typography information is not available on this theme.';
143143
}
@@ -175,7 +175,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
175175
/// @param {Map} $theme The theme
176176
/// @return {Number} The density scale.
177177
@function get-theme-density($theme) {
178-
$theme: _get-m2-config($theme);
178+
$theme: get-m2-config($theme);
179179
@if not theme-has($theme, density) {
180180
@error 'Density information is not available on this theme.';
181181
}
@@ -188,7 +188,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
188188
/// @param {String} $system The system to check
189189
/// @param {Boolean} Whether the theme has information about the system.
190190
@function theme-has($theme, $system) {
191-
$theme: _get-m2-config($theme);
191+
$theme: get-m2-config($theme);
192192
@if $system == base {
193193
@return true;
194194
}

src/material/core/theming/prebuilt/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sass_binary(
5151
src = "rose-red.scss",
5252
deps = [
5353
"//src/material/core/theming:_palettes",
54-
"//src/material/core/tokens:m3_system",
54+
"//src/material/core/tokens:system",
5555
],
5656
)
5757

@@ -60,7 +60,7 @@ sass_binary(
6060
src = "azure-blue.scss",
6161
deps = [
6262
"//src/material/core/theming:_palettes",
63-
"//src/material/core/tokens:m3_system",
63+
"//src/material/core/tokens:system",
6464
],
6565
)
6666

@@ -69,7 +69,7 @@ sass_binary(
6969
src = "cyan-orange.scss",
7070
deps = [
7171
"//src/material/core/theming:_palettes",
72-
"//src/material/core/tokens:m3_system",
72+
"//src/material/core/tokens:system",
7373
],
7474
)
7575

@@ -78,6 +78,6 @@ sass_binary(
7878
src = "magenta-violet.scss",
7979
deps = [
8080
"//src/material/core/theming:_palettes",
81-
"//src/material/core/tokens:m3_system",
81+
"//src/material/core/tokens:system",
8282
],
8383
)

src/material/core/theming/prebuilt/azure-blue.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@use '../../theming/palettes';
2-
@use '../../../core/tokens/m3-system';
2+
@use '../../tokens/system';
33

44
html {
5-
@include m3-system.theme((
5+
@include system.theme((
66
color: (
77
theme-type: light,
88
primary: palettes.$azure-palette,

src/material/core/theming/prebuilt/cyan-orange.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@use '../../theming/palettes';
2-
@use '../../../core/tokens/m3-system';
2+
@use '../../tokens/system';
33

44
html {
5-
@include m3-system.theme((
5+
@include system.theme((
66
color: (
77
theme-type: dark,
88
primary: palettes.$cyan-palette,

src/material/core/theming/prebuilt/magenta-violet.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@use '../../theming/palettes';
2-
@use '../../../core/tokens/m3-system';
2+
@use '../../tokens/system';
33

44
html {
5-
@include m3-system.theme((
5+
@include system.theme((
66
color: (
77
theme-type: dark,
88
primary: palettes.$magenta-palette,

src/material/core/theming/prebuilt/rose-red.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@use '../../theming/palettes';
2-
@use '../../../core/tokens/m3-system';
2+
@use '../../tokens/system';
33

44
html {
5-
@include m3-system.theme((
5+
@include system.theme((
66
color: (
77
theme-type: light,
88
primary: palettes.$rose-palette,

src/material/core/tokens/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ sass_library(
1313
"_token-utils.scss",
1414
],
1515
deps = [
16-
":m3_system",
16+
":system",
1717
"//src/material/core/style:elevation",
1818
"//src/material/core/style:sass_utils",
1919
],
2020
)
2121

2222
sass_library(
23-
name = "m3_system",
24-
srcs = ["_m3-system.scss"],
23+
name = "system",
24+
srcs = ["_system.scss"],
2525
deps = [
2626
":m3_tokens",
2727
"//src/material/core/style:elevation",

src/material/core/tokens/_m3-system.scss renamed to src/material/core/tokens/_system.scss

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
@use '../style/elevation';
1919
@use '../theming/config-validation';
2020
@use '../theming/definition';
21+
@use '../theming/m2-inspection';
2122
@use '../theming/palettes';
2223
@use '../style/sass-utils';
24+
@use './m2';
2325
@use './m3';
2426
@use 'sass:map';
2527
@use 'sass:meta';
@@ -273,7 +275,6 @@
273275
$shadow-color: map.get(
274276
$theme, definition.$internals, palettes, neutral, 0);
275277

276-
277278
@include sass-utils.current-selector-or-root {
278279
@each $name, $value in m3.md-sys-elevation-values() {
279280
$level: map.get($overrides, $name) or $value;
@@ -307,3 +308,54 @@
307308
}
308309
@return $result;
309310
}
311+
312+
// Defines Angular Material system variables using M2 values provided by
313+
// an M2 theme config created with `mat.m2-define-light-theme` or `mat.m2-define-dark-theme`.
314+
//
315+
// This enables applications to style custom components with system-level CSS
316+
// variables instead of creating a separate component theme mixin that relies on
317+
// the theme config.
318+
//
319+
// For example, use `var(--mat-sys-primary)` to get a theme's primary color instead of
320+
// pulling it from the theme with `m2-get-color-from-palette($primary-palette, default)`.
321+
//
322+
// Unlike M3's `mat.theme()`, this mixin does not replace the need to call
323+
// individual component theme mixins for Angular Material components.
324+
@mixin m2-theme($theme-config, $overrides: ()) {
325+
$config: m2-inspection.get-m2-config($theme-config);
326+
327+
$color: map.get($config, color);
328+
@if (m2-inspection.theme-has($theme-config, color)) {
329+
$system-colors: if(map.get($color, is-dark),
330+
m2.md-sys-color-values-dark($color),
331+
m2.md-sys-color-values-light($color),
332+
);
333+
@include _define-m2-system-vars($system-colors, $overrides);
334+
335+
$shadow: map.get($theme-config, _mat-system, shadow);
336+
$system-elevations: ();
337+
@each $name, $value in m2.md-sys-elevation-values() {
338+
$level: map.get($overrides, $name) or $value;
339+
$value: elevation.get-box-shadow($level, $shadow);
340+
$system-elevations: map.set($system-elevations, $name, $value);
341+
}
342+
@include _define-m2-system-vars($system-elevations, $overrides);
343+
}
344+
345+
$typography: map.get($config, typography);
346+
@if ($typography) {
347+
$system-typography: m2.md-sys-typescale-values($typography);
348+
@include _define-m2-system-vars($system-typography, $overrides);
349+
}
350+
351+
@include _define-m2-system-vars(m2.md-sys-shape-values(), $overrides);
352+
@include _define-m2-system-vars(m2.md-sys-state-values(), $overrides);
353+
}
354+
355+
@mixin _define-m2-system-vars($vars, $overrides) {
356+
@include sass-utils.current-selector-or-root {
357+
@each $name, $value in $vars {
358+
--#{definition.$system-level-prefix}-#{$name}: #{map.get($overrides, $name) or $value};
359+
}
360+
}
361+
}

0 commit comments

Comments
 (0)