Skip to content
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-ui] Add warning when using noSsr incorrectly #44624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions packages/mui-material/src/styles/ThemeProviderNoVars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ export interface ThemeProviderNoVarsProps<Theme = DefaultTheme> {
theme: Partial<Theme> | ((outerTheme: Theme) => Theme);
}

let warnedOnceNoSsr = false;

export default function ThemeProviderNoVars<Theme = DefaultTheme>({
theme: themeInput,
...props
}: ThemeProviderNoVarsProps<Theme>): React.ReactElement<ThemeProviderNoVarsProps<Theme>> {
const scopedTheme = THEME_ID in themeInput ? themeInput[THEME_ID] : undefined;
if (process.env.NODE_ENV !== 'production') {
Copy link
Member

@oliviertassinari oliviertassinari Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to run this test inside ThemeProvider.tsx rather than in this file? It seems that it could be easier to refactor down the line.

// @ts-ignore
if (process.env.NODE_ENV !== 'test' && props.noSsr && !warnedOnceNoSsr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test env check seems suspicious. Do we have leaks in our own tests?

Suggested change
if (process.env.NODE_ENV !== 'test' && props.noSsr && !warnedOnceNoSsr) {
if (props.noSsr && !warnedOnceNoSsr) {

console.error(
[
'MUI: The `noSsr` prop must be used with the theme that contains light and dark colorSchemes.',
'See https://mui.com/material-ui/customization/dark-mode/#built-in-support for more details.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback as in #43783 (comment).

].join('\n'),
);
warnedOnceNoSsr = true;
}
}
return (
<SystemThemeProvider
{...props}
Expand Down