Skip to content

Mixins should accept LESS blocks #965

Closed

Description

@rbu

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.

This is basically the "Passing Content Blocks to a Mixin" feature of SASS:
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#mixin-content

In LESS this would mean something like:

.mobile() {
  @media all and (max-device-width: 480px) {
    @content
  }
}
.big-desktop-button {
  ...
  .mobile {
    display:none;
  }
}

Activity

  1. dwt commented on Oct 2, 2012

    @dwt

    Oh, I need this too!

  2. caseyohara commented on Oct 12, 2012

    @caseyohara

    +1

  3. BruceClark commented on Oct 12, 2012

    @BruceClark

    Absolutely. Please!

  4. caseyohara commented on Oct 12, 2012

    @caseyohara

    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.

  5. PiotrSkon commented on Nov 12, 2012

    @PiotrSkon

    +1 This is a must.

  6. Soviut commented on Nov 28, 2012

    @Soviut

    +1 SASS can do this.

  7. cescalante commented on Dec 3, 2012

    @cescalante

    +1

  8. matthew-dean commented on Dec 3, 2012

    @matthew-dean
    Member

    I don't understand these examples. What is @content defined as in the first example?

  9. chimericdream commented on Dec 3, 2012

    @chimericdream

    With SASS, you can pass a whole content block to a mixin independent of the parameters. So you have something like this:

    .mobile() {
        @media your-query-here {
            @content
        }
    }
    
    .someclass {
        // some general styles here
        .mobile() {
            // your mobile styles here
        }
    }

    that would compile to:

    .someclass {
        // some general styles here
    }
    
    @media your-query-here {
        .someclass {
            // your mobile styles here
        }
    }

    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.

  10. davidrivers commented on Mar 4, 2013

    @davidrivers

    +1!!!

  11. jonschlinkert commented on Mar 4, 2013

    @jonschlinkert
    Contributor

    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?

  12. davidrivers commented on Mar 5, 2013

    @davidrivers

    @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.

  13. jonschlinkert commented on Mar 5, 2013

    @jonschlinkert
    Contributor

    @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!

  14. matthew-dean commented on Mar 5, 2013

    @matthew-dean
    Member

    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.

  15. voodoom commented on Mar 5, 2013

    @voodoom

    +1!!

  16. 36 remaining items

  17. hubertgrzeskowiak commented on Jul 12, 2013

    @hubertgrzeskowiak

    +1

  18. jcc8 commented on Aug 5, 2013

    @jcc8
  19. parisholley commented on Sep 5, 2013

    @parisholley

    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

  20. seven-phases-max commented on Sep 29, 2013

    @seven-phases-max
    Member

    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.:

    .something(@value) {
        @a: 1;
        @b: 2;
        @value(@a, @b); 
    }
  21. seven-phases-max commented on Oct 3, 2013

    @seven-phases-max
    Member

    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.:

    #use-mixin {
      .mixin(@variable);
      @variable: { /* ... */};
    }
    #short-expression-use-mixin {
      .mixin({ /* ... */ });
    }
    

    ­­... we won't need any special syntax for a "named mixin assignment" as we'll be able to do it with:

    #use-mixin {
      .mixin(@variable);
      @variable: {.block()};
    }
    #short-expression-use-mixin {
      .mixin({.block()});
    }
    

    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.

  22. lukeapage commented on Oct 4, 2013

    @lukeapage
    Member

    Just sketching and thinking about how this could be accomplished with mixin extends.. what do you think about this?

    .a :extend(.b()) {
        .c {
            color: black;
        }
    }
    @media x {
        .b() {
        }
    }
    @media y {
        .b() {
        }
    }
    

    essentially you make a template, put your mixin in the template and then extend the template...

  23. matthew-dean commented on Feb 1, 2014

    @matthew-dean
    Member

    Per discussion on other issue threads, +1 to @seven-phases-max's proposal of assigning mixins to variables.

  24. nelsonpecora commented on Feb 12, 2014

    @nelsonpecora

    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).

  25. lukeapage commented on Feb 13, 2014

    @lukeapage
    Member

    Implemented and will be in 1.7 release soon, see #1859. seperate issue for passing mixins.

  26. katomonster commented on Jun 21, 2014

    @katomonster

    +1

  27. nelsonpecora commented on Jun 23, 2014

    @nelsonpecora

    By the by, I can confirm this is working in 1.7.

  28. AndrewEastwood commented on Jun 17, 2015

    @AndrewEastwood

    is it working right now?

  29. seven-phases-max commented on Jun 17, 2015

    @seven-phases-max
    Member
  30. AndrewEastwood commented on Jun 17, 2015

    @AndrewEastwood

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions