Scenario :
- I have an app which in Id like to use 3 different graphical themes.
- To propagate my themes, I'm using 3 different classes that I apply to my root component,
let call those classes .themeA, .themeB, .themeC.
For every themes,
I have a Sass variable which encapsulate all the colors I need.
For example:
$themeAColors: (
my-background-color: 'blue';
my-color: 'green';
);
$themeBColors: (
my-background-color: 'red';
my-color: 'yellow';
);
$themeCColors: (
my-background-color: 'white';
my-color: 'black';
)
Then in every sub components in which I wish to apply my theme I'm using the following pattern:
@mixin subComponentStyle($theme) {
.title {
background-color: map-get($theme, my-background-color);
color: map-get($theme, my-color);
}
}
:host-context(.themeA) {
@include subComponentStyle($themeAColors);
}
:host-context(.themeB) {
@include subComponentStyle($themeBColors);
}
:host-context(.themeC) {
@include subComponentStyle($themeCColors);
}
Problem :
- Is there a way to avoid or factorize the use of :host-context() selector in every sub components AND respecting component style encapsulation ?
Update: Thank you, that is helping simplify things a bit. Now we would like to find a way to avoid copying this block in every sub-components:
@each $param in ($themeAColors, $themeBColors, $themeCColors) {
$name: map-get($param, name);
:host-context(#{ $name }) {
@include subComponentStyle($param);
}
}
Ideally, we would like to replace this by a function call that would take any mixin in parameter and apply it. So in every components we would just have to call this function with the right mixin to handle theming.