Skip to content

Variable property #36

Closed
Closed

Description

@gjunge

See: http://groups.google.com/group/dotless/browse_thread/thread/9d580e75248b3250

Hi,

SHORT VERSION:
Is the following doable:
.bla {
padding-@variable: 5px; //where @variable could be left, right, top, bottom.
}

INTRO:
I'm working on a website that supports both LTR and RTL. So far I made
two stylesheets, one for LTR and one for RTL. The RTL stylesheet
imports the LTR stylesheet and "overrides" those items that are RTL
specific. (padding-right vs padding-left etc).

float: right/left I manage by having a variable called @SIDE (and
@side-opposite) and setting it to the correct side in both stylesheets
(in RTL it is "right", and in LTR it is "left"). In my LTR stylesheet
I write : float : @SIDE

That was the intro....

QUESTION:
I would also like to eliminate the "overriding" issue, to keep my code
more DRY.

The ultimate thing would be if I could make the property variable:

.bla {
padding-@SIDE: 20px;
}

Can this be done?

Thanks a lot.
Gidon

Activity

  1. cloudhead commented on Jun 16, 2010

    @cloudhead
    Member

    Hey, you can do that with pattern matching, let me illustrate:

    .blah ()       { color: black }                     // All blahs
    .blah(right)  { padding-right: 20px }          // Right blahs
    .blah(left)    { padding-left: 20px }           // Left blahs
    
    @side: left;
    .class { .blah(@side) }
    

    Output

    .class { color: black; padding-left: 20px;}
    

    So in essence, you can match an arbitrary mixin depending on a variable, instead of creating an arbitrary property name.

  2. jamesfoster commented on Jun 17, 2010

    @jamesfoster
    Contributor

    Gidon, this is in dotless so you could use it as cloudhead suggests.

    One issue with this it takes a lot of setting up. instead of potentially having 4 mixins, one each for padding-top, -left, -bottom and -right. you would just have 1.

    as an aside. I like the sass sub property syntax...

    .class {
      padding: {
        top: 10px;
        left: 20px;
        bottom: 30px;
        right: 40px;
      }
    }
    

    this outputs as the usual padding-top: 10px;

    perhaps it would be possible to store that in a variable?

    @left: 1;
    @right: 0;
    
    @mypadding: {
        left: 20px * @left;
        right: 20px * @right;
      }
    
    .class {
      padding: @mypadding;
    }
    

    (I'm just thinking out loud, it's probably not very elegant but looks interesting.)

  3. gjunge commented on Jun 18, 2010

    @gjunge
    Author

    Looks also nice, though it doesn't look very "lessy", does it?

    LESS also doesn't support conditions, or does it?
    Because that would also solve it:

    .mypadding {
    [if @direction="ltr"]
    padding-left : 20px;
    [else]
    padding-right: 20px;
    [endif]
    }

  4. cloudhead commented on Jun 18, 2010

    @cloudhead
    Member

    the best solution is the one I proposed imo. Cascading stylesheets shouldn't have logic in them, so if/then/else is not an option.

  5. jamesfoster commented on Jun 18, 2010

    @jamesfoster
    Contributor

    :)

    I love it when people say things like that.

    surely, when i write .blah(right) there is logic which determines which mixin to mixin!

    there is logic in less. It just doesn't feel like logic because it doesn't include the word "if".

    i still like the #{} idea from sass, I just think implementing it would be a nightmare.

  6. cloudhead commented on Jun 18, 2010

    @cloudhead
    Member

    there's logic in the implementation of course, but not in the stylesheet. It's declarative. Yea, attribute interpolation is powerful, but I don't think we need it.

  7. gjunge commented on Jun 19, 2010

    @gjunge
    Author

    Yep, I got your point. I can work with the solution provided in less.js

  8. dylan commented on Jul 8, 2010

    @dylan

    How would you recommend doing a CSS3 mixin? right now I can do this:
    .transition-property(e("box-shadow, top"));
    .transition-property(@attr) {
    transition-property: @attr;
    -webkit-transition-property: e(%("-webkit-%d", @attr));
    -o-transition-property: e(%("-o-%d", @attr));
    -moz-transition-property: e(%("-moz-%d", @attr));
    }

    But that only allows for one cross-browser compatible call per selector. :\

  9. jamesfoster commented on Jul 8, 2010

    @jamesfoster
    Contributor

    How would you prefer to use it?

  10. dylan commented on Jul 8, 2010

    @dylan

    I am not entirely sure what an elegant way would be to pull this off, not without iterating through arguments and having logic of some sort to modify the strings. :P As it stands there are only a few properties that you would need to do for this sort of thing, but I am spoiled and dont want to do them all by hand. :P

  11. dylan commented on Jul 8, 2010

    @dylan

    Gradients are another thing that logic would help with generation. Especially considering the differences in browser syntax at the moment. Right now I have to make different gradient mixins, ones that accept 3 arguments, and ones that accept 2. :D

  12. cloudhead commented on Jul 8, 2010

    @cloudhead
    Member

    Hmm, could you show me the desired CSS output, maybe in a Gist, and I'll see if I can think of anything.

  13. dylan commented on Mar 11, 2011

    @dylan

    nice!

  14. 18 remaining items

  15. Soviut commented on Jun 29, 2013

    @Soviut

    Probably when 1.5 comes out and no, there is no definitive timeline for that.

    There has never been any resistance to this feature, only discussion. Being a library that others rely upon, the developers have be very sure-footed and as forward-looking as possible when they make implementation decisions.

  16. jonschlinkert commented on Jun 29, 2013

    @jonschlinkert
    Contributor

    Agreed with @matthew-dean, there isn't resistance to it, but there is limited time. I don't recall seeing @lukeapage mention this as being planned for 1.5, he's super busy atm. @theLucid are you interested in implementing the code for this?

  17. Soviut commented on Jun 29, 2013

    @Soviut

    Link the pull request if you can. I'm sure if it was rejected that there was a good reason for it.

  18. TedDriggs commented on Aug 16, 2013

    @TedDriggs

    For the specific case of bidirectional sites, I'd like to suggest a more-scoped alternative: Create near and far and replace them with left/right as appropriate. That way your stylesheets remain concise and logic-free, and now actually do a better job of being directionally agnostic.

  19. Soviut commented on Aug 16, 2013

    @Soviut

    @TedDriggs what are you referring to? This is a discussion about allowing attributes/properties to have variables in their names. Any references to "left" or "right" are just examples one might use for wanting to add left float and left margin using a mixin.

  20. cloudhead commented on Aug 16, 2013

    @cloudhead
    Member

    Given that we've introduced variable interpolation in selectors since then, I'd say it's OK (if not expected) to have it in property names too now.

  21. jonschlinkert commented on Aug 16, 2013

    @jonschlinkert
    Contributor

    👍

  22. Soviut commented on Aug 16, 2013

    @Soviut

    👍 agreed

  23. seven-phases-max commented on Oct 14, 2013

    @seven-phases-max
    Member

    Above. I did not create a pull-request to the master repo yet as I guess it would probably be too dramatic change for 1.5.0-beta at the last moment.

  24. cwygoda commented on Dec 14, 2013

    @cwygoda

    👍

  25. lukeapage commented on Feb 13, 2014

    @lukeapage
    Member

    In 1.6.0

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions