Skip to content

Conversation

sina-byn
Copy link

@sina-byn sina-byn commented Jul 28, 2025

Working on a Next.js app router and MUI project using tailwindcss for customizations. I found that when changing to RTL the rtl cache provider does not wrap CSS with @layer mui even though enableCssLayer is set to true. This ruins the tailwindcss integration because the layer ordering is not properly applied. So based on these two issues #44700 - emotion-js/emotion#3134 (comment). I refactored the mui-stylis-plugin-rtl to export a plugin factory that accepts an optinal layer name. If provided the output CSS will be wrapped with an @layer <layer-name> and if not the output will be as is.

If this pull request is merged I'm willing to update both tests and the documentation website ASAP, which isn't possible for now since there's a major change in tests which happen to use the installed version of the plugin, so updating tests isn't possible before updating the package.

import { compile, middleware, serialize, stringify } from "stylis";

import { stylisRTLPluginFactory } from "./plugin"; // * local diretory

const css = serialize(
  compile(`
    h1{all:unset}

    @media (width > 768px) {
      h2 { display: none }
    }
    `),
  middleware([stylisRTLPluginFactory('mui'), stringify])
);

console.log(css);

@mui-bot
Copy link

mui-bot commented Jul 28, 2025

Netlify deploy preview

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

Bundle size report

ℹ️ Using snapshot from parent commit 499f347 (fallback from merge base cf8e5ab).

Bundle Parsed Size Gzip Size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 883edfa

@zannager zannager added the scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI label Jul 28, 2025
@zannager zannager requested a review from siriwatknp July 28, 2025 10:24
@siriwatknp
Copy link
Member

siriwatknp commented Aug 13, 2025

Thanks for submitting the PR but there is no need to create a factory function.
I assume that you have followed this guide for AppRouter + CSS layer setup.

The last piece to add RTL support is:

import { AppRouterCacheProvider } from '@mui/material-nextjs/v15-appRouter';
import { prefixer } from 'stylis';
import rtlPlugin from '@mui/stylis-plugin-rtl'

export default function RootLayout() {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <AppRouterCacheProvider options={{ enableCssLayer: true, stylisPlugins: [prefixer, rtlPlugin] }}>
          {/* Your app */}
        </AppRouterCacheProvider>
      </body>
    </html>
  );
}

Here is a preview https://stackblitz.com/edit/github-z8dxvj7v?file=src%2Fapp%2Fpage.tsx

Please close the PR if it works on your side.

@sina-byn
Copy link
Author

sina-byn commented Aug 14, 2025

In this example, you are correct — I should have provided more context. I ran into issues setting up StackBlitz, so I am providing a GitHub repository instead.

Github Repository: https://github.com/sina-byn/next-mui-boilerplate

The problem occurs when there are two languages:

  • One RTL

  • One LTR

When switching between the two, the styles applied by Tailwind to a link are not applied in RTL.

After inspecting, I found that the mui layer is not applied at this stage. From what I’ve read, this issue previously existed before Tailwind CSS support became stable.

What I Tried / Proposed Solution
I implemented a fix and applied it to the RTL plugin.

With this update:

When supporting both directions (LTR & RTL), you should add the factoryFn.

When supporting only one direction, the current implementation remains sufficient.

The problem was originally reported by you, and I found my solution from this comment:
emotion-js/emotion#3134 (comment)

Related Issues

Original issue: #44700

@siriwatknp @zannager

@sina-byn
Copy link
Author

sina-byn commented Sep 1, 2025

I'm still waiting for your feedback. Would be glad if you checked it.

@siriwatknp @zannager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants