Skip to content

Commit

Permalink
[system] Pass the stylesheet directly to GlobalStyles (#43739)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Sep 17, 2024
1 parent f4a9848 commit 4852745
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
8 changes: 1 addition & 7 deletions packages/mui-system/src/cssVars/createCssVarsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,13 @@ export default function createCssVarsProvider(options) {

const element = (
<React.Fragment>
{shouldGenerateStyleSheet && (
<React.Fragment>
{(theme.generateStyleSheets?.() || []).map((styles, index) => (
<GlobalStyles key={index} styles={styles} />
))}
</React.Fragment>
)}
<ThemeProvider
themeId={scopedTheme ? themeId : undefined}
theme={resolveTheme ? resolveTheme(theme) : theme}
>
{children}
</ThemeProvider>
{shouldGenerateStyleSheet && <GlobalStyles styles={theme.generateStyleSheets?.() || []} />}
</React.Fragment>
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import {
ThemeProvider,
createTheme,
StyledEngineProvider,
useColorScheme,
} from '@mui/material/styles';
import Box from '@mui/material/Box';

const theme = createTheme({
colorSchemes: { dark: true },
cssVariables: { colorSchemeSelector: '.regression-inject-first-%s' },
});

function AutoDark() {
const { setMode } = useColorScheme();
React.useEffect(() => {
setMode('dark');
}, [setMode]);
return null;
}

export default function InjectFirstWithThemeVars() {
return (
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme} modeStorageKey="regression-inject-first">
<AutoDark />
<Box
sx={{
border: 2,
borderColor: 'divider',
backgroundColor: 'background.default',
width: 100,
height: 100,
m: 1,
}}
/>
</ThemeProvider>
</StyledEngineProvider>
);
}

0 comments on commit 4852745

Please sign in to comment.