Replies: 1 comment
-
Unless I'm mistaken, I think |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Underlying issue
I've come to love the property/variable accessors for writing & passing around configs, as it makes everything much more readable and finally does away with manually namespacing variables when managing a large central config.
Yet using it to pass variables to a mixin has a limitation: Mixins can't lookup up "default values" from within detached variable rulesets, as the detached ruleset as a whole is the fallback value for the mixin parameter, and calling it with only a partially filled out ruleset as parameter won't merge this ruleset with the fallback default.
This hasn't ever been an issue before property/variable accessors, as mixins could always define default values for parameters, and you could just use a guard and define a dummy
@optional = false;
for anything else.Example
Suppose we'd be using lookup values in a mixin like this:
Calling the mixin without passing parameters works as intended, as it has a default value:
But there seems to be currently no way of treating the contents of a detached ruleset as defaults. Using it like this will fail as expected, as there is no
[value-01]
to be accessed in this context:Proposed Solution
A nice solution would be to introduce some way of checking if a variable or detached property exists before trying to access/use it (if it is possible at all to implement a checking function like this with the way LESS parses & processes variables and mixins).
This could ideally be implemented similarly to type-checking functions, and used in conjunction with guards:
Example Usecase
Finally, to explain why I was trying to use variable rulesets in this way, and would prefer to do so compared to other available methods that don't have this limitation:
In my projects I use mixins to facilitate the creation of repetitive styles that need to be available in an undefined amount of different variants, like button sizes & colors.
In this buton-color-variant scenario, it isn't ideal to use with just variables as parameters
So I'd like to refactor those mixins to use a detached ruleset that allows nesting of these parameters, to have a nicer and more readable interface and be able to use it like this:
but this currently requires all the properties to always being set in the variable ruleset, and as much as it is nicer to write and to read, making certain properties optional won't work without the ability to check for the existence of a variable to be looked up.
Beta Was this translation helpful? Give feedback.
All reactions