I have been working on a project that requires runtime theming. To achieve this, I developed a theme system that combines SCSS variables with CSS Variables. Here's an example of how it functions.
:root {
--primary-color: 196;
}
// Primary
$primary-100: hsl(var(--primary-color), 90%, 98%);
$primary-400: hsl(var(--primary-color), 90%, 65%);
$primary-main: hsl(var(--primary-color), 90%, 50%);
$primary-700: hsl(var(--primary-color), 90%, 30%);
$primary-900: hsl(var(--primary-color), 90%, 10%);
While this approach works seamlessly with my custom components, I am encountering difficulties when trying to integrate it with the Material design theme system.
My initial plan was to follow the guidelines outlined in the Angular material documentation and utilize my SCSS variables instead of static colors. Below is an excerpt from my theme.scss file depicting this concept.
@import '~@angular/material/theming';
@import 'var.scss';
@include mat-core();
$shop-primary: (
50: $primary-100,
100: $primary-100,
200: $primary-200,
300: $primary-300,
400: $primary-400,
// ..... all other colors
contrast: (
50: $black-87-opacity,
100: $black-87-opacity,
200: $black-87-opacity,
// ..... all other colors
)
);
$shop-app-primary: mat-palette($shop-primary);
$shop-app-accent: mat-palette($shop-primary);
$shop-app-warn: mat-palette($shop-primary);
$shop-app-theme: mat-light-theme($shop-app-primary, $shop-app-accent, $shop-app-warn);
@include angular-material-theme($shop-app-theme);
However, I encountered the following error:
Argument `$color` of `rgba($color, $alpha)` must be a color
This error seems to be caused by the fact that the Angular Material mixin expects a valid color
, rather than an hsl()
value.
Therefore, my query is how can I successfully create a customized material theme utilizing runtime CSS variables?