Replies: 1 comment 1 reply
-
@krsdcbl It's really a matter of managing loops, as you would in other programming languages. This input: @COLORTHEMES: '', '.theme-primary', '.theme-secondary';
// accessor
.mapByTheme( @dr, @themes: @COLORTHEMES ) {
each( @themes, {
@theme-selector: e(@value); // sanitize theme selector
@{theme-selector} & {
each(@dr, #(@LESS-var, @property-name) {
@property-value: extract( @LESS-var, @key); // extract value for respective theme
// output each variant with the globally specified selector conditions
@{property-name}: @property-value;
})
}
})
}
// usage
@variable-1: red, blue, green;
@variable-2: white, black, grey;
@variable-3: 0.5em, 0.75em, 1em;
.example {
.mapByTheme({
--color: @variable-1;
--background: @variable-2;
padding: @variable-3;
});
color: var(--color);
background: var(--background);
} produces this output: .example {
--color: red;
--background: white;
padding: 0.5em;
color: var(--color);
background: var(--background);
}
.theme-primary .example {
--color: blue;
--background: black;
padding: 0.75em;
}
.theme-secondary .example {
--color: green;
--background: grey;
padding: 1em;
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Outline
I've recently encountered the need to merge properties printed by individual mixin calls into a single selector block, and realised it's likely not possible currently. I'm not certain if this is even feasible to implement, but imagine a possible solution through an API similar to
:extend
. I'll outline the use case with an example of what I'm currently doing & what I'm trying to achieve:Problem
I've created a mixin to implement theming in my current utility framework: It allows me do declare variables to be themed as lists instead of disctinct values & generate all available variants for different themes by accessing those LESS variables through a mixin call.
This pattern works beautifully, but leaves the issue of producing individual selector statements for each call of the
.mapByTheme()
mixin regardless of shared selectors.That doesn't look too problematic with this limited examples, but quickly results in heavy selector bloat throughout a full project, and there seems to currently be no way or practical workaround in less to dynamically add properties to an existing selector.
Proposed solution
Introducing an
:expand( intoTargetSelector )
function that accepts interpolated selectors/variables for defining an existing selector to be written to would make this and probably many other issues much easier to handle:... enabling to produce merged statements as opposed to the current output that writes an individual selector statement for every mixin call:
Remarks
Beta Was this translation helpful? Give feedback.
All reactions