-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
[material-ui] Opt-in support for @layer
to fix Tailwind v4 integration
#44700
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
Comments
I agree with this to fix the integration with Tailwind v4, keeping in mind the requirements:
|
@layer
@layer
to fix Tailwind v4 integration
@layer
to fix Tailwind v4 integration@layer
to fix Tailwind v4 integration
I don't know if this is 100% related to the topic of the issue, but I was having some issues when using /* index.css */
@import "tailwindcss" important; /* <==== just add the important keyword */
/* other project styles */ Although simple, it is not the same as the previous |
|
Nice! Thanks for the fix. I knew there was some way to mark layer as important, but couldn't find it on MDN somehow. You don't have to import all of tailwindcss parts. Based on the instructions here (https://tailwindcss.com/docs/preflight), I'm doing this: @layer theme, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities) important; I just put |
@HasanMothaffar @mrdjohnson @Malivuca Please see this repo for proper configuration. Repo: https://github.com/siriwatknp/mui6-tailwind4-vite Summary: set up CSS layer for Material UI to be |
Wow, I've tried this solution before (as i've seen it at your issue's description), but couldn't get it to work. By following your repository guides it worked like a charm mate. This might be the best solution available for the Thanks a lot 💯💯💯 |
When will the official documentation provide the tailwindcss v4 integration? Or do I need to enable an additional doc feedback? |
@PunkFleet the team is working in having an easier way of integrating with Tailwind (e.g. #45428) |
@aarongarciah is there a plan to also fix it in combination with Next.js? Because I have the issue even though there is the |
Please check the repo I provided, you need to insert mui into Tailwind CSS layer order. |
Thanks @siriwatknp |
Hey folks! I had the same issue and solved it with a simple approach. Here's what worked for me: When using I created a function MuiLayerOverride() {
useEffect(() => {
const styleElement = document.createElement("style");
styleElement.setAttribute("id", "mui-layer-override");
styleElement.setAttribute("data-priority", "high");
styleElement.textContent = `
@layer theme, base, mui, components, utilities;
@layer mui {
.mui-layer-override {
--mui-layer-order-fixed: true;
}
}
`;
document.head.insertBefore(styleElement, document.head.firstChild);
return () => document.getElementById("mui-layer-override")?.remove();
}, []);
return null;
} For Next.js App Router users, make sure you're using <AppRouterCacheProvider
options={{
key: "css",
enableCssLayer: true,
}}
>
<ThemeProvider>
<MuiLayerOverride />
<MuiThemeProvider theme={theme}>
{children}
</MuiThemeProvider>
</ThemeProvider>
</AppRouterCacheProvider> The key parts are:
This works because it defines a clear layer order that both MUI and Tailwind respect, and prevents MUI from changing the layer order on its own. No more fighting with specificity or using !important everywhere. Works perfectly with Tailwind v4! |
@originalai Thanks for sharing. Would it be simpler if you use <AppRouterCacheProvider
options={{
key: "css",
enableCssLayer: true,
}}
>
<GlobalStyles styles="@layer theme, base, mui, components, utilities;" />
<ThemeProvider>
<MuiThemeProvider theme={theme}>
{children}
</MuiThemeProvider>
</ThemeProvider>
</AppRouterCacheProvider> I think the useEffect is not required. |
What is the workaround for pigment-css? |
Demo
Vite
Repo: https://github.com/siriwatknp/mui6-tailwind4-vite
Preview: https://siriwatknp.github.io/mui6-tailwind4-vite/
Next.js
Repo: https://github.com/siriwatknp/mui6-tailwind4-nextjs
Preview: https://mui6-tailwind4-nextjs.vercel.app/
Motivation
Tailwind integration
The current Material UI v6 is hard to use with CSS utilities like Tailwind CSS because some components have higher CSS specificity than (0,1,0) so Tailwind CSS could not be used without
!important
.Also, Tailwind v4 (will be stable soon) is using native CSS layer so Material UI won't be able to override it without a configurable layer.
Here is the issue using Material UI v6 + Tailwind v4 (beta). The
text-2xl
cannot override the Typography base styles.Different override behavior when using CSS variables
Another issue is the behavior change when transition from dynamic CSS using
theme.palette.mode
to CSS variables paradigm (static CSS). Thesx
prop cannot override the:where()
because Emotion always append:where()
at the end of the styles.Requirements
Options
1. A custom stylis plugin for single layer
Based on this solution, this will capture emotion styles into a single layer, likely named
@layer mui
.I think this is a simplest solution that works really well. It will fix the Tailwind v4 integration but it won't fix the "Different override behavior when using CSS variables" because all Material UI styles are in the same layer.
2. Add flag to the theme for multiple layers
To solve "Different override behavior when using CSS variables", the sx styles must be in a different layer so that it overrides the styles of the component regardless of selector's specificity.
A custom stylis plugin (option 1) is not possible to support multiple layers, so the layer has to be created from the
styleFunctionSx
.Feel free to share other possible options that we could do.
Search keywords:
The text was updated successfully, but these errors were encountered: