Skip to content

Extend - around media queries #1165

Closed
Closed

Description

@lukeapage
  1. Extend should match things inside media queries
  2. if the extend is inside a media query it should
    a) only match other things inside media queries?
    b) only match things inside its own media query
    c) match everything inside its own media query plus everything not inside another media query, but it should bring that extend inside itself..
  3. is uncontested

2 I am thinking should maybe be (b) in the short term till we figure out if it should match anything else.

Activity

  1. Soviut commented on Feb 7, 2013

    @Soviut

    Well what would CSS proper do? How would it react? The closer we are to that, the better. Since media queries are theoretically at the root level in regular CSS, that might give some guidance.

  2. DesignByOnyx commented on Feb 8, 2013

    @DesignByOnyx

    EDIT: I have since put a lot more time in thought into the :extend feature and do not really stand by this comment any more. I would like to leave it here for the purpose of conversation, but this should not be a guideline or a suggestion.


    I have just run into scenario number 2, and I would like to voice my opinion about it. Let me provide some example code:

    .shared-style { width: 10px; height: 10px; } 
    
    @media screen and (min-width: 30em) {
        .shared-style { padding: 5px; } 
    }
    @media screen and (max-width: 30em) {
        .shared-style { margin: 8px; }
    }
    @media screen and (min-width: 48em) {
        .some-element:extend(.shared-style) { }
        .some-other-element:extend(.shared-style) { }
    }
    @media screen and (min-width: 60em) {
        .shared-style { background-color: red; }
    }
    @media screen and (max-width: 60em) {
        .shared-style { background-color: green; } 
    }
    

    After spending much time thinking about this, I don't know that there is really one single elegant solution. The most elegant solution (in my opinion) is for the compiler to copy styles from other media queries and build a brand new style declaration so that the above might end up as:

    .shared-style { width: 10px; height: 10px; } 
    
    @media screen and (min-width: 30em) {
        .shared-style { padding: 5px; } 
    }
    @media screen and (max-width: 30em) {
        .shared-style { margin: 8px; }
    }
    @media screen and (min-width: 48em) {
        .some-element,
        .some-other-element { 
            width: 10px;
            height: 10px;
            padding: 5px;
            background-color: green;
        }
    }
    @media screen and (min-width: 60em) {
        .shared-style { background-color: red; }
    }
    @media screen and (max-width: 60em) {
        .shared-style { background-color: green; } 
    }
    

    The only way this works is for the compiler to be smart enough to know which styles to copy and which styles not to copy. The most realistic solution is to disallow the use of :extend within a media query in favor of using classic mixins.

  3. dmi3y commented on Feb 20, 2013

    @dmi3y

    Definitely I like:

    c) match everything inside its own media query plus everything not inside another media query, but it should bring that extend inside itself..

    I could miss something right now but it sounds logical. You would not commonly put something shared on mixed styles into particular media query, and as well you would not usually need shared styles from another media query set other than you working with. I am obviously missing something :/

  4. DesignByOnyx commented on Feb 20, 2013

    @DesignByOnyx

    @dmi3y - you kind of touch on a point that I don't [currently] endorse anymore. I agree that extend within a media query should:

    1. Match everything within its own media query - which is a very "normal" thing to expect and should be considered implied moving forward IMO (for the sake of typing).
    2. Match everything outside of media queries, but should copy those styles into itself

    Even though I agree, the interesting thing about number 2 is we are no longer "extending" but rather duplicating styles in the fashion of a plain ol' mixin. This is very important IMO because the whole idea of :extend was to minimize duplication. But for the time being, let's assume that this is how extend should work within media queries.

    By the same token of grabbing everything "outside" of media queries, should we not grab everything in other currently matching media queries? Best shown by example:

    .b { }  <-- These get copied
    
    @media only screen and (min-width: 30em) {
        .b { }  // <-- Why shouldn't this get copied?  It applies to anything greater than 30em
    }
    @media only screen and (min-width: 48em) {
        .a:extend( .b ) { }
    }
    @media only screen and (max-width: 60em) {
        .b { }  <-- This hurts my head (notice the MAX-width)
    }
  5. matthew-dean commented on Feb 20, 2013

    @matthew-dean
    Member

    This one you kind of have to wring your brain through. Think of :extend in general usage as a replacement for referencing mixins (so, copying the entire contents of the mixin within your selector).

    So, I guess what I'm saying is that I would kind of expect us to do whatever has the same effect as referencing the mixin. Media queries create isolated sets, so I think extends should get walled off. As in:

    .b { }
    
    @media only screen and (min-width: 48em) {
        .a:extend( .b ) { }
    }
    
    // output
    .b {
    }
    @media only screen and (min-width: 48em) {
        .a {  // EDITED: contains properties of .b, effectively same as using a class as mixin
        }
    }
    

    Yes, @DesignByOnyx, it would be a copy, but it's not necessarily the SAME copy because of the different ways you can extend (could contain base properties only).

    Illustrated more clearly:

    .b {
    }
    .c {
    }
    .d {
    }
    @media only screen and (min-width: 48em) {
        .a:extend( .b, .c ) { }
    }
    @media only screen and (max-width: 60em) {
        .a:extend( .c, .d ) { }
    }
    

    Hopefully, I've made obvious in the above example that the root classes should not be modified. The "result" of .a is local to that media query.

    UPDATE:
    If it's copying (not modifying the original block) it doesn't really need to include the original.... so the output of the above would end up being:

    .b {
      color: blue;
    }
    .c {
      border-width: 1px;
    }
    .d {
      background: none;
    }
    @media only screen and (min-width: 48em) {
        .a{ 
           color: blue;
           border-width: 1px;
         }  
    }
    @media only screen and (max-width: 60em) {
        .a{ 
           border-width: 1px;
           background: none;
         }  
    }
    
  6. dmi3y commented on Feb 20, 2013

    @dmi3y

    ops, sorry, overlooked, correct myself

    c) match everything inside its own media query plus everything not inside another media query, but it should bring that extend inside itself.. and leave it, with peace, where they are.

  7. matthew-dean commented on Feb 20, 2013

    @matthew-dean
    Member

    So, I guess, like @Soviut said, classes within a media query have a local scope; therefore the result of :extend must also. It cannot "escape" and modify classes of a broader or different media scope, because that's not what CSS would do.

  8. matthew-dean commented on Feb 20, 2013

    @matthew-dean
    Member

    So I think @DesignByOnyx, @dmi3y - we're all suggesting the same behavior?

  9. dmi3y commented on Feb 20, 2013

    @dmi3y

    I like how it tastes, but not yet sure

    .b { }  <-- Extended with .a
    .c { } <-- Extended with .d
    
    @media only screen and (min-width: 30em) {
        .c {}
        .b {}
    }
    @media only screen and (min-width: 48em) {
        .c {}
        .a:extend( .b ) { }
    }
    @media only screen and (max-width: 60em) {
        .c {} <-- Extended with .d too
        .b {}
        .d:extend( .c ) {}
    }
    

    Results to:

    .a, .b {}
    .d .c {}
    
    @media only screen and (min-width: 30em) {
        .c {}
        .b {}
    }
    @media only screen and (min-width: 48em) {
        .c {}
        .a{ }
    }
    @media only screen and (max-width: 60em) {
        .d, .c {}
        .b {}
        .d {}
    }
    
  10. DesignByOnyx commented on Feb 20, 2013

    @DesignByOnyx

    @MatthewDL - I totally agree with you and @Soviut with extend not "escaping" the scope of the media query. However, there is this issue of copying from outside the media query.

    With the example you provided, I would expect a different result. Let me add a property to show you:

    .b { padding: 5px }
    @media only screen and (min-width: 48em) {
        .a:extend( .b ) { }
    }
    /*
    .b { padding: 5px }
    @media only screen and (min-width: 48em) {
        .a { padding: 5px }
    }
    */

    That's what I meant by copying. I ran into this issue in the real world where I wanted an element to inherit my grid styles ONLY beginning at min-width: 48em. Here is some real world code I tried to use with alpha extend:

    .grid-wrap {
        letter-spacing: -0.31em !important;
        word-spacing: -0.43em !important;
    
        > * {
            letter-spacing: normal;
            word-spacing: normal;
            display: inline-block;
            vertical-align: top;
            .box-sizing(border-box);
        }
    }
    
    @media only screen and (min-width: 48em) {
        .artist-list:extend( .grid-wrap ) {
            > li { width: 33.3% }
        }
    }

    I personally would expect the .grid-wrap styles to be copied into the media query, the same as writing the following (which is what I ended up with in my real world code):

    @media only screen and (min-width: 48em) {
        .artist-list {
            .grid-wrap;
            > li { width: 33.3% }
        }
    }

    Does anybody else think this is the way :extend should work? We would get the benefits of using the modifiers (deep, shallow, etc) so that I would really write it like this today:

    @media only screen and (min-width: 48em) {
        .artist-list:extend( .grid-wrap deep ) {
            > li { width: 33.3% }
        }
    }
  11. dmi3y commented on Feb 20, 2013

    @dmi3y

    As my prospective, you just answered your question, why not just use mixins? Sorry if I missed it again.

  12. lukeapage commented on Feb 20, 2013

    @lukeapage
    MemberAuthor

    It would basically work like a mixin call but with the options.. I was trying to think of usecases though and I'm stumped..

    We use extend to introduce a new selector to an existing style block.. when will different media queries require new selectors not used outside the query?

    Reading @MatthewDL earlier comments persuaded me towards my option. b - no copying from outside the query

  13. matthew-dean commented on Feb 20, 2013

    @matthew-dean
    Member

    @DesignByOnyx - You said you would expect a different result, yet your result looks exactly the same as what I ended up with. Of course, I edited it after I had originally posted it, so if you received via email, you might have seen my original draft. So, yes, I agree, in a media block :extend should essentially behave the same as importing a class, with the one caveat of joining multiple extend calls within the same media block.

  14. matthew-dean commented on Feb 20, 2013

    @matthew-dean
    Member

    @dmi3y - To your above example, I don't agree. The extend is called within the context of that media query, so it is inherently declaring: "Extend this other class, but ONLY in the context of this media query."

    As @DesignByOnyx said, the only way to serve that declaration is to copy the styles within the media query, and leave the outside block alone.

  15. DesignByOnyx commented on Feb 20, 2013

    @DesignByOnyx

    @MatthewDL - you did change your example while I was posting. Apologies and glad we are on the same page there. Also, do you consider my example above to be a poor use case (willing to shoot me over)? Aside from the :extend I think it's a perfectly normal scenario:

    .b { padding: 5px } 
    
    @media only screen and (min-width: 30em) {
        .b { color: red } 
    }
    @media only screen and (min-width: 48em) {
        .a:extend( .b ) { }
    }
    @media only screen and (max-width: 60em) {
        .b { background: green } 
    }

    I also think it's reasonable to expect that styles are copied from other "matching" media queries. I'm going on a limb here, but consider this output:

    .b { padding: 5px } 
    
    @media only screen and (min-width: 30em) {
        .b { color: red } 
    }
    @media only screen and (min-width: 48em) {
        .a { 
            padding: 5px;
            color: red;
        }
    }
    @media only screen and (min-width: 48em) and (max-width: 60em) {
        .a { background: green } 
    }

    Notice the brand new media query which was created. I don't want to take this discussion where it doesn't need to go, but I think the above scenario is easily solved and reasonable to expect, no?

    EDIT: ... but forgot to mention the most important part. This requires the LESS parser to be able to understand media queries on a whole different level than it does now.

  16. 23 remaining items

  17. matthew-dean commented on Feb 22, 2013

    @matthew-dean
    Member

    @DesignByOnyx - Yes, that summary seems correct. What is copied into the media query is dictated by the same :extend keywords. So, if I do a deep extend (called from within a media query), then all of the properties / nested selectors are imported into the media query within the selector I wish to extend. Pretty similar to calling a mixin, except for the collapsing behavior I illustrated if I do this multiple times / with multiple selectors (and, you make a good note, ALSO extending the selectors that already exist within that media block).

    @dmi3y - Agree with @agatronic and @DesignByOnyx that your previous examples are overkill and I'm afraid your recent example is still slightly incorrect.

    .a { ... }
    
    @media ..case1.. {
        .b:extend(.a){ ... }
    }
    

    ...creates:

    .a { ... }
    
    @media ..case1.. {
        .b { ... }  // contains properties from external .a and local .b
                     // not ".a, .b { }" because the properties / reference of .a are already inherited into this media block scope
    }
    
  18. dmi3y commented on Feb 22, 2013

    @dmi3y

    @MatthewDL thanks, corrected. Yes, now it fits better in my head, especially with your previous clarifications.

  19. dmi3y commented on Feb 22, 2013

    @dmi3y

    Alright, still something is not just agree inside of me with making extend works in a mixin-ish way, yes I know that's not the same, but. In that paticular case I would rather write more in less code in favor of making it logically clear, I might be wrong. But considering example with two extends for one mixin in the same mediaquery.

    a. { ... }
    
    @media ..case1.. {
        .b:extend(.a){ ... }
        .d:extend(.a){ ... }
    }
    
    @media ..case2.. {
       .a { ... }
       .c:extend(.a){ ... }
    }
    

    and having than

    ...
    @media ..case1... {
       .b,.d { // copied styles from .a above }
       .b { ... }
       .d { ... }
    }
    ...
    

    would not be it something similar to this approach?

    @media ..case1... {
       .a() { .a }
       .b :extend(a){ ... }
       .d :extend(a){ ... }
    }
    ...
    
  20. matthew-dean commented on Feb 22, 2013

    @matthew-dean
    Member

    @dmi3y - As stated, similar, but not the same. Extend has more options. For example:

    #header .a { color: blue; }
    
    @media only screen {
       .b:extend(.a any) { border-color: black; }
       .c:extend(.a any) { height: 1em; }
    }
    

    becomes:

    #header .a { color: blue; }
    
    @media only screen {
       #header .b, #header .c { color: blue; }
       .b { border-color: black; }
       .c { height: 1em; }
    }
    

    The reason is that without a media query, it would output like this:

    #header .a, #header .b, #header .c { color: blue; }
    .b { border-color: black; }
    .c { height: 1em; }
    

    However, the scope of #header .b and #header .c need to be limited to the @media declaration where they were defined.

    So, in cases of @media queries, at times, the behavior is similar to mixins, but it is not the same. :extend collapses within its scope, and can optionally match partial classes or opt to not extend nested selectors, which mixins cannot do. Mixins just output their properties inline.

  21. dmi3y commented on Feb 22, 2013

    @dmi3y

    Finally I do understand it! And support in that point. Probably just not such satisfied with final syntax inside mediaqueries :/ Though it is far away another question. Just still something popping out in my mind about the mediaquery like variant.

    .....
    @media ..case1... {
       @extend (#header .a all) { // IMO more feels that something needs to be copied to this place than moved out
          .b{ ... }
          .d{ ... }
       }
    }
    

    and in some circumstances that could be sugared to just

    ....
    @media:extend(#header .a all) ..case1... {
          .b{ ... }
          .d{ ... }
    }
    

    Does it make any sense? Or I am too wired today;)

  22. matthew-dean commented on Feb 22, 2013

    @matthew-dean
    Member

    While your @extend syntax is compatible with my example, I feel like in most cases, it will be done at the declaration level. As in, on a case-by-case basis for individual selector declarations, and I don't think people necessarily want to move it around to include it in an @extend block. Because it's a different approach for declaring extend, I feel like it would be confusing to do both. Similar thoughts with the second example. It looks like you're extending the @media query itself, so to support that plus extending selectors feels confusing.

  23. dmi3y commented on Feb 23, 2013

    @dmi3y

    Yes, agree, that's another level of complication, which is not good.

  24. lukeapage commented on Mar 4, 2013

    @lukeapage
    MemberAuthor

    Please feel free to look at the new test case extend-media.less and extend-media.css and suggest anything I've missed or anything you think disagrees with what is in this issue.

  25. matthew-dean commented on Sep 17, 2014

    @matthew-dean
    Member

    Just revisiting this as I ran into an apparent bug today related to this. extend-media.less and extend-media.css appear to have incorrect output according to this thread. See @DesignByOnyx's summary:

    An :extend(.a) which takes place within a media query will:
    a) Extend every .a within that same media query
    b) Copy over all root-level .a styles which exist outside any media query (similar to a mixin)

    The second part of this is currently not occurring as of 1.7.5. Selectors outside of @media are being ignored.

  26. seven-phases-max commented on Sep 17, 2014

    @seven-phases-max
    Member

    Just revisiting this as I ran into an apparent bug today

    No, not a really a bug (As I understand it the "copying" was intentionally postponed by @lukeapage for "better times" and that's why this behaviour is mentioned in the docs :)

  27. matthew-dean commented on Sep 17, 2014

    @matthew-dean
    Member

    @seven-phases-max I tried to re-reread as much of the original related issues. I just couldn't find where this was mentioned, so I couldn't tell if it was accidental or intentional. Was that mentioned in the original commit?

    One of the reasons I want to keep track is sometimes we implement part of a feature, then close the issue, and don't track the remainder of the feature. If we have a central way to track it, then maintainers may have something easier to point to when someone asks a related question, to the effect of: "Yes, we're planning on that, here's the feature spec, sub-features A,B,C have been implemented, sub-features X,Y,Z have yet to be implemented." Something like what W3C does with CSS3 specs. http://www.w3.org/Style/CSS/current-work

  28. seven-phases-max commented on Sep 17, 2014

    @seven-phases-max
    Member

    so I couldn't tell if it was accidental or intentional.

    I'm assuming one could not "not to code" a relatively large piece of code by accident and then documenting this in very details :).

  29. matthew-dean commented on Sep 17, 2014

    @matthew-dean
    Member

    Maybe I'll start something in the wiki...

  30. matthew-dean commented on Sep 17, 2014

    @matthew-dean
    Member

    I'm assuming one could not "not to code" a relatively large piece of code by accident and then documenting this in very details

    Ha. I could. ;-)

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions