Skip to content
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

Using variables as selector, issue in inheritance #1817

Closed
jeremypetrequin opened this issue Jan 20, 2014 · 4 comments
Closed

Using variables as selector, issue in inheritance #1817

jeremypetrequin opened this issue Jan 20, 2014 · 4 comments

Comments

@jeremypetrequin
Copy link

Hi everybody, thank for your great work!

I'm using LESS on a projet where we have one color by master page, so all the children page use 1 different color for a lot of background, hover, color, border... depending of it parent.

I can't simply use a class .background-color for all my elements to colorize for example, for several reasons.

I wanted to create one variable with all the selectors to colorize, for example :

@background-color : ~'selector1, selector2:hover, selector3 ...';
@text-color : ~'selector4, selector2:hover, selector10 ...';
@border-color : ~'selector4, selector2:hover, selector10 ...'

And after that, using my page parent class name :

.master-1 {
    @{background-color} { background-color:@color1;}
}

.master-2 {
    @{background-color} { background-color:@color2;}
}

But the issue is that the output should be :

.master-1 selector1, .master-1 selector2:hover, .master-1 selector3 {
    background-color:black;
}

And in fact, it's :

.master-1 selector1, selector2:hover, selector3 {
    background-color:black;
}

To summarize :

.master-page-1 {
    .selector-1, .selector-2 {
        background-color:@color1;
    }
}

.master-page-2 {
    .selector-1, .selector-2 {
        background-color:@color2;
    }
}

And

@my-selector: ~'.selector-1, .selector-2';
.master-page-1 {
    @{my-selector} {
        background-color:@color1;
    }
}

.master-page-2 {
    @{my-selector} {
        background-color:@color2;
    }
}

Should output the same CSS, but only the first is correct :

.master-page-1 .selector-1, .master-page-1 .selector-2 {
    background-color:black;
}
.master-page-2 .selector-1, .master-page-2 .selector-2{
    background-color:black;
}

The second output :

.master-page-1 .selector-1, .selector-2 {
    background-color:grey;
}

.master-page-2 .selector-1, .selector-2 {
    background-color:grey;
}

Do you have any idea about this?

@seven-phases-max
Copy link
Member

See #1421 (also #1694 and other issues linked there).

@SomMeri
Copy link
Member

SomMeri commented Jan 20, 2014

Variable values are included into selector "as is", without further analysis. So, list placed into a variable can not be used the way you would like.

You can avoid selectors list repetition if you rearrange your less like this:

@color1: blue;
@color2: red;
.selector1, selector2:hover, selector3 {
  .master-page-1 &  {
    background-color: @color1;
  }

  .master-page-2 & {
    background-color: @color2;
  }
}

it compiles into:

.master-page-1 .selector1, .master-page-1 selector2:hover, .master-page-1 selector3 {
  background-color: #0000ff;
}
.master-page-2 .selector1, .master-page-2 selector2:hover, .master-page-2 selector3 {
  background-color: #ff0000;
}

@SomMeri
Copy link
Member

SomMeri commented Jan 20, 2014

Closing as a duplicate of #1421 and #1694. Feel free to make your case there.

@SomMeri SomMeri closed this as completed Jan 20, 2014
@jeremypetrequin
Copy link
Author

I was writing the same thing @SomMeri :), I've saw that solution in issue #1694, thanks everybody.

matthew-dean added a commit to matthew-dean/less.js that referenced this issue Jun 3, 2018
matthew-dean added a commit that referenced this issue Jun 16, 2018
…3217)

* Adds passing test from #3098
* Added passing test example from #1817
* Allow lists to be re-evaluated as selectors (Fixes #1694)
matthew-dean added a commit that referenced this issue Jun 25, 2018
…no.2) (#3227)

* Fix element to selector list conversion, passing all tests!
* Add passing test from #3098
* Added passing test example from #1817
* Allow lists to be re-evaluated as selectors (Fixes #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

3 participants