@mui/system 5.0.6 → 5.1.0

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