Skip to content
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

[styled-engine] Add enableCssLayer prop to StyledEngineProvider #45428

Merged
merged 11 commits into from
Mar 12, 2025

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Feb 27, 2025

First step for #44700.

  • Added a new enableCssLayer prop (same prop name as the AppRouter integration)
  • It's independent of the existing injectFirst prop.

The next step is to create a Tailwind v4 guide in a separate PR.

Try it out

  1. Create a vite project with tailwind css v4 and Material UI installed with:
  "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/cbeefad4/@mui/material",
  1. Set enableCssLayer to StyleEngineProvider component
<StyledEngineProvider enableCssLayer>
  1. Add CSS layer order using GlobalStyles under StyledEngineProvider.
// src/App.jsx
<StyledEngineProvider>
  <GlobalStyles styles="@layer base, theme, components, mui, utilities;" /></StyledEngineProvider>

The reason to do this from Material UI side is to ensure that CSS layer order is configured correctly before Material UI components render.

image

Review recommendation

toggle "Hide whitespace"


Sorry, something went wrong.

@siriwatknp siriwatknp added the package: styled-engine Specific to @mui/styled-engine label Feb 27, 2025
@siriwatknp siriwatknp changed the title [styled-engine] Add enableCssLayer option to StyledEngineProvider [styled-engine] Add enableCssLayer prop to StyledEngineProvider Feb 27, 2025
@mui-bot
Copy link

mui-bot commented Feb 27, 2025

Netlify deploy preview

https://deploy-preview-45428--material-ui.netlify.app/

@mui/joy: parsed: +0.06% , gzip: +0.11%
@material-ui/core: parsed: +0.05% , gzip: +0.07%
@material-ui/system: parsed: +0.34% , gzip: +0.38%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against baf2ed3

@siriwatknp siriwatknp force-pushed the feat/style-engine-csslayer branch from 46e63b3 to 52ec40d Compare February 27, 2025 06:55

Verified

This commit was signed with the committer’s verified signature.
wyattjoh Wyatt Johnson
@siriwatknp siriwatknp force-pushed the feat/style-engine-csslayer branch from 52ec40d to 3409cb9 Compare February 27, 2025 07:51
@siriwatknp siriwatknp marked this pull request as ready for review February 27, 2025 08:46
@siriwatknp siriwatknp added the needs cherry-pick The PR should be cherry-picked to master after merge label Feb 27, 2025
Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some tests? Looks like StyledEngineProvider doesn't have any, but still worth it IMO.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@siriwatknp
Copy link
Member Author

Should we add some tests? Looks like StyledEngineProvider doesn't have any, but still worth it IMO.

Added the test. It's not that simple to test the generated CSS with @layer so I have to add private object to intercept the styles.

It won't leak to user because it's not exported through barrel files.

@aarongarciah
Copy link
Member

@siriwatknp can you add instructions on how to properly test this change to get a sense of how the final DX looks like? I'm testing in a Vite app and when I enable enableCssLayer some styles are gone. I assume we need to disable Tailwind's preflight and include the mui layer somewhere?

@siriwatknp
Copy link
Member Author

@siriwatknp can you add instructions on how to properly test this change to get a sense of how the final DX looks like? I'm testing in a Vite app and when I enable enableCssLayer some styles are gone. I assume we need to disable Tailwind's preflight and include the mui layer somewhere?

Added the instruction in the PR description.

Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and it works as expected 👍 I'd love more eyes on this before approving, also on the testing strategy.

Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +12 to +14
TEST_INTERNALS_DO_NOT_USE.insert = (...args) => {
rule = args[0];
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should move the override and delete of TEST_INTERNALS_DO_NOT_USE.insert into beforeAll and afterAll calls

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siriwatknp there's still some left over from the refactor:

  • line 33 delete
  • The last two tests still have the .insert definition and the delete at the end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ Opps, removed.

@siriwatknp siriwatknp requested a review from DiegoAndai March 10, 2025 06:41
Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@siriwatknp remember that if we need to cherry-pick to v6, the v6.x label should be added as well

@siriwatknp siriwatknp merged commit 03c0b90 into mui:master Mar 12, 2025
20 checks passed
Copy link

Cherry-pick PRs will be created targeting branches: v6.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs cherry-pick The PR should be cherry-picked to master after merge package: styled-engine Specific to @mui/styled-engine v6.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants