Skip to content

Can't extend a nested class (concatenated selector) #1597

Open

Description

@DennisJohnsen

Heya guys.

I have found an annoying issue, which is pretty basic use of two mechanics:
I cant extend a class i generate by nesting it using "& {}"

Heres my example:

.box {
    padding: @base-spacing;

    &--large {
        padding: @large-spacing;
    }
}

.foo {
    &:extend(.box--large);
}

Expected result:

.box {
    padding: 24px;
}

.box--large,
.foo {
        padding: 48px;
}

What actually happens:

.box {
    padding: 24px;
}

.box--large {
    padding: 48px;
}

Imo this is very basic use of two mechanics, and it's not like its generated by some kind of wizard looping mixin.
Looking forward to have this fixed :-)

Kind regards, Dennis

Activity

  1. jonschlinkert commented on Oct 15, 2013

    @jonschlinkert
    Contributor
  2. jonschlinkert commented on Oct 15, 2013

    @jonschlinkert
    Contributor
  3. jonschlinkert commented on Oct 15, 2013

    @jonschlinkert
    Contributor
  4. lukeapage commented on Oct 15, 2013

    @lukeapage
    Member

    although closest to #1539 i don't think it is a duplicate.. same root cause as #1539 though..

    .a {
       &b
    

    is treated as .a followed by a b element, with no space between them
    .ab is treated as the class ab.. so less thinks they are different.

    quite a hard one to solve, though I guess easier to solve than #1539

  5. jonschlinkert commented on Oct 15, 2013

    @jonschlinkert
    Contributor

    fair enough, I guess if solving one doesn't solve the other then it makes sense to keep both open.

  6. DennisJohnsen commented on Nov 20, 2013

    @DennisJohnsen
    Author

    Hello again : - )

    I'm just writing to hear if theres some updates regarding this issue.

    I have recently forwarded multiple people this issue, regarding to their LESS question about not beeing able to extend nested/generated classes.

    Kind regards
    Dennis

  7. Johns3n commented on Nov 20, 2013

    @Johns3n

    +1

    I tried doing the exact same thing:

    .top{
        &-first{
            background:black;
            &-item{
                color:white;
            }
        }
        &-second{
            background:green;
            &-item:extend(.top-first-item){}
        }
    }

    expecting:

    .top-first {
        background: black;
    }
    .top-first-item,
    .top-second-item{
        color: white;
    }
    .top-second {
        background: green;
    }

    But got instead:

    .top-first {
        background: black;
    }
    .top-first-item{
        color: white;
    }
    .top-second {
        background: green;
    }

    Soo +1 for this issue and looking forward to a fix! :)

    Original: http://stackoverflow.com/questions/20091264/less-extend-a-previously-defined-nested-selector

  8. dvdln commented on Dec 21, 2013

    @dvdln

    A lot of people are into BEM these days...

    .block {
      &__elem {
        width: 200;
      }
    
      &__elem_mod_blue {
        &:extend(.block__elem);
        color: blue;
      }
    
      &__elem_mod_green {
        &:extend(.block__elem);
        color: green;
      }
    }
    
  9. lukeapage commented on Dec 21, 2013

    @lukeapage
    Member

    I guess the easiest way to fix is in the code handling & to change it to combine the value if there are no combinators between the previous value and the next value, then this would work. patches welcome!

  10. DennisJohnsen commented on May 17, 2014

    @DennisJohnsen
    Author

    Hello again.

    Any updates into this? I havn't been following it for some time, but i find my self not beeing able to extend these dynamic classes a abit too often :(

    Kind regards,
    Dennis

  11. Johns3n commented on May 20, 2014

    @Johns3n

    Again a +1 from me! Found myself needing to be able to extend a nested dynamic class the other day :)

    .ui{
        &__theme{
            background-image:url(../images/watermark.gif);
            background-position:top left;
            background-attachment:fixed;
            background-repeat:no-repeat;
            &--inverted{
                &:extend(.ui__theme);
                background-image:url(../images/watermark--inverted.gif);
                background-position:top right;
            }
        }
    

    expected:

    ui__theme, ui__theme--inverted{
       //shared rules
    }
    ui__theme--inverted{
     //individual
    }
    
  12. slantz commented on Jul 18, 2014

    @slantz

    I've been working on the singlepage project, where each page has it's own less file compiled. Each page has it's own body class, so I can extend concatanated classes and other properties like so:
    Let the pages have classes:

    <body class="foo-bar-page">
    <body class="bar-baz-page">
    

    In my less files i start from not existing classes

    .foo-bar {
      &-page {
        min-height:100%;
        #somediv {
          color : red;
        }
        &-holder {
          display : inline-block;
        }
      }  
    }
    

    For the other page i can just write:

    .bar-baz {
      &:extend(.foo-bar all);
    }
    

    And the output will be:

    .foo-bar-page,
    .bar-baz-page {
      min-height: 100%;
    }
    .foo-bar-page #somediv,
    .bar-baz-page #somediv {
      color: red;
    }
    .foo-bar-page-holder,
    .bar-baz-page-holder {
      display: inline-block;
    }
    

    Sure if you need to inherit everything. Less 1.7.0

  13. jaredly commented on Aug 21, 2014

    @jaredly

    +1!

  14. changed the title [-]Can't extend a nested class[/-] [+]Can't extend a nested class (with "concatenated identifier")[/+] on Aug 21, 2014
  15. changed the title [-]Can't extend a nested class (with "concatenated identifier")[/-] [+]Can't extend a nested class (concatenated identifier)[/+] on Sep 18, 2014
  16. seven-phases-max commented on Sep 18, 2014

    @seven-phases-max
    Member

    Merging to #2200.

  17. changed the title [-]Can't extend a nested class (concatenated identifier)[/-] [+]Can't extend a nested class (concatenated selector)[/+] on Sep 18, 2014
  18. guillaume-duchemin commented on Sep 18, 2018

    @guillaume-duchemin

    Hello, I know it's now into #2200 BUT 5 years without solution is it outdated ??

  19. matthew-dean commented on Sep 18, 2018

    @matthew-dean
    Member

    @guillaume-duchemin Not out-dated. Marked as up-for-grabs, so anyone can submit a PR for it at anytime.

  20. rejhgadellaa commented on Jul 24, 2019

    @rejhgadellaa

    +1

  21. kubami19 commented on Jul 31, 2019

    @kubami19

    +1

  22. acjbizar commented on Aug 14, 2019

    @acjbizar

    +1

  23. matthew-dean commented on Sep 19, 2019

    @matthew-dean
    Member

    In case someone comes here with a case like @Johns3n's example, you can always just convert that to the following to "share" rules between BEM classes:

    .ui {
        &__theme {
            background-image:url(../images/watermark.gif);
            background-position:top left;
            &, &--inverted {
                background-attachment:fixed;
                background-repeat:no-repeat;
            }
            &--inverted {
                background-image:url(../images/watermark--inverted.gif);
                background-position:top right;
            }
        }
    }

    i.e. the key useful "trick" in Less is the &, &[something] { pattern, where you can abstract common rules without using "extend"

  24. kerryj89 commented on Apr 16, 2020

    @kerryj89

    This would be a great feature as utility classes become even more popular. I was wanting to remove some utility classes from the markup where the sizing was set in stone. It would've been nice to just drop in &:extend(.padding-left-small); or &:extend(.padding-all-medium-lg-up);. I suppose I could wrap it in a mixin or something? Still, that's not intuitive when we have extends. Thanks for all the work you devs have done, though!

    .padding {
      &-left {
        &-small {
            padding-left: @gutter-small !important;
        }
        &-...
      }
      &-...
    }
    
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