@mui/system 5.0.6 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/LICENSE +21 -21
  3. package/breakpoints.js +41 -8
  4. package/createBox.d.ts +5 -1
  5. package/createBox.js +5 -3
  6. package/createTheme/createBreakpoints.js +2 -2
  7. package/cssVars/createCssVarsProvider.d.ts +88 -38
  8. package/cssVars/createCssVarsProvider.js +83 -61
  9. package/cssVars/cssVarsParser.d.ts +14 -3
  10. package/cssVars/cssVarsParser.js +41 -11
  11. package/cssVars/getInitColorSchemeScript.d.ts +7 -2
  12. package/cssVars/getInitColorSchemeScript.js +27 -5
  13. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  14. package/cssVars/useCurrentColorScheme.js +235 -0
  15. package/esm/breakpoints.js +39 -8
  16. package/esm/createBox.js +5 -3
  17. package/esm/createTheme/createBreakpoints.js +2 -2
  18. package/esm/cssVars/createCssVarsProvider.js +82 -63
  19. package/esm/cssVars/cssVarsParser.js +40 -11
  20. package/esm/cssVars/getInitColorSchemeScript.js +24 -3
  21. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  22. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  23. package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
  24. package/index.js +1 -1
  25. package/legacy/breakpoints.js +39 -8
  26. package/legacy/createBox.js +6 -3
  27. package/legacy/createTheme/createBreakpoints.js +2 -2
  28. package/legacy/cssVars/createCssVarsProvider.js +83 -70
  29. package/legacy/cssVars/cssVarsParser.js +37 -9
  30. package/legacy/cssVars/getInitColorSchemeScript.js +13 -4
  31. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  32. package/legacy/index.js +1 -1
  33. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  34. package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
  35. package/modern/breakpoints.js +39 -8
  36. package/modern/createBox.js +5 -3
  37. package/modern/createTheme/createBreakpoints.js +2 -2
  38. package/modern/cssVars/createCssVarsProvider.js +82 -63
  39. package/modern/cssVars/cssVarsParser.js +40 -11
  40. package/modern/cssVars/getInitColorSchemeScript.js +24 -3
  41. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  42. package/modern/index.js +1 -1
  43. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  44. package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
  45. package/package.json +7 -7
  46. package/styleFunctionSx/extendSxProp.js +21 -1
  47. package/styleFunctionSx/styleFunctionSx.d.ts +2 -1
  48. package/styleFunctionSx/styleFunctionSx.js +46 -36
  49. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
@@ -24,53 +24,63 @@ function callIfFn(maybeFn, arg) {
24
24
 
25
25
  function styleFunctionSx(props) {
26
26
  var _ref = props || {},
27
- styles = _ref.sx,
27
+ sx = _ref.sx,
28
28
  _ref$theme = _ref.theme,
29
29
  theme = _ref$theme === void 0 ? {} : _ref$theme;
30
30
 
31
- if (!styles) {
32
- return null;
31
+ if (!sx) {
32
+ return null; // emotion & styled-components will neglect null
33
33
  }
34
+ /*
35
+ * Receive `sxInput` as object or callback
36
+ * and then recursively check keys & values to create media query object styles.
37
+ * (the result will be used in `styled`)
38
+ */
34
39
 
35
- var stylesObject = styles;
36
40
 
37
- if (typeof styles === 'function') {
38
- stylesObject = styles(theme);
39
- } else if (_typeof(styles) !== 'object') {
40
- // value
41
- return styles;
42
- }
41
+ function traverse(sxInput) {
42
+ var sxObject = sxInput;
43
43
 
44
- var emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
45
- var breakpointsKeys = Object.keys(emptyBreakpoints);
46
- var css = emptyBreakpoints;
47
- Object.keys(stylesObject).forEach(function (styleKey) {
48
- var value = callIfFn(stylesObject[styleKey], theme);
44
+ if (typeof sxInput === 'function') {
45
+ sxObject = sxInput(theme);
46
+ } else if (_typeof(sxInput) !== 'object') {
47
+ // value
48
+ return sxInput;
49
+ }
49
50
 
50
- if (_typeof(value) === 'object') {
51
- if (propToStyleFunction[styleKey]) {
52
- css = merge(css, getThemeValue(styleKey, value, theme));
53
- } else {
54
- var breakpointsValues = handleBreakpoints({
55
- theme: theme
56
- }, value, function (x) {
57
- return _defineProperty({}, styleKey, x);
58
- });
51
+ var emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
52
+ var breakpointsKeys = Object.keys(emptyBreakpoints);
53
+ var css = emptyBreakpoints;
54
+ Object.keys(sxObject).forEach(function (styleKey) {
55
+ var value = callIfFn(sxObject[styleKey], theme);
59
56
 
60
- if (objectsHaveSameKeys(breakpointsValues, value)) {
61
- css[styleKey] = styleFunctionSx({
62
- sx: value,
57
+ if (_typeof(value) === 'object') {
58
+ if (propToStyleFunction[styleKey]) {
59
+ css = merge(css, getThemeValue(styleKey, value, theme));
60
+ } else {
61
+ var breakpointsValues = handleBreakpoints({
63
62
  theme: theme
63
+ }, value, function (x) {
64
+ return _defineProperty({}, styleKey, x);
64
65
  });
65
- } else {
66
- css = merge(css, breakpointsValues);
66
+
67
+ if (objectsHaveSameKeys(breakpointsValues, value)) {
68
+ css[styleKey] = styleFunctionSx({
69
+ sx: value,
70
+ theme: theme
71
+ });
72
+ } else {
73
+ css = merge(css, breakpointsValues);
74
+ }
67
75
  }
76
+ } else {
77
+ css = merge(css, getThemeValue(styleKey, value, theme));
68
78
  }
69
- } else {
70
- css = merge(css, getThemeValue(styleKey, value, theme));
71
- }
72
- });
73
- return removeUnusedBreakpoints(breakpointsKeys, css);
79
+ });
80
+ return removeUnusedBreakpoints(breakpointsKeys, css);
81
+ }
82
+
83
+ return Array.isArray(sx) ? sx.map(traverse) : traverse(sx);
74
84
  }
75
85
 
76
86
  styleFunctionSx.filterProps = ['sx'];
@@ -8,12 +8,12 @@ export const values = {
8
8
  xs: 0,
9
9
  // phone
10
10
  sm: 600,
11
- // tablets
11
+ // tablet
12
12
  md: 900,
13
13
  // small laptop
14
14
  lg: 1200,
15
15
  // desktop
16
- xl: 1536 // large screens
16
+ xl: 1536 // large screen
17
17
 
18
18
  };
19
19
  const defaultBreakpoints = {
@@ -106,11 +106,41 @@ export function mergeBreakpointsInOrder(breakpointsInput, ...styles) {
106
106
  const emptyBreakpoints = createEmptyBreakpointObject(breakpointsInput);
107
107
  const mergedOutput = [emptyBreakpoints, ...styles].reduce((prev, next) => deepmerge(prev, next), {});
108
108
  return removeUnusedBreakpoints(Object.keys(emptyBreakpoints), mergedOutput);
109
+ } // compute base for responsive values; e.g.,
110
+ // [1,2,3] => {xs: true, sm: true, md: true}
111
+ // {xs: 1, sm: 2, md: 3} => {xs: true, sm: true, md: true}
112
+
113
+ export function computeBreakpointsBase(breakpointValues, themeBreakpoints) {
114
+ // fixed value
115
+ if (typeof breakpointValues !== 'object') {
116
+ return {};
117
+ }
118
+
119
+ const base = {};
120
+ const breakpointsKeys = Object.keys(themeBreakpoints);
121
+
122
+ if (Array.isArray(breakpointValues)) {
123
+ breakpointsKeys.forEach((breakpoint, i) => {
124
+ if (i < breakpointValues.length) {
125
+ base[breakpoint] = true;
126
+ }
127
+ });
128
+ } else {
129
+ breakpointsKeys.forEach(breakpoint => {
130
+ if (breakpointValues[breakpoint] != null) {
131
+ base[breakpoint] = true;
132
+ }
133
+ });
134
+ }
135
+
136
+ return base;
109
137
  }
110
138
  export function resolveBreakpointValues({
111
139
  values: breakpointValues,
112
- base
140
+ breakpoints: themeBreakpoints,
141
+ base: customBase
113
142
  }) {
143
+ const base = customBase || computeBreakpointsBase(breakpointValues, themeBreakpoints);
114
144
  const keys = Object.keys(base);
115
145
 
116
146
  if (keys.length === 0) {
@@ -118,14 +148,15 @@ export function resolveBreakpointValues({
118
148
  }
119
149
 
120
150
  let previous;
121
- return keys.reduce((acc, breakpoint) => {
122
- if (typeof breakpointValues === 'object') {
123
- acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous];
151
+ return keys.reduce((acc, breakpoint, i) => {
152
+ if (Array.isArray(breakpointValues)) {
153
+ acc[breakpoint] = breakpointValues[i] != null ? breakpointValues[i] : breakpointValues[previous];
154
+ previous = i;
124
155
  } else {
125
- acc[breakpoint] = breakpointValues;
156
+ acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous] || breakpointValues;
157
+ previous = breakpoint;
126
158
  }
127
159
 
128
- previous = breakpoint;
129
160
  return acc;
130
161
  }, {});
131
162
  }
@@ -10,7 +10,9 @@ import useTheme from './useTheme';
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
11
  export default function createBox(options = {}) {
12
12
  const {
13
- defaultTheme
13
+ defaultTheme,
14
+ defaultClassName = 'MuiBox-root',
15
+ generateClassName
14
16
  } = options;
15
17
  const BoxRoot = styled('div')(styleFunctionSx);
16
18
  const Box = /*#__PURE__*/React.forwardRef(function Box(inProps, ref) {
@@ -26,7 +28,7 @@ export default function createBox(options = {}) {
26
28
  return /*#__PURE__*/_jsx(BoxRoot, _extends({
27
29
  as: component,
28
30
  ref: ref,
29
- className: clsx(className, 'MuiBox-root'),
31
+ className: clsx(className, generateClassName ? generateClassName(defaultClassName) : defaultClassName),
30
32
  theme: theme
31
33
  }, other));
32
34
  });
@@ -52,7 +54,7 @@ export default function createBox(options = {}) {
52
54
  /**
53
55
  * @ignore
54
56
  */
55
- sx: PropTypes.object
57
+ sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
56
58
  } : void 0;
57
59
  return Box;
58
60
  }
@@ -13,12 +13,12 @@ export default function createBreakpoints(breakpoints) {
13
13
  xs: 0,
14
14
  // phone
15
15
  sm: 600,
16
- // tablets
16
+ // tablet
17
17
  md: 900,
18
18
  // small laptop
19
19
  lg: 1200,
20
20
  // desktop
21
- xl: 1536 // large screens
21
+ xl: 1536 // large screen
22
22
 
23
23
  },
24
24
  unit = 'px',
@@ -8,37 +8,21 @@ import PropTypes from 'prop-types';
8
8
  import { GlobalStyles } from '@mui/styled-engine';
9
9
  import { deepmerge } from '@mui/utils';
10
10
  import cssVarsParser from './cssVarsParser';
11
- import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_STORAGE_KEY } from './getInitColorSchemeScript';
11
+ import ThemeProvider from '../ThemeProvider';
12
+ import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
13
+ import useCurrentColorScheme from './useCurrentColorScheme';
12
14
  import { jsx as _jsx } from "react/jsx-runtime";
13
15
  import { jsxs as _jsxs } from "react/jsx-runtime";
14
-
15
- const resolveMode = (key, fallback, supportedColorSchemes) => {
16
- if (typeof window === 'undefined') {
17
- return undefined;
18
- }
19
-
20
- let value;
21
-
22
- try {
23
- value = localStorage.getItem(key) || undefined;
24
-
25
- if (!supportedColorSchemes.includes(value)) {
26
- value = undefined;
27
- }
28
- } catch (e) {// Unsupported
29
- }
30
-
31
- return value || fallback;
32
- };
33
-
34
- export default function createCssVarsProvider(ThemeContext, options) {
16
+ export default function createCssVarsProvider(options) {
35
17
  const {
36
18
  theme: baseTheme = {},
19
+ defaultMode: desisgnSystemMode = 'light',
37
20
  defaultColorScheme: designSystemColorScheme,
38
- prefix: designSystemPrefix = ''
21
+ prefix: designSystemPrefix = '',
22
+ shouldSkipGeneratingVar
39
23
  } = options;
40
24
 
41
- if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
25
+ if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.dark]) {
42
26
  console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
43
27
  }
44
28
 
@@ -58,8 +42,9 @@ export default function createCssVarsProvider(ThemeContext, options) {
58
42
  children,
59
43
  theme: themeProp = {},
60
44
  prefix = designSystemPrefix,
61
- storageKey = DEFAULT_STORAGE_KEY,
45
+ modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
62
46
  attribute = DEFAULT_ATTRIBUTE,
47
+ defaultMode = desisgnSystemMode,
63
48
  defaultColorScheme = designSystemColorScheme
64
49
  }) {
65
50
  const {
@@ -75,14 +60,44 @@ export default function createCssVarsProvider(ThemeContext, options) {
75
60
  let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
76
61
  const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
77
62
  const allColorSchemes = Object.keys(colorSchemes);
78
- const joinedColorSchemes = allColorSchemes.join(',');
79
- const [colorScheme, setColorScheme] = React.useState(() => resolveMode(storageKey, defaultColorScheme, allColorSchemes));
80
- const resolvedColorScheme = colorScheme || defaultColorScheme;
63
+ const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
64
+ const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
65
+ const {
66
+ mode,
67
+ setMode,
68
+ lightColorScheme,
69
+ darkColorScheme,
70
+ colorScheme,
71
+ setColorScheme
72
+ } = useCurrentColorScheme({
73
+ supportedColorSchemes: allColorSchemes,
74
+ defaultLightColorScheme,
75
+ defaultDarkColorScheme,
76
+ modeStorageKey,
77
+ defaultMode
78
+ });
79
+
80
+ const resolvedColorScheme = (() => {
81
+ if (!colorScheme) {
82
+ // This scope occurs on the server
83
+ if (defaultMode === 'dark') {
84
+ return defaultDarkColorScheme;
85
+ } // use light color scheme, if default mode is 'light' | 'auto'
86
+
87
+
88
+ return defaultLightColorScheme;
89
+ }
90
+
91
+ return colorScheme;
92
+ })();
93
+
81
94
  const {
82
95
  css: rootCss,
83
96
  vars: rootVars
84
97
  } = cssVarsParser(mergedTheme, {
85
- prefix
98
+ prefix,
99
+ basePrefix: designSystemPrefix,
100
+ shouldSkipGeneratingVar
86
101
  });
87
102
  mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
88
103
  vars: rootVars
@@ -93,15 +108,29 @@ export default function createCssVarsProvider(ThemeContext, options) {
93
108
  css,
94
109
  vars
95
110
  } = cssVarsParser(scheme, {
96
- prefix
111
+ prefix,
112
+ basePrefix: designSystemPrefix,
113
+ shouldSkipGeneratingVar
97
114
  });
98
115
 
99
116
  if (key === resolvedColorScheme) {
100
117
  mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
101
118
  }
102
119
 
103
- if (key === defaultColorScheme) {
104
- styleSheet[':root'] = deepmerge(rootCss, css);
120
+ const resolvedDefaultColorScheme = (() => {
121
+ if (typeof defaultColorScheme === 'string') {
122
+ return defaultColorScheme;
123
+ }
124
+
125
+ if (defaultMode === 'dark') {
126
+ return defaultColorScheme.dark;
127
+ }
128
+
129
+ return defaultColorScheme.light;
130
+ })();
131
+
132
+ if (key === resolvedDefaultColorScheme) {
133
+ styleSheet[':root'] = css;
105
134
  } else {
106
135
  styleSheet[`[${attribute}="${key}"]`] = css;
107
136
  }
@@ -109,41 +138,26 @@ export default function createCssVarsProvider(ThemeContext, options) {
109
138
  React.useEffect(() => {
110
139
  if (colorScheme) {
111
140
  document.body.setAttribute(attribute, colorScheme);
112
- localStorage.setItem(storageKey, colorScheme);
113
- }
114
- }, [colorScheme, attribute, storageKey]); // local storage modified in the context of another document
115
-
116
- React.useEffect(() => {
117
- const handleStorage = event => {
118
- const storageColorScheme = event.newValue;
119
-
120
- if (event.key === storageKey && joinedColorSchemes.match(storageColorScheme)) {
121
- if (storageColorScheme) {
122
- setColorScheme(storageColorScheme);
123
- }
124
- }
125
- };
126
-
127
- window.addEventListener('storage', handleStorage);
128
- return () => window.removeEventListener('storage', handleStorage);
129
- }, [setColorScheme, storageKey, joinedColorSchemes]);
130
- const wrappedSetColorScheme = React.useCallback(val => {
131
- if (typeof val === 'string' && !allColorSchemes.includes(val)) {
132
- console.error(`\`${val}\` does not exist in \`theme.colorSchemes\`.`);
133
- } else {
134
- setColorScheme(val);
135
141
  }
136
- }, [setColorScheme, allColorSchemes]);
142
+ }, [colorScheme, attribute]);
137
143
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
138
144
  value: {
145
+ mode,
146
+ setMode,
147
+ lightColorScheme,
148
+ darkColorScheme,
139
149
  colorScheme,
140
- setColorScheme: wrappedSetColorScheme,
150
+ setColorScheme,
141
151
  allColorSchemes
142
152
  },
143
153
  children: [/*#__PURE__*/_jsx(GlobalStyles, {
154
+ styles: {
155
+ ':root': rootCss
156
+ }
157
+ }), /*#__PURE__*/_jsx(GlobalStyles, {
144
158
  styles: styleSheet
145
- }), /*#__PURE__*/_jsx(ThemeContext.Provider, {
146
- value: mergedTheme,
159
+ }), /*#__PURE__*/_jsx(ThemeProvider, {
160
+ theme: mergedTheme,
147
161
  children: children
148
162
  })]
149
163
  });
@@ -163,17 +177,22 @@ export default function createCssVarsProvider(ThemeContext, options) {
163
177
  /**
164
178
  * The initial color scheme used.
165
179
  */
166
- defaultColorScheme: PropTypes.string,
180
+ defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
167
181
 
168
182
  /**
169
- * css variable prefix
183
+ * The initial mode used.
170
184
  */
171
- prefix: PropTypes.string,
185
+ defaultMode: PropTypes.string,
172
186
 
173
187
  /**
174
188
  * The key in the local storage used to store current color scheme.
175
189
  */
176
- storageKey: PropTypes.string,
190
+ modeStorageKey: PropTypes.string,
191
+
192
+ /**
193
+ * css variable prefix
194
+ */
195
+ prefix: PropTypes.string,
177
196
 
178
197
  /**
179
198
  * The calculated theme object that will be passed through context.
@@ -1,3 +1,5 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+
1
3
  /**
2
4
  * This function create an object from keys, value and then assign to target
3
5
  *
@@ -50,7 +52,7 @@ export const walkObjectDeep = (obj, callback) => {
50
52
  if (typeof value === 'object' && Object.keys(value).length > 0) {
51
53
  recurse(value, [...parentKeys, key]);
52
54
  } else {
53
- callback([...parentKeys, key], value);
55
+ callback([...parentKeys, key], value, object);
54
56
  }
55
57
  }
56
58
  });
@@ -75,7 +77,16 @@ const getCssValue = (keys, value) => {
75
77
  * a function that parse theme and return { css, vars }
76
78
  *
77
79
  * @param {Object} theme
78
- * @param {{ prefix?: string }} options
80
+ * @param {{
81
+ * prefix?: string,
82
+ * basePrefix?: string,
83
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
84
+ * }} options.
85
+ * `basePrefix`: defined by design system.
86
+ * `prefix`: defined by application
87
+ *
88
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
89
+ *
79
90
  * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
80
91
  *
81
92
  * @example
@@ -90,19 +101,37 @@ const getCssValue = (keys, value) => {
90
101
  */
91
102
 
92
103
 
93
- export default function cssVarsParser(obj, options) {
104
+ export default function cssVarsParser(theme, options) {
105
+ const clonedTheme = _extends({}, theme);
106
+
107
+ delete clonedTheme.vars; // remove 'vars' from the structure
108
+
94
109
  const {
95
- prefix
110
+ prefix,
111
+ basePrefix = '',
112
+ shouldSkipGeneratingVar
96
113
  } = options || {};
97
114
  const css = {};
98
115
  const vars = {};
99
- walkObjectDeep(obj, (keys, value) => {
100
- if (typeof value === 'string' || typeof value === 'number') {
101
- const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
102
- Object.assign(css, {
103
- [cssVar]: getCssValue(keys, value)
104
- });
105
- assignNestedKeys(vars, keys, `var(${cssVar})`);
116
+ walkObjectDeep(clonedTheme, (keys, val, scope) => {
117
+ if (typeof val === 'string' || typeof val === 'number') {
118
+ let value = val;
119
+
120
+ if (typeof value === 'string' && value.startsWith('var')) {
121
+ // replace the value of the `scope` object with the prefix or remove basePrefix from the value
122
+ value = prefix ? value.replace(basePrefix, prefix) : value.replace(`${basePrefix}-`, ''); // scope is the deepest object in the tree, keys is the theme path keys
123
+
124
+ scope[keys.slice(-1)[0]] = value;
125
+ }
126
+
127
+ if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
128
+ // only create css & var if `shouldSkipGeneratingVar` return false
129
+ const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
130
+ Object.assign(css, {
131
+ [cssVar]: getCssValue(keys, value)
132
+ });
133
+ assignNestedKeys(vars, keys, `var(${cssVar})`);
134
+ }
106
135
  }
107
136
  });
108
137
  return {
@@ -1,17 +1,38 @@
1
1
  import * as React from 'react';
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- export const DEFAULT_STORAGE_KEY = 'mui-color-scheme';
3
+ export const DEFAULT_MODE_STORAGE_KEY = 'mui-mode';
4
+ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
4
5
  export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
5
6
  export default function getInitColorSchemeScript(options) {
6
7
  const {
7
- storageKey = DEFAULT_STORAGE_KEY,
8
+ defaultMode = 'light',
9
+ defaultLightColorScheme = 'light',
10
+ defaultDarkColorScheme = 'dark',
11
+ modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
12
+ colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
8
13
  attribute = DEFAULT_ATTRIBUTE
9
14
  } = options || {};
10
15
  return /*#__PURE__*/_jsx("script", {
11
16
  // eslint-disable-next-line react/no-danger
12
17
  dangerouslySetInnerHTML: {
13
18
  __html: `(function() { try {
14
- var colorScheme = localStorage.getItem('${storageKey}');
19
+ var mode = localStorage.getItem('${modeStorageKey}');
20
+ var colorScheme = '';
21
+ if (mode === 'system' || (!mode && ${defaultMode} === 'system')) {
22
+ // handle system mode
23
+ var mql = window.matchMedia('(prefers-color-scheme: dark)');
24
+ if (mql.matches) {
25
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultLightColorScheme};
26
+ } else {
27
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultDarkColorScheme};
28
+ }
29
+ }
30
+ if (mode === 'light') {
31
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultLightColorScheme};
32
+ }
33
+ if (mode === 'dark') {
34
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultDarkColorScheme};
35
+ }
15
36
  if (colorScheme) {
16
37
  document.body.setAttribute('${attribute}', colorScheme);
17
38
  }