Skip to content

Commit 1d935e4

Browse files
committed
feat(material/schematics): Update custom theme schematic to work with light-dark and use theme-overrides mixin
1 parent ea0d1ba commit 1d935e4

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

src/material/schematics/ng-generate/theme-color/README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,46 @@ html {
3434
}
3535
```
3636

37-
High contrast override theme mixins are also generated in the file if specified
38-
(`high-contrast-light-theme-overrides` and `high-contrast-dark-theme-overrides`). These mixins
37+
## High contrast override mixins
38+
High contrast override theme mixins are also generated in the file if specified. These mixins
3939
override the system level variables with high contrast equivalent values from your theme. This is
4040
helpful for users who prefer more contrastful colors for either preference or accessibility reasons.
4141

42+
### Creating one theme for light and dark mode
43+
As of v19, the `theme` mixin can create one theme that detects and adapts to a user if they have
44+
light or dark theme with the [`light-dark` function](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark).
45+
46+
Use the `high-contrast-overrides` mixin which will also adapt accordingly.
47+
```scss
48+
@use '@angular/material';
49+
@use './path/to/my-theme'; // location of generated file
50+
51+
html {
52+
// Must specify color-scheme for theme mixin to automatically work
53+
color-scheme: light;
54+
55+
// Create one theme that works automatically for light and dark theme
56+
@include material.theme((
57+
color: (
58+
primary: my-theme.$primary-palette,
59+
tertiary: my-theme.$tertiary-palette,
60+
),
61+
typography: Roboto,
62+
density: 0,
63+
));
64+
65+
// Use high contrast values when users prefer contrast
66+
@media (prefers-contrast: more) {
67+
@include my-theme.high-contrast-overrides();
68+
}
69+
}
70+
```
71+
72+
### Creating separate themes for light and dark mode
73+
You can manually define the light theme and dark theme separately. This is recommended if you need
74+
granular control over when to show each specific theme in your application. Prior to v19, this was
75+
the only way to create light and dark themes.
76+
4277
```scss
4378
@use '@angular/material';
4479
@use './path/to/my-theme'; // location of generated file
@@ -49,6 +84,7 @@ html {
4984
color: (
5085
primary: my-theme.$primary-palette,
5186
tertiary: my-theme.$tertiary-palette,
87+
theme-type: light,
5288
),
5389
typography: Roboto,
5490
density: 0,

src/material/schematics/ng-generate/theme-color/index.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ function getHighContrastOverides(
236236
overrides.set('surface-dim', hexFromArgb(scheme.surfaceDim));
237237
overrides.set('surface-bright', hexFromArgb(scheme.surfaceBright));
238238
overrides.set('surface-container-lowest', hexFromArgb(scheme.surfaceContainerLowest));
239-
overrides.set('surface-container-lowest', hexFromArgb(scheme.surfaceContainerLow));
240239
overrides.set('surface-container', hexFromArgb(scheme.surfaceContainer));
241240
overrides.set('surface-container-high', hexFromArgb(scheme.surfaceContainerHigh));
242241
overrides.set('surface-container-highest', hexFromArgb(scheme.surfaceContainerHighest));
@@ -278,22 +277,48 @@ function generateHighContrastOverrideMixinsSCSS(
278277
neutralPalette: TonalPalette,
279278
neutralVariantPalette: TonalPalette,
280279
): string {
280+
const lightOverrides = getHighContrastOverides(
281+
primaryPalette,
282+
secondaryPalette,
283+
tertiaryPalette,
284+
neutralPalette,
285+
neutralVariantPalette,
286+
/** isDark **/ false,
287+
);
288+
289+
const darkOverrides = getHighContrastOverides(
290+
primaryPalette,
291+
secondaryPalette,
292+
tertiaryPalette,
293+
neutralPalette,
294+
neutralVariantPalette,
295+
/** isDark **/ true,
296+
);
297+
281298
let scss = '\n';
299+
300+
// Create high-contrast-overrides mixin which works with light-dark
301+
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark
302+
scss += '\n@mixin high-contrast-overrides {\n';
303+
scss += ' @include mat.theme-overrides((\n';
304+
for (const [key, value] of lightOverrides!.entries()) {
305+
scss += ' ' + key + ': light-dark(' + value + ', ' + darkOverrides.get(key) + '),\n';
306+
}
307+
scss += ' ))\n';
308+
scss += '};\n';
309+
310+
// Create high-contrast-light-theme-overrides and high-contrast-dark-theme-overrides mixins
282311
for (const themeType of ['light', 'dark']) {
283-
const overrides = getHighContrastOverides(
284-
primaryPalette,
285-
secondaryPalette,
286-
tertiaryPalette,
287-
neutralPalette,
288-
neutralVariantPalette,
289-
themeType === 'dark',
290-
);
291312
scss += '\n@mixin high-contrast-' + themeType + '-theme-overrides {\n';
313+
scss += ' @include mat.theme-overrides((\n';
314+
const overrides = themeType === 'light' ? lightOverrides : darkOverrides;
292315
for (const [key, value] of overrides!.entries()) {
293-
scss += ' --mat-sys-' + key + ': ' + value + ';\n';
316+
scss += ' ' + key + ': ' + value + ',\n';
294317
}
318+
scss += ' ))\n';
295319
scss += '};\n';
296320
}
321+
297322
return scss;
298323
}
299324

0 commit comments

Comments
 (0)