Skip to content

[material-ui] Fix theme object changes between renders (@siriwatknp) #45874

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

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/mui-material/src/styles/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer } from '@mui/internal-test-utils';
import { createRenderer, renderHook } from '@mui/internal-test-utils';
import { ThemeProvider, createTheme, useColorScheme, useTheme } from '@mui/material/styles';
import Button from '@mui/material/Button';

Expand Down Expand Up @@ -46,6 +46,22 @@ describe('ThemeProvider', () => {
).not.toWarnDev();
});

it('theme should be stable between renders if created outside of component', () => {
const theme = createTheme();
const themeContext = renderHook(
() => {
return useTheme();
},
{
wrapper: ({ children }) => <ThemeProvider theme={theme}>{children}</ThemeProvider>,
},
);
const firstRender = themeContext.result.current;
themeContext.rerender();
const secondRender = themeContext.result.current;
expect(firstRender).to.equal(secondRender);
});

describe('light & dark', () => {
function ModeSwitcher() {
const { mode, setMode } = useColorScheme();
Expand Down
27 changes: 17 additions & 10 deletions packages/mui-material/src/styles/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,24 @@ export default function ThemeProvider<Theme = DefaultTheme>({
theme,
...props
}: ThemeProviderProps<Theme>) {
if (typeof theme === 'function') {
return <ThemeProviderNoVars theme={theme} {...props} />;
}
const muiTheme = (THEME_ID in theme ? theme[THEME_ID] : theme) as ThemeProviderProps['theme'];
if (!('colorSchemes' in muiTheme)) {
if (!('vars' in muiTheme)) {
// For non-CSS variables themes, set `vars` to null to prevent theme inheritance from the upper theme.
// The example use case is the docs demo that uses ThemeProvider to customize the theme while the upper theme is using CSS variables.
return <ThemeProviderNoVars theme={{ ...theme, vars: null }} {...props} />;
const noVarsTheme = React.useMemo(() => {
if (typeof theme === 'function') {
return theme;
}
const muiTheme = (THEME_ID in theme ? theme[THEME_ID] : theme) as ThemeProviderProps['theme'];
if (!('colorSchemes' in muiTheme)) {
if (!('vars' in muiTheme)) {
// For non-CSS variables themes, set `vars` to null to prevent theme inheritance from the upper theme.
// The example use case is the docs demo that uses ThemeProvider to customize the theme while the upper theme is using CSS variables.
return { ...theme, vars: null };
}
return theme;
}
return <ThemeProviderNoVars theme={theme} {...props} />;
return null;
}, [theme]);

if (noVarsTheme) {
return <ThemeProviderNoVars theme={noVarsTheme} {...props} />;
}
return <CssVarsProvider theme={theme as unknown as CssVarsTheme} {...props} />;
}
Loading