I'm trying to create a Less mixin to generate media queries. The goal is to store my breakpoints in a variables.less
file, and loop through them to create @media
blocks.
The mixin would then be used as:
.mq-medium({
// rules
});
and generate CSS like:
@media only screen and (min-width: 640px) {
// rules
}
Here's my current mixin:
variables.less
/* media queries */
@breakpoints: small 0, medium 640px, large 1024px, xlarge 1281px, xxlarge 1440px;
mediaqueries.less
.createMQClasses(@iterator:1) when(@iterator <= length(@breakpoints)-1) {
@breakpoint: extract(extract(@breakpoints, @iterator),1);
@breakpoint-next: extract(@breakpoints, (@iterator + 1));
@breakpoint-next-px: extract(@breakpoint-next, 2);
.mq-@{breakpoint} {
@media only screen and (min-width: extract(extract(@breakpoints, @iterator),2)) {
}
}
.createMQClasses((@iterator + 1));
}
.createMQClasses();
So far my code loops through and generates empty @media
blocks. However, I need to pass any @rules
through to the output. I've done this with static classnames previously, like this:
.mq-medium(@rules) {
@media only screen and (min-width: extract(extract(@breakpoints, 2),2)) {
@rules();
}
}
And that works fine.
But with the dynamic name it's causing errors. I've tried adding an additional parameter to the .mq-@{breakpoint}
statements, like:
.mq-@{breakpoint}(@rules) {
@media only screen and (min-width: extract(extract(@breakpoints, @iterator),2)) {
@rules();
}
}
This results in various errors. How can pass the included rules through so they're included in the mixin's output?