Repository navigation
Description
This feature request was proposed as a solution to this issue #1155
This is the synopsis
The awesome thing about mixins is that they speed up development, keep my work surface clean, I only have to write them once, and I know what the compiled result will be. However, a disadvantage of mixins is that they aren't DRY. Especially when you use "utility" mixins a lot, like clearfix. This is where extends could be a much better option. In fact, this is a great, concrete use case for extending mixins. It would basically work like extending nested selectors, accept with mixins. So mixins wouldn't change at all, they would still work the same way. If you have a mixin and don't use it, it won't show in the compiled CSS. If you do use it, it's properties will still show up in the compiled code in each selector where it was used. And if you extend a mixin, the selector (or selectors) extending the mixin will show up in place of the mixin. So if you do this:
.clearfix() {
// stuff
}
.navbar {
&:extend(.clearfix());
}
.banner {
&:extend(.clearfix());
}and the compiled result will be:
.navbar,
.banner {
// clearfix stuff
}So the mixins properties are still inherited by the selectors that extended it, but the mixin (selector) itself doesn't show up in the compiled result.
This would make both extends and mixins far more powerful than they are on their own.
Activity
lukeapage commentedon Feb 13, 2013
I like it, but just to be clear.. this doesn't allow you do something you couldn't do before, it just means that you get..
rather than
or have I misunderstood?
dmi3y commentedon Feb 13, 2013
I would love have both, parametric and usual mixins, to have
or
whenever you need, I know this is the simular way how mixins already works, so the question mostly having flexibility and way for better handling the final CSS output in terms of optimization.
surely
is shorter than
and for more complicated examples that could be critical, and I know some people switched to SASS just because of this shortage
jonschlinkert commentedon Feb 13, 2013
Regular mixins couldn't change. They are pretty much a fixture of Less. @agatronic, yeah I see it that way as well. There are challenges to this though, like would we be able to extend parametric mixins? And if so how would that work? Assuming this was supported, let's say you extend a parametric mixin twice but use different variables each time, like this:
.transition(@transition) { -webkit-transition: @transition; -moz-transition: @transition; -o-transition: @transition; transition: @transition; } .navbar { &:extend(.transition(opacity .2s linear)); } .banner { &:extend(.transition(opacity .3s linear)); }I imagine it might create two copies of the mixin, which is no less code, and no advantage over regular mixins:
However, you might use this particular mixin a lot, so when you use the mixin again on
.dropdownwith the same variables as.banner, it might result in this:And this is what makes this interesting to me. it would be good to hear feedback from others too
dmi3y commentedon Feb 13, 2013
@jonschlinkert yes, that's even better illustration, thank you. as i said both hands on that! but understand though it is adding another level of complicity, and needs to be done/not done thoughtfully
krismeister commentedon Feb 14, 2013
Extension in the method you're reference can be achieved pretty easily with this pattern:
The above is injecting semi-extended mixins into the the 2 new declarations.
For the clearfix its also easy to use a similar pattern below, which will not only create the clearfix as a class, but inject those clearfix styles into a new declaration.
jonschlinkert commentedon Feb 14, 2013
@krismeister I think there might be confusion on this. The pattern you describe is really just nested mixins, or a mixin inheritance pattern. Extends work in the inverse of that.
krismeister commentedon Feb 14, 2013
@jonschlinkert Mixin inheritence - a great name. It seemed that both of the examples above where trying to inherit output from the parent - which is a core feature of extension. You probably agree that the output of my example was your expected output.
I understand from rereading your original comment however- you'd like the output to be joined into one CSS rule. Though it was written as 2 originally.
jonschlinkert commentedon Feb 14, 2013
Actually
:extendis pretty awesome. I'm going to go full nerd, but it all comes down to "what gets moved where". Think of a mixin like "beaming the mixin's properties down to the selector that used it". And think of:extendas the inverse, "beaming the selector that used it up to the mixin itself." Not the _selector's properties, just the selector itself. So if you did this:.some-mixin() { padding-top: 100px; background: #f7f7f7; } // a selector that is extending the mixin .alert:extend( .some-mixin() ) { border: 1px solid #e5e5e5; } // another selector extending the mixin section:extend(.some-mixin()) { margin: 20px 0; }It would result in this:
Hopefully that makes more sense. I'm happy to help anytime.
jonschlinkert commentedon Feb 14, 2013
@agatronic, @MatthewDL, @DesignByOnyx, just a thought, I know this is an unusual approach, but assuming there are no technical challenges to this, and assuming we allow a comma separated list of selectors, maybe extend should feel more like an array. What do you think about changing the syntax to:
:extend[N].I think it's easier on the eyes too when mixins are extended:
I'm fine either way, but something just feels right about the square brackets.
lukeapage commentedon Feb 14, 2013
@jonschlinkert -
[]means attribute in css, not array, where as:extend()was chosen because of its link to pseudo selectors, so I strongly think normal brackets should stay.jonschlinkert commentedon Feb 14, 2013
good point, I agree.
DesignByOnyx commentedon Feb 14, 2013
I agree that the brackets looks and reads nice, but yeah we have to stick with syntax we have already chosen.
dmi3y commentedon Feb 14, 2013
thumb up for extend(N) syntax, more feels like nature CSS, same reason as @agatronic
matthew-dean commentedon Feb 14, 2013
Extending mixins is not a bad idea. Not sure about the example syntax, @jonschlinkert. Not that I don't like it; I just actually don't understand what you've written.
I think what you mean is that you want to define classes that "pull in" the same mixin content, but don't actually repeat in the resulting CSS. I wouldn't call that extending mixins. That is, you're not altering the mixin definition (in the same way that the extend definition would do to selectors), you're actually altering (extending) the resulting classes.
So, correct me if I'm wrong, but wouldn't your navbar / banner / dropdown example be supported by:
That is, extending the classes as you normally would? I'm just trying to wrap my head around your usage pattern, and the use of extend for mixins seems at odds with its use and nomenclature for extending selectors. Can you provide further details on why you would need / want to reference the mixin in an extend call, and not extend a selector that referenced the mixin?
dmi3y commentedon Feb 14, 2013
That's how I get it
DESIRED FINAL CSS OUTPUT
THE WAY YOU HAS TO GO WITH CURRENT VERSION OF LESS TO GET DESIRED OUTPUT
SUGESSTED LESS SYNTAX
While the extends could be useful only when you have really amount of shared code, in other cases ordinary mixins usage have favor.
And just somewhat, live dummy sample, aimed to just playaround
http://jsbin.com/opekon/1/edit
http://jsbin.com/opekon/2/edit
http://jsbin.com/opekon/3/edit
http://jsbin.com/opekon/4/edit
as said, there could be really performance boost in certain circumstances for people who lazy like me:) to optimize LESS code just because it is affected further flexibility
85 remaining items
Grawl commentedon Apr 7, 2015
@seven-phases-max lol no I'm just frontend/designer.
@CelC looks really bad 😅
matthew-dean commentedon Apr 7, 2015
Yeah, what am I paying you people for, anyway? ;-)
stevenvachon commentedon Mar 2, 2016
3 years and counting. Fortunately, Sass v4 is coming (with
sass-*prefixed functions) and I can get rid of Less.SomMeri commentedon Mar 4, 2016
@stevenvachon We hold no grudge against Sass. Of course they are competitors in a sense, but projects have different philosophy and trying to hurt us by invoking them is senseless.
Of course, pull requests are welcome, from anyone. We will gladly merge in functional pull request with this feature.
stevenvachon commentedon Mar 7, 2016
I'm not trying to hurt anyone, in fact probably trying to prevent such. I think it's better to express how one may feel and the new directions they may be taking as a result. I think that it's better to know sooner than later; before everyone leaves. For example, Bootstrap v4 will not use Less.js and I'm sure that will put a big dent on your user base. You can ignore such politics all you want, of course.
krismeister commentedon Mar 7, 2016
@stevenvachon why so offtopic? Sass and Less are both great. I love pure JS-ness of less. node-sass needs a c compiler to get installed, which can be a pain depending on the environment. I've used the :extend feature that is already built in LESS and meets all my needs. I don't even know why this issue is still open.
Heck, I'm a rails developer and use gulp+LESS when it's not a ruby project.
stevenvachon commentedon Mar 7, 2016
Off topic? This feature has not been implemented in 3 years and it'd be very useful.
I'd prefer if Sass were written in JS, but it isn't, and that isn't what's most important. Its features are. Sass allows us to extend placeholders but Less does not allow us to extend mixins.
matthew-dean commentedon Mar 7, 2016
Public Service Announcement
At any point in time, you may submit a pull request, or work with a developer to get one in, and it's very likely this feature be merged in. The Less.js library needs more regular contributions from individuals like the people in this thread. It's not any one person's responsibility. It's entirely dependant on one individual seeing this thread, or needing this feature, and taking the time to make it happen.
If your response is that you're not a developer, then there are many OTHER ways you can support this project in non-developmental roles, be it providing needed documentation for new features, or design support on the website, running tests, providing feedback on issues, project managing, answering questions on Stack Overflow, writing blog posts, tweeting about Less, contributing to CSS and web communities, and writing Less libraries.
Many of these tasks are being done by individuals who are developers, so the time they could be contributing to development is spent on these other tasks.
If you are a developer, and your response is, "I don't have the time," then that's the exact reason for why this issue has sat open. It's 100% up to you if a feature is completed, so asking why it hasn't been completed by "someone" is not useful. You are that someone. It will happen, I guarantee it will happen, if and when you get involved in this project. You can become involved in one of the most popular open source tools on the web. You can make a difference in the lives of... thousands? Hundreds of thousands? Millions? And as a side benefit, you can make sure that your favorite features happen sooner rather than later.
If you want this or any feature to happen, make it happen. We would love to have you. We would love to work with you! Less can keep getting better and better the larger this community grows!
And, listen, I appreciate that sometimes people really have absolutely no time to contribute and yet still would really like a particular feature to happen. That's fine. I would just say that if you're in that scenario, that you aim for empathy and gratitude for people donating their time (and sometimes money) to help you, and I'm sure other people in the Less community will do their best to continue to do so, as they are able.
Sincerely,
Matthew Dean, Member of Less.js Core Team
stevenvachon commentedon Mar 7, 2016
I already support my own projects and I listen to their feature requests. I honestly don't have time to dig into Less.js core.
stevenvachon commentedon Aug 9, 2016
For those coming here for sass placeholder (or "silent class") support, read this comment. It won't get you all the way there (due to #1851 and having such classes not in a "referenced" file), but it's better than nothing.