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

[system] Warn when calling setMode without configuring colorSchemeSelector #43783

Merged
merged 8 commits into from
Nov 15, 2024
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
52 changes: 50 additions & 2 deletions packages/mui-material/src/styles/ThemeProviderWithVars.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, screen } from '@mui/internal-test-utils';
import { createRenderer, screen, fireEvent } from '@mui/internal-test-utils';
import Box from '@mui/material/Box';
import { CssVarsProvider, extendTheme, useTheme } from '@mui/material/styles';
import {
CssVarsProvider,
extendTheme,
useTheme,
ThemeProvider,
createTheme,
useColorScheme,
} from '@mui/material/styles';

describe('[Material UI] ThemeProviderWithVars', () => {
let originalMatchmedia;
Expand Down Expand Up @@ -360,4 +367,45 @@ describe('[Material UI] ThemeProviderWithVars', () => {
borderBottomRightRadius: '16px',
});
});

it('warns when using `setMode` without configuring `colorSchemeSelector`', () => {
function Test() {
const { setMode } = useColorScheme();
return <button onClick={() => setMode('dark')}>Dark</button>;
}
render(
<ThemeProvider
theme={createTheme({ cssVariables: true, colorSchemes: { light: true, dark: true } })}
>
<Test />
</ThemeProvider>,
);

expect(() => {
fireEvent.click(screen.getByText('Dark'));
}).toErrorDev([
'MUI: The `setMode` function has no effect if `colorSchemeSelector` is `media` (`media` is the default value).\nTo toggle the mode manually, please configure `colorSchemeSelector` to use a class or data attribute.\nTo learn more, visit https://mui.com/material-ui/customization/css-theme-variables/configuration/#toggling-dark-mode-manually',
]);
});

it('do not warn when using `setMode` with `colorSchemeSelector` that is not `media`', () => {
function Test() {
const { setMode } = useColorScheme();
return <button onClick={() => setMode('dark')}>Dark</button>;
}
render(
<ThemeProvider
theme={createTheme({
cssVariables: { colorSchemeSelector: 'class' },
colorSchemes: { light: true, dark: true },
})}
>
<Test />
</ThemeProvider>,
);

expect(() => {
fireEvent.click(screen.getByText('Dark'));
}).not.toErrorDev();
});
});
17 changes: 16 additions & 1 deletion packages/mui-system/src/cssVars/createCssVarsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,21 @@ export default function createCssVarsProvider(options) {
lightColorScheme,
mode,
setColorScheme,
setMode,
setMode:
process.env.NODE_ENV === 'production'
? setMode
: (newMode) => {
if (theme.colorSchemeSelector === 'media') {
console.error(
[
'MUI: The `setMode` function has no effect if `colorSchemeSelector` is `media` (`media` is the default value).',
Copy link
Member

Choose a reason for hiding this comment

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

  • () To match with how we reference functions in the docs?
  • if -> when. If could create doubt about either this is true or not in the case the warning triggers?
Suggested change
'MUI: The `setMode` function has no effect if `colorSchemeSelector` is `media` (`media` is the default value).',
'MUI: The `setMode()` function has no effect when `colorSchemeSelector` is `media` (`media` is the default value).',

Rule of error messages: https://www.notion.so/mui-org/Technical-writing-guidance-7e55b517ac2e489a9ddb6d0f6dd765de?pvs=4#85219822c3194b6f8a49ee08ea82b90a.

'To toggle the mode manually, please configure `colorSchemeSelector` to use a class or data attribute.',
'To learn more, visit https://mui.com/material-ui/customization/css-theme-variables/configuration/#toggling-dark-mode-manually',
Copy link
Member

Choose a reason for hiding this comment

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

How about we enforce all of those links to be permalink? mui/mui-public#222

Suggested change
'To learn more, visit https://mui.com/material-ui/customization/css-theme-variables/configuration/#toggling-dark-mode-manually',
'To learn more, visit https://mui.com/r/set-mode-media',

Especially a hash link, it seems too brittle.

].join('\n'),
);
}
setMode(newMode);
},
systemMode,
}),
[
Expand All @@ -254,6 +268,7 @@ export default function createCssVarsProvider(options) {
setColorScheme,
setMode,
systemMode,
theme.colorSchemeSelector,
],
);

Expand Down
Loading