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] Replace enableSystem with defaultMode #33960

Merged
merged 2 commits into from
Sep 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@ const StyledComponent = styled('button')(({ theme }) => ({

**options**

- `enableSystem?: boolean`: - If `true` and the selected mode is not `light` or `dark`, the system mode is used
- `defaultMode?: 'light' | 'dark' | 'system'`: - Application's default mode before React renders the tree (`light` by default)
- `modeStorageKey?: string`: - localStorage key used to store application `mode`
- `attribute?: string` - DOM attribute for applying color scheme
20 changes: 15 additions & 5 deletions packages/mui-system/src/cssVars/getInitColorSchemeScript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,36 @@ describe('getInitColorSchemeScript', () => {
expect(document.documentElement.getAttribute(DEFAULT_ATTRIBUTE)).to.equal('bright');
});

describe('[option: `enableSystem`]', () => {
it('should set dark color scheme to body, given `enableSystem` is true and prefers-color-scheme is `dark`', () => {
it('defaultMode: `dark`', () => {
const { container } = render(
getInitColorSchemeScript({
defaultMode: 'dark',
}),
);
eval(container.firstChild.textContent);
expect(document.documentElement.getAttribute(DEFAULT_ATTRIBUTE)).to.equal('dark');
});

describe('defaultMode: `system`', () => {
it('should set dark color scheme to body, given prefers-color-scheme is `dark`', () => {
window.matchMedia = createMatchMedia(true);

const { container } = render(
getInitColorSchemeScript({
enableSystem: true,
defaultMode: 'system',
defaultDarkColorScheme: 'trueDark',
}),
);
eval(container.firstChild.textContent);
expect(document.documentElement.getAttribute(DEFAULT_ATTRIBUTE)).to.equal('trueDark');
});

it('should set light color scheme to body, given `enableSystem` is true and prefers-color-scheme is NOT `dark`', () => {
it('should set light color scheme to body, given prefers-color-scheme is NOT `dark`', () => {
window.matchMedia = createMatchMedia(false);

const { container } = render(
getInitColorSchemeScript({
enableSystem: true,
defaultMode: 'system',
defaultLightColorScheme: 'yellow',
}),
);
Expand Down
14 changes: 8 additions & 6 deletions packages/mui-system/src/cssVars/getInitColorSchemeScript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ export interface GetInitColorSchemeScriptOptions {
*/
enableColorScheme?: boolean;
/**
* If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
* @default false
* The mode to be used for the first visit
* @default 'light'
*/
enableSystem?: boolean;
defaultMode?: 'light' | 'dark' | 'system';
/**
* The default color scheme to be used on the light mode
* @default 'light'
*/
defaultLightColorScheme?: string;
/**
* The default color scheme to be used on the dark mode
* * @default 'dark'
*/
defaultDarkColorScheme?: string;
/**
Expand Down Expand Up @@ -48,7 +50,7 @@ export interface GetInitColorSchemeScriptOptions {
export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions) {
const {
enableColorScheme = true,
enableSystem = false,
defaultMode = 'light',
defaultLightColorScheme = 'light',
defaultDarkColorScheme = 'dark',
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
Expand All @@ -61,10 +63,10 @@ export default function getInitColorSchemeScript(options?: GetInitColorSchemeScr
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `(function() { try {
var mode = localStorage.getItem('${modeStorageKey}');
var mode = localStorage.getItem('${modeStorageKey}') || '${defaultMode}';
var cssColorScheme = mode;
var colorScheme = '';
if (mode === 'system' || (!mode && !!${enableSystem})) {
if (mode === 'system') {
// handle system mode
var mql = window.matchMedia('(prefers-color-scheme: dark)');
if (mql.matches) {
Expand Down