@mui/system 5.0.3 → 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 (77) hide show
  1. package/CHANGELOG.md +274 -0
  2. package/borders.js +1 -1
  3. package/breakpoints.js +45 -11
  4. package/colorManipulator.js +8 -8
  5. package/createBox.d.ts +5 -1
  6. package/createBox.js +5 -3
  7. package/createStyled.d.ts +16 -190
  8. package/createStyled.js +6 -2
  9. package/createTheme/createBreakpoints.d.ts +53 -6
  10. package/createTheme/createBreakpoints.js +3 -3
  11. package/cssVars/createCssVarsProvider.d.ts +131 -0
  12. package/cssVars/createCssVarsProvider.js +228 -0
  13. package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
  14. package/cssVars/cssVarsParser.d.ts +68 -0
  15. package/cssVars/cssVarsParser.js +156 -0
  16. package/cssVars/getInitColorSchemeScript.d.ts +12 -0
  17. package/cssVars/getInitColorSchemeScript.js +60 -0
  18. package/cssVars/index.d.ts +2 -0
  19. package/cssVars/index.js +15 -0
  20. package/cssVars/package.json +6 -0
  21. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  22. package/cssVars/useCurrentColorScheme.js +235 -0
  23. package/display.js +1 -1
  24. package/esm/breakpoints.js +39 -8
  25. package/esm/createBox.js +5 -3
  26. package/esm/createStyled.js +5 -1
  27. package/esm/createTheme/createBreakpoints.js +2 -2
  28. package/esm/cssVars/createCssVarsProvider.js +207 -0
  29. package/esm/cssVars/cssVarsParser.js +141 -0
  30. package/esm/cssVars/getInitColorSchemeScript.js +42 -0
  31. package/esm/cssVars/index.js +1 -0
  32. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  33. package/esm/index.js +2 -1
  34. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  35. package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
  36. package/flexbox.js +1 -1
  37. package/getThemeValue.js +1 -1
  38. package/grid.js +1 -1
  39. package/index.d.ts +6 -0
  40. package/index.js +77 -68
  41. package/legacy/breakpoints.js +39 -8
  42. package/legacy/createBox.js +6 -3
  43. package/legacy/createStyled.js +5 -1
  44. package/legacy/createTheme/createBreakpoints.js +2 -2
  45. package/legacy/cssVars/createCssVarsProvider.js +215 -0
  46. package/legacy/cssVars/cssVarsParser.js +153 -0
  47. package/legacy/cssVars/getInitColorSchemeScript.js +27 -0
  48. package/legacy/cssVars/index.js +1 -0
  49. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  50. package/legacy/index.js +3 -2
  51. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  52. package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
  53. package/modern/breakpoints.js +39 -8
  54. package/modern/createBox.js +5 -3
  55. package/modern/createStyled.js +5 -1
  56. package/modern/createTheme/createBreakpoints.js +2 -2
  57. package/modern/cssVars/createCssVarsProvider.js +207 -0
  58. package/modern/cssVars/cssVarsParser.js +141 -0
  59. package/modern/cssVars/getInitColorSchemeScript.js +42 -0
  60. package/modern/cssVars/index.js +1 -0
  61. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  62. package/modern/index.js +3 -2
  63. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  64. package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
  65. package/package.json +8 -8
  66. package/palette.js +1 -1
  67. package/positions.js +1 -1
  68. package/sizing.js +1 -1
  69. package/spacing.js +3 -3
  70. package/style.d.ts +2 -2
  71. package/style.js +1 -1
  72. package/styleFunctionSx/extendSxProp.js +21 -1
  73. package/styleFunctionSx/styleFunctionSx.d.ts +7 -1
  74. package/styleFunctionSx/styleFunctionSx.js +46 -36
  75. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
  76. package/typography.js +1 -1
  77. package/useTheme.js +1 -1
@@ -0,0 +1,153 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
5
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
6
+
7
+ /**
8
+ * This function create an object from keys, value and then assign to target
9
+ *
10
+ * @param {Object} obj : the target object to be assigned
11
+ * @param {string[]} keys
12
+ * @param {string | number} value
13
+ *
14
+ * @example
15
+ * const source = {}
16
+ * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
17
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
18
+ *
19
+ * @example
20
+ * const source = { palette: { primary: 'var(--palette-primary)' } }
21
+ * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
22
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
23
+ */
24
+ export var assignNestedKeys = function assignNestedKeys(obj, keys, value) {
25
+ var temp = obj;
26
+ keys.forEach(function (k, index) {
27
+ if (index === keys.length - 1) {
28
+ if (temp && _typeof(temp) === 'object') {
29
+ temp[k] = value;
30
+ }
31
+ } else if (temp && _typeof(temp) === 'object') {
32
+ if (!temp[k]) {
33
+ temp[k] = {};
34
+ }
35
+
36
+ temp = temp[k];
37
+ }
38
+ });
39
+ };
40
+ /**
41
+ *
42
+ * @param {Object} obj : source object
43
+ * @param {Function} callback : a function that will be called when
44
+ * - the deepest key in source object is reached
45
+ * - the value of the deepest key is NOT `undefined` | `null`
46
+ *
47
+ * @example
48
+ * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
49
+ * // ['palette', 'primary', 'main'] '#000000'
50
+ */
51
+
52
+ export var walkObjectDeep = function walkObjectDeep(obj, callback) {
53
+ function recurse(object) {
54
+ var parentKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
55
+ Object.entries(object).forEach(function (_ref) {
56
+ var _ref2 = _slicedToArray(_ref, 2),
57
+ key = _ref2[0],
58
+ value = _ref2[1];
59
+
60
+ if (value !== undefined && value !== null) {
61
+ if (_typeof(value) === 'object' && Object.keys(value).length > 0) {
62
+ recurse(value, [].concat(_toConsumableArray(parentKeys), [key]));
63
+ } else {
64
+ callback([].concat(_toConsumableArray(parentKeys), [key]), value, object);
65
+ }
66
+ }
67
+ });
68
+ }
69
+
70
+ recurse(obj);
71
+ };
72
+
73
+ var getCssValue = function getCssValue(keys, value) {
74
+ if (typeof value === 'number') {
75
+ if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(function (prop) {
76
+ return keys.includes(prop);
77
+ })) {
78
+ // css property that are unitless
79
+ return value;
80
+ }
81
+
82
+ return "".concat(value, "px");
83
+ }
84
+
85
+ return value;
86
+ };
87
+ /**
88
+ * a function that parse theme and return { css, vars }
89
+ *
90
+ * @param {Object} theme
91
+ * @param {{
92
+ * prefix?: string,
93
+ * basePrefix?: string,
94
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
95
+ * }} options.
96
+ * `basePrefix`: defined by design system.
97
+ * `prefix`: defined by application
98
+ *
99
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
100
+ *
101
+ * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
102
+ *
103
+ * @example
104
+ * const { css, vars } = parser({
105
+ * fontSize: 12,
106
+ * lineHeight: 1.2,
107
+ * palette: { primary: { 500: '#000000' } }
108
+ * })
109
+ *
110
+ * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
111
+ * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
112
+ */
113
+
114
+
115
+ export default function cssVarsParser(theme, options) {
116
+ var clonedTheme = _extends({}, theme);
117
+
118
+ delete clonedTheme.vars; // remove 'vars' from the structure
119
+
120
+ var _ref3 = options || {},
121
+ prefix = _ref3.prefix,
122
+ _ref3$basePrefix = _ref3.basePrefix,
123
+ basePrefix = _ref3$basePrefix === void 0 ? '' : _ref3$basePrefix,
124
+ shouldSkipGeneratingVar = _ref3.shouldSkipGeneratingVar;
125
+
126
+ var css = {};
127
+ var vars = {};
128
+ walkObjectDeep(clonedTheme, function (keys, val, scope) {
129
+ if (typeof val === 'string' || typeof val === 'number') {
130
+ var _value = val;
131
+
132
+ if (typeof _value === 'string' && _value.startsWith('var')) {
133
+ // replace the value of the `scope` object with the prefix or remove basePrefix from the value
134
+ _value = prefix ? _value.replace(basePrefix, prefix) : _value.replace("".concat(basePrefix, "-"), ''); // scope is the deepest object in the tree, keys is the theme path keys
135
+
136
+ scope[keys.slice(-1)[0]] = _value;
137
+ }
138
+
139
+ if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, _value)) {
140
+ // only create css & var if `shouldSkipGeneratingVar` return false
141
+ var cssVar = "--".concat(prefix ? "".concat(prefix, "-") : '').concat(keys.join('-'));
142
+
143
+ _extends(css, _defineProperty({}, cssVar, getCssValue(keys, _value)));
144
+
145
+ assignNestedKeys(vars, keys, "var(".concat(cssVar, ")"));
146
+ }
147
+ }
148
+ });
149
+ return {
150
+ css: css,
151
+ vars: vars
152
+ };
153
+ }
@@ -0,0 +1,27 @@
1
+ import * as React from 'react';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ export var DEFAULT_MODE_STORAGE_KEY = 'mui-mode';
4
+ export var DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
5
+ export var DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
6
+ export default function getInitColorSchemeScript(options) {
7
+ var _ref = options || {},
8
+ _ref$defaultMode = _ref.defaultMode,
9
+ defaultMode = _ref$defaultMode === void 0 ? 'light' : _ref$defaultMode,
10
+ _ref$defaultLightColo = _ref.defaultLightColorScheme,
11
+ defaultLightColorScheme = _ref$defaultLightColo === void 0 ? 'light' : _ref$defaultLightColo,
12
+ _ref$defaultDarkColor = _ref.defaultDarkColorScheme,
13
+ defaultDarkColorScheme = _ref$defaultDarkColor === void 0 ? 'dark' : _ref$defaultDarkColor,
14
+ _ref$modeStorageKey = _ref.modeStorageKey,
15
+ modeStorageKey = _ref$modeStorageKey === void 0 ? DEFAULT_MODE_STORAGE_KEY : _ref$modeStorageKey,
16
+ _ref$colorSchemeStora = _ref.colorSchemeStorageKey,
17
+ colorSchemeStorageKey = _ref$colorSchemeStora === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _ref$colorSchemeStora,
18
+ _ref$attribute = _ref.attribute,
19
+ attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute;
20
+
21
+ return /*#__PURE__*/_jsx("script", {
22
+ // eslint-disable-next-line react/no-danger
23
+ dangerouslySetInnerHTML: {
24
+ __html: "(function() { try {\n var mode = localStorage.getItem('".concat(modeStorageKey, "');\n var colorScheme = '';\n if (mode === 'system' || (!mode && ").concat(defaultMode, " === 'system')) {\n // handle system mode\n var mql = window.matchMedia('(prefers-color-scheme: dark)');\n if (mql.matches) {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-dark') || ").concat(defaultLightColorScheme, ";\n } else {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || ").concat(defaultDarkColorScheme, ";\n }\n }\n if (mode === 'light') {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || ").concat(defaultLightColorScheme, ";\n }\n if (mode === 'dark') {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-dark') || ").concat(defaultDarkColorScheme, ";\n }\n if (colorScheme) {\n document.body.setAttribute('").concat(attribute, "', colorScheme);\n }\n } catch (e) {} })();")
25
+ }
26
+ });
27
+ }
@@ -0,0 +1 @@
1
+ export { default } from './createCssVarsProvider';
@@ -0,0 +1,231 @@
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
+ var 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, function (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
+ var 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
+ var _options$defaultMode = options.defaultMode,
61
+ defaultMode = _options$defaultMode === void 0 ? 'light' : _options$defaultMode,
62
+ defaultLightColorScheme = options.defaultLightColorScheme,
63
+ defaultDarkColorScheme = options.defaultDarkColorScheme,
64
+ _options$supportedCol = options.supportedColorSchemes,
65
+ supportedColorSchemes = _options$supportedCol === void 0 ? [] : _options$supportedCol,
66
+ _options$modeStorageK = options.modeStorageKey,
67
+ modeStorageKey = _options$modeStorageK === void 0 ? DEFAULT_MODE_STORAGE_KEY : _options$modeStorageK,
68
+ _options$colorSchemeS = options.colorSchemeStorageKey,
69
+ colorSchemeStorageKey = _options$colorSchemeS === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _options$colorSchemeS;
70
+ var joinedColorSchemes = supportedColorSchemes.join(',');
71
+
72
+ var _React$useState = React.useState(function () {
73
+ var initialMode = resolveValue(modeStorageKey, defaultMode);
74
+ return {
75
+ mode: initialMode,
76
+ systemMode: getSystemMode(initialMode),
77
+ lightColorScheme: resolveValue("".concat(colorSchemeStorageKey, "-light")) || defaultLightColorScheme,
78
+ darkColorScheme: resolveValue("".concat(colorSchemeStorageKey, "-dark")) || defaultDarkColorScheme
79
+ };
80
+ }),
81
+ state = _React$useState[0],
82
+ setState = _React$useState[1];
83
+
84
+ var colorScheme = getColorScheme(state);
85
+ var setMode = React.useCallback(function (mode) {
86
+ setState(function (currentState) {
87
+ var newMode = !mode ? defaultMode : mode;
88
+
89
+ if (typeof localStorage !== 'undefined') {
90
+ localStorage.setItem(modeStorageKey, newMode);
91
+ }
92
+
93
+ return _extends({}, currentState, {
94
+ mode: newMode,
95
+ systemMode: getSystemMode(newMode)
96
+ });
97
+ });
98
+ }, [modeStorageKey, defaultMode]);
99
+ var setColorScheme = React.useCallback(function (value) {
100
+ if (!value || typeof value === 'string') {
101
+ if (value && !supportedColorSchemes.includes(value)) {
102
+ console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
103
+ } else {
104
+ setState(function (currentState) {
105
+ var newState = _extends({}, currentState);
106
+
107
+ if (!value) {
108
+ // reset to default color scheme
109
+ newState.lightColorScheme = defaultLightColorScheme;
110
+ newState.darkColorScheme = defaultDarkColorScheme;
111
+ return newState;
112
+ }
113
+
114
+ processState(currentState, function (mode) {
115
+ localStorage.setItem("".concat(colorSchemeStorageKey, "-").concat(mode), value);
116
+
117
+ if (mode === 'light') {
118
+ newState.lightColorScheme = value;
119
+ }
120
+
121
+ if (mode === 'dark') {
122
+ newState.darkColorScheme = value;
123
+ }
124
+ });
125
+ return newState;
126
+ });
127
+ }
128
+ } else if (value.light && !supportedColorSchemes.includes(value.light) || value.dark && !supportedColorSchemes.includes(value.dark)) {
129
+ console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
130
+ } else {
131
+ setState(function (currentState) {
132
+ var newState = _extends({}, currentState);
133
+
134
+ if (value.light || value.light === null) {
135
+ newState.lightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
136
+ }
137
+
138
+ if (value.dark || value.dark === null) {
139
+ newState.darkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
140
+ }
141
+
142
+ return newState;
143
+ });
144
+
145
+ if (value.light) {
146
+ localStorage.setItem("".concat(colorSchemeStorageKey, "-light"), value.light);
147
+ }
148
+
149
+ if (value.dark) {
150
+ localStorage.setItem("".concat(colorSchemeStorageKey, "-dark"), value.dark);
151
+ }
152
+ }
153
+ }, [colorSchemeStorageKey, supportedColorSchemes, defaultLightColorScheme, defaultDarkColorScheme]);
154
+ var handleMediaQuery = React.useCallback(function (e) {
155
+ if (state.mode === 'system') {
156
+ setState(function (currentState) {
157
+ return _extends({}, currentState, {
158
+ systemMode: e.matches ? 'dark' : 'light'
159
+ });
160
+ });
161
+ }
162
+ }, [state.mode]); // Ref hack to avoid adding handleMediaQuery as a dep
163
+
164
+ var mediaListener = React.useRef(handleMediaQuery);
165
+ mediaListener.current = handleMediaQuery;
166
+ React.useEffect(function () {
167
+ var handler = function handler() {
168
+ return mediaListener.current.apply(mediaListener, arguments);
169
+ }; // Always listen to System preference
170
+
171
+
172
+ var media = window.matchMedia('(prefers-color-scheme: dark)'); // Intentionally use deprecated listener methods to support iOS & old browsers
173
+
174
+ media.addListener(handler);
175
+ handler(media);
176
+ return function () {
177
+ return media.removeListener(handler);
178
+ };
179
+ }, []); // Save mode, lightColorScheme & darkColorScheme to localStorage
180
+
181
+ React.useEffect(function () {
182
+ if (state.mode) {
183
+ localStorage.setItem(modeStorageKey, state.mode);
184
+ }
185
+
186
+ processState(state, function (mode) {
187
+ if (mode === 'light') {
188
+ localStorage.setItem("".concat(colorSchemeStorageKey, "-light"), state.lightColorScheme);
189
+ }
190
+
191
+ if (mode === 'dark') {
192
+ localStorage.setItem("".concat(colorSchemeStorageKey, "-dark"), state.darkColorScheme);
193
+ }
194
+ });
195
+ }, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
196
+
197
+ React.useEffect(function () {
198
+ var handleStorage = function handleStorage(event) {
199
+ var value = event.newValue;
200
+
201
+ if (typeof event.key === 'string' && event.key.startsWith(colorSchemeStorageKey) && (!value || joinedColorSchemes.match(value))) {
202
+ // If the key is deleted, value will be null then reset color scheme to the default one.
203
+ if (event.key.endsWith('light')) {
204
+ setColorScheme({
205
+ light: value
206
+ });
207
+ }
208
+
209
+ if (event.key.endsWith('dark')) {
210
+ setColorScheme({
211
+ dark: value
212
+ });
213
+ }
214
+ }
215
+
216
+ if (event.key === modeStorageKey && (!value || ['light', 'dark', 'system'].includes(value))) {
217
+ setMode(value || defaultMode);
218
+ }
219
+ };
220
+
221
+ window.addEventListener('storage', handleStorage);
222
+ return function () {
223
+ return window.removeEventListener('storage', handleStorage);
224
+ };
225
+ }, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode]);
226
+ return _extends({}, state, {
227
+ colorScheme: colorScheme,
228
+ setMode: setMode,
229
+ setColorScheme: setColorScheme
230
+ });
231
+ }
package/legacy/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license MUI v5.0.3
1
+ /** @license MUI v5.1.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.
@@ -41,4 +41,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
41
41
  export { default as useTheme } from './useTheme';
42
42
  export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
43
43
  export * from './colorManipulator';
44
- export { default as ThemeProvider } from './ThemeProvider';
44
+ export { default as ThemeProvider } from './ThemeProvider';
45
+ export { default as unstable_createCssVarsProvider } from './cssVars/createCssVarsProvider';
@@ -1,5 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
3
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4
+ import { isPlainObject } from '@mui/utils';
3
5
  import { propToStyleFunction } from '../getThemeValue';
4
6
 
5
7
  var splitProps = function splitProps(props) {
@@ -25,7 +27,25 @@ export default function extendSxProp(props) {
25
27
  systemProps = _splitProps.systemProps,
26
28
  otherProps = _splitProps.otherProps;
27
29
 
30
+ var finalSx;
31
+
32
+ if (Array.isArray(inSx)) {
33
+ finalSx = [systemProps].concat(_toConsumableArray(inSx));
34
+ } else if (typeof inSx === 'function') {
35
+ finalSx = function finalSx() {
36
+ var result = inSx.apply(void 0, arguments);
37
+
38
+ if (!isPlainObject(result)) {
39
+ return systemProps;
40
+ }
41
+
42
+ return _extends({}, systemProps, result);
43
+ };
44
+ } else {
45
+ finalSx = _extends({}, systemProps, inSx);
46
+ }
47
+
28
48
  return _extends({}, otherProps, {
29
- sx: _extends({}, systemProps, inSx)
49
+ sx: finalSx
30
50
  });
31
51
  }
@@ -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
- if (typeof styles === 'function') {
36
- return styles(theme);
37
- }
38
40
 
39
- 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(styles).forEach(function (styleKey) {
48
- var value = callIfFn(styles[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
  }
@@ -113,7 +113,11 @@ export default function createStyled(input = {}) {
113
113
 
114
114
  const muiStyledResolver = (styleArg, ...expressions) => {
115
115
  const expressionsWithDefaultTheme = expressions ? expressions.map(stylesArg => {
116
- return typeof stylesArg === 'function' ? _ref => {
116
+ // On the server emotion doesn't use React.forwardRef for creating components, so the created
117
+ // component stays as a function. This condition makes sure that we do not interpolate functions
118
+ // which are basically components used as a selectors.
119
+ // eslint-disable-next-line no-underscore-dangle
120
+ return typeof stylesArg === 'function' && stylesArg.__emotion_real !== stylesArg ? _ref => {
117
121
  let {
118
122
  theme: themeInput
119
123
  } = _ref,