Skip to content

Feature request: Property lookup (a.k.a Properties are Variables too) #2433

Closed

Description

@krnlde

I found a pretty neat feature in Stylus recently, a property lookup which allows you to use properties in the current or closest parent ancestor and use it for calculations.

In Stylus this would look like this:

.test
  foo: 200
  bar: (@foo/2)
  .child
    baz: @foo

which produces this CSS:

.test {
  foo: 200;
  bar: 100;
}
.test .child {
  baz: 200;
}

For less we could use the $-sign or maybe brackets to select properties. It then would look something similar to this:

.test {
  foo: 200;
  bar: $foo / 2;
  .child {
    baz: $foo;
  }
}

or this respectively:

.test {
  foo: 200;
  bar: [foo] / 2;
  .child {
    baz: [foo];
  }
}

Thoughts on this?

Activity

  1. SomMeri commented on Feb 3, 2015

    @SomMeri
    Member

    What is your use case? In current less.js, you can place foo value into variable and achieve something similar.

    This:

    .test {
      @foo: 200;
      foo: @foo;
      bar: @foo / 2;
      .child {
        baz: @foo;
      }
    }

    compiles into:

    .test {
      foo: 200;
      bar: 100;
    }
    .test .child {
      baz: 200;
    }

    If you have a case where such refactoring is not possible, it would be helpful if you would posted it here. Generally speaking, less.js adds new features when some use case is impossible or too cumbersome to do. Use case would help us to decide how much is such feature needed (or we could find another way how to achieve what you wants).

  2. krnlde commented on Feb 3, 2015

    @krnlde
    Author

    The usecase would be that you can build up semantic contexts, for example:

    @gutter-width: 30px;
    .row {
      margin-left: -(@gutter-width / 2);
      margin-right: -(@gutter-width / 2);
    
      .col-half {
        width: 50%;
        float: left;
        padding-left: -$margin-left;
        padding-right: -$margin-right;
      }
    }

    Since in bootstrap the margin is dependent on the padding and vice versa, why not connecting them directly rather than just using the same variable?

    You would delete multiple variable references and build up a more coherent module that relies on its context rather than just variables. The guys from Stylus obviously see a usecase for this otherwise they would have deprecated this as a relict.

  3. krnlde commented on Feb 3, 2015

    @krnlde
    Author

    Another example would be alert boxes:

    .alert {
      color: #f00;
      background: lighten($color, 25%);
    }
  4. Justineo commented on Feb 3, 2015

    @Justineo
    Contributor

    I think it's not necessary but nice to have. Variables can do the job but direct property references are more readable.

  5. krnlde commented on Feb 3, 2015

    @krnlde
    Author

    Of course it is not necessary, at all. We worked with variables for years and it's awesome. Especially the lazy loading behaviour every other Pre-Processor is lacking. Nonetheless I wanted to post this for consideration.

  6. matthew-dean commented on Feb 3, 2015

    @matthew-dean
    Member

    I agree that something like this would be intuitive. A lot of times I have an initial value, set for a property, and if I want to use that value elsewhere, it has to be refactored into a variable and then re-added to the property. This proposal would be simpler for authors.

    There have been similar requests to have parts of the immediate Less tree accessible as structured data, so I'm for having more "accessors" to what you've already written.

  7. krnlde commented on Feb 3, 2015

    @krnlde
    Author

    I see the concern to blow up the language too much so it might not be considered "sleek" anymore though.

  8. lukeapage commented on Feb 4, 2015

    @lukeapage
    Member

    Related to #76 and #6 - though this approach is simpler. It does save on useless code assigning to a variable and then reading from it

  9. krnlde commented on Feb 4, 2015

    @krnlde
    Author

    Reading #76 I found some similarities which might be combined, say if you background-color: lighten([color], 25%); it would lookup the current ancestor tree. However if you write background-color: lighten(#mySpecialSelector[color], 25%); it'll instead use the ancestor tree of the selector.

    That would really be useful and opens a whole new level of opportunities.

  10. matthew-dean commented on Feb 4, 2015

    @matthew-dean
    Member

    @lukeapage What do you think the code impact would be? If local (current tree) properties could be shimmed to behave similar to local vars, it seems like it would be not that major (but you would know better). It could add a certain elegance to the language, since people could do:

    .rectangle {
      width: 200px;
      height: ([width]/2);
    }
    

    Instead of:

    .rectangle {
      @width: 200px;
      width: @width;
      height: (@width/2);
    }
    

    The first example seems easier to read, and feels closer to the intention of the author. I personally have written code like the second example many times, so I like this idea.

  11. lukeapage commented on Feb 4, 2015

    @lukeapage
    Member
  12. lukeapage commented on Feb 4, 2015

    @lukeapage
    Member
  13. matthew-dean commented on Feb 4, 2015

    @matthew-dean
    Member

    Attribute values use square brackets, so if we're including selectors in the property lookup, then square brackets wouldn't work, because this is a valid selector:

    #mySpecialSelector[color] {
    }
    // matches:
    <div id="mySpecialSelector" color></div>
    

    The square brackets match an element that has the attribute "color". We could be more explicit, with something like prop().

    .rectangle {
      width: 200px;
      height: (prop(width)/2);
    }
    

    However, I know in the past we've tried to avoid nested parentheses hell, so... what if we combine the $ with our interpolated variable syntax?

    .rectangle {
      width: 200px;
      height: (${width}/2);
    }
    .example {
      background-color: lighten(${color}, 25%); 
      background-color: lighten(${color, #mySpecialSelector}, 25%);
    }
    

    Something like that? That way attribute selectors are valid:

    .parent[id] {
      height: 50%;
      .child {
        height: (${height, .parent[id]} / 2);
      }
    }
    

    What do you guys think?

  14. krnlde commented on Feb 4, 2015

    @krnlde
    Author

    I like ${color}.

  15. 33 remaining items

  16. seven-phases-max commented on Feb 6, 2015

    @seven-phases-max
    Member

    @SomMeri

    All those four issues are the same underlying problem? Is it easy/hard to fix in your opinion?

    Yes, it's the same underlying problem but in most cases it's more about different expectations of how these things should work (of how scoping should work and how it works actually) and not really the code that suffers from #1316 . We discussed possible ways to fix #1316 itself around #2212 (comment) (not so easy but probably possible) but those issues (half-issues/half-feature-requests) I mentioned (except your example above of course) are actually asking to fix it in opposite way or provide a backdoor for that (thus breaking current Less behaviour, the good example is #2435).

    @krnlde I'm sorry for mismentioning...

  17. matthew-dean commented on Feb 6, 2015

    @matthew-dean
    Member

    When I read a style declaration I don't want to go crazy to understand where the property value is coming from. Like now it has to be a normal value or a variable, or maximum an inline calculation that uses variables. That's it. This makes css preprocessors a pleasure to use. What you're proposing is a kind of dynamic way to define variables, which is just weird. I don't how javascript developers can not agree with the fact that is weird.

    I think you're probably misunderstanding. There's nothing proposed here that is a dynamic way to define variables. It actually seems like a very intuitive step for a few reasons.

    Variables in Less came directly from (or was inspired by) the behaviour of properties. In some preprocessors, if you assign a variable, then read it, then assign it, then read it, all within the same scope, you'll get a different result. In Less, the behavior is like CSS properties: the latest declaration in the scope wins. So, right now, variables and properties have a lot of overlap. If you call a mixin, you get not only its properties but its variables. And you can actually do things like add properties together (or rather, add its values) with the property+: and property+_: syntax. The one thing you can't yet do is reference the property.

    So, variables are meant to behave like properties, and properties have evolved to behave a lot like variables. In the end, you're assigning a value to a key. We have a way to reference the value for one type of key, and what's proposed here is simply a way to reference the value for the other type of key.

    Less scoping has too many rules to remember (imo) already

    There wouldn't be any additional scoping rules to remember. These two blocks would become functionally equivalent as far as scoping.

    .block {
      @height: 10px;
      width: @height / 2;
    }
    .block {
      height: 10px;
      width: $height / 2;
    }

    The difference is that height is output as a property in the second block. But scoping does not change.

  18. matthew-dean commented on Feb 6, 2015

    @matthew-dean
    Member

    Or the TL;DR way to say it: properties in Less, by and large, already act like variables, but you cannot reference them. This feature would allow property referencing.

    Another way to demonstrate the above:

    //Less
    .block {
      @height: 10px;
      width: @height / 2;
      @height: 20px;
    }
    // output
    .block {
      width: 10px;
    }
    //Less w/ property reference
    .block {
      height: 10px;
      width: $height / 2;
      height: 20px;
    }
    //output
    .block {
      width: 10px;
      height: 20px;
    }
  19. matthew-dean commented on Feb 6, 2015

    @matthew-dean
    Member

    @krnlde's example inspired another demo:

    .square(@width) {
      height: @width;
    }
    @row: 100px;
    .column-2 {
      width: @row / 2;
      .square($width);
    }
    .column-4 {
      width: @row / 4;
      .square($width);
    }
    
    
    // output
    .column-2 {
      width: 50px;
      height: 50px;
    }
    .column-4 {
      width: 25px;
      height: 25px;
    }
  20. ase69s commented on Mar 10, 2015

    @ase69s

    This is a useful feature, in my case i needed to extend only some specific properties from another class (that class is defined in an external stylesheet so in order to use variables i would have to modify that stylesheet in each new version they publish)

    With this new syntaxis i could do it like this:

    The stylesheet.less provided from another source:

    .divheader{
       height:50px;
       color: red;
       line-height: 1.5;
       width: 100px;
    }

    My stylesheet.less:

    .mydivheader{
       .divheader.height; //invented syntax
       .divheader.line-height; //invented syntax
       color: black;
    }
    //output
    .mydivheader{
       height:50px; //inherited
       line-height: 1.5; //inherited
       color: black;
    }

    So i only inherit the height and line-height properties of the divheader class...

  21. matthew-dean commented on Mar 10, 2015

    @matthew-dean
    Member

    @ase69s In your case we would need to address referencing variables within rulesets. See #1848. Essentially, this feature would make property assignment behave (somewhat) like variable assignment (albeit with different referencing syntax), so your case wouldn't happen before #1848 is addressed.

    If / when implemented, I would see the syntax being a little closer to this:

    .divheader{
       height: 50px;
       color: red;
       line-height: 1.5;
       width: 100px;
    }
    .mydivheader {
      height: ${.divheader > height};
      line-height: ${.divheader > line-height};
      color: black;
    }

    However, since you're not calculating anything, there are a few other options available to you right now, such as:

    @divheader: {
      height: 50px;
      line-height: 1.5;
    };
    .divheader{
      @divheader();
       color: red;
       width: 100px;
    }
    .mydivheader {
      @divheader();
      color: black;
    }

    You could also have a mixin that sets properties, or use :extend on a common ruleset. So rather than directly referencing another class, you could have two divs reference another detached ruleset or mixin.

  22. self-assigned this
    on Aug 5, 2015
  23. ldetomi commented on Oct 9, 2015

    @ldetomi

    +1 I completely agree with Justineo:
    "I think it's not necessary but nice to have. Variables can do the job but direct property references are more readable."

  24. jlem commented on Oct 23, 2015

    @jlem

    I'd say this is more than nice to have. I have a site that lets you build skins by picking a trim color that controls buttons, links, and misc elements. This is stored in a database, and gets bootstrapped onto the page via a <style> tag in the header as the class "trim". But because this value is hard-coded, I have no easy way to reference it to do things like create faded versions of that same color on demand, and I'd rather not use JS for styling...

    As it stands now, I'll have to manually pick off-shade variants of the trim color and assign those to classes as well, but even then, I don't have very easy control over WHICH properties that faded trim color would get applied to (e.g. borders), inner backgrounds etc.

  25. matthew-dean commented on Oct 23, 2015

    @matthew-dean
    Member

    @jlem There's already a PR in progress (that I worked on - #2654), and basically working, but there are a few small issues to iron out. Interesting use case, but that wouldn't apply here, as the other styles would not be accessible by Less.

  26. stale commented on Nov 14, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  27. matthew-dean commented on Nov 28, 2017

    @matthew-dean
    Member

    Note that simple property referencing ($prop) is implemented, but not documented (AFAIK).

  28. olets commented on May 13, 2024

    @olets

    For others who like me find this issue before the relevant docs: this was added to the documentation in 2018 https://lesscss.org/features/#variables-feature-properties-as-variables-new-

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions