Since the counter-based solution seems to still have some drawbacks depending on the possible use cases (besides the thing based on the hack, see the comment for the corresponding answer) I decided to publish a solution based on a list / outline, about which I mentioned earlier. I keep the code here as close to the counter-based as possible so that they can be easily compared. (But in general, everything can be made very clean, structured and general, followed by polishing by renaming and reordering all these namespaces / selectors / mixins / variables, removing unnecessary quotes, etc.).
Opt. one
If you only need a custom sprite icon to have your class in CSS output:
@row: 1; // ...... .my-icon-bundle { .myIcon(@row, @index) { @x: (@row * @index); @y: (@row * @index); background-position: -@x -@y ; } .myIconX(@name) { @icons: "classX1", "classYY1", "classZZZ", "anotheRR9", "etc."; .find(1); .find(@i) when (@name = extract(@icons, @i)) { @name_: e(@name); .my-icon-@ {name_} { #my-icon-bundle.myIcon(@row, @i); } } .find(@i) when not (@name = extract(@icons, @i)) { .find((@i + 1)); } } } // ...... // usage: #my-icon-bundle.myIconX("anotheRR9"); #my-icon-bundle.myIconX("classX1");
Opt. 2
When you just need to create the appropriate classes for all the icons in the sprite:
@row: 1; // ...... #my-icon-bundle { .myIcon(@row, @index) { @x: (@row * @index); @y: (@row * @index); background-position: -@x -@y ; } .icons() { @icons: "classX1", "classYY1", "classZZZ", "anotheRR9", "etc."; .make(length(@icons)); .make(@i) when (@i > 0) { .make((@i - 1)); @name_: e(extract(@icons, @i)); .my-icon-@ {name_} { #my-icon-bundle.myIcon(@row, @i); } } } } // ...... // usage: #my-icon-bundle.icons();
PS All this for LESS 1.5.x, I'm too lazy to make it compatible with earlier versions - sorry.
seven-phases-max
source share