You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be helpful if mixins had access to a content block passed to them, so one could encapsulate media queries or browser hacks in a central place and reference them symbolically.
I can see some collisions by adding this functionality to the normal mixin syntax. Just by looking, it would impossible to tell if the call above would compile to
.big-desktop-button.mobile{display: none;}
or
@media all and(max-device-width:480px){.big-desktop-button{display: none;}}
So I think it would need new syntax. Here is my idea.
Allow blocks of LESS to be passed into parametric mixins, as a normal positional parameter surrounded by braces.
.mobile(@content){@media all and(max-device-width:480px){@content}}.big-desktop-button{....mobile({display:none;});}
Due to variable scoping and evaluation, I imagine this would require that mixins that accept LESS content blocks can only accept content blocks.
I'm still confused by this. Some real world examples of this being used would really help a lot. I tried to read through the SASS docs, but I still struggled with it. It sounds like @content is really just passing a literal block of CSS to the bottom of the inheriting declaration block. Is that right?
@jonschlinkert You're right. It's extremely useful when you need a "wrapper" selector. For example, I have a project where we have to share the page with styles from a third-party, so we "pseudo-namespace" our styles in order to make them more specific than the third party styles. We do this by wrapping all of our styles with an arbitrary selector, and we can nest styles within that selector and leverage the fact that a complex selector such as .wrapper .module { /* styles here*/ } is more specific than most or all of our third-party's selectors (and therefor shouldn't be affected by them since we also embed our own "reset" styles on that wrapper before specifying styles that we want).
Another use for having an arbitrary selector to wrap styles is if you want to reuse Sass or Less "modules" between different projects or documents and for some reason you need to give them different wrapper hooks in your markup (or want the flexibility to do so).
My use cases are attempting to bridge the fact that the CSS cascade lacks a concept of a true namespace, but you can leverage a stylesheet preprocessor to get something practically close to one. "Media query bubbling" is a similar use case (since it allows "blocks" of styles to be passed around and wrapped with different selectors), although I don't know how Less supports this internally, but a cursory Google search suggests that Less does support this feature.
@davidrivers thanks, the wrapper analogy helps, that's sort of how I saw it in my mind. Looking at this semantically, this is a better use case for the term @include than @content, which makes no sense to me here. (Sass had to pick a different term since they used @include for mixins.)
David if you find some time, would you mind adding some detailed code examples here so when @MatthewDL and @lukeapage review again they can make a better evaluation of this? thanks!
Yes, LESS supports media query bubbling. There's probably ways to do namespacing as you describe, but like @jonschlinkert, the examples are still a head-scratcher. That is, I can't see how it's not currently supported. You can segment code into, say, mobile-specific definitions using variables / mixin guards.
Here is a SASS mixin library I wrote using display: table that I think shows a good idea of how powerful this feature could be (semantics aside). I'm obviously making many assumptions/compromises but there is so much more you can do with this type of functionality.
Just my +1 to support SomMeri's syntax proposal...
We could also have another syntax for this kind of stuff (in addition to the "expanded rule at the point of assignment"):
.block {
size: 2;
}
#use-something {
.something(@variable);
@variable: .block;
/* ^ assign any arbitrary "name" to a variable (at this point nothing indicates that the variable points to a mixin - it's only a "raw" value and we can use it just as any other variable - for example putting it as a CSS property value) */
}
.something(@value) {
@value();
// ^ now the parens show that variable's value// is a mixin name and we want to expand it here
}
The idea behind this syntax is a possibility to provide arguments for the expansion at any level if the variable points to a parametric mixin, i.e.:
I've just noticed a "minor" problem with 18462037's@variable: .block(); syntax.
Look at it... Yes, it looks exactly like calling a .block function and assigning its return value to the @variable.
And even if this feature ("mixin returning a value with direct assignment to a variable") is somewhat anticipated (?) for the moment or even barely appear in LESS at all, it would be a good idea to keep this syntax reserved just in case (as we never know how the things will evolve). After all, @variable: .mixin();will confuse users if it has "not a function call" semantics.
So thinking of possible alternatives:
Actually, as soon as we have a "{} block as a variable or mixin parameter" syntax, i.e.:
And though @variable: {.block()}; or @variable: {.block}; are probably not absolutely equal to 18462037's@variable: .block(); (different points of expansion?) their results promise to be similar.
I'll also +1 @seven-phases-max's proposal. The .mixin(@variable); / .mixin({ key: value; key: value; }); syntax feels very intuitive for me (in fact, I tried to use it before finding out it didn't work).
Activity
dwt commentedon Oct 2, 2012
Oh, I need this too!
caseyohara commentedon Oct 12, 2012
+1
BruceClark commentedon Oct 12, 2012
Absolutely. Please!
caseyohara commentedon Oct 12, 2012
I can see some collisions by adding this functionality to the normal mixin syntax. Just by looking, it would impossible to tell if the call above would compile to
or
So I think it would need new syntax. Here is my idea.
Allow blocks of LESS to be passed into parametric mixins, as a normal positional parameter surrounded by braces.
Due to variable scoping and evaluation, I imagine this would require that mixins that accept LESS content blocks can only accept content blocks.
PiotrSkon commentedon Nov 12, 2012
+1 This is a must.
Soviut commentedon Nov 28, 2012
+1 SASS can do this.
cescalante commentedon Dec 3, 2012
+1
matthew-dean commentedon Dec 3, 2012
I don't understand these examples. What is @content defined as in the first example?
chimericdream commentedon Dec 3, 2012
With SASS, you can pass a whole content block to a mixin independent of the parameters. So you have something like this:
that would compile to:
I'm adding my support for this. Real logic operators (if/else) and this feature are the biggest things keeping me from using LESS at this point.
davidrivers commentedon Mar 4, 2013
+1!!!
jonschlinkert commentedon Mar 4, 2013
I'm still confused by this. Some real world examples of this being used would really help a lot. I tried to read through the SASS docs, but I still struggled with it. It sounds like
@contentis really just passing a literal block of CSS to the bottom of the inheriting declaration block. Is that right?davidrivers commentedon Mar 5, 2013
@jonschlinkert You're right. It's extremely useful when you need a "wrapper" selector. For example, I have a project where we have to share the page with styles from a third-party, so we "pseudo-namespace" our styles in order to make them more specific than the third party styles. We do this by wrapping all of our styles with an arbitrary selector, and we can nest styles within that selector and leverage the fact that a complex selector such as
.wrapper .module { /* styles here*/ }is more specific than most or all of our third-party's selectors (and therefor shouldn't be affected by them since we also embed our own "reset" styles on that wrapper before specifying styles that we want).Another use for having an arbitrary selector to wrap styles is if you want to reuse Sass or Less "modules" between different projects or documents and for some reason you need to give them different wrapper hooks in your markup (or want the flexibility to do so).
My use cases are attempting to bridge the fact that the CSS cascade lacks a concept of a true namespace, but you can leverage a stylesheet preprocessor to get something practically close to one. "Media query bubbling" is a similar use case (since it allows "blocks" of styles to be passed around and wrapped with different selectors), although I don't know how Less supports this internally, but a cursory Google search suggests that Less does support this feature.
jonschlinkert commentedon Mar 5, 2013
@davidrivers thanks, the wrapper analogy helps, that's sort of how I saw it in my mind. Looking at this semantically, this is a better use case for the term
@includethan@content, which makes no sense to me here. (Sass had to pick a different term since they used@includefor mixins.)David if you find some time, would you mind adding some detailed code examples here so when @MatthewDL and @lukeapage review again they can make a better evaluation of this? thanks!
matthew-dean commentedon Mar 5, 2013
Yes, LESS supports media query bubbling. There's probably ways to do namespacing as you describe, but like @jonschlinkert, the examples are still a head-scratcher. That is, I can't see how it's not currently supported. You can segment code into, say, mobile-specific definitions using variables / mixin guards.
voodoom commentedon Mar 5, 2013
+1!!
36 remaining items
hubertgrzeskowiak commentedon Jul 12, 2013
+1
jcc8 commentedon Aug 5, 2013
+1 - Bounty here: https://www.catincan.com/bounty/mixins-should-accept-less-blocks-issue-965-less-less-js-github
Crowdfund issue & get merged into main branch to collect.
parisholley commentedon Sep 5, 2013
Here is a SASS mixin library I wrote using display: table that I think shows a good idea of how powerful this feature could be (semantics aside). I'm obviously making many assumptions/compromises but there is so much more you can do with this type of functionality.
https://github.com/parisholley/tablespooncss
seven-phases-max commentedon Sep 29, 2013
Just my +1 to support SomMeri's syntax proposal...
We could also have another syntax for this kind of stuff (in addition to the "expanded rule at the point of assignment"):
The idea behind this syntax is a possibility to provide arguments for the expansion at any level if the variable points to a parametric mixin, i.e.:
seven-phases-max commentedon Oct 3, 2013
I've just noticed a "minor" problem with 18462037's
@variable: .block();syntax.Look at it... Yes, it looks exactly like calling a
.blockfunction and assigning its return value to the@variable.And even if this feature ("mixin returning a value with direct assignment to a variable") is somewhat anticipated (?) for the moment or even barely appear in LESS at all, it would be a good idea to keep this syntax reserved just in case (as we never know how the things will evolve). After all,
@variable: .mixin();will confuse users if it has "not a function call" semantics.So thinking of possible alternatives:
Actually, as soon as we have a "{} block as a variable or mixin parameter" syntax, i.e.:
... we won't need any special syntax for a "named mixin assignment" as we'll be able to do it with:
And though
@variable: {.block()};or@variable: {.block};are probably not absolutely equal to 18462037's@variable: .block();(different points of expansion?) their results promise to be similar.lukeapage commentedon Oct 4, 2013
Just sketching and thinking about how this could be accomplished with mixin extends.. what do you think about this?
essentially you make a template, put your mixin in the template and then extend the template...
matthew-dean commentedon Feb 1, 2014
Per discussion on other issue threads, +1 to @seven-phases-max's proposal of assigning mixins to variables.
nelsonpecora commentedon Feb 12, 2014
I'll also +1 @seven-phases-max's proposal. The
.mixin(@variable);/.mixin({ key: value; key: value; });syntax feels very intuitive for me (in fact, I tried to use it before finding out it didn't work).lukeapage commentedon Feb 13, 2014
Implemented and will be in 1.7 release soon, see #1859. seperate issue for passing mixins.
katomonster commentedon Jun 21, 2014
+1
nelsonpecora commentedon Jun 23, 2014
By the by, I can confirm this is working in 1.7.
AndrewEastwood commentedon Jun 17, 2015
is it working right now?
seven-phases-max commentedon Jun 17, 2015
@AndrewEastwood See the docs.
AndrewEastwood commentedon Jun 17, 2015
Thanks