Skip to content

New Feature: Ranges #1481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bizzehdee opened this issue Aug 9, 2013 · 6 comments
Closed

New Feature: Ranges #1481

bizzehdee opened this issue Aug 9, 2013 · 6 comments

Comments

@bizzehdee
Copy link

At the moment, it is possible to do something such as

.container_@{columnCount} {
margin-left: auto;
margin-right: auto;
width: unit(@baseWidth, px);
}

What would it take to have something in LESS that allows you to do:

.container_[1-24] {

or

.container_[1-@{columnCount}] {

which would then cause less to generate

.container_1,
.container_2,
.container_3,
...,
.container_24 {
/code/
}

I am willing to look into adding this myself, but would need pointing in the right direction as i am new to the LESS codebase.

@dangreen
Copy link

Good idea.

@SomMeri
Copy link
Member

SomMeri commented Aug 14, 2013

Off-topic: it is better to close the code in four tics:
````
your code here
````

or add four spaces in from of each line. It will be easier to read and github will not send mails to whoever happen to have the same user name as your variables.

@SomMeri
Copy link
Member

SomMeri commented Aug 14, 2013

Looping support was discussed here #869. The bad news is that the idea was rejected in the end,because there is an existing workaround to this problem. Of course, feel free to make your case there.

That being said, less.js would welcome new coders and pull requests. It is just that the trend is to be careful with features that do not solve new use cases, because each such feature has to be maintained forever and may complicate future development.

The workaround involves looping mixin:

.loop (@index) when (@index > 0) {
  .loop((@index - 1)); 
  // rulesets are defined here
  .container_@{index} {
    property: value;
  }
}
.loop (@index) when (@index =< 0) {
}

.loop(5);

Compiled css is more verbose then what you suggests, but still functionally equivalent:

.container_1 {
  property: value;
}
.container_2 {
  property: value;
}
.container_3 {
  property: value;
}
.container_4 {
  property: value;
}
.container_5 {
  property: value;
}

@lukeapage
Copy link
Member

The biggest issue is the syntax.. making something that isn't being used and will never be used in the future for css.

@calvinjuarez
Copy link
Member

Actually, with &:extend(), you can get exactly the same output with the .loop(@index) mixin technique like this:

.example-1 {
  /* stuff */
}
.example {
    // Set the Loop (see footnote [1])
    .loop(@index) when (@index > 1) {
        .loop((@index - 1));
        &-@{index} {
            &:extend(.example-1);
        }
    }
    .loop(@index) when (@index =< 1) { }

    // Generate
    .loop(5);
}

Which outputs this:

.example-1,
.example-2,
.example-3,
.example-4,
.example-5 {
    /* stuff */
}

[1] Including the loop inside the base class prefix scopes it so it won't interfere with any other .loop()s you may want to have in other places.

Tested with CodeKit and at less2css.org.

@lukeapage
Copy link
Member

Merge to #1694

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants