Skip to content

Commit a58e6f6

Browse files
authoredOct 11, 2024··
feat(material/core): add theme-overrides mixin (#29858)
1 parent 9262a01 commit a58e6f6

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed
 

‎src/dev-app/theme/theme-demo.html

+8
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,11 @@ <h1>Elevation</h1>
229229
box-shadow: var(--mat-app-level{{level}})
230230
</div>
231231
}
232+
233+
<h1>Overrides</h1>
234+
235+
<div class="demo-overrides">
236+
This container has several system variables applied and includes the
237+
<span class="demo-surface-variable">mat.theme-overrides</span> mixin
238+
to change their values from the existing theme.
239+
</div>

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@use '@angular/material';
1+
@use '@angular/material' as mat;
22

33
:host {
44
display: block;
@@ -82,7 +82,8 @@ a {
8282
.demo-surface-variable {
8383
display: inline-block;
8484
font-family: monospace;
85-
background: var(--mat-app-surface-dim);
85+
background: var(--mat-app-primary-container);
86+
color: var(--mat-app-on-primary-container);
8687
padding: 2px 6px;
8788
margin: 0 2px;
8889
border-radius: 4px;
@@ -91,7 +92,7 @@ a {
9192
mat-expansion-panel {
9293
margin-top: 24px;
9394
overflow: visible;
94-
@include material.expansion-overrides((
95+
@include mat.expansion-overrides((
9596
'container-text-font': var(--mat-app-body-medium-font),
9697
'container-text-size': var(--mat-app-body-medium-size),
9798
'container-text-weight': var(--mat-app-body-medium-weight),
@@ -165,6 +166,9 @@ mat-expansion-panel {
165166
display: flex;
166167
align-items: center;
167168
justify-content: center;
169+
background: var(--mat-app-surface-container);
170+
color: var(--mat-app-on-surface);
171+
border-radius: var(--mat-app-corner-extra-small);
168172
}
169173

170174
.demo-code-block {
@@ -173,3 +177,20 @@ mat-expansion-panel {
173177
border-radius: var(--mat-app-corner-small);
174178
border: 1px solid var(--mat-app-outline);
175179
}
180+
181+
.demo-overrides {
182+
background-color: var(--mat-app-primary);
183+
color: var(--mat-app-on-primary);
184+
font: var(--mat-app-body-medium);
185+
border-radius: var(--mat-app-corner-large);
186+
box-shadow: var(--mat-app-level3);
187+
padding: 16px;
188+
189+
@include mat.theme-overrides((
190+
primary: #ebdcff,
191+
on-primary: #230f46,
192+
body-medium: 600 1.5rem / 2.25rem Arial,
193+
corner-large: 32px,
194+
level3: 0 4px 6px 1px var(--mat-app-surface-dim),
195+
));
196+
}

‎src/material/_index.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@forward './core/tokens/m2' show m2-tokens-from-theme;
2121
@forward './core/tokens/m3-system' show system-level-colors,
2222
system-level-typography, system-level-elevation, system-level-shape,
23-
system-level-motion, system-level-state, theme;
23+
system-level-motion, system-level-state, theme, theme-overrides;
2424

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

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

+28
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ $_system-level-prefix: sys;
8484
@include system-level-state($overrides: $overrides, $prefix: $_system-fallback-prefix);
8585
}
8686

87+
/// Emits the system-level CSS variables for each of the provided override values. E.g. to
88+
/// change the primary color to red, use `mat.theme-overrides((primary: red));`
89+
@mixin theme-overrides($overrides, $prefix: $_system-fallback-prefix) {
90+
$sys-names: map-merge-all(
91+
definitions.md-sys-color-values-light(),
92+
definitions.md-sys-typescale-values(),
93+
definitions.md-sys-elevation-values(),
94+
definitions.md-sys-shape-values(),
95+
definitions.md-sys-state-values());
96+
97+
& {
98+
@each $name, $value in $overrides {
99+
@if (map.has-key($sys-names, $name)) {
100+
--#{$prefix}-#{$name}: #{map.get($overrides, $name)};
101+
}
102+
}
103+
}
104+
}
105+
87106
@mixin system-level-colors($theme, $overrides: (), $prefix: null) {
88107
$palettes: map.get($theme, _mat-theming-internals-do-not-access, palettes);
89108
$base-palettes: (
@@ -226,3 +245,12 @@ $_system-level-prefix: sys;
226245
m3-tokens.generate-density-tokens(0)
227246
);
228247
}
248+
249+
/// Creates a single merged map from the provided variable-length map arguments
250+
@function map-merge-all($maps...) {
251+
$result: ();
252+
@each $map in $maps {
253+
$result: map.merge($result, $map);
254+
}
255+
@return $result;
256+
}

0 commit comments

Comments
 (0)
Please sign in to comment.