react icon indicating copy to clipboard operation
react copied to clipboard

Support !important for styles?

Open syranide opened this issue 12 years ago • 73 comments

We currently don't support !important as it has to be set using style.setProperty(name, value, priority). This should be trivially easy to implement if it's something want to support, although I'm not sure about the performance implications (EDIT: #1886), although I can't imagine it would be measurable as we would be doing the string test ourselves and those are cheap.

http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration

~~OK... apparently IE8 doesn't support this at all, it might still be worth implementing, for the future and for those who don't care about IE8.~~ ~~After further experimentation, apparently it is! style.setAttribute(name, value) (priority is part of the value).~~ But apparently that was wrong as well, the only way to set it seems to be with cssText, an acceptable work-around may be to just use cssText when we detect an !important style, which should be seldom enough that performance is a non-issue.

An important consideration is that we already support this for the initial render, but subsequent updates will fail as !important isn't recognized for properties.

syranide avatar Jul 19 '14 13:07 syranide

!important is horribly abused in css, and if people write their css properly their is never a need for !important. That said I don't think it should be included, because then people can use "bad practices" in their apps and styles. Personally inline styling with react is bad enough.

AdamKyle avatar Jul 19 '14 15:07 AdamKyle

On a random note, I thought that inline styles override !important but I was wrong ( http://jsfiddle.net/do0hpcvm/ ).

vjeux avatar Aug 08 '14 15:08 vjeux

@vjeux I'm going to leave this to you. I think we should probably just maintain status quo here and not support !important, but there may be valid uses somewhere.

zpao avatar Aug 11 '14 21:08 zpao

@zpao We do support it though, we just don't support updating a style that has it.

syranide avatar Aug 11 '14 21:08 syranide

That's not support, but I see what you mean (that's like saying we "support" hyphenated styles)

zpao avatar Aug 11 '14 21:08 zpao

Any word on the progress of this? I'm using react in a widget and a browser extension that work on third party sites and not being able to update !important styles is an issue for me

nelix avatar Dec 14 '14 23:12 nelix

@nelix Maybe you can use something like this as a workaround:

var sheet = document.createElement('style');
document.body.appendChild(sheet);
sheet.innerHTML = 'html {font-size: 1em !important;}';

waldreiter avatar Dec 15 '14 06:12 waldreiter

@cody It would probably be easier to just manually manage the style property of the node itself ;)

syranide avatar Dec 15 '14 09:12 syranide

+1 to Nelix's comment. For example, in Bootstrap CSS there are a number of utility classes that all use !important (hidden, pull-right, etc) that I wouldn't be able to override without !important inline styles. (I totally agree that !important is abused--and probably Bootstrap too, for that matter--but I think React should still be able to work with them.)

ccnokes avatar Jul 05 '15 05:07 ccnokes

We use !important to set a dynamic background color on an element and have it still show up when printing. Without !important the background is not printed.

EDIT: Since I only needed ie9+, I used setProperty in componentDidMount & componentDidUpdate

thomassuckow avatar Aug 04 '15 16:08 thomassuckow

IMO, it should be supported. It's not React's place to be dogmatic about how people author their styles.

quantizor avatar Aug 04 '15 19:08 quantizor

I had a longer post here and then lost it, oh well…

The summary is that it would be expensive to check every value for !important, so supporting this isn't awesome. Even if we do get a good option, it's unlikely we'd do this while we support IE8 because I don't want to go down that cssText path. This isn't really at all about dogma, I would say that it's more about perf. And the fact that we're faced with a number of limitations when working with styles in CSS.

What we could perhaps do is change the data structure for style.

style = {
  color: 'red',
  backgroundColor: {
    value: 'blue',
    important: 'true'
  }
}

It would be backwards compatible (if the value isn't an object, we do the exact same thing). And it would allow us to start using the setProperty API with important. Downside is obviously more objects. But we don't have to do any string parsing.

zpao avatar Aug 04 '15 21:08 zpao

@zpao That seems reasonable. Perf is obviously a very important factor to consider and I appreciate the creative thinking... your solution would at least allow a developer to support the need, even if it's a little more effort.

quantizor avatar Aug 04 '15 21:08 quantizor

I'm facing the same issue as @nelix. It is quite an edge case, but it would be nice to have support for this.

Charca avatar Feb 19 '16 18:02 Charca

@zpao that api suites me fine, I will be really happy to rip out dynamically generated css + manual style manipulation I have from my chrome extension's IFrame armor.

nelix avatar Feb 26 '16 11:02 nelix

Ran into this today, trying to dynamically set the background color of a Semantic UI Icon. They have those colors set as !important, presumably for legitimate reasons. Fortunately I could work around it like @thomassuckow did. Would be nice to avoid the extra step though.

mwschall avatar Mar 13 '16 05:03 mwschall

what's the status on this?

6ewis avatar Apr 15 '16 14:04 6ewis

No change. As we invest more in JS styles I think we'll end up with some changes to how styles work. In the mean time I don't think we'll make any changes without a fuller picture of how that will play out.

zpao avatar Apr 15 '16 22:04 zpao

This makes it pretty hard for React to play nice with Semantic UI (which frustratingly uses !important nearly everywhere).

japgolly avatar Jun 15 '16 05:06 japgolly

For the record, since 15.x it no longer works even on initial render (since moving away from innerHTML). All rules with "!important" are now being thrown out.

harmony7 avatar Jul 22 '16 15:07 harmony7