@mui/system 5.0.4 → 5.1.1

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 (63) hide show
  1. package/CHANGELOG.md +287 -6
  2. package/breakpoints.js +41 -8
  3. package/createBox.d.ts +5 -1
  4. package/createBox.js +5 -3
  5. package/createStyled.d.ts +16 -190
  6. package/createStyled.js +5 -1
  7. package/createTheme/createBreakpoints.js +2 -2
  8. package/cssVars/createCssVarsProvider.d.ts +131 -0
  9. package/cssVars/createCssVarsProvider.js +228 -0
  10. package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
  11. package/cssVars/cssVarsParser.d.ts +68 -0
  12. package/cssVars/cssVarsParser.js +156 -0
  13. package/cssVars/getInitColorSchemeScript.d.ts +12 -0
  14. package/cssVars/getInitColorSchemeScript.js +60 -0
  15. package/cssVars/index.d.ts +2 -0
  16. package/cssVars/index.js +15 -0
  17. package/cssVars/package.json +6 -0
  18. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  19. package/cssVars/useCurrentColorScheme.js +235 -0
  20. package/esm/breakpoints.js +39 -8
  21. package/esm/createBox.js +5 -3
  22. package/esm/createStyled.js +5 -1
  23. package/esm/createTheme/createBreakpoints.js +2 -2
  24. package/esm/cssVars/createCssVarsProvider.js +207 -0
  25. package/esm/cssVars/cssVarsParser.js +141 -0
  26. package/esm/cssVars/getInitColorSchemeScript.js +42 -0
  27. package/esm/cssVars/index.js +1 -0
  28. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  29. package/esm/index.js +2 -1
  30. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  31. package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
  32. package/index.d.ts +6 -0
  33. package/index.js +11 -2
  34. package/legacy/breakpoints.js +39 -8
  35. package/legacy/createBox.js +6 -3
  36. package/legacy/createStyled.js +5 -1
  37. package/legacy/createTheme/createBreakpoints.js +2 -2
  38. package/legacy/cssVars/createCssVarsProvider.js +215 -0
  39. package/legacy/cssVars/cssVarsParser.js +153 -0
  40. package/legacy/cssVars/getInitColorSchemeScript.js +27 -0
  41. package/legacy/cssVars/index.js +1 -0
  42. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  43. package/legacy/index.js +3 -2
  44. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  45. package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
  46. package/modern/breakpoints.js +39 -8
  47. package/modern/createBox.js +5 -3
  48. package/modern/createStyled.js +5 -1
  49. package/modern/createTheme/createBreakpoints.js +2 -2
  50. package/modern/cssVars/createCssVarsProvider.js +207 -0
  51. package/modern/cssVars/cssVarsParser.js +141 -0
  52. package/modern/cssVars/getInitColorSchemeScript.js +42 -0
  53. package/modern/cssVars/index.js +1 -0
  54. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  55. package/modern/index.js +3 -2
  56. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  57. package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
  58. package/package.json +8 -9
  59. package/style.d.ts +1 -1
  60. package/styleFunctionSx/extendSxProp.js +21 -1
  61. package/styleFunctionSx/styleFunctionSx.d.ts +7 -1
  62. package/styleFunctionSx/styleFunctionSx.js +46 -36
  63. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
@@ -14,53 +14,63 @@ function callIfFn(maybeFn, arg) {
14
14
 
15
15
  function styleFunctionSx(props) {
16
16
  const {
17
- sx: styles,
17
+ sx,
18
18
  theme = {}
19
19
  } = props || {};
20
20
 
21
- if (!styles) {
22
- return null;
21
+ if (!sx) {
22
+ return null; // emotion & styled-components will neglect null
23
23
  }
24
+ /*
25
+ * Receive `sxInput` as object or callback
26
+ * and then recursively check keys & values to create media query object styles.
27
+ * (the result will be used in `styled`)
28
+ */
24
29
 
25
- if (typeof styles === 'function') {
26
- return styles(theme);
27
- }
28
30
 
29
- if (typeof styles !== 'object') {
30
- // value
31
- return styles;
32
- }
31
+ function traverse(sxInput) {
32
+ let sxObject = sxInput;
33
33
 
34
- const emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
35
- const breakpointsKeys = Object.keys(emptyBreakpoints);
36
- let css = emptyBreakpoints;
37
- Object.keys(styles).forEach(styleKey => {
38
- const value = callIfFn(styles[styleKey], theme);
34
+ if (typeof sxInput === 'function') {
35
+ sxObject = sxInput(theme);
36
+ } else if (typeof sxInput !== 'object') {
37
+ // value
38
+ return sxInput;
39
+ }
39
40
 
40
- if (typeof value === 'object') {
41
- if (propToStyleFunction[styleKey]) {
42
- css = merge(css, getThemeValue(styleKey, value, theme));
43
- } else {
44
- const breakpointsValues = handleBreakpoints({
45
- theme
46
- }, value, x => ({
47
- [styleKey]: x
48
- }));
41
+ const emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
42
+ const breakpointsKeys = Object.keys(emptyBreakpoints);
43
+ let css = emptyBreakpoints;
44
+ Object.keys(sxObject).forEach(styleKey => {
45
+ const value = callIfFn(sxObject[styleKey], theme);
49
46
 
50
- if (objectsHaveSameKeys(breakpointsValues, value)) {
51
- css[styleKey] = styleFunctionSx({
52
- sx: value,
53
- theme
54
- });
47
+ if (typeof value === 'object') {
48
+ if (propToStyleFunction[styleKey]) {
49
+ css = merge(css, getThemeValue(styleKey, value, theme));
55
50
  } else {
56
- css = merge(css, breakpointsValues);
51
+ const breakpointsValues = handleBreakpoints({
52
+ theme
53
+ }, value, x => ({
54
+ [styleKey]: x
55
+ }));
56
+
57
+ if (objectsHaveSameKeys(breakpointsValues, value)) {
58
+ css[styleKey] = styleFunctionSx({
59
+ sx: value,
60
+ theme
61
+ });
62
+ } else {
63
+ css = merge(css, breakpointsValues);
64
+ }
57
65
  }
66
+ } else {
67
+ css = merge(css, getThemeValue(styleKey, value, theme));
58
68
  }
59
- } else {
60
- css = merge(css, getThemeValue(styleKey, value, theme));
61
- }
62
- });
63
- return removeUnusedBreakpoints(breakpointsKeys, css);
69
+ });
70
+ return removeUnusedBreakpoints(breakpointsKeys, css);
71
+ }
72
+
73
+ return Array.isArray(sx) ? sx.map(traverse) : traverse(sx);
64
74
  }
65
75
 
66
76
  styleFunctionSx.filterProps = ['sx'];
package/index.d.ts CHANGED
@@ -107,6 +107,9 @@ export {
107
107
  GlobalStyles,
108
108
  GlobalStylesProps,
109
109
  StyledEngineProvider,
110
+ Interpolation,
111
+ CSSInterpolation,
112
+ CSSObject,
110
113
  } from '@mui/styled-engine';
111
114
 
112
115
  export * from './style';
@@ -154,3 +157,6 @@ export * from './colorManipulator';
154
157
 
155
158
  export { default as ThemeProvider } from './ThemeProvider';
156
159
  export * from './ThemeProvider';
160
+
161
+ export { default as unstable_createCssVarsProvider } from './cssVars';
162
+ export * from './cssVars';
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license MUI v5.0.4
1
+ /** @license MUI v5.1.1
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.
@@ -47,7 +47,8 @@ var _exportNames = {
47
47
  getThemeProps: true,
48
48
  useTheme: true,
49
49
  useThemeWithoutDefault: true,
50
- ThemeProvider: true
50
+ ThemeProvider: true,
51
+ unstable_createCssVarsProvider: true
51
52
  };
52
53
  Object.defineProperty(exports, "Box", {
53
54
  enumerable: true,
@@ -229,6 +230,12 @@ Object.defineProperty(exports, "typography", {
229
230
  return _typography.default;
230
231
  }
231
232
  });
233
+ Object.defineProperty(exports, "unstable_createCssVarsProvider", {
234
+ enumerable: true,
235
+ get: function () {
236
+ return _createCssVarsProvider.default;
237
+ }
238
+ });
232
239
  Object.defineProperty(exports, "unstable_extendSxProp", {
233
240
  enumerable: true,
234
241
  get: function () {
@@ -450,6 +457,8 @@ Object.keys(_colorManipulator).forEach(function (key) {
450
457
 
451
458
  var _ThemeProvider = _interopRequireDefault(require("./ThemeProvider"));
452
459
 
460
+ var _createCssVarsProvider = _interopRequireDefault(require("./cssVars/createCssVarsProvider"));
461
+
453
462
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
454
463
 
455
464
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -10,12 +10,12 @@ export var values = {
10
10
  xs: 0,
11
11
  // phone
12
12
  sm: 600,
13
- // tablets
13
+ // tablet
14
14
  md: 900,
15
15
  // small laptop
16
16
  lg: 1200,
17
17
  // desktop
18
- xl: 1536 // large screens
18
+ xl: 1536 // large screen
19
19
 
20
20
  };
21
21
  var defaultBreakpoints = {
@@ -122,10 +122,40 @@ export function mergeBreakpointsInOrder(breakpointsInput) {
122
122
  return deepmerge(prev, next);
123
123
  }, {});
124
124
  return removeUnusedBreakpoints(Object.keys(emptyBreakpoints), mergedOutput);
125
+ } // compute base for responsive values; e.g.,
126
+ // [1,2,3] => {xs: true, sm: true, md: true}
127
+ // {xs: 1, sm: 2, md: 3} => {xs: true, sm: true, md: true}
128
+
129
+ export function computeBreakpointsBase(breakpointValues, themeBreakpoints) {
130
+ // fixed value
131
+ if (_typeof(breakpointValues) !== 'object') {
132
+ return {};
133
+ }
134
+
135
+ var base = {};
136
+ var breakpointsKeys = Object.keys(themeBreakpoints);
137
+
138
+ if (Array.isArray(breakpointValues)) {
139
+ breakpointsKeys.forEach(function (breakpoint, i) {
140
+ if (i < breakpointValues.length) {
141
+ base[breakpoint] = true;
142
+ }
143
+ });
144
+ } else {
145
+ breakpointsKeys.forEach(function (breakpoint) {
146
+ if (breakpointValues[breakpoint] != null) {
147
+ base[breakpoint] = true;
148
+ }
149
+ });
150
+ }
151
+
152
+ return base;
125
153
  }
126
154
  export function resolveBreakpointValues(_ref) {
127
155
  var breakpointValues = _ref.values,
128
- base = _ref.base;
156
+ themeBreakpoints = _ref.breakpoints,
157
+ customBase = _ref.base;
158
+ var base = customBase || computeBreakpointsBase(breakpointValues, themeBreakpoints);
129
159
  var keys = Object.keys(base);
130
160
 
131
161
  if (keys.length === 0) {
@@ -133,14 +163,15 @@ export function resolveBreakpointValues(_ref) {
133
163
  }
134
164
 
135
165
  var previous;
136
- return keys.reduce(function (acc, breakpoint) {
137
- if (_typeof(breakpointValues) === 'object') {
138
- acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous];
166
+ return keys.reduce(function (acc, breakpoint, i) {
167
+ if (Array.isArray(breakpointValues)) {
168
+ acc[breakpoint] = breakpointValues[i] != null ? breakpointValues[i] : breakpointValues[previous];
169
+ previous = i;
139
170
  } else {
140
- acc[breakpoint] = breakpointValues;
171
+ acc[breakpoint] = breakpointValues[breakpoint] != null ? breakpointValues[breakpoint] : breakpointValues[previous] || breakpointValues;
172
+ previous = breakpoint;
141
173
  }
142
174
 
143
- previous = breakpoint;
144
175
  return acc;
145
176
  }, {});
146
177
  }
@@ -9,7 +9,10 @@ import useTheme from './useTheme';
9
9
  import { jsx as _jsx } from "react/jsx-runtime";
10
10
  export default function createBox() {
11
11
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
12
- var defaultTheme = options.defaultTheme;
12
+ var defaultTheme = options.defaultTheme,
13
+ _options$defaultClass = options.defaultClassName,
14
+ defaultClassName = _options$defaultClass === void 0 ? 'MuiBox-root' : _options$defaultClass,
15
+ generateClassName = options.generateClassName;
13
16
  var BoxRoot = styled('div')(styleFunctionSx);
14
17
  var Box = /*#__PURE__*/React.forwardRef(function Box(inProps, ref) {
15
18
  var theme = useTheme(defaultTheme);
@@ -23,7 +26,7 @@ export default function createBox() {
23
26
  return /*#__PURE__*/_jsx(BoxRoot, _extends({
24
27
  as: component,
25
28
  ref: ref,
26
- className: clsx(className, 'MuiBox-root'),
29
+ className: clsx(className, generateClassName ? generateClassName(defaultClassName) : defaultClassName),
27
30
  theme: theme
28
31
  }, other));
29
32
  });
@@ -49,7 +52,7 @@ export default function createBox() {
49
52
  /**
50
53
  * @ignore
51
54
  */
52
- sx: PropTypes.object
55
+ sx: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
53
56
  } : void 0;
54
57
  return Box;
55
58
  }
@@ -118,7 +118,11 @@ export default function createStyled() {
118
118
  }
119
119
 
120
120
  var expressionsWithDefaultTheme = expressions ? expressions.map(function (stylesArg) {
121
- return typeof stylesArg === 'function' ? function (_ref) {
121
+ // On the server emotion doesn't use React.forwardRef for creating components, so the created
122
+ // component stays as a function. This condition makes sure that we do not interpolate functions
123
+ // which are basically components used as a selectors.
124
+ // eslint-disable-next-line no-underscore-dangle
125
+ return typeof stylesArg === 'function' && stylesArg.__emotion_real !== stylesArg ? function (_ref) {
122
126
  var themeInput = _ref.theme,
123
127
  other = _objectWithoutProperties(_ref, ["theme"]);
124
128
 
@@ -10,12 +10,12 @@ export default function createBreakpoints(breakpoints) {
10
10
  xs: 0,
11
11
  // phone
12
12
  sm: 600,
13
- // tablets
13
+ // tablet
14
14
  md: 900,
15
15
  // small laptop
16
16
  lg: 1200,
17
17
  // desktop
18
- xl: 1536 // large screens
18
+ xl: 1536 // large screen
19
19
 
20
20
  } : _breakpoints$values,
21
21
  _breakpoints$unit = breakpoints.unit,
@@ -0,0 +1,215 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
5
+ import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
6
+ import * as React from 'react';
7
+ import PropTypes from 'prop-types';
8
+ import { GlobalStyles } from '@mui/styled-engine';
9
+ import { deepmerge } from '@mui/utils';
10
+ import cssVarsParser from './cssVarsParser';
11
+ import ThemeProvider from '../ThemeProvider';
12
+ import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
13
+ import useCurrentColorScheme from './useCurrentColorScheme';
14
+ import { jsx as _jsx } from "react/jsx-runtime";
15
+ import { jsxs as _jsxs } from "react/jsx-runtime";
16
+ export default function createCssVarsProvider(options) {
17
+ var _options$theme = options.theme,
18
+ baseTheme = _options$theme === void 0 ? {} : _options$theme,
19
+ _options$defaultMode = options.defaultMode,
20
+ desisgnSystemMode = _options$defaultMode === void 0 ? 'light' : _options$defaultMode,
21
+ designSystemColorScheme = options.defaultColorScheme,
22
+ _options$prefix = options.prefix,
23
+ designSystemPrefix = _options$prefix === void 0 ? '' : _options$prefix,
24
+ shouldSkipGeneratingVar = options.shouldSkipGeneratingVar;
25
+
26
+ 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
+ console.error("MUI: `".concat(designSystemColorScheme, "` does not exist in `theme.colorSchemes`."));
28
+ }
29
+
30
+ var ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
31
+
32
+ var useColorScheme = function useColorScheme() {
33
+ var value = React.useContext(ColorSchemeContext);
34
+
35
+ if (!value) {
36
+ throw new Error(process.env.NODE_ENV !== "production" ? "MUI: `useColorScheme` must be called under <CssVarsProvider />" : _formatMuiErrorMessage(19));
37
+ }
38
+
39
+ return value;
40
+ };
41
+
42
+ function CssVarsProvider(_ref) {
43
+ var children = _ref.children,
44
+ _ref$theme = _ref.theme,
45
+ themeProp = _ref$theme === void 0 ? {} : _ref$theme,
46
+ _ref$prefix = _ref.prefix,
47
+ prefix = _ref$prefix === void 0 ? designSystemPrefix : _ref$prefix,
48
+ _ref$modeStorageKey = _ref.modeStorageKey,
49
+ modeStorageKey = _ref$modeStorageKey === void 0 ? DEFAULT_MODE_STORAGE_KEY : _ref$modeStorageKey,
50
+ _ref$attribute = _ref.attribute,
51
+ attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute,
52
+ _ref$defaultMode = _ref.defaultMode,
53
+ defaultMode = _ref$defaultMode === void 0 ? desisgnSystemMode : _ref$defaultMode,
54
+ _ref$defaultColorSche = _ref.defaultColorScheme,
55
+ defaultColorScheme = _ref$defaultColorSche === void 0 ? designSystemColorScheme : _ref$defaultColorSche;
56
+
57
+ var _baseTheme$colorSchem = baseTheme.colorSchemes,
58
+ baseColorSchemes = _baseTheme$colorSchem === void 0 ? {} : _baseTheme$colorSchem,
59
+ restBaseTheme = _objectWithoutProperties(baseTheme, ["colorSchemes"]);
60
+
61
+ var _themeProp$colorSchem = themeProp.colorSchemes,
62
+ colorSchemesProp = _themeProp$colorSchem === void 0 ? {} : _themeProp$colorSchem,
63
+ restThemeProp = _objectWithoutProperties(themeProp, ["colorSchemes"]);
64
+
65
+ var mergedTheme = deepmerge(restBaseTheme, restThemeProp);
66
+ var colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
67
+ var allColorSchemes = Object.keys(colorSchemes);
68
+ var defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
69
+ var defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
70
+
71
+ var _useCurrentColorSchem = useCurrentColorScheme({
72
+ supportedColorSchemes: allColorSchemes,
73
+ defaultLightColorScheme: defaultLightColorScheme,
74
+ defaultDarkColorScheme: defaultDarkColorScheme,
75
+ modeStorageKey: modeStorageKey,
76
+ defaultMode: defaultMode
77
+ }),
78
+ mode = _useCurrentColorSchem.mode,
79
+ setMode = _useCurrentColorSchem.setMode,
80
+ lightColorScheme = _useCurrentColorSchem.lightColorScheme,
81
+ darkColorScheme = _useCurrentColorSchem.darkColorScheme,
82
+ colorScheme = _useCurrentColorSchem.colorScheme,
83
+ setColorScheme = _useCurrentColorSchem.setColorScheme;
84
+
85
+ var resolvedColorScheme = function () {
86
+ if (!colorScheme) {
87
+ // This scope occurs on the server
88
+ if (defaultMode === 'dark') {
89
+ return defaultDarkColorScheme;
90
+ } // use light color scheme, if default mode is 'light' | 'auto'
91
+
92
+
93
+ return defaultLightColorScheme;
94
+ }
95
+
96
+ return colorScheme;
97
+ }();
98
+
99
+ var _cssVarsParser = cssVarsParser(mergedTheme, {
100
+ prefix: prefix,
101
+ basePrefix: designSystemPrefix,
102
+ shouldSkipGeneratingVar: shouldSkipGeneratingVar
103
+ }),
104
+ rootCss = _cssVarsParser.css,
105
+ rootVars = _cssVarsParser.vars;
106
+
107
+ mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
108
+ vars: rootVars
109
+ });
110
+ var styleSheet = {};
111
+ Object.entries(colorSchemes).forEach(function (_ref2) {
112
+ var _ref3 = _slicedToArray(_ref2, 2),
113
+ key = _ref3[0],
114
+ scheme = _ref3[1];
115
+
116
+ var _cssVarsParser2 = cssVarsParser(scheme, {
117
+ prefix: prefix,
118
+ basePrefix: designSystemPrefix,
119
+ shouldSkipGeneratingVar: shouldSkipGeneratingVar
120
+ }),
121
+ css = _cssVarsParser2.css,
122
+ vars = _cssVarsParser2.vars;
123
+
124
+ if (key === resolvedColorScheme) {
125
+ mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
126
+ }
127
+
128
+ var resolvedDefaultColorScheme = function () {
129
+ if (typeof defaultColorScheme === 'string') {
130
+ return defaultColorScheme;
131
+ }
132
+
133
+ if (defaultMode === 'dark') {
134
+ return defaultColorScheme.dark;
135
+ }
136
+
137
+ return defaultColorScheme.light;
138
+ }();
139
+
140
+ if (key === resolvedDefaultColorScheme) {
141
+ styleSheet[':root'] = css;
142
+ } else {
143
+ styleSheet["[".concat(attribute, "=\"").concat(key, "\"]")] = css;
144
+ }
145
+ });
146
+ React.useEffect(function () {
147
+ if (colorScheme) {
148
+ document.body.setAttribute(attribute, colorScheme);
149
+ }
150
+ }, [colorScheme, attribute]);
151
+ return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
152
+ value: {
153
+ mode: mode,
154
+ setMode: setMode,
155
+ lightColorScheme: lightColorScheme,
156
+ darkColorScheme: darkColorScheme,
157
+ colorScheme: colorScheme,
158
+ setColorScheme: setColorScheme,
159
+ allColorSchemes: allColorSchemes
160
+ },
161
+ children: [/*#__PURE__*/_jsx(GlobalStyles, {
162
+ styles: {
163
+ ':root': rootCss
164
+ }
165
+ }), /*#__PURE__*/_jsx(GlobalStyles, {
166
+ styles: styleSheet
167
+ }), /*#__PURE__*/_jsx(ThemeProvider, {
168
+ theme: mergedTheme,
169
+ children: children
170
+ })]
171
+ });
172
+ }
173
+
174
+ process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
175
+ /**
176
+ * The body attribute name to attach colorScheme.
177
+ */
178
+ attribute: PropTypes.string,
179
+
180
+ /**
181
+ * Your component tree.
182
+ */
183
+ children: PropTypes.node,
184
+
185
+ /**
186
+ * The initial color scheme used.
187
+ */
188
+ defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
189
+
190
+ /**
191
+ * The initial mode used.
192
+ */
193
+ defaultMode: PropTypes.string,
194
+
195
+ /**
196
+ * The key in the local storage used to store current color scheme.
197
+ */
198
+ modeStorageKey: PropTypes.string,
199
+
200
+ /**
201
+ * css variable prefix
202
+ */
203
+ prefix: PropTypes.string,
204
+
205
+ /**
206
+ * The calculated theme object that will be passed through context.
207
+ */
208
+ theme: PropTypes.object
209
+ } : void 0;
210
+ return {
211
+ CssVarsProvider: CssVarsProvider,
212
+ useColorScheme: useColorScheme,
213
+ getInitColorSchemeScript: getInitColorSchemeScript
214
+ };
215
+ }
@@ -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
+ }