Skip to content

Commit

Permalink
feat(material/schematics): Switch custom theme schematic to use theme…
Browse files Browse the repository at this point in the history
… mixin instead of define-theme and add high contrast override mixins
  • Loading branch information
amysorto committed Aug 26, 2024
1 parent ac324fb commit 2d7fe98
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 382 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"@bazel/terser": "5.8.1",
"@bazel/worker": "5.8.1",
"@firebase/app-types": "^0.7.0",
"@material/material-color-utilities": "^0.2.7",
"@material/material-color-utilities": "^0.3.0",
"@octokit/rest": "18.3.5",
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-node-resolve": "^13.1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/material/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@forward './core/typography/typography' show typography-hierarchy;
@forward './core/typography/typography-utils' show font-shorthand;
@forward './core/tokens/m2' show m2-tokens-from-theme;
@forward './core/tokens/m3-system' show system-level-colors,
@forward './core/tokens/m3-system' show theme, system-level-colors,
system-level-typography, system-level-elevation, system-level-shape,
system-level-motion, system-level-state;

Expand Down
75 changes: 52 additions & 23 deletions src/material/schematics/ng-generate/m3-theme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,62 @@ secondary, tertiary, and neutral colors. It is recommended to choose colors that
are contrastful, Material has more detailed guidance for [accessible design](https://m3.material.io/foundations/accessible-design/patterns).

The output of the schematic will create a file named `m3-theme.scss` at the
specified directory or the project root with the generated themes. The exported
themes (`$light-theme` and/or `$dark-theme`) can be provided to component theme
mixins.

If you're using the system variables option, you should remember to either provide values for the
system variables (all prefixed with `--sys-`), or to include the `system-level-colors` and
`system-level-typography` mixins which will generate the values based on your theme.

The default prefix for system variables is `--sys-`. This prefix can be customized. For
example, to change the prefix to `--md-sys-`, use the following configuration at the color or typography level:
`system-prefix: md-sys`.
specified directory or the project root with the generated palettes. The exported
palettes (`$primary-palette` and `$tertiary-palette`) can be provided to the `theme`
mixin within your theme file to use the custom colors.

```scss
@use '@angular/material' as mat;
@use './path/to/my-theme';
@use './path/to/my-theme'; // location of generated file

@include mat.core();
html {
@include mat.theme(
color: (
primary: my-theme.$primary-palette,
tertiary: my-theme.$tertiary-palette,
),
)
}
```

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

```scss
@use '@angular/material';
@use './path/to/my-theme'; // location of generated file

html {
// Apply the light theme by default
@include mat.core-theme(my-theme.$light-theme);
@include mat.button-theme(my-theme.$light-theme);

// When using system variables, remember to provide values for them
// or uncomment the lines below to generate them from the theme.
// @include mat.system-level-colors(my-theme.$light-theme);
// @include mat.system-level-typography(my-theme.$light-theme);
@include material.theme((
color: (
primary: my-theme.$primary-palette,
tertiary: my-theme.$tertiary-palette,
),
));

// Use high contrast light theme colors when users prefer contrast
@media (prefers-contrast: more) {
@include my-theme.high-contrast-light-theme-overrides();
}

// Apply dark theme when users prefer a dark color scheme
@media (prefers-color-scheme: dark) {
@include material.theme((
color: (
primary: my-theme.$primary-palette,
tertiary: my-theme.$tertiary-palette,
theme-type: dark,
),
));

// Use high contrast dark theme colors when users prefers a dark color scheme and contrast
@media (prefers-contrast: more) {
@include my-theme.high-contrast-dark-theme-overrides();
}
}
}
```

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

0 comments on commit 2d7fe98

Please sign in to comment.