I'm trying to use LESS to dynamically generate a set of mixins that would help me write cleaner media query code. So far in my limited knowledge of the language I've put together code that looks like this:
@sizes: xxs, xs, sm, md, lg;
.mediaQueries(@iterator:1) when(@iterator <= length(@sizes)) {
//Extract name
@sizeName: extract(@sizes, @iterator);
//Attempt to build min-width query
.MQ-min-@{sizeName} (@content) {
@media (min-width: @screen-@{sizeName}) {
@content();
}
}
//Attempt to build max-width query
.MQ-max-@{sizeName} (@content) {
@media (max-width: @screen-@{sizeName}) {
@content();
}
}
.mediaQueries((@iterator + 1));
}
.mediaQueries();
The goal is to have a set of mixins that would allow me to easily and cleanly define some CSS properties for a specific breakpoint, like so:
.generic-class {
background: black;
//Sizes @screen-sm and up
.MQ-min-sm({
background: transparent;
})
}
It doesn't work. Something to note, I'm trying to interpolate the size name into a variable name that would then output me a the px
value of that variable into the @media
query. Is something like this even possible?
Otherwise my compiler currently breaks on the start of the nested mixin (.MQ-min-@{sizeName} (@content) {
) with the error:
Potentially unhandled rejection [2] Missing closing ')' in file ../mixins.less line no. 43
Is something like what I'm trying to achieve possible?