Would it be possible in LESS to have a mixin nested inside another one so that the former gets called only when the element is child of an element with the latter mixin?
I know, confusing, here is a simple example (not working code, just concept):
LESS
.foo(@x) {
width: @x;
.foo(@y) {
width: @y/@x;
}
}
.a {
.foo(20px);
.b {
.foo(2);
}
}
Output CSS
.a {
width: 20px;
}
.a .b {
width: 10px;
}
When I do this, calling .foo(2)
on .b
gives compiles to width: 2
.
Is this supposed to be like this by design, or am I getting something wrong in the syntax? Also, am I approaching the problem from a completely wrong angle and there is perhaps a much simpler solution that I am not considering?
EDIT
Ok, apparently that was fixed with the newest versions of LESS, what I am trying to achieve, though, is slightly more complicated than the minimal example I gave above.
Basically what I would like to happen is that every .foo
which is a child of another element with the .foo
mixin would take its parent variable for calculation, so, ideally
LESS
.foo(@x) {
width: @x;
.foo(@y) {
width: (@x/@y);
}
}
.a {
.foo(100px);
.b {
.foo(2px);
.c {
.foo(5px);
/* ...and so on */
}
}
}
Output CSS
.a {
width: 100px;
}
.a .b {
width: 50px;
}
.a .b .c {
width: 10px;
}
What I get is, instead:
.a .b .c {
width: 50px;
}
I tried to modify the LESS as follows:
.foo(@x) {
width: @x;
.foo(@y) {
@x: (@x/@y)
width: (@x);
}
}
But I get a syntax error for recursive variable definition. Apparently LESS doesn't allow for definitions like:
@a: 1;
@a: (@a+1);