@mui/system 5.2.0 → 5.2.4

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 (52) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +244 -1
  3. package/createBox.d.ts +3 -3
  4. package/createBox.spec.d.ts +1 -0
  5. package/createTheme/createBreakpoints.d.ts +6 -0
  6. package/createTheme/createBreakpoints.js +16 -0
  7. package/createTheme/createSpacing.d.ts +10 -10
  8. package/cssVars/createCssVarsProvider.d.ts +24 -10
  9. package/cssVars/createCssVarsProvider.js +81 -8
  10. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  11. package/cssVars/cssVarsParser.d.ts +68 -68
  12. package/cssVars/cssVarsParser.js +18 -17
  13. package/cssVars/getInitColorSchemeScript.d.ts +12 -12
  14. package/cssVars/getInitColorSchemeScript.js +1 -1
  15. package/cssVars/index.d.ts +2 -2
  16. package/cssVars/useCurrentColorScheme.d.ts +50 -50
  17. package/esm/createTheme/createBreakpoints.js +16 -0
  18. package/esm/cssVars/createCssVarsProvider.js +77 -8
  19. package/esm/cssVars/cssVarsParser.js +18 -16
  20. package/esm/cssVars/getInitColorSchemeScript.js +1 -1
  21. package/esm/index.js +1 -0
  22. package/esm/sx/index.js +1 -0
  23. package/esm/sx/sx.js +12 -0
  24. package/esm/useThemeProps/getThemeProps.js +2 -17
  25. package/index.d.ts +2 -0
  26. package/index.js +10 -1
  27. package/index.spec.d.ts +1 -1
  28. package/legacy/createTheme/createBreakpoints.js +16 -0
  29. package/legacy/cssVars/createCssVarsProvider.js +80 -8
  30. package/legacy/cssVars/cssVarsParser.js +20 -14
  31. package/legacy/cssVars/getInitColorSchemeScript.js +1 -1
  32. package/legacy/index.js +2 -1
  33. package/legacy/sx/index.js +1 -0
  34. package/legacy/sx/sx.js +13 -0
  35. package/legacy/useThemeProps/getThemeProps.js +2 -17
  36. package/modern/createTheme/createBreakpoints.js +16 -0
  37. package/modern/cssVars/createCssVarsProvider.js +75 -8
  38. package/modern/cssVars/cssVarsParser.js +18 -16
  39. package/modern/cssVars/getInitColorSchemeScript.js +1 -1
  40. package/modern/index.js +2 -1
  41. package/modern/sx/index.js +1 -0
  42. package/modern/sx/sx.js +12 -0
  43. package/modern/useThemeProps/getThemeProps.js +2 -17
  44. package/package.json +5 -5
  45. package/styleFunctionSx/styleFunctionSx.d.ts +1 -1
  46. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
  47. package/sx/index.d.ts +1 -0
  48. package/sx/index.js +15 -0
  49. package/sx/package.json +6 -0
  50. package/sx/sx.d.ts +4 -0
  51. package/sx/sx.js +22 -0
  52. package/useThemeProps/getThemeProps.js +2 -17
package/esm/sx/sx.js ADDED
@@ -0,0 +1,12 @@
1
+ import styleFunctionSx from '../styleFunctionSx';
2
+
3
+ function sx(styles) {
4
+ return ({
5
+ theme
6
+ }) => styleFunctionSx({
7
+ sx: styles,
8
+ theme
9
+ });
10
+ }
11
+
12
+ export default sx;
@@ -1,6 +1,4 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
-
3
- /* eslint-disable no-restricted-syntax */
1
+ import { internal_resolveProps as resolveProps } from '@mui/utils';
4
2
  export default function getThemeProps(params) {
5
3
  const {
6
4
  theme,
@@ -12,18 +10,5 @@ export default function getThemeProps(params) {
12
10
  return props;
13
11
  }
14
12
 
15
- const output = _extends({}, props); // Resolve default props, code borrow from React source.
16
- // https://github.com/facebook/react/blob/15a8f031838a553e41c0b66eb1bcf1da8448104d/packages/react/src/ReactElement.js#L221
17
-
18
-
19
- const defaultProps = theme.components[name].defaultProps;
20
- let propName;
21
-
22
- for (propName in defaultProps) {
23
- if (output[propName] === undefined) {
24
- output[propName] = defaultProps[propName];
25
- }
26
- }
27
-
28
- return output;
13
+ return resolveProps(theme.components[name].defaultProps, props);
29
14
  }
package/index.d.ts CHANGED
@@ -121,6 +121,8 @@ export {
121
121
  } from './styleFunctionSx';
122
122
  export * from './styleFunctionSx';
123
123
 
124
+ export { default as experimental_sx } from './sx';
125
+
124
126
  export { default as Box } from './Box';
125
127
  export * from './Box';
126
128
 
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license MUI v5.2.0
1
+ /** @license MUI v5.2.4
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.
@@ -34,6 +34,7 @@ var _exportNames = {
34
34
  typography: true,
35
35
  unstable_styleFunctionSx: true,
36
36
  unstable_extendSxProp: true,
37
+ experimental_sx: true,
37
38
  unstable_getThemeValue: true,
38
39
  Box: true,
39
40
  createBox: true,
@@ -134,6 +135,12 @@ Object.defineProperty(exports, "display", {
134
135
  return _display.default;
135
136
  }
136
137
  });
138
+ Object.defineProperty(exports, "experimental_sx", {
139
+ enumerable: true,
140
+ get: function () {
141
+ return _sx.default;
142
+ }
143
+ });
137
144
  Object.defineProperty(exports, "flexbox", {
138
145
  enumerable: true,
139
146
  get: function () {
@@ -405,6 +412,8 @@ Object.keys(_typography).forEach(function (key) {
405
412
 
406
413
  var _styleFunctionSx = _interopRequireWildcard(require("./styleFunctionSx"));
407
414
 
415
+ var _sx = _interopRequireDefault(require("./sx"));
416
+
408
417
  var _getThemeValue = _interopRequireDefault(require("./getThemeValue"));
409
418
 
410
419
  var _Box = _interopRequireDefault(require("./Box"));
package/index.spec.d.ts CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -49,6 +49,21 @@ export default function createBreakpoints(breakpoints) {
49
49
  return up(key);
50
50
  }
51
51
 
52
+ function not(key) {
53
+ // handle first and last key separately, for better readability
54
+ var keyIndex = keys.indexOf(key);
55
+
56
+ if (keyIndex === 0) {
57
+ return up(keys[1]);
58
+ }
59
+
60
+ if (keyIndex === keys.length - 1) {
61
+ return down(keys[keyIndex]);
62
+ }
63
+
64
+ return between(key, keys[keys.indexOf(key) + 1]).replace('@media', '@media not all and');
65
+ }
66
+
52
67
  return _extends({
53
68
  keys: keys,
54
69
  values: values,
@@ -56,6 +71,7 @@ export default function createBreakpoints(breakpoints) {
56
71
  down: down,
57
72
  between: between,
58
73
  only: only,
74
+ not: not,
59
75
  unit: unit
60
76
  }, other);
61
77
  }
@@ -7,21 +7,32 @@ import * as React from 'react';
7
7
  import PropTypes from 'prop-types';
8
8
  import { GlobalStyles } from '@mui/styled-engine';
9
9
  import { deepmerge } from '@mui/utils';
10
+ import createSpacing from '../createTheme/createSpacing';
11
+ import createBreakpoints from '../createTheme/createBreakpoints';
10
12
  import cssVarsParser from './cssVarsParser';
11
13
  import ThemeProvider from '../ThemeProvider';
12
14
  import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
13
15
  import useCurrentColorScheme from './useCurrentColorScheme';
14
16
  import { jsx as _jsx } from "react/jsx-runtime";
15
17
  import { jsxs as _jsxs } from "react/jsx-runtime";
18
+ export var DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
16
19
  export default function createCssVarsProvider(options) {
20
+ var _baseTheme$breakpoint;
21
+
17
22
  var _options$theme = options.theme,
18
23
  baseTheme = _options$theme === void 0 ? {} : _options$theme,
19
24
  _options$defaultMode = options.defaultMode,
20
25
  desisgnSystemMode = _options$defaultMode === void 0 ? 'light' : _options$defaultMode,
21
26
  designSystemColorScheme = options.defaultColorScheme,
27
+ _options$disableTrans = options.disableTransitionOnChange,
28
+ disableTransitionOnChange = _options$disableTrans === void 0 ? false : _options$disableTrans,
29
+ _options$enableColorS = options.enableColorScheme,
30
+ enableColorScheme = _options$enableColorS === void 0 ? true : _options$enableColorS,
22
31
  _options$prefix = options.prefix,
23
32
  designSystemPrefix = _options$prefix === void 0 ? '' : _options$prefix,
24
33
  shouldSkipGeneratingVar = options.shouldSkipGeneratingVar;
34
+ var systemSpacing = createSpacing(baseTheme.spacing);
35
+ var systemBreakpoints = createBreakpoints((_baseTheme$breakpoint = baseTheme.breakpoints) != null ? _baseTheme$breakpoint : {});
25
36
 
26
37
  if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || _typeof(designSystemColorScheme) === 'object' && !baseTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.light] || _typeof(designSystemColorScheme) === 'object' && !baseTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.dark]) {
27
38
  console.error("MUI: `".concat(designSystemColorScheme, "` does not exist in `theme.colorSchemes`."));
@@ -53,16 +64,27 @@ export default function createCssVarsProvider(options) {
53
64
  defaultMode = _ref$defaultMode === void 0 ? desisgnSystemMode : _ref$defaultMode,
54
65
  _ref$defaultColorSche = _ref.defaultColorScheme,
55
66
  defaultColorScheme = _ref$defaultColorSche === void 0 ? designSystemColorScheme : _ref$defaultColorSche;
67
+ // make sure that baseTheme is always independent of each <CssVarsProvider /> call.
68
+ // JSON.parse(JSON.stringify(...)) is okay to be used as long as the baseTheme is a plain object.
69
+ var clonedBaseTheme = React.useMemo(function () {
70
+ return JSON.parse(JSON.stringify(baseTheme));
71
+ }, []);
56
72
 
57
- var _baseTheme$colorSchem = baseTheme.colorSchemes,
58
- baseColorSchemes = _baseTheme$colorSchem === void 0 ? {} : _baseTheme$colorSchem,
59
- restBaseTheme = _objectWithoutProperties(baseTheme, ["colorSchemes"]);
73
+ var _clonedBaseTheme$colo = clonedBaseTheme.colorSchemes,
74
+ baseColorSchemes = _clonedBaseTheme$colo === void 0 ? {} : _clonedBaseTheme$colo,
75
+ restBaseTheme = _objectWithoutProperties(clonedBaseTheme, ["colorSchemes"]);
60
76
 
61
77
  var _themeProp$colorSchem = themeProp.colorSchemes,
62
78
  colorSchemesProp = _themeProp$colorSchem === void 0 ? {} : _themeProp$colorSchem,
63
79
  restThemeProp = _objectWithoutProperties(themeProp, ["colorSchemes"]);
64
80
 
65
- var mergedTheme = deepmerge(restBaseTheme, restThemeProp);
81
+ var hasMounted = React.useRef(false); // eslint-disable-next-line prefer-const
82
+
83
+ var _deepmerge = deepmerge(restBaseTheme, restThemeProp),
84
+ _deepmerge$components = _deepmerge.components,
85
+ components = _deepmerge$components === void 0 ? {} : _deepmerge$components,
86
+ mergedTheme = _objectWithoutProperties(_deepmerge, ["components"]);
87
+
66
88
  var colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
67
89
  var allColorSchemes = Object.keys(colorSchemes);
68
90
  var defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
@@ -77,6 +99,7 @@ export default function createCssVarsProvider(options) {
77
99
  }),
78
100
  mode = _useCurrentColorSchem.mode,
79
101
  setMode = _useCurrentColorSchem.setMode,
102
+ systemMode = _useCurrentColorSchem.systemMode,
80
103
  lightColorScheme = _useCurrentColorSchem.lightColorScheme,
81
104
  darkColorScheme = _useCurrentColorSchem.darkColorScheme,
82
105
  colorScheme = _useCurrentColorSchem.colorScheme,
@@ -105,8 +128,11 @@ export default function createCssVarsProvider(options) {
105
128
  rootVars = _cssVarsParser.vars;
106
129
 
107
130
  mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
131
+ components: components,
108
132
  colorSchemes: colorSchemes,
109
- vars: rootVars
133
+ vars: rootVars,
134
+ spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
135
+ breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints
110
136
  });
111
137
  var styleSheet = {};
112
138
  Object.entries(colorSchemes).forEach(function (_ref2) {
@@ -144,9 +170,55 @@ export default function createCssVarsProvider(options) {
144
170
  });
145
171
  React.useEffect(function () {
146
172
  if (colorScheme) {
147
- document.body.setAttribute(attribute, colorScheme);
173
+ // attaches attribute to <html> because the css variables are attached to :root (html)
174
+ document.documentElement.setAttribute(attribute, colorScheme);
148
175
  }
149
176
  }, [colorScheme, attribute]);
177
+ React.useEffect(function () {
178
+ if (!mode || !enableColorScheme) {
179
+ return undefined;
180
+ }
181
+
182
+ var priorColorScheme = document.documentElement.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
183
+
184
+ if (mode === 'system') {
185
+ document.documentElement.style.setProperty('color-scheme', systemMode);
186
+ } else {
187
+ document.documentElement.style.setProperty('color-scheme', mode);
188
+ }
189
+
190
+ return function () {
191
+ document.documentElement.style.setProperty('color-scheme', priorColorScheme);
192
+ };
193
+ }, [mode, systemMode]);
194
+ React.useEffect(function () {
195
+ var timer;
196
+
197
+ if (disableTransitionOnChange && hasMounted.current) {
198
+ // credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
199
+ var css = document.createElement('style');
200
+ css.appendChild(document.createTextNode(DISABLE_CSS_TRANSITION));
201
+ document.head.appendChild(css); // Force browser repaint
202
+
203
+ (function () {
204
+ return window.getComputedStyle(document.body);
205
+ })();
206
+
207
+ timer = setTimeout(function () {
208
+ document.head.removeChild(css);
209
+ }, 1);
210
+ }
211
+
212
+ return function () {
213
+ clearTimeout(timer);
214
+ };
215
+ }, [colorScheme]);
216
+ React.useEffect(function () {
217
+ hasMounted.current = true;
218
+ return function () {
219
+ hasMounted.current = false;
220
+ };
221
+ }, []);
150
222
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
151
223
  value: {
152
224
  mode: mode,
@@ -177,7 +249,7 @@ export default function createCssVarsProvider(options) {
177
249
  attribute: PropTypes.string,
178
250
 
179
251
  /**
180
- * Your component tree.
252
+ * The component tree.
181
253
  */
182
254
  children: PropTypes.node,
183
255
 
@@ -197,7 +269,7 @@ export default function createCssVarsProvider(options) {
197
269
  modeStorageKey: PropTypes.string,
198
270
 
199
271
  /**
200
- * css variable prefix
272
+ * CSS variable prefix.
201
273
  */
202
274
  prefix: PropTypes.string,
203
275
 
@@ -49,7 +49,7 @@ export var assignNestedKeys = function assignNestedKeys(obj, keys, value) {
49
49
  * // ['palette', 'primary', 'main'] '#000000'
50
50
  */
51
51
 
52
- export var walkObjectDeep = function walkObjectDeep(obj, callback) {
52
+ export var walkObjectDeep = function walkObjectDeep(obj, callback, shouldSkipPaths) {
53
53
  function recurse(object) {
54
54
  var parentKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
55
55
  Object.entries(object).forEach(function (_ref) {
@@ -57,11 +57,13 @@ export var walkObjectDeep = function walkObjectDeep(obj, callback) {
57
57
  key = _ref2[0],
58
58
  value = _ref2[1];
59
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);
60
+ if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([].concat(_toConsumableArray(parentKeys), [key]))) {
61
+ if (value !== undefined && value !== null) {
62
+ if (_typeof(value) === 'object' && Object.keys(value).length > 0) {
63
+ recurse(value, [].concat(_toConsumableArray(parentKeys), [key]));
64
+ } else {
65
+ callback([].concat(_toConsumableArray(parentKeys), [key]), value, object);
66
+ }
65
67
  }
66
68
  }
67
69
  });
@@ -75,7 +77,7 @@ var getCssValue = function getCssValue(keys, value) {
75
77
  if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(function (prop) {
76
78
  return keys.includes(prop);
77
79
  })) {
78
- // css property that are unitless
80
+ // CSS property that are unitless
79
81
  return value;
80
82
  }
81
83
 
@@ -113,10 +115,6 @@ var getCssValue = function getCssValue(keys, value) {
113
115
 
114
116
 
115
117
  export default function cssVarsParser(theme, options) {
116
- var clonedTheme = _extends({}, theme);
117
-
118
- delete clonedTheme.vars; // remove 'vars' from the structure
119
-
120
118
  var _ref3 = options || {},
121
119
  prefix = _ref3.prefix,
122
120
  _ref3$basePrefix = _ref3.basePrefix,
@@ -125,13 +123,18 @@ export default function cssVarsParser(theme, options) {
125
123
 
126
124
  var css = {};
127
125
  var vars = {};
128
- walkObjectDeep(clonedTheme, function (keys, val, scope) {
126
+ walkObjectDeep(theme, function (keys, val, scope) {
129
127
  if (typeof val === 'string' || typeof val === 'number') {
130
128
  var _value = val;
131
129
 
132
130
  if (typeof _value === 'string' && _value.startsWith('var')) {
133
131
  // 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
132
+ if (!basePrefix && prefix) {
133
+ _value = _value.replace(/var\(--/g, "var(--".concat(prefix, "-"));
134
+ } else {
135
+ _value = prefix ? _value.replace(new RegExp(basePrefix, 'g'), prefix) : _value.replace(new RegExp("".concat(basePrefix, "-"), 'g'), '');
136
+ } // scope is the deepest object in the tree, keys is the theme path keys
137
+
135
138
 
136
139
  scope[keys.slice(-1)[0]] = _value;
137
140
  }
@@ -145,7 +148,10 @@ export default function cssVarsParser(theme, options) {
145
148
  assignNestedKeys(vars, keys, "var(".concat(cssVar, ")"));
146
149
  }
147
150
  }
148
- });
151
+ }, function (keys) {
152
+ return keys[0] === 'vars';
153
+ } // skip 'vars/*' paths
154
+ );
149
155
  return {
150
156
  css: css,
151
157
  vars: vars
@@ -20,7 +20,7 @@ export default function getInitColorSchemeScript(options) {
20
20
  return /*#__PURE__*/_jsx("script", {
21
21
  // eslint-disable-next-line react/no-danger
22
22
  dangerouslySetInnerHTML: {
23
- __html: "(function() { try {\n var mode = localStorage.getItem('".concat(modeStorageKey, "');\n var colorScheme = '';\n if (mode === 'system' || (!mode && !!").concat(enableSystem, ")) {\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(defaultDarkColorScheme, "';\n } else {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || '").concat(defaultLightColorScheme, "';\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) {} })();")
23
+ __html: "(function() { try {\n var mode = localStorage.getItem('".concat(modeStorageKey, "');\n var colorScheme = '';\n if (mode === 'system' || (!mode && !!").concat(enableSystem, ")) {\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(defaultDarkColorScheme, "';\n } else {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || '").concat(defaultLightColorScheme, "';\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.documentElement.setAttribute('").concat(attribute, "', colorScheme);\n }\n } catch (e) {} })();")
24
24
  }
25
25
  });
26
26
  }
package/legacy/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license MUI v5.2.0
1
+ /** @license MUI v5.2.4
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.
@@ -27,6 +27,7 @@ export { default as style, getPath } from './style';
27
27
  export { default as typography } from './typography';
28
28
  export * from './typography';
29
29
  export { default as unstable_styleFunctionSx, extendSxProp as unstable_extendSxProp } from './styleFunctionSx';
30
+ export { default as experimental_sx } from './sx';
30
31
  export { default as unstable_getThemeValue } from './getThemeValue';
31
32
  export { default as Box } from './Box';
32
33
  export { default as createBox } from './createBox';
@@ -0,0 +1 @@
1
+ export { default } from './sx';
@@ -0,0 +1,13 @@
1
+ import styleFunctionSx from '../styleFunctionSx';
2
+
3
+ function sx(styles) {
4
+ return function (_ref) {
5
+ var theme = _ref.theme;
6
+ return styleFunctionSx({
7
+ sx: styles,
8
+ theme: theme
9
+ });
10
+ };
11
+ }
12
+
13
+ export default sx;
@@ -1,6 +1,4 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
-
3
- /* eslint-disable no-restricted-syntax */
1
+ import { internal_resolveProps as resolveProps } from '@mui/utils';
4
2
  export default function getThemeProps(params) {
5
3
  var theme = params.theme,
6
4
  name = params.name,
@@ -10,18 +8,5 @@ export default function getThemeProps(params) {
10
8
  return props;
11
9
  }
12
10
 
13
- var output = _extends({}, props); // Resolve default props, code borrow from React source.
14
- // https://github.com/facebook/react/blob/15a8f031838a553e41c0b66eb1bcf1da8448104d/packages/react/src/ReactElement.js#L221
15
-
16
-
17
- var defaultProps = theme.components[name].defaultProps;
18
- var propName;
19
-
20
- for (propName in defaultProps) {
21
- if (output[propName] === undefined) {
22
- output[propName] = defaultProps[propName];
23
- }
24
- }
25
-
26
- return output;
11
+ return resolveProps(theme.components[name].defaultProps, props);
27
12
  }
@@ -51,6 +51,21 @@ export default function createBreakpoints(breakpoints) {
51
51
  return up(key);
52
52
  }
53
53
 
54
+ function not(key) {
55
+ // handle first and last key separately, for better readability
56
+ const keyIndex = keys.indexOf(key);
57
+
58
+ if (keyIndex === 0) {
59
+ return up(keys[1]);
60
+ }
61
+
62
+ if (keyIndex === keys.length - 1) {
63
+ return down(keys[keyIndex]);
64
+ }
65
+
66
+ return between(key, keys[keys.indexOf(key) + 1]).replace('@media', '@media not all and');
67
+ }
68
+
54
69
  return _extends({
55
70
  keys,
56
71
  values,
@@ -58,6 +73,7 @@ export default function createBreakpoints(breakpoints) {
58
73
  down,
59
74
  between,
60
75
  only,
76
+ not,
61
77
  unit
62
78
  }, other);
63
79
  }
@@ -2,25 +2,33 @@ import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
3
  import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
4
4
  const _excluded = ["colorSchemes"],
5
- _excluded2 = ["colorSchemes"];
5
+ _excluded2 = ["colorSchemes"],
6
+ _excluded3 = ["components"];
6
7
  import * as React from 'react';
7
8
  import PropTypes from 'prop-types';
8
9
  import { GlobalStyles } from '@mui/styled-engine';
9
10
  import { deepmerge } from '@mui/utils';
11
+ import createSpacing from '../createTheme/createSpacing';
12
+ import createBreakpoints from '../createTheme/createBreakpoints';
10
13
  import cssVarsParser from './cssVarsParser';
11
14
  import ThemeProvider from '../ThemeProvider';
12
15
  import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
13
16
  import useCurrentColorScheme from './useCurrentColorScheme';
14
17
  import { jsx as _jsx } from "react/jsx-runtime";
15
18
  import { jsxs as _jsxs } from "react/jsx-runtime";
19
+ export const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}';
16
20
  export default function createCssVarsProvider(options) {
17
21
  const {
18
22
  theme: baseTheme = {},
19
23
  defaultMode: desisgnSystemMode = 'light',
20
24
  defaultColorScheme: designSystemColorScheme,
25
+ disableTransitionOnChange = false,
26
+ enableColorScheme = true,
21
27
  prefix: designSystemPrefix = '',
22
28
  shouldSkipGeneratingVar
23
29
  } = options;
30
+ const systemSpacing = createSpacing(baseTheme.spacing);
31
+ const systemBreakpoints = createBreakpoints(baseTheme.breakpoints ?? {});
24
32
 
25
33
  if (!baseTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !baseTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.light] || typeof designSystemColorScheme === 'object' && !baseTheme.colorSchemes[designSystemColorScheme?.dark]) {
26
34
  console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
@@ -47,17 +55,28 @@ export default function createCssVarsProvider(options) {
47
55
  defaultMode = desisgnSystemMode,
48
56
  defaultColorScheme = designSystemColorScheme
49
57
  }) {
58
+ // make sure that baseTheme is always independent of each <CssVarsProvider /> call.
59
+ // JSON.parse(JSON.stringify(...)) is okay to be used as long as the baseTheme is a plain object.
60
+ const clonedBaseTheme = React.useMemo(() => JSON.parse(JSON.stringify(baseTheme)), []);
61
+
50
62
  const {
51
63
  colorSchemes: baseColorSchemes = {}
52
- } = baseTheme,
53
- restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
64
+ } = clonedBaseTheme,
65
+ restBaseTheme = _objectWithoutPropertiesLoose(clonedBaseTheme, _excluded);
54
66
 
55
67
  const {
56
68
  colorSchemes: colorSchemesProp = {}
57
69
  } = themeProp,
58
70
  restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
59
71
 
60
- let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
72
+ const hasMounted = React.useRef(false); // eslint-disable-next-line prefer-const
73
+
74
+ let _deepmerge = deepmerge(restBaseTheme, restThemeProp),
75
+ {
76
+ components = {}
77
+ } = _deepmerge,
78
+ mergedTheme = _objectWithoutPropertiesLoose(_deepmerge, _excluded3);
79
+
61
80
  const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
62
81
  const allColorSchemes = Object.keys(colorSchemes);
63
82
  const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
@@ -65,6 +84,7 @@ export default function createCssVarsProvider(options) {
65
84
  const {
66
85
  mode,
67
86
  setMode,
87
+ systemMode,
68
88
  lightColorScheme,
69
89
  darkColorScheme,
70
90
  colorScheme,
@@ -100,8 +120,11 @@ export default function createCssVarsProvider(options) {
100
120
  shouldSkipGeneratingVar
101
121
  });
102
122
  mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
123
+ components,
103
124
  colorSchemes,
104
- vars: rootVars
125
+ vars: rootVars,
126
+ spacing: themeProp.spacing ? createSpacing(themeProp.spacing) : systemSpacing,
127
+ breakpoints: themeProp.breakpoints ? createBreakpoints(themeProp.breakpoints) : systemBreakpoints
105
128
  });
106
129
  const styleSheet = {};
107
130
  Object.entries(colorSchemes).forEach(([key, scheme]) => {
@@ -135,9 +158,53 @@ export default function createCssVarsProvider(options) {
135
158
  });
136
159
  React.useEffect(() => {
137
160
  if (colorScheme) {
138
- document.body.setAttribute(attribute, colorScheme);
161
+ // attaches attribute to <html> because the css variables are attached to :root (html)
162
+ document.documentElement.setAttribute(attribute, colorScheme);
139
163
  }
140
164
  }, [colorScheme, attribute]);
165
+ React.useEffect(() => {
166
+ if (!mode || !enableColorScheme) {
167
+ return undefined;
168
+ }
169
+
170
+ const priorColorScheme = document.documentElement.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
171
+
172
+ if (mode === 'system') {
173
+ document.documentElement.style.setProperty('color-scheme', systemMode);
174
+ } else {
175
+ document.documentElement.style.setProperty('color-scheme', mode);
176
+ }
177
+
178
+ return () => {
179
+ document.documentElement.style.setProperty('color-scheme', priorColorScheme);
180
+ };
181
+ }, [mode, systemMode]);
182
+ React.useEffect(() => {
183
+ let timer;
184
+
185
+ if (disableTransitionOnChange && hasMounted.current) {
186
+ // credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
187
+ const css = document.createElement('style');
188
+ css.appendChild(document.createTextNode(DISABLE_CSS_TRANSITION));
189
+ document.head.appendChild(css); // Force browser repaint
190
+
191
+ (() => window.getComputedStyle(document.body))();
192
+
193
+ timer = setTimeout(() => {
194
+ document.head.removeChild(css);
195
+ }, 1);
196
+ }
197
+
198
+ return () => {
199
+ clearTimeout(timer);
200
+ };
201
+ }, [colorScheme]);
202
+ React.useEffect(() => {
203
+ hasMounted.current = true;
204
+ return () => {
205
+ hasMounted.current = false;
206
+ };
207
+ }, []);
141
208
  return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
142
209
  value: {
143
210
  mode,
@@ -168,7 +235,7 @@ export default function createCssVarsProvider(options) {
168
235
  attribute: PropTypes.string,
169
236
 
170
237
  /**
171
- * Your component tree.
238
+ * The component tree.
172
239
  */
173
240
  children: PropTypes.node,
174
241
 
@@ -188,7 +255,7 @@ export default function createCssVarsProvider(options) {
188
255
  modeStorageKey: PropTypes.string,
189
256
 
190
257
  /**
191
- * css variable prefix
258
+ * CSS variable prefix.
192
259
  */
193
260
  prefix: PropTypes.string,
194
261