less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Designate a mixin as !important

Open reefdog opened this issue 14 years ago • 12 comments

When calling a mixin (parametric or otherwise), if the call ends in !important, it would be splendid if this !important flowed down to all the mixin declarations.

So:

.bordered {
    border-top: dotted 1px black;
    border-bottom: solid 2px black;
}

#menu a {
    color: #111;
    .bordered !important;
}

Would generate:

#menu a {
    color: #111;
    border-top: dotted 1px black !important;
    border-bottom: solid 2px black !important;
}

reefdog avatar Jan 05 '12 19:01 reefdog

Just to continue the conversation from Issue #100, I would say that specially designating a keyword as having a different mixin behavior is arbitrary and probably not ideal. With using variables, you can append any keyword value as you like into your mixin.

matthew-dean avatar Jan 05 '12 19:01 matthew-dean

I think of this less as "extending LESS" and more as "making LESS look and act more like CSS", which is one of the appeals of LESS. It's CSS on steroids.

Semantically, if I call .bordered() !important, I'm saying "this declaration is important" and expect it to work just like background-color: #000 !important or whatever.

Having said that, I can see the appeal to allowing non-escaped !important as part of variables, particularly when the mixin might deal with multiple properties and you only want some to be important. I don't think that negates the usefulness of this feature, though.

reefdog avatar Jan 05 '12 19:01 reefdog

To respond to your other comment (https://github.com/cloudhead/less.js/issues/100#issuecomment-3375093), yes. For this feature, I would expect a 14-line mixin designated !important to add !important to all 14 lines.

Naturally, as mentioned above, there will be times you may only want some variables to be important, in which case variable-specific !important would be nice, but that's covered by #100. I think there's a use case for both requests.

reefdog avatar Jan 05 '12 19:01 reefdog

Damn, you're actually starting to sway me, partly because !important is a special keyword that is available to all CSS declarations. Hmmm..... I guess the next step would be to do a test implementation, and see if @cloudhead wants to pull it into the project. Or maybe ask his thoughts on it first. But I can see your rationale.

Again, though, my caveat. Usually, when I see !important all over CSS, it means that someone probably isn't doing things wisely. BUT one should be free to abuse LESS as much as they abuse CSS. ^_^

matthew-dean avatar Jan 05 '12 19:01 matthew-dean

Hehe, agreed. It's easy to abuse. This actually came up because of a co-dev's specific need to override some Bootstrap stuff. There were other solutions (he went with an unnecessarily specific selector chain), but in this case !important was the contextually-appropriate solution, and it felt weird not to be able to use it, not least of which was because LESS feels so familiar and so like CSS.

I'll try to dig into the parser and see if I can figure out how to make the change. I'm dumb.

reefdog avatar Jan 05 '12 19:01 reefdog

No more dumb than any of the rest of us. The parser stretches my JavaScript brain quite a bit. :-)

I've used Twitter Bootstrap, and have often found the need to override and replace sections. I'd actually love it if subsequent mixins replaced previous mixin definitions. Most of the CSS3 syntax, especially gradients, I've overridden in Bootstrap because I use CSS3Pie for rendering CSS3 in IE, which is much more reliable and flexible than the filter syntax.

Bootstrap is great if you're using as is, but like jQuery Mobile, fails when you really need to customize. It's just not how it's designed.

matthew-dean avatar Jan 05 '12 19:01 matthew-dean

I like this idea actually, I'll have a look.

cloudhead avatar Jan 05 '12 20:01 cloudhead

ok, it's in! https://github.com/cloudhead/less.js/commit/aab66a4d10e38a4d92c003ecb5ef03b9ff3067cc

cloudhead avatar Jan 05 '12 20:01 cloudhead

.thanks(@cloudhead) !important;

;)

reefdog avatar Jan 05 '12 20:01 reefdog

Radness.

matthew-dean avatar Jan 05 '12 22:01 matthew-dean

Sweet, just came here from google, this is great

vjsingh avatar Jul 02 '12 20:07 vjsingh

.trythis(@iWantIt e("!important"));

llturro avatar Jan 29 '16 11:01 llturro