Skip to content

Commit

Permalink
add InitColorSchemeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed May 15, 2024
1 parent 3c888ed commit 82d7240
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
47 changes: 23 additions & 24 deletions packages/mui-material/src/styles/CssVarsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,34 @@ import THEME_ID from './identifier';

const defaultTheme = experimental_extendTheme();

const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssVarsProvider<
SupportedColorScheme,
typeof THEME_ID
>({
themeId: THEME_ID,
theme: defaultTheme,
attribute: 'data-mui-color-scheme',
modeStorageKey: 'mui-mode',
colorSchemeStorageKey: 'mui-color-scheme',
defaultColorScheme: {
light: 'light',
dark: 'dark',
},
resolveTheme: (theme) => {
const newTheme = {
...theme,
typography: createTypography(theme.palette, theme.typography),
};
const { CssVarsProvider, useColorScheme, getInitColorSchemeScript, InitColorSchemeScript } =
createCssVarsProvider<SupportedColorScheme, typeof THEME_ID>({
themeId: THEME_ID,
theme: defaultTheme,
attribute: 'data-mui-color-scheme',
modeStorageKey: 'mui-mode',
colorSchemeStorageKey: 'mui-color-scheme',
defaultColorScheme: {
light: 'light',
dark: 'dark',
},
resolveTheme: (theme) => {
const newTheme = {
...theme,
typography: createTypography(theme.palette, theme.typography),
};

newTheme.unstable_sx = function sx(props: SxProps<CssVarsTheme>) {
return styleFunctionSx({ sx: props, theme: this });
};
newTheme.unstable_sx = function sx(props: SxProps<CssVarsTheme>) {
return styleFunctionSx({ sx: props, theme: this });
};

return newTheme;
},
});
return newTheme;
},
});

export {
useColorScheme,
getInitColorSchemeScript,
CssVarsProvider as Experimental_CssVarsProvider,
InitColorSchemeScript,
};
3 changes: 3 additions & 0 deletions packages/mui-system/src/cssVars/createCssVarsProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export interface CreateCssVarsProviderResult<
) => React.ReactElement;
useColorScheme: () => ColorSchemeContextValue<ColorScheme>;
getInitColorSchemeScript: typeof getInitColorSchemeScript;
InitColorSchemeScript: (
props: Parameters<typeof getInitColorSchemeScript>[0],
) => React.ReactElement;
}

export default function createCssVarsProvider<
Expand Down
6 changes: 5 additions & 1 deletion packages/mui-system/src/cssVars/createCssVarsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,9 @@ export default function createCssVarsProvider(options) {
...params,
});

return { CssVarsProvider, useColorScheme, getInitColorSchemeScript };
const InitColorSchemeScript = React.memo(function InitColorSchemeScript(props) {
return getInitColorSchemeScript(props);
});

return { CssVarsProvider, useColorScheme, getInitColorSchemeScript, InitColorSchemeScript };
}
5 changes: 5 additions & 0 deletions packages/mui-system/src/cssVars/getInitColorSchemeScript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface GetInitColorSchemeScriptOptions {
* @default 'data-color-scheme'
*/
attribute?: string;
/** Nonce string to pass to the inline script for CSP headers */
nonce?: string | undefined;
}

export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions) {
Expand All @@ -51,10 +53,13 @@ export default function getInitColorSchemeScript(options?: GetInitColorSchemeScr
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
attribute = DEFAULT_ATTRIBUTE,
colorSchemeNode = 'document.documentElement',
nonce,
} = options || {};
return (
<script
key="mui-color-scheme-init"
suppressHydrationWarning
nonce={typeof window === 'undefined' ? nonce : ''}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `(function() {
Expand Down

0 comments on commit 82d7240

Please sign in to comment.