-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[MUI v6] Setting default mode flickers the light/dark theme at first render #43972
Comments
@aarongarciah do you know why this might be happening? |
Can you provide a code sample that you are using for the ThemeProvider? I tested locally with a fresh Next.js project and it works fine. // theme.js
const theme = createTheme({
cssVariables: {
colorSchemeSelector: "class",
},
colorSchemes: {
light: true,
dark: true,
},
typography: {
fontFamily: roboto.style.fontFamily,
},
});
// layout.tsx
export default function RootLayout(props: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<AppRouterCacheProvider options={{ enableCssLayer: true }}>
<ThemeProvider defaultMode="light" theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
{props.children}
</ThemeProvider>
</AppRouterCacheProvider>
</body>
</html>
);
} |
Sure, I think you're missing the Here is the 'use client';
import {memo, ReactNode} from "react";
import {Roboto} from "next/font/google";
import {createTheme, ThemeProvider as MuiThemeProvider} from "@mui/material/styles";
import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';
import CssBaseline from "@mui/material/CssBaseline";
import ColorModeContextProvider from "@/theme/ColorModeContext";
import {AppRouterCacheProvider} from "@mui/material-nextjs/v14-appRouter";
const font = Roboto({weight: ['300', '400', '500', '700'], subsets: ['latin']});
export const theme = createTheme({
cssVariables: {colorSchemeSelector: 'class'},
defaultColorScheme: 'light',
typography: {
fontFamily: font.style.fontFamily,
h1: {fontSize: 42, fontWeight: 'bold'},
h2: {fontSize: 30},
h3: {fontSize: 26},
h4: {fontSize: 20},
h5: {fontSize: 18},
h6: {fontSize: 16},
},
colorSchemes: {
light: {
palette: {
background: {default: '#ffffff', paper: '#ffffff'},
primary: {main: '#212b36'},
secondary: {main: '#fa541c'},
info: {main: '#f44336'},
success: {main: '#4caf50'},
error: {main: '#f44336'},
// @ts-ignore
console: {main: '#e0e0e0'},
secondaryAction: {main: '#000000'},
neutral: {main: '#ffffff', contrastText: '#212121'},
unavailable: {main: '#363636'},
},
},
dark: {
palette: {
background: {default: '#171717', paper: '#212121'},
primary: {main: '#ffffff'},
secondary: {main: '#fa541c'},
info: {main: '#f44336'},
success: {main: '#388e3c'},
error: {main: '#d32f2f'},
// @ts-ignore
console: {main: '#212121'},
secondaryAction: {main: '#fff'},
neutral: {main: '#212121', contrastText: '#fff'},
unavailable: {main: '#797979'},
},
},
},
});
function ThemeProvider({children}: {children: ReactNode}) {
return <>
<AppRouterCacheProvider options={{ enableCssLayer: true }}>
<InitColorSchemeScript modeStorageKey="theme-mode" attribute="class" />
<MuiThemeProvider modeStorageKey="theme-mode" defaultMode="light" theme={theme}>
<ColorModeContextProvider>
<CssBaseline />
{children}
</ColorModeContextProvider>
</MuiThemeProvider>
</AppRouterCacheProvider>
</>
}
export default memo(ThemeProvider); And here is the import {createContext, memo, ReactNode} from "react";
import {useColorScheme} from "@mui/material/styles";
export type PaletteMode = 'light' | 'dark' | 'system';
interface ColorModeContextProps {
mode: PaletteMode;
setMode: (mode: PaletteMode) => void;
}
export const ColorModeContext = createContext<ColorModeContextProps>({
mode: 'light',
setMode: (_: PaletteMode) => {},
});
function ColorModeContextProvider({children}: {children: ReactNode}) {
const {mode, setMode} = useColorScheme();
return <ColorModeContext.Provider value={{mode: mode ?? 'light', setMode: setMode}}>
{children}
</ColorModeContext.Provider>
}
export default memo(ColorModeContextProvider); When I open the app in incognito mode (fresh window every time), it opens up in |
Gotcha, you are right. The problem is the I will open a PR to fix this. You will need to provide the |
Sounds good, I think it would be pretty straightforward from the users' perspective (I assume that in most projects |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @MartinXPN How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
Steps to reproduce
Link to the initial issue: #43622
Steps:
light
dark
mode and then changes tolight
, causing flickeringMake sure to re-open the page in incognito mode or change the
modeStorageKey
. Otherwise, the key will be stored and you won't notice any flickering.Here is a deployed example: https://profound.academy
Current behavior
If your system default mode is
dark
, and you set your website default theme to belight
, the very first render isdark
, which then switches tolight
, causing flickering. That's pretty unpleasant for the visitors.Expected behavior
It should render using the
defaultMode
right away without any flickering.Context
No response
Your environment
npx @mui/envinfo
Using Chrome
Search keywords: default mode, flickering, MUI v6
The text was updated successfully, but these errors were encountered: