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
Ability to insert styles at a specific position in <head> #2037
Comments
That's not quite true - probably some validators might yell at you, but all browsers supports div elements within the head.
It's not possible with Emotion 10 and we do not plan to introduce such an option. However, Emotion 11 - which is just around the corner - has a new |
I had tried with a I'll probably stick with the meta approach - at least until I run into any insurmountable issues - since it seems to work and lets me hydrate to that container, but it's good to hear that Emotion 11 will offer a bit more control. I suppose your suggestion with the Emotion 11 |
Yes, pretty much that 😃 |
I have another use case for this where we need Emotion's CSS classes to have lower specificity than a user's own CSS classes. We're building a library using Emotion where we want a user's CSS classes to be able to override the styles of our library components. I noticed that the One could provide import { createEmotion } from '@emotion/create-emotion';
const emotion = createEmotion({
container: document.head,
insertBefore: document.head.firstElementChild
}); Is this a possible change to make in v10? If so, I'd be happy to submit a PR. |
We do not plan to introduce this to v10. As mentioned - v11 has the |
@Andarist kk, thx, good to know that v11 is close then. Quick question if you happen to know the answer: are there any performance issues in browsers caused by prepending a <html>
<head>
<style data-emotion="css"></style> <!-- exists second -->
<link rel="stylesheet" href="sheet.css"> <!-- exists first -->
</head>
<body>
...
</body>
</html> Do you know of any performance issues either during initial page load, or at a later time by dynamically prepending rules in front of maybe a few thousand existing rules? |
I don't know about any perf issues regarding that but I haven't really ever thought about this. My intuition would be that there shouldn't be much of a difference between appending & prepending but I might be wrong. |
Ah ok, no worries, I'll try to do some testing and will let you know if I find anything |
@Andarist Did some testing and didn't find any difference in re-render performance in Chrome with prepending, at least in the case that styles are prepended after initial page load. Still need to do a test for initial page load with regular stylesheets + prepending, and in other browsers. |
Does the new |
I believe it does - we dont intend to introduce more control over insertion point than what is already provided in v11 |
@Andarist is the motivation for rejecting the feature in #2037 (comment) lack of bandwidth or concern about the extra complexity involved? |
@oliviertassinari mainly about the added complexity. We can reconsider this though - the new API (option?) would have to replace |
@Andarist OK, We might POC around to explore the feasibility of bringing this with a PR in the future. At this point, MUI v5 has about 1% of adoption of v4, so we will hear more about the need for this feature as more developers migrate. We have recently seen an interesting use case with Tailwind, where the TW CSS reset needs to apply before MUI but the TW classes after MUI. Sure we could say that, nop don't use TW with MUI but I don't think that it would be the best strategical direction. Better embrace the alternatives options and make the ones you propose more appealing. |
@Andarist @oliviertassinari, I'm migrating to MUI v5 and this became a bit of a problem for me. My styles insertion order should be like this:
Now, this is fairly straightforward during SSR, since I can control where the styles from emotion cache are inserted. However, during CSR there's no way to control where the styles are inserted inside the My workaround was to move styles into
I'm not using Tailwind, but my use case is pretty similar, so I agree with this 💯%. |
Since this got merged in and shipped this statement is no longer true. I'm not sure if this option has been exposed in MUI though - you'd have to look there for an answer to that. |
@Andarist, I'm using There's also another somewhat related issue, which probably warrants a new issue in MUI repo (but I don't currently have time to properly submit it with minimal repro, etc.), but just FYI:Some animations (seems to be from MUI LinearProgress) are still appended at the end of And on the server render, these animations are at the beginning of |
Interesting - would love to get a repro case for that, I could then investigate this quickly. |
The problem
I see that
@emotion/cache
provides acontainer
option, which seems like it's useful for having some control over the DOM element that emotion inserts its<style>
nodes into. Unfortunately, I can't find a way to use this for my purpose, which is to control the position of the inserted nodes in the document head.The reason I need this is to have more fine-grained control over rule ordering in a page that has existing styles (which are sadly often composed with the newer Emotion-generated classnames). I'd like Emotion (both on the server and client) to insert all of its
<styles>
(both those initially on the page as well as those dynamically inserted later on) at a specific position in the<head>
.Initially I thought I could use the
container
option, but that seems to have two problems here:<style>
children in the<head>
Together, these two issues make it hard to find a way to do what I want here with any existing Emotion apis.
Proposed solution
Webpack's
style-loader
provides aninsert
option which can be given a function to be run in order to determine the position of any inserted styles. Something like this might work here too.Alternative solutions
Another option might be to introduce a more flexible version of
container
- not a full function likestyle-loader
, but aninsertAdjacentTo
,replaceCommentWith
or something similar to allow for more control over the placement?Additional context
Also, based on my problem statement - if there's any existing way to do what I want in Emotion 10, it goes without saying that I'd be very grateful to hear so :)
The text was updated successfully, but these errors were encountered: