|
/** |
|
* Less Value Accessors | Summary: Variable & Property Access + Sugar |
|
* |
|
* In addition to the extended variable reference syntax (`@{}`), a property |
|
* reference syntax would be introduced to allow direct access to both local |
|
* and namespaced properties. This syntax would be nearly identical to the |
|
* variable reference syntax, but with a different symbol. `$` is proposed. |
|
* |
|
* The syntax sugar would have to wait for the next major version because of |
|
* ambiguity concerning color hexes (`#abc@var` → `#aabbcc [value]` atm; see |
|
* https://github.com/less/less.js/issues/1848#issuecomment-96118255). |
|
*/ |
|
|
|
|
|
#ns() { |
|
@var: bar; |
|
prop: bar; |
|
.kid { |
|
@var: baz; |
|
prop: baz; |
|
} |
|
} |
|
|
|
.selector { |
|
@var: foo; |
|
prop: foo; |
|
|
|
|
|
// BASIC USE |
|
// ----- |
|
|
|
// accessing a locally available variable is the same as always |
|
local-var: @var; // → foo |
|
// accessing local properties |
|
local-prop: ${ prop }; // → foo |
|
local-prop: $prop; // → foo // alternate syntax (sugar)* |
|
|
|
// accessing variables from namespaces (or mixins or rules) |
|
ns-var: @{ #ns > var }; // → bar |
|
ns-var: @{ #ns>var }; // → bar |
|
ns-var: @{ #ns var }; // → bar // whitespace or `>` required** |
|
ns-var: #ns@var; // → bar // alternate syntax (sugar)* |
|
// accessing properties from namespaces (or mixins or rules) |
|
ns-prop: ${ #ns > prop }; // → bar |
|
ns-prop: ${ #ns>prop }; // → bar |
|
ns-prop: ${ #ns prop }; // → bar // whitespace or `>` required** |
|
ns-prop: #ns$prop; // → bar // alternate syntax (sugar)* |
|
|
|
// accessing variables or properties from nested namespaces |
|
ns-kid-var: @{ #ns.kid var }; // → baz |
|
ns-kid-prop: ${ #ns.kid prop }; // → baz |
|
|
|
// * Alternate syntaxes are allowed wherever `@var` is allowed. |
|
// ** The ambiguity of `@{ #nsvar }` and `${ #nsprop }` make them impractical to implement; |
|
// they should throw an error. |
|
|
|
|
|
// INTERPOLATION |
|
// ----- |
|
|
|
// interpolating a locally available variable is the same as always |
|
.@{var} { /* ... */ } // → .foo |
|
|
|
// interpolating a local property (always requires `${}`) |
|
.${prop} { /* ... */ } // → .foo |
|
|
|
// interpolating an accessed variable or property (always requires `@{}` or `${}`) |
|
.@{#ns var} { /* ... */ } // → .bar |
|
.${#ns prop} { /* ... */ } // → .bar |
|
|
|
// interpolation is allowed wherever variable interpolation is allowed |
|
background: url("path/@{#ns var}/img.png"); // → url("path/bar/img.png") |
|
} |
|
|
|
|
|
// OVERRIDING |
|
// ----- |
|
|
|
#ns() { // overriding is straight-forward |
|
@var: qux; |
|
} |
|
|
|
.selector { |
|
.@{#ns var} { /* ... */ } // → .qux |
|
} |
What if we allowed the interpolation form to return a reference to just the namespace?
Only thing is, this would serve this feature, but if you wanted to reference a selector for some other feature, element names vs. vars or props would be ambiguous. Unless we require explicit referencing within the interpolated form. e.g.
Of course, that makes it a bit redundant. OR maybe a different form to point to just the namespace?