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] Add enableColorScheme option to getInitColorSchemeScript #33261

Merged
merged 1 commit into from Jun 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mui-system/src/cssVars/createCssVarsProvider.js
Expand Up @@ -305,6 +305,7 @@ export default function createCssVarsProvider(options) {
attribute: defaultAttribute,
colorSchemeStorageKey: defaultColorSchemeStorageKey,
modeStorageKey: defaultModeStorageKey,
enableColorScheme: designSystemEnableColorScheme,
...params,
});

Expand Down
12 changes: 12 additions & 0 deletions packages/mui-system/src/cssVars/getInitColorSchemeScript.tsx
Expand Up @@ -5,6 +5,11 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme';
export const DEFAULT_ATTRIBUTE = 'data-color-scheme';

export interface GetInitColorSchemeScriptOptions {
/**
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
* @default true
*/
enableColorScheme?: boolean;
/**
* If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
* @default false
Expand Down Expand Up @@ -42,6 +47,7 @@ export interface GetInitColorSchemeScriptOptions {

export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions) {
const {
enableColorScheme = true,
enableSystem = false,
defaultLightColorScheme = 'light',
defaultDarkColorScheme = 'dark',
Expand All @@ -56,13 +62,16 @@ export default function getInitColorSchemeScript(options?: GetInitColorSchemeScr
dangerouslySetInnerHTML={{
__html: `(function() { try {
var mode = localStorage.getItem('${modeStorageKey}');
var cssColorScheme = mode;
var colorScheme = '';
if (mode === 'system' || (!mode && !!${enableSystem})) {
// handle system mode
var mql = window.matchMedia('(prefers-color-scheme: dark)');
if (mql.matches) {
cssColorScheme = 'dark';
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
} else {
cssColorScheme = 'light';
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
}
}
Expand All @@ -75,6 +84,9 @@ export default function getInitColorSchemeScript(options?: GetInitColorSchemeScr
if (colorScheme) {
${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
}
if (${enableColorScheme} && !!cssColorScheme) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (${enableColorScheme} && !!cssColorScheme) {
if (enableColorScheme && !!cssColorScheme) {

?

Copy link
Member Author

Choose a reason for hiding this comment

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

if (${enableColorScheme} && !!cssColorScheme) { is correct because the whole function is in a literal template string.

Copy link
Member

Choose a reason for hiding this comment

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

Ah it is a variable outside of the __html string, makes sense 👍

${colorSchemeNode}.style.setProperty('color-scheme', cssColorScheme);
}
} catch (e) {} })();`,
}}
/>
Expand Down