-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[material-nextjs] Add option to enable CSS layers for pages router #45596
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
[material-nextjs] Add option to enable CSS layers for pages router #45596
Conversation
Netlify deploy previewBundle size report |
…/nextjs-pages-csslayers
…/nextjs-pages-csslayers
…/nextjs-pages-csslayers
@import 'tailwindcss/theme.css' layer(theme); | ||
@import 'tailwindcss/utilities.css' layer(utilities); |
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.
Based on the Tailwind CSS docs for disabling preflight
@tailwind utilities; | ||
@import 'tailwindcss/theme.css' layer(theme); | ||
@import 'tailwindcss/utilities.css' layer(utilities); | ||
@config '../tailwind.config.mjs'; |
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.
To use the plugin define in the config file (Tailwind v3)
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.
Only one question on the @mui/material-next-js
side, I would request @alexfauquette's review for the docs-infra side.
packages/mui-material-nextjs/src/v13-pagesRouter/pagesRouterV13Document.tsx
Show resolved
Hide resolved
…/nextjs-pages-csslayers
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.
Looks good to me from the @mui/material-nextjs
side. I'm not approving because we need a review from @mui/docs-infra for the docs side, but feel free to merge when that's done.
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.
Sorry for the delay. Take this approval with a grain of salt, I never used TailwindCSS on a personal project, so I do not know the subtility between v3 and v4 at all
let styleElement: HTMLStyleElement | null = null; | ||
const abortController = new AbortController(); | ||
|
||
// Fetch the CSS content directly to avoid CORS issues with cssRules |
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 understand how we could get CORS issues. Everything comes from the same origin 🤔
But more security do not hurt
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
…/nextjs-pages-csslayers
…/nextjs-pages-csslayers
…aterial-ui into feat/nextjs-pages-csslayers
Docs: https://deploy-preview-45596--material-ui.netlify.app/material-ui/integrations/nextjs/#pages-router
closes #45220
part of #44700
Motivation
To unblock Tailwind CSS v4 integration. Only pages router is missing this option.
Changes for
@mui/material-nextjs
export
createEmotionCache
Add option
enableCssLayer
to pages routercreateEmotionCache
and reexport it.The logic of CSS layer wrapping is the same as App Router and StyledEngineProvider.
update SSR emotion styles
Required to make it work with Tailwind CSS v4 or other CSS layer implementation.
The setup requires a
GlobalStyles
component to configure the order of layers.This is the before:
Screen.Recording.2568-03-17.at.15.31.54.mov
In summary, the flicker occurs at hydration phase because Emotion hydrates at cache creation step.
Emotion hydrates only
<style data-emotion="mui *">
(see the file) which does not include global styles, causing the CSS layer order declaration to appear below component styles.Here is what happens chronologically:
So, at hydration phase, Tailwind base styles will override Material UI components causing the flicker.
GlobalStyles
renders and the layer order is moved the top of the component styles. ✅After updating the file, there is no flicker because the layer order is always at the top before any component/global styles.
Screen.Recording.2568-03-17.at.15.25.25.mov
Upgrade Tailwind CSS to v4
Upgrading Tailwind CSS to v4 to dogfood the API added to
@mui/material-nextjs
and to experience the impact on adopting CSS layer.By moving to CSS layer, I have to update the
useLazyCSS
to wrap the docsearch cdn CSS with@layer docsearch
too, otherwise it won't be overridden by Material UI styled.