Issue
I have a styles.theme.scss
that looks like the following.
@media (prefers-color-scheme: dark) {
@include example-theme($dark-theme);
}
@media (prefers-color-scheme: light) {
@include example-theme($light-theme);
}
[data-theme="dark-theme"] {
@include example-theme($dark-theme);
}
[data-theme="light-theme"] {
@include example-theme($light-theme);
}
The goal is to use the prefers-color-scheme
if that is configured by the user agent, but override it if the user has configured it on the website.
The current SCSS causes the following warning, however:
WARNING: The same color styles are generated multiple times. Read more about how style duplication can be avoided in a dedicated guide. https://github.com/angular/components/blob/master/guides/duplicate-theming-styles.md
node_modules/@angular/material/_theming.scss 1648:7 -mat-check-duplicate-theme-styles()
node_modules/@angular/material/_theming.scss 7010:3 angular-material-theme()
stdin 29:3 example-theme()
stdin 57:3 root stylesheet
I've checked the provided documentation, but it doesn't appear to cover this case, and I'm unsure on how to better structure this to avoid duplicating the styles.
The only solution I think would work is to detect the preference with JavaScript, and then set the data-theme
attribute if a theme isn't configured in the application.
Is there a better solution than this?
What I've tried:
- To see if I could string a media query and selector together like
[data-theme="dark-theme"], @media (prefers-color-scheme: dark)
, which I don't believe is possible. - If I can use SASS
@if
and@else
to check if thedata-theme
selectors match any elements, else include the@media
queries.
Solution
You should include mat.all-component-colors
instead of mat.all-component-themes
Example: below is wrong
// Generates styles for all systems configured in the theme. In this case, color styles
// and default density styles are generated. Density is in themes by default.
@include mat.all-component-themes($light-theme);
.dark-theme {
// Generates styles for all systems configured in the theme. In this case, color styles
// and the default density styles are generated. **Note** that this is a problem because it
// means that density styles are generated *again*, even though only the color should change.
@include mat.all-component-themes($dark-theme);
}
Use this instead:
@use '@angular/material' as mat;
...
@include mat.all-component-themes($light-theme);
.dark-theme {
// This mixin only generates the color styles now.
@include mat.all-component-colors($dark-theme);
}
For more information, Official Docs
Answered By - Vugar Abdullayev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.