@mui/system 5.0.5 → 5.2.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 (57) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +302 -9
  3. package/breakpoints.js +42 -9
  4. package/createBox.d.ts +5 -1
  5. package/createBox.js +5 -3
  6. package/createStyled.js +5 -1
  7. package/createTheme/createBreakpoints.js +2 -2
  8. package/createTheme/createSpacing.d.ts +10 -10
  9. package/cssVars/createCssVarsProvider.d.ts +88 -38
  10. package/cssVars/createCssVarsProvider.js +84 -64
  11. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  12. package/cssVars/cssVarsParser.d.ts +68 -57
  13. package/cssVars/cssVarsParser.js +41 -11
  14. package/cssVars/getInitColorSchemeScript.d.ts +12 -7
  15. package/cssVars/getInitColorSchemeScript.js +27 -5
  16. package/cssVars/index.d.ts +2 -2
  17. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  18. package/cssVars/useCurrentColorScheme.js +235 -0
  19. package/esm/breakpoints.js +40 -9
  20. package/esm/createBox.js +5 -3
  21. package/esm/createStyled.js +5 -1
  22. package/esm/createTheme/createBreakpoints.js +2 -2
  23. package/esm/cssVars/createCssVarsProvider.js +83 -66
  24. package/esm/cssVars/cssVarsParser.js +40 -11
  25. package/esm/cssVars/getInitColorSchemeScript.js +24 -3
  26. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  27. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  28. package/esm/styleFunctionSx/styleFunctionSx.js +47 -35
  29. package/index.js +1 -1
  30. package/index.spec.d.ts +1 -1
  31. package/legacy/breakpoints.js +40 -9
  32. package/legacy/createBox.js +6 -3
  33. package/legacy/createStyled.js +5 -1
  34. package/legacy/createTheme/createBreakpoints.js +2 -2
  35. package/legacy/cssVars/createCssVarsProvider.js +85 -73
  36. package/legacy/cssVars/cssVarsParser.js +37 -9
  37. package/legacy/cssVars/getInitColorSchemeScript.js +12 -4
  38. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  39. package/legacy/index.js +1 -1
  40. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  41. package/legacy/styleFunctionSx/styleFunctionSx.js +47 -35
  42. package/modern/breakpoints.js +40 -9
  43. package/modern/createBox.js +5 -3
  44. package/modern/createStyled.js +5 -1
  45. package/modern/createTheme/createBreakpoints.js +2 -2
  46. package/modern/cssVars/createCssVarsProvider.js +83 -66
  47. package/modern/cssVars/cssVarsParser.js +40 -11
  48. package/modern/cssVars/getInitColorSchemeScript.js +24 -3
  49. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  50. package/modern/index.js +1 -1
  51. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  52. package/modern/styleFunctionSx/styleFunctionSx.js +47 -35
  53. package/package.json +8 -8
  54. package/styleFunctionSx/extendSxProp.js +21 -1
  55. package/styleFunctionSx/styleFunctionSx.d.ts +2 -1
  56. package/styleFunctionSx/styleFunctionSx.js +49 -37
  57. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
@@ -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,16 +60,47 @@ 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], {
103
+ colorSchemes,
88
104
  vars: rootVars
89
105
  });
90
106
  const styleSheet = {};
@@ -93,15 +109,26 @@ export default function createCssVarsProvider(ThemeContext, options) {
93
109
  css,
94
110
  vars
95
111
  } = cssVarsParser(scheme, {
96
- prefix
112
+ prefix,
113
+ basePrefix: designSystemPrefix,
114
+ shouldSkipGeneratingVar
97
115
  });
116
+ mergedTheme.vars = deepmerge(mergedTheme.vars, vars);
98
117
 
99
- if (key === resolvedColorScheme) {
100
- mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
101
- }
118
+ const resolvedDefaultColorScheme = (() => {
119
+ if (typeof defaultColorScheme === 'string') {
120
+ return defaultColorScheme;
121
+ }
122
+
123
+ if (defaultMode === 'dark') {
124
+ return defaultColorScheme.dark;
125
+ }
102
126
 
103
- if (key === defaultColorScheme) {
104
- styleSheet[':root'] = deepmerge(rootCss, css);
127
+ return defaultColorScheme.light;
128
+ })();
129
+
130
+ if (key === resolvedDefaultColorScheme) {
131
+ styleSheet[':root'] = css;
105
132
  } else {
106
133
  styleSheet[`[${attribute}="${key}"]`] = css;
107
134
  }
@@ -109,41 +136,26 @@ export default function createCssVarsProvider(ThemeContext, options) {
109
136
  React.useEffect(() => {
110
137
  if (colorScheme) {
111
138
  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
139
  }
136
- }, [setColorScheme, allColorSchemes]);
140
+ }, [colorScheme, attribute]);
137
141
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
138
142
  value: {
143
+ mode,
144
+ setMode,
145
+ lightColorScheme,
146
+ darkColorScheme,
139
147
  colorScheme,
140
- setColorScheme: wrappedSetColorScheme,
148
+ setColorScheme,
141
149
  allColorSchemes
142
150
  },
143
151
  children: [/*#__PURE__*/_jsx(GlobalStyles, {
152
+ styles: {
153
+ ':root': rootCss
154
+ }
155
+ }), /*#__PURE__*/_jsx(GlobalStyles, {
144
156
  styles: styleSheet
145
- }), /*#__PURE__*/_jsx(ThemeContext.Provider, {
146
- value: mergedTheme,
157
+ }), /*#__PURE__*/_jsx(ThemeProvider, {
158
+ theme: mergedTheme,
147
159
  children: children
148
160
  })]
149
161
  });
@@ -163,17 +175,22 @@ export default function createCssVarsProvider(ThemeContext, options) {
163
175
  /**
164
176
  * The initial color scheme used.
165
177
  */
166
- defaultColorScheme: PropTypes.string,
178
+ defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
167
179
 
168
180
  /**
169
- * css variable prefix
181
+ * The initial mode used.
170
182
  */
171
- prefix: PropTypes.string,
183
+ defaultMode: PropTypes.string,
172
184
 
173
185
  /**
174
186
  * The key in the local storage used to store current color scheme.
175
187
  */
176
- storageKey: PropTypes.string,
188
+ modeStorageKey: PropTypes.string,
189
+
190
+ /**
191
+ * css variable prefix
192
+ */
193
+ prefix: PropTypes.string,
177
194
 
178
195
  /**
179
196
  * 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
+ enableSystem,
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 && !!${enableSystem})) {
22
+ // handle system mode
23
+ var mql = window.matchMedia('(prefers-color-scheme: dark)');
24
+ if (mql.matches) {
25
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
26
+ } else {
27
+ colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}';
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
  }
@@ -0,0 +1,217 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import * as React from 'react';
3
+ import { DEFAULT_MODE_STORAGE_KEY, DEFAULT_COLOR_SCHEME_STORAGE_KEY } from './getInitColorSchemeScript';
4
+ export function getSystemMode(mode) {
5
+ if (typeof window !== 'undefined' && mode === 'system') {
6
+ const mql = window.matchMedia('(prefers-color-scheme: dark)');
7
+
8
+ if (mql.matches) {
9
+ return 'dark';
10
+ }
11
+
12
+ return 'light';
13
+ }
14
+
15
+ return undefined;
16
+ }
17
+
18
+ function processState(state, callback) {
19
+ if (state.mode === 'light' || state.mode === 'system' && state.systemMode === 'light') {
20
+ return callback('light');
21
+ }
22
+
23
+ if (state.mode === 'dark' || state.mode === 'system' && state.systemMode === 'dark') {
24
+ return callback('dark');
25
+ }
26
+
27
+ return undefined;
28
+ }
29
+
30
+ export function getColorScheme(state) {
31
+ return processState(state, mode => {
32
+ if (mode === 'light') {
33
+ return state.lightColorScheme;
34
+ }
35
+
36
+ if (mode === 'dark') {
37
+ return state.darkColorScheme;
38
+ }
39
+
40
+ return undefined;
41
+ });
42
+ }
43
+
44
+ function resolveValue(key, defaultValue) {
45
+ if (typeof window === 'undefined') {
46
+ return undefined;
47
+ }
48
+
49
+ let value;
50
+
51
+ try {
52
+ value = localStorage.getItem(key) || undefined;
53
+ } catch (e) {// Unsupported
54
+ }
55
+
56
+ return value || defaultValue;
57
+ }
58
+
59
+ export default function useCurrentColorScheme(options) {
60
+ const {
61
+ defaultMode = 'light',
62
+ defaultLightColorScheme,
63
+ defaultDarkColorScheme,
64
+ supportedColorSchemes = [],
65
+ modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
66
+ colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY
67
+ } = options;
68
+ const joinedColorSchemes = supportedColorSchemes.join(',');
69
+ const [state, setState] = React.useState(() => {
70
+ const initialMode = resolveValue(modeStorageKey, defaultMode);
71
+ return {
72
+ mode: initialMode,
73
+ systemMode: getSystemMode(initialMode),
74
+ lightColorScheme: resolveValue(`${colorSchemeStorageKey}-light`) || defaultLightColorScheme,
75
+ darkColorScheme: resolveValue(`${colorSchemeStorageKey}-dark`) || defaultDarkColorScheme
76
+ };
77
+ });
78
+ const colorScheme = getColorScheme(state);
79
+ const setMode = React.useCallback(mode => {
80
+ setState(currentState => {
81
+ const newMode = !mode ? defaultMode : mode;
82
+
83
+ if (typeof localStorage !== 'undefined') {
84
+ localStorage.setItem(modeStorageKey, newMode);
85
+ }
86
+
87
+ return _extends({}, currentState, {
88
+ mode: newMode,
89
+ systemMode: getSystemMode(newMode)
90
+ });
91
+ });
92
+ }, [modeStorageKey, defaultMode]);
93
+ const setColorScheme = React.useCallback(value => {
94
+ if (!value || typeof value === 'string') {
95
+ if (value && !supportedColorSchemes.includes(value)) {
96
+ console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
97
+ } else {
98
+ setState(currentState => {
99
+ const newState = _extends({}, currentState);
100
+
101
+ if (!value) {
102
+ // reset to default color scheme
103
+ newState.lightColorScheme = defaultLightColorScheme;
104
+ newState.darkColorScheme = defaultDarkColorScheme;
105
+ return newState;
106
+ }
107
+
108
+ processState(currentState, mode => {
109
+ localStorage.setItem(`${colorSchemeStorageKey}-${mode}`, value);
110
+
111
+ if (mode === 'light') {
112
+ newState.lightColorScheme = value;
113
+ }
114
+
115
+ if (mode === 'dark') {
116
+ newState.darkColorScheme = value;
117
+ }
118
+ });
119
+ return newState;
120
+ });
121
+ }
122
+ } else if (value.light && !supportedColorSchemes.includes(value.light) || value.dark && !supportedColorSchemes.includes(value.dark)) {
123
+ console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
124
+ } else {
125
+ setState(currentState => {
126
+ const newState = _extends({}, currentState);
127
+
128
+ if (value.light || value.light === null) {
129
+ newState.lightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
130
+ }
131
+
132
+ if (value.dark || value.dark === null) {
133
+ newState.darkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
134
+ }
135
+
136
+ return newState;
137
+ });
138
+
139
+ if (value.light) {
140
+ localStorage.setItem(`${colorSchemeStorageKey}-light`, value.light);
141
+ }
142
+
143
+ if (value.dark) {
144
+ localStorage.setItem(`${colorSchemeStorageKey}-dark`, value.dark);
145
+ }
146
+ }
147
+ }, [colorSchemeStorageKey, supportedColorSchemes, defaultLightColorScheme, defaultDarkColorScheme]);
148
+ const handleMediaQuery = React.useCallback(e => {
149
+ if (state.mode === 'system') {
150
+ setState(currentState => _extends({}, currentState, {
151
+ systemMode: e.matches ? 'dark' : 'light'
152
+ }));
153
+ }
154
+ }, [state.mode]); // Ref hack to avoid adding handleMediaQuery as a dep
155
+
156
+ const mediaListener = React.useRef(handleMediaQuery);
157
+ mediaListener.current = handleMediaQuery;
158
+ React.useEffect(() => {
159
+ const handler = (...args) => mediaListener.current(...args); // Always listen to System preference
160
+
161
+
162
+ const media = window.matchMedia('(prefers-color-scheme: dark)'); // Intentionally use deprecated listener methods to support iOS & old browsers
163
+
164
+ media.addListener(handler);
165
+ handler(media);
166
+ return () => media.removeListener(handler);
167
+ }, []); // Save mode, lightColorScheme & darkColorScheme to localStorage
168
+
169
+ React.useEffect(() => {
170
+ if (state.mode) {
171
+ localStorage.setItem(modeStorageKey, state.mode);
172
+ }
173
+
174
+ processState(state, mode => {
175
+ if (mode === 'light') {
176
+ localStorage.setItem(`${colorSchemeStorageKey}-light`, state.lightColorScheme);
177
+ }
178
+
179
+ if (mode === 'dark') {
180
+ localStorage.setItem(`${colorSchemeStorageKey}-dark`, state.darkColorScheme);
181
+ }
182
+ });
183
+ }, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
184
+
185
+ React.useEffect(() => {
186
+ const handleStorage = event => {
187
+ const value = event.newValue;
188
+
189
+ if (typeof event.key === 'string' && event.key.startsWith(colorSchemeStorageKey) && (!value || joinedColorSchemes.match(value))) {
190
+ // If the key is deleted, value will be null then reset color scheme to the default one.
191
+ if (event.key.endsWith('light')) {
192
+ setColorScheme({
193
+ light: value
194
+ });
195
+ }
196
+
197
+ if (event.key.endsWith('dark')) {
198
+ setColorScheme({
199
+ dark: value
200
+ });
201
+ }
202
+ }
203
+
204
+ if (event.key === modeStorageKey && (!value || ['light', 'dark', 'system'].includes(value))) {
205
+ setMode(value || defaultMode);
206
+ }
207
+ };
208
+
209
+ window.addEventListener('storage', handleStorage);
210
+ return () => window.removeEventListener('storage', handleStorage);
211
+ }, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode]);
212
+ return _extends({}, state, {
213
+ colorScheme,
214
+ setMode,
215
+ setColorScheme
216
+ });
217
+ }
package/modern/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license MUI v5.0.5
1
+ /** @license MUI v5.2.0
2
2
  *
3
3
  * This source code is licensed under the MIT license found in the
4
4
  * LICENSE file in the root directory of this source tree.
@@ -1,6 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
3
  const _excluded = ["sx"];
4
+ import { isPlainObject } from '@mui/utils';
4
5
  import { propToStyleFunction } from '../getThemeValue';
5
6
 
6
7
  const splitProps = props => {
@@ -28,7 +29,25 @@ export default function extendSxProp(props) {
28
29
  systemProps,
29
30
  otherProps
30
31
  } = splitProps(other);
32
+ let finalSx;
33
+
34
+ if (Array.isArray(inSx)) {
35
+ finalSx = [systemProps, ...inSx];
36
+ } else if (typeof inSx === 'function') {
37
+ finalSx = (...args) => {
38
+ const result = inSx(...args);
39
+
40
+ if (!isPlainObject(result)) {
41
+ return systemProps;
42
+ }
43
+
44
+ return _extends({}, systemProps, result);
45
+ };
46
+ } else {
47
+ finalSx = _extends({}, systemProps, inSx);
48
+ }
49
+
31
50
  return _extends({}, otherProps, {
32
- sx: _extends({}, systemProps, inSx)
51
+ sx: finalSx
33
52
  });
34
53
  }