Skip to content

Commit 84cdc16

Browse files
committed
feat(material/schematics): Switch custom theme schematic to use theme mixin instead of define-theme and add high contrast override mixins
1 parent 64cf19c commit 84cdc16

File tree

7 files changed

+492
-384
lines changed

7 files changed

+492
-384
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"@bazel/terser": "5.8.1",
9999
"@bazel/worker": "5.8.1",
100100
"@firebase/app-types": "^0.7.0",
101-
"@material/material-color-utilities": "^0.2.7",
101+
"@material/material-color-utilities": "^0.3.0",
102102
"@octokit/rest": "18.3.5",
103103
"@rollup/plugin-commonjs": "^21.0.0",
104104
"@rollup/plugin-node-resolve": "^13.1.3",

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

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,76 @@
44
ng generate @angular/material:m3-theme
55
```
66

7-
This schematic allows users to create new Material 3 theme configurations based
7+
This schematic allows users to create new Material 3 theme palettes based
88
on custom colors by using [Material Color Utilities](https://github.com/material-foundation/material-color-utilities).
99

1010
The generated [color palettes](https://m3.material.io/styles/color/roles) are
1111
optimized to have enough contrast to be more accessible. See [Science of Color Design](https://material.io/blog/science-of-color-design) for more information about Material's color design.
1212

13-
For more customization, custom palette colors can be also be provided for the
14-
secondary, tertiary, and neutral colors. It is recommended to choose colors that
13+
For more customization, custom colors can be also be provided for the
14+
secondary, tertiary, and neutral palette colors. It is recommended to choose colors that
1515
are contrastful, Material has more detailed guidance for [accessible design](https://m3.material.io/foundations/accessible-design/patterns).
1616

1717
The output of the schematic will create a file named `m3-theme.scss` at the
18-
specified directory or the project root with the generated themes. The exported
19-
themes (`$light-theme` and/or `$dark-theme`) can be provided to component theme
20-
mixins.
21-
22-
If you're using the system variables option, you should remember to either provide values for the
23-
system variables (all prefixed with `--sys-`), or to include the `system-level-colors` and
24-
`system-level-typography` mixins which will generate the values based on your theme.
25-
26-
The default prefix for system variables is `--sys-`. This prefix can be customized. For
27-
example, to change the prefix to `--md-sys-`, use the following configuration at the color or typography level:
28-
`system-prefix: md-sys`.
18+
specified directory or the project root with the generated palettes. The exported
19+
palettes (`$primary-palette` and `$tertiary-palette`) can be provided to the `private-experimental-theme` mixin within your theme file to use the custom colors.
2920

3021
```scss
3122
@use '@angular/material' as mat;
32-
@use './path/to/my-theme';
23+
@use './path/to/my-theme'; // location of generated file
3324

34-
@include mat.core();
25+
html {
26+
@include mat.private-experimental-theme(
27+
color: (
28+
primary: my-theme.$primary-palette,
29+
tertiary: my-theme.$tertiary-palette,
30+
),
31+
typography: Roboto,
32+
density: 0,
33+
)
34+
}
35+
```
36+
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
39+
override the system level variables with high contrast equivalent values from your theme. This is
40+
helpful for users who prefer more contrastful colors for either preference or accessibility reasons.
41+
42+
```scss
43+
@use '@angular/material';
44+
@use './path/to/my-theme'; // location of generated file
3545

3646
html {
3747
// Apply the light theme by default
38-
@include mat.core-theme(my-theme.$light-theme);
39-
@include mat.button-theme(my-theme.$light-theme);
40-
41-
// When using system variables, remember to provide values for them
42-
// or uncomment the lines below to generate them from the theme.
43-
// @include mat.system-level-colors(my-theme.$light-theme);
44-
// @include mat.system-level-typography(my-theme.$light-theme);
48+
@include material.private-experimental-theme((
49+
color: (
50+
primary: my-theme.$primary-palette,
51+
tertiary: my-theme.$tertiary-palette,
52+
),
53+
typography: Roboto,
54+
density: 0,
55+
));
56+
57+
// Use high contrast light theme colors when users prefer contrast
58+
@media (prefers-contrast: more) {
59+
@include my-theme.high-contrast-light-theme-overrides();
60+
}
61+
62+
// Apply dark theme when users prefer a dark color scheme
63+
@media (prefers-color-scheme: dark) {
64+
@include material.private-experimental-theme((
65+
color: (
66+
primary: my-theme.$primary-palette,
67+
tertiary: my-theme.$tertiary-palette,
68+
theme-type: dark,
69+
),
70+
));
71+
72+
// Use high contrast dark theme colors when users prefers a dark color scheme and contrast
73+
@media (prefers-contrast: more) {
74+
@include my-theme.high-contrast-dark-theme-overrides();
75+
}
76+
}
4577
}
4678
```
4779

@@ -61,8 +93,8 @@ secondary color generated from Material based on the primary.
6193
tertiary color generated from Material based on the primary.
6294
* `neutralColor` - Color to use for app's neutral color palette. Defaults to
6395
neutral color generated from Material based on the primary.
96+
* `includeHighContrast` - Whether to add high contrast override mixins to generated
97+
theme file. Developers can call the mixin when they want to show a high contrast version of their
98+
theme. Defaults to false.
6499
* `directory` - Relative path to a directory within the project that the
65100
generated theme file should be created in. Defaults to the project root.
66-
* `themeTypes` - Theme types ('light', 'dark', or 'both') to generate themes for. Defaults to both.
67-
* `useSystemVariables` - Whether to generate a theme that uses system-level variables for easier
68-
dynamic theming. Defaults to false.

0 commit comments

Comments
 (0)