-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[system] set before
directly without using prepend for global styles
#44648
base: master
Are you sure you want to change the base?
Conversation
before
directly without using prepend for global styles under <StyledEngine injectFirst>
before
directly without using prepend for global styles
Netlify deploy previewhttps://deploy-preview-44648--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @siriwatknp! Sorry for the delay
Have you identified with what change (PR) this broke from v5 to v6?
Should we also update this section and this warning on the docs?
packages/mui-styled-engine/src/StyledEngineProvider/StyledEngineProvider.js
Show resolved
Hide resolved
*/ | ||
class MyStyleSheet extends StyleSheet { | ||
insert(rule, options) { | ||
if (this.key && this.key.endsWith('global')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before, we weren't checking this.key.endsWith('global')
to add prepend: true
. Why is this required now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I was wrong before that the Emotion (with prepend
) will have the same behavior if a CustomSheet is provided.
However, the behavior for the GlobalStyles
is unpredictable if you order the styles differently:
<GlobalStyles />
<CssBaseline /> // this is also using GlobalStyles
<Typography />
inject the styles in different order compared to:
<Typography />
<GlobalStyles />
<CssBaseline /> // this is also using GlobalStyles
That's why we cannot rely on prepend
(deprecated) but to switch to insertionPoint
+ custom insert
API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow.
Let me rephrase my question:
- Before this PR, we were always using
prepend: true
, regardless ofthis.key
's value - Now, we're replacing
prepend: true
withinsertionPoint
. This makes perfect sense to me - But also, we're now checking for
this.key.endsWith('global')
to decide if we should useinsertionPoint
. We weren't doing that before when usingprepend: true
. This is what I don't understand. Why do we need to checkthis.key.endsWith('global')
now?
Besides that, I don't understand the code examples. In both I see CssBaseline
after GlobalStyles
, and Typography
's placement changes. I would expect Typography
's placement not to have any effect on CssBaseline
and GlobalStyles
insertion points. Am I wrong or is the example incorrect?
packages/mui-styled-engine/src/StyledEngineProvider/StyledEngineProvider.js
Outdated
Show resolved
Hide resolved
packages/mui-styled-engine/src/StyledEngineProvider/StyledEngineProvider.js
Outdated
Show resolved
Hide resolved
…neProvider.js Signed-off-by: Siriwat K <[email protected]>
…styled-engine-inject-first
To be honest, I think they are separate issues, not specific to |
closes #44597
Root cause
Each
GlobalStyles
create its own sheet and injected at this line:This cause the 2nd GlobalStyles in the app to be injected above (
insertBefore
) the 1st GlobalStyles.Solution
set
before
to be the insertion point at theinsert
method of the custom sheet.Benefit of insertion point
prepend
(deprecated) is replaced byinsertionPoint
https://github.com/emotion-js/emotion/tree/main/packages/sheet#prepend.prepend
is unpredictable forGlobalStyles
. Different order ofGlobalStyles
and components produce different injection order 😮 (even without<StyledEngine injectFirst>
. This PR makes the order of GlobalStyles and components same as using the default cache.Test
Added a regression test. I don't think this can be easily tested with a unit test.