-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support !important for styles? #1881
Comments
|
On a random note, I thought that inline styles override !important but I was wrong ( http://jsfiddle.net/do0hpcvm/ ). |
@vjeux I'm going to leave this to you. I think we should probably just maintain status quo here and not support |
@zpao We do support it though, we just don't support updating a style that has it. |
That's not support, but I see what you mean (that's like saying we "support" hyphenated styles) |
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 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;}'; |
@cody It would probably be easier to just manually manage the style property of the node itself ;) |
+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.) |
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 |
IMO, it should be supported. It's not React's place to be dogmatic about how people author their styles. |
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 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 |
@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. |
I'm facing the same issue as @nelix. It is quite an edge case, but it would be nice to have support for this. |
@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. |
Ran into this today, trying to dynamically set the background color of a Semantic UI Icon. They have those colors set as |
what's the status on this? |
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. |
This makes it pretty hard for React to play nice with Semantic UI (which frustratingly uses |
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. |
For non-IE8 (which we actually don't actively care about anymore) this can technically be solved. EDIT: If that is something we want to look into. |
Over two years ago, and this is still debated? To me restricting it's use because of some subjective argument is just regressive. If it's mainly to prevent people to abuse it, well, it is quite idiotic as it's use is... important on many use cases. If it is part of the specification, then why not supporting it in React?
|
I literally just lost an hour debugging this. Given the amount of assistance react provides users when they do something unexpected (suggesting camel-case when using hyphenated class names, etc), i would have expected something in the console in this. It would be awesome to be non-dogmatic and support people doing something unnecessary or at least warning about it. |
I'm having this issue as well. The reason I need the " !important" override on an inline style is that my project is in a transition state, and still has CSS being applied using classes--one of which is using a css " !important" tag. (originally to override some JQueryUI css, I believe) Having support for the tag would prevent me from having to use another css class (I'm trying to transition away from css classes), or modifying the old css class to no longer have the !important tag--which is a pain since it's used globally and would break other areas of the UI. (which are still using the old, JQuery rendering) |
For the moment, I wrote a library that should address this use case: brigand/react-with-important-style. It uses |
this is something that should really be implemented even if we don't use the ! and just do say
I would be comfortable with either being implemented. edit: or even just let me do |
We managed to remove lots of !important from the SUI components in V2.7.x recently 🙂 |
I was running into this issue because I was using 2.6, so lucky I saw this. Drop jQuery next 🙂 |
So still no easy solution to this problem? I'm unable to change a Semantic UI element's color dynamically without access to !important. My workaround was to create my own class that emulated what Semantic UI was doing for a certain element..but this is obviously far from ideal. |
Yeah, extending something just to fix one property doesn't seem like a scalable solution, given that you will have to adjust it to match the lib on every update. Uncool. |
Same thing @champeleon , I need to change some Semantic UI properties and really needed this !important. It should be supported for such a big library as React... |
I suggest the react team should consider implementing a !super-important tag. |
This is stupid. I need !important in @media print, to remove the body from the page and setting the visibility of the dialog to be visible !important and overflow visible, when printing the contents of a dialog with 'multiple pages' within, like this -> So yeah, it should be included and supported. I know it's NOT OK to use !important, but sometimes it's necessary. |
What if the styles supported a tuple (an array)?
|
Love that idea, @milesj. It gets us a lot closer to having the support when needed, but making it stand out beyond the normal practice of writing CSS, so hopefully it would be flagged by casual PR reviews as bad practice. 😆 |
It's not actually a react problem. is it?! I see the other frameworks had the same problem with |
@behnood-eghbali just because other frameworks exhibit the same behavior doesn't make it a non-issue. It just means that they have the same problem as well. |
I can't believe that it has been 5 years and nothing has been done to address this at all. Working with frameworks that already have pre-defined |
I recommend using styled components, if you have a good reason to make use of "!important": Here is an example where we overwrite Semantic-UI's padding on grid columns. You simply can't without important. const StyledColumn = styled.div(({size}) => ({className: `${size} wide column`})`
&&&&& {
padding-top: 0.3rem !important;
padding-bottom: 0.3rem !important;
}
`
<StyledColumn size="three"></StyledColumn> &&&&&& <- bumps up specifity |
&&&&& seems to be very similar to this category of selectors. https://speakerdeck.com/csswizardry/refactoring-css-without-losing-your-mind?slide=64 |
Meeehhh!!! React y sus cosas baratas!!! No acepta !important... |
¡Ay, caramba! Tendrás que usar clases y una hoja de estilo vinculada en lugar de estilos en línea, mi hombre. |
GGRRRR...!!! 😏😏😏😏 |
I submitted a PR for this which was rejected #12181, so I'm going to close this as "wontfix" |
Rejected or just requesting changes? 🤔 |
It appears it was rejected because they didn't like how the patch was implemented, from comments like these:
I don't have time to dig into the code, really... But it feels like there is a high demand for this feature so maybe it should be left re-opened, just a thought. |
I think a lot of commenters are probably missing the fact that setting styles as I’m going to lock this thread to prevent further misinformation — it really does seem like most people don’t realize it, and adding more replies causes this information to get lost. If you have a particular proposal for a more ergonomic alternative, please send an RFC here: https://github.com/reactjs/rfcs |
To be fair, the ref solution doesn’t help for server rendering. Again, if you have a particular API proposal, sending an RFC would be a better place to discuss it. Thank you! |
We currently don't support
!important
as it has to be set usingstyle.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!But apparently that was wrong as well, the only way to set it seems to be withstyle.setAttribute(name, value)
(priority is part of the value).cssText
, an acceptable work-around may be to just usecssText
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.The text was updated successfully, but these errors were encountered: