Skip to content

Commit b519b47

Browse files
authoredOct 23, 2024··
feat(material/core): default to color-scheme theme type (#29907)
1 parent d93900c commit b519b47

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed
 

‎src/dev-app/theme-m3.scss

+2-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ $dark-theme: create-theme($type: dark);
3232

3333
// Include the default theme styles.
3434
html {
35+
color-scheme: light;
3536
body:not(.demo-experimental-theme) {
3637
@include mat.all-component-themes($light-theme);
3738
@include mat.system-level-colors($light-theme);
@@ -44,7 +45,6 @@ html {
4445
body.demo-experimental-theme {
4546
@include mat.theme((
4647
color: (
47-
theme-type: light,
4848
primary: $primary,
4949
tertiary: $tertiary,
5050
),
@@ -68,6 +68,7 @@ html {
6868
// `.demo-unicorn-dark-theme` will be affected by this alternate dark theme instead of the
6969
// default theme.
7070
body.demo-unicorn-dark-theme {
71+
color-scheme: dark;
7172
&:not(.demo-experimental-theme) {
7273
// Include the dark theme color styles.
7374
@include mat.all-component-colors($dark-theme);
@@ -77,16 +78,6 @@ body.demo-unicorn-dark-theme {
7778
// @include matx.popover-edit-color($dark-theme);
7879
}
7980

80-
&.demo-experimental-theme {
81-
@include mat.theme((
82-
color: (
83-
theme-type: dark,
84-
primary: $primary,
85-
tertiary: $tertiary,
86-
),
87-
));
88-
}
89-
9081
// Include the dark theme colors for focus indicators.
9182
&.demo-strong-focus {
9283
@include mat.strong-focus-indicators-color($dark-theme);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100
);
101101
}
102102
@if $config and map.has-key($config, theme-type) and
103-
not list.index((light, dark), map.get($config, theme-type)) {
103+
not list.index((light, dark, color-scheme), map.get($config, theme-type)) {
104104
@return (
105-
#{'Expected $config.theme-type to be one of: light, dark. Got:'}
105+
#{'Expected $config.theme-type to be one of: light, dark, color-scheme. Got:'}
106106
map.get($config, theme-type)
107107
);
108108
}

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

+36-9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ $_system-level-prefix: sys;
1818
/// config map. The config map can have values color, typography, and/or density.
1919
///
2020
/// If the config map's color value is an Angular Material color palette, it will be used as the
21-
/// primary and tertiary colors with a light theme type. Otherwise if the color value is a map,
22-
/// it must have a `primary` value containing an Angular Material color palette, and optionally
23-
/// a different `tertiary` palette (defaults to primary palette) and `theme-type` that is either
24-
/// `light` or `dark` (defaults to light). Color variable definitions will not be emitted if there
25-
/// are no color values in the config.
21+
/// primary and tertiary colors with a `color-scheme` theme type. Otherwise if the color value is a
22+
/// map, it must have a `primary` value containing an Angular Material color palette, and
23+
/// optionally a different `tertiary` palette (defaults to primary palette) and `theme-type` that
24+
/// is either `light`, `dark`, or 'color-scheme` (defaults to `color-scheme`). Color variable
25+
/// definitions will not be emitted if there are no color values in the config.
2626
///
2727
/// If the config map's typography value is a font family string, it will be used as the
2828
/// plain and brand font family with default bold, medium, and regular weights of 700, 500, and 400,
@@ -44,9 +44,14 @@ $_system-level-prefix: sys;
4444
$color: map.get($config, color);
4545
$color-config: null;
4646
@if ($color) {
47+
// Default to "color-scheme" theme type if the config's color does not provide one.
48+
@if (meta.type-of($color) == 'map' and not map.has-key($color, theme-type)) {
49+
$color: map.set($color, theme-type, color-scheme);
50+
}
51+
4752
$color-config: if(meta.type-of($color) == 'map',
4853
definition.define-colors($color),
49-
definition.define-colors((primary: $color)));
54+
definition.define-colors((primary: $color, theme-type: color-scheme)));
5055
@include system-level-colors($color-config, $overrides, $_system-fallback-prefix);
5156
@include system-level-elevation($color-config, $overrides, $_system-fallback-prefix);
5257
}
@@ -126,9 +131,7 @@ $_system-level-prefix: sys;
126131
md-ref-palette: m3-tokens.generate-ref-palette-tokens($primary, $tertiary, $error)
127132
);
128133

129-
$sys-colors: if($type == dark,
130-
definitions.md-sys-color-values-dark($ref),
131-
definitions.md-sys-color-values-light($ref));
134+
$sys-colors: _generate-sys-colors($ref, $type);
132135

133136
// Manually insert a subset of palette values that are used directly by components
134137
// instead of system variables.
@@ -144,6 +147,30 @@ $_system-level-prefix: sys;
144147
}
145148
}
146149

150+
@function _generate-sys-colors($ref, $type) {
151+
$light-sys-colors: definitions.md-sys-color-values-light($ref);
152+
@if ($type == light) {
153+
@return $light-sys-colors;
154+
}
155+
156+
$dark-sys-colors: definitions.md-sys-color-values-dark($ref);
157+
@if ($type == dark) {
158+
@return $dark-sys-colors;
159+
}
160+
161+
@if ($type == color-scheme) {
162+
$light-dark-sys-colors: ();
163+
@each $name, $light-value in $light-sys-colors {
164+
$dark-value: map.get($dark-sys-colors, $name);
165+
$light-dark-sys-colors:
166+
map.set($light-dark-sys-colors, $name, light-dark($light-value, $dark-value));
167+
}
168+
@return $light-dark-sys-colors;
169+
}
170+
171+
@error 'Unknown theme-type provided: #{$type}';
172+
}
173+
147174
@mixin system-level-typography($theme, $overrides: (), $prefix: null) {
148175
$font-definition: map.get($theme, _mat-theming-internals-do-not-access, font-definition);
149176
$brand: map.get($font-definition, brand);

0 commit comments

Comments
 (0)