For my project, I have decided to use CSS Modules alongside Less which provides me with the best of both worlds.
Within my src
folder, you will find the following structure:
components/
[all components]
theme/
themes/
lightTheme.less
darkTheme.less
palette.less
palette.less:
@import './themes/lightTheme.less';
Each component that requires colors from the theme follows this pattern:
component.module.less:
@import '../../theme/palette.less';
.element {
background-color: @primary;
}
This setup allows me to modify palette.less
in order to import the desired theme. However, I would like to give users the option to choose their preferred theme dynamically. This means having both themes compiled and switchable at runtime.
An ideal solution could be structured like this:
app.less
body {
@theme: @light-theme;
&.dark-theme {
@theme: @dark-theme;
}
}
I envision importing the @theme
variable in each component and accessing its properties (e.g. @theme[primary]
).
Unfortunately, the scoping of Less variables does not support this approach.
I am willing to consider any solution that leverages Less modules.
Your thoughts are appreciated!