-
Notifications
You must be signed in to change notification settings - Fork 3.4k
dot notation for variables #1357
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
Comments
It would be even more interesting if the "base" variable could be interpolated. @palette: something;
@{palette}: {
primary: #0000E0;
info: #02d7e1;
success: #02e10c;
}
.btn {
background: @{palette}.info;
} |
First, I love this. As an idea, why not just use the same pattern as how selectors are defined? (Drop the colon and follow with the block.)
You could then also do something like the LESS pattern for namespacing mixins, which follows the CSS pattern for child selectors:
I think also that the interpolation might be different. If you're wishing to dynamically aim it at another variable, with the
Which doesn't quite make sense. |
So, to be clear, this might be your final syntax, with interpolation:
Palette is substituted with a reference to @something. I believe that's how @@ works, although I haven't used it personally. |
I thought about that and that's initially what I had when I started writing it, but I put the colon there since variables are already defined with a colon, and specifically because selectors are defined without them. It just seems more axiomatic to the Less.js syntax. However, the proposed spec for CSS custom properties ("CSS variables"), does have a syntax like this: :root {
var-foo: calc(var(one) + 20px);
var-bar: calc(var(two) - 20px);
} (from memory, I think that's pretty close to an example in their spec to give credit where it's due - unless it's wrong then it's mine ;-) I'm not convinced of whether that supports going with the colon or without it, I just wanted to mention it. (and this has no bearing, but for the record I'm not a fan of that proposal). Your point about To your last suggestion, I still prefer the dot notation, IMO it's more obvious that |
Variables have a colon normally because it's a key / value pair. CSS follows that too. When blocks are defined, they typically just have a keyword and then the following block. Again, I would refer to something like @Keyframes definition or normal selector blocks. I get where you're coming from; however, I don't think you would ever see a pattern like On So, I agree that Other variants that are CSS-style?
? Dunno. |
Thanks for pointing those out, but I think those requests are from individuals who are far more intelligent than myself. This is just a request for "syntactic sugar" for variables. Not (technically) for accessors or accessing properties on rule-sets. Since there would be no change in the way variables are used or created, nor in how they are scoped. (Before I go on, @lukeapage I'm not attached to this, I just thought it was a neat idea. We have more important issues to tackle so you should feel comfortable closing this if you don't think it makes sense for Less.) In #76, "Accessing specific properties from rule-sets", the OP was asking for the ability to access the value of any property in any given rule-set, which is not what I'm proposing. There might be dialog at some point that turns to something like what I'm proposing, but admittedly I skimmed that one. And #6 is a request for accessors, which is also not what I'm proposing and it's more similar in spirit to the #76. IMO both requests stayed pretty focused on using selectors or mixins for namespacing variables. That is possibly part of the reason they were stuck discussing clunky syntaxes. // #color[@brand]
#color {
@brand: #DDD;
@background: #EEE;
@title: #555;
} But I guess in spirit both requests are asking for "namespaced" variables, which is related to what I'm proposing. And both requests had a lot of +1s so the concept was popular. Just my 2c |
@jonschlinkert Looking at that second example, you COULD namespace using mixins right now. Calling a mixin that has variables imports that into the current scope, which could become the global scope. So you could do:
So, does that work if it allows you to do the same thing in a different syntax? |
@matthew-dean we decided that was a bug which has massive nasty consequences in more complicated cases. |
thats the same as selector interpolation, so we couldn't tell if it was a ruleset or a variable definiton |
With the colon even? |
The colon could be the start of a pseudo selector.
Oops. I've been relying on this and actually coding stuff because of this "supported" behavior, lol. Which means I may not be the only one. (Something to consider.) Why is it a bug? Aren't I dumping the contents of that mixin at that location, including variable declarations? |
I don't know, the "mixin" syntax proposed by @matthew-dean is not at all what I wanted to accomplish with this request. It's a "nice-to-have", but the request was really centered on using dot notation for variables, since that syntax has such obvious denotations in programming, and using the "object" syntax to wrap "groups" of variables, since that syntax also has an established relationship with dot notation. IMO the most important point made was @lukeapage's, regarding how For inspiration, CSS already has at-rules with the following syntaxes (I know there are more, but none are coming to mind right away): @rule "";
@rule {}
@rule () {}
@rule url();
@rule url() screen and (key: value);
@rule:pseudo-class {}
@rule( (key: value) or (key: value) or (key: value) or (key: value) or (key: value) ) { // any nested at-rules }
... I'm assuming interpolation would be a challenge with any of these? Maybe we should think about using something like |
I forgot that we(or me?) then decided it was too much of a breaking change and kept the existing behaviour |
@jonschlinkert is it not achieving the same thing, with a simpler syntax to do this..
it then matches the scoping you can do for mixins and it doesn't complicate the parsing (as much) |
@lukeapage yeah I'm sold on that, that's better than my suggestion I think. |
+1 for allowing variables in existing namespace syntax. I've got a grid semantic system that I'd really like to namespace but can't because I won't be able to properly expose any of the settings or constants. |
I have a need for something like this, and was going to scan the issue list and potentially start a proposal, but realized that none of the several options I considered were anything but inconsistent and problematic; some of those options are listed here. First, the
Regarding the original
According to CSS semantics, since non-space separated dot prefixes indicate an unordered collection of classes, Whatever the definition syntax for nested variables (whether you're selecting variables out of mixins/blocks or specialized deep variables as initially suggested, it seems like the cleanest thing to do would be to introduce a new syntax. For example: In any case, I feel the real usefulness of something like this would be general settings with specific overrides, e.g.:
Of course, being able to fetch the applicable value of a given attribute would be far more useful for this kind of thing in practice, since there's no need for a @border-width variable if you can fetch the value of a given border-width attribute. |
I have commented that I don't want less to be parsing selectors directly as values.. but I would support having a special function e.g.
Why would an error be hard to debug? I think it should error if it matches multiple.
if we matched properties or variables and showed an error if we matched 2 then people would just have to rename their variables if they wanted to use this feature
I agree it is probably a good idea to have a way of wrapping it.. we could then implement simple cases to begin with and have the option of expanding it to what you describe with multi-selectors etc. |
All very reasonable. I hadn't considered the practicality of the same syntax selecting either attrs or vars, but especially if we can select attrs too, there'd be little reason to have same-named variables in practice. I think 'select' would be a slightly better name, since 'selector', while accurate to what it's operating on, suggests it's "forming a thing", while 'select' suggests it's picking something.
That syntax suggestion wasn't meant to be taken serious in form, but rather function: it's unambiguous with current syntax rules, and would allow a way to interpolate arbitrary expressions. Admittedly, your intention for 'selector' and its syntactically identical handling of var and attrs eliminates the stated need for @{[]}, though @{[]} attempted to solve one thing that 'selector' does not: It seems to be an unfortunate (but most likely thoroughly considered) legacy of the syntax that interpolation doesn't accept an expression, e.g. |
This! I just thought up the concept myself, and after getting an error on test decided to see if anyone else had the idea. I think Less needs to evolve in a manner that resembles JS, and this would be a great step forward! Being able to assign properties to variables would make it much easier to organize and pass arguments, switch out groups of properties based on other values, etc. It would also be great if you could could assign a mixin as the value of a var, like: Also, FWIW, I prefer the dot notation to the ">", and think it looks cleaner. |
agreed! |
Less is designed to resemble CSS. JS just happens to be the language of the Less language parser, so Less features are not, by design, JavaScript-based. Because many of us are JavaScript developers, that's sometimes where some inspiration comes, but I don't believe part of the mission is to merge the two. Discussion should maybe be continued on #1848? |
The whole point of LESS and SCSS is to be inviting to web designers who already write CSS, not programmers. What you're describing is a different product. |
I suppose what I meant is that I'd like to see Less adopt a more object-oriented approach, not necessarily anything JS specific. Thanks for the link to the other issue! |
closing as duplicate |
Just throwing this out because I thought it was an interesting idea, and it could be a nice addition to the language for organizing code.
The concept is that variables could be defined in a similar way to JavaScript objects, so given this:
You could then apply the variables like this:
I suppose there are no transformative advantages to doing this, but it could be useful as a way of organizing "groups" of variables.
The text was updated successfully, but these errors were encountered: