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 3 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
31 changes: 29 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,24 @@ describe('[Material UI] ThemeProviderWithVars', () => {
borderBottomRightRadius: '16px',
});
});

it('show warning when using `setMode` without configuring `colorSchemeSelector`', () => {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
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 not configured.\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',
]);
});
});
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 @@ -241,7 +241,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 not configured.',
'To toggle the mode manually, please configure `colorSchemeSelector` to use a class or data attribute.',
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
'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 @@ -253,6 +267,7 @@ export default function createCssVarsProvider(options) {
setColorScheme,
setMode,
systemMode,
theme.colorSchemeSelector,
],
);

Expand Down
Loading