@mui/system 5.0.6 → 5.2.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 (67) hide show
  1. package/Box/Box.spec.d.ts +1 -1
  2. package/CHANGELOG.md +299 -9
  3. package/LICENSE +21 -21
  4. package/breakpoints.js +42 -9
  5. package/createBox.d.ts +5 -1
  6. package/createBox.js +5 -3
  7. package/createTheme/createBreakpoints.js +2 -2
  8. package/createTheme/createSpacing.d.ts +10 -10
  9. package/cssVars/createCssVarsProvider.d.ts +88 -38
  10. package/cssVars/createCssVarsProvider.js +84 -64
  11. package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
  12. package/cssVars/cssVarsParser.d.ts +68 -57
  13. package/cssVars/cssVarsParser.js +41 -11
  14. package/cssVars/getInitColorSchemeScript.d.ts +12 -7
  15. package/cssVars/getInitColorSchemeScript.js +27 -5
  16. package/cssVars/index.d.ts +2 -2
  17. package/cssVars/useCurrentColorScheme.d.ts +50 -0
  18. package/cssVars/useCurrentColorScheme.js +235 -0
  19. package/esm/breakpoints.js +40 -9
  20. package/esm/createBox.js +5 -3
  21. package/esm/createTheme/createBreakpoints.js +2 -2
  22. package/esm/cssVars/createCssVarsProvider.js +83 -66
  23. package/esm/cssVars/cssVarsParser.js +40 -11
  24. package/esm/cssVars/getInitColorSchemeScript.js +24 -3
  25. package/esm/cssVars/useCurrentColorScheme.js +217 -0
  26. package/esm/index.js +1 -0
  27. package/esm/styleFunctionSx/extendSxProp.js +20 -1
  28. package/esm/styleFunctionSx/styleFunctionSx.js +47 -35
  29. package/esm/sx/index.js +1 -0
  30. package/esm/sx/sx.js +12 -0
  31. package/index.d.ts +2 -0
  32. package/index.js +10 -1
  33. package/index.spec.d.ts +1 -1
  34. package/legacy/breakpoints.js +40 -9
  35. package/legacy/createBox.js +6 -3
  36. package/legacy/createTheme/createBreakpoints.js +2 -2
  37. package/legacy/cssVars/createCssVarsProvider.js +85 -73
  38. package/legacy/cssVars/cssVarsParser.js +37 -9
  39. package/legacy/cssVars/getInitColorSchemeScript.js +12 -4
  40. package/legacy/cssVars/useCurrentColorScheme.js +231 -0
  41. package/legacy/index.js +2 -1
  42. package/legacy/styleFunctionSx/extendSxProp.js +21 -1
  43. package/legacy/styleFunctionSx/styleFunctionSx.js +47 -35
  44. package/legacy/sx/index.js +1 -0
  45. package/legacy/sx/sx.js +13 -0
  46. package/modern/breakpoints.js +40 -9
  47. package/modern/createBox.js +5 -3
  48. package/modern/createTheme/createBreakpoints.js +2 -2
  49. package/modern/cssVars/createCssVarsProvider.js +83 -66
  50. package/modern/cssVars/cssVarsParser.js +40 -11
  51. package/modern/cssVars/getInitColorSchemeScript.js +24 -3
  52. package/modern/cssVars/useCurrentColorScheme.js +217 -0
  53. package/modern/index.js +2 -1
  54. package/modern/styleFunctionSx/extendSxProp.js +20 -1
  55. package/modern/styleFunctionSx/styleFunctionSx.js +47 -35
  56. package/modern/sx/index.js +1 -0
  57. package/modern/sx/sx.js +12 -0
  58. package/package.json +8 -8
  59. package/styleFunctionSx/extendSxProp.js +21 -1
  60. package/styleFunctionSx/styleFunctionSx.d.ts +2 -1
  61. package/styleFunctionSx/styleFunctionSx.js +49 -37
  62. package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
  63. package/sx/index.d.ts +1 -0
  64. package/sx/index.js +15 -0
  65. package/sx/package.json +6 -0
  66. package/sx/sx.d.ts +4 -0
  67. package/sx/sx.js +22 -0
@@ -1,13 +1,15 @@
1
1
  import * as React from 'react';
2
+ import { Result, Mode } from './useCurrentColorScheme';
2
3
 
3
- type PartialDeep<T> = {
4
- [K in keyof T]?: PartialDeep<T[K]>;
4
+ type RequiredDeep<T> = {
5
+ [K in keyof T]-?: RequiredDeep<T[K]>;
5
6
  };
6
7
 
7
8
  export type BuildCssVarsTheme<ThemeInput> = ThemeInput extends {
8
- colorSchemes: Record<string, infer Colors>;
9
+ colorSchemes: Record<string, infer ColorSystems>;
9
10
  }
10
- ? Omit<ThemeInput, 'colorSchemes'> & { vars: Omit<ThemeInput, 'colorSchemes'> & Colors }
11
+ ? Omit<ThemeInput, 'colorSchemes'> &
12
+ ColorSystems & { vars: Omit<ThemeInput, 'colorSchemes'> & ColorSystems }
11
13
  : never;
12
14
 
13
15
  /**
@@ -18,59 +20,107 @@ export type BuildCssVarsTheme<ThemeInput> = ThemeInput extends {
18
20
  * If yes, they must provide the palette of the extended colorScheme. Otherwise `theme` is optional.
19
21
  */
20
22
  type DecideTheme<
21
- Theme extends { colorSchemes: Record<DesignSystemColorScheme | ApplicationColorScheme, any> },
23
+ DesignSystemTheme extends { colorSchemes: Record<DesignSystemColorScheme, any> },
22
24
  DesignSystemColorScheme extends string,
25
+ ApplicationTheme extends { colorSchemes: Record<ApplicationColorScheme, any> },
23
26
  ApplicationColorScheme extends string | never,
24
27
  > = [ApplicationColorScheme] extends [never]
25
- ? { theme?: PartialDeep<Theme> }
28
+ ? { theme?: DesignSystemTheme }
26
29
  : {
27
- theme: PartialDeep<Omit<Theme, 'colorSchemes'>> & {
28
- colorSchemes: PartialDeep<
29
- Record<DesignSystemColorScheme, Theme['colorSchemes'][DesignSystemColorScheme]>
30
+ theme: Omit<ApplicationTheme, 'colorSchemes'> & {
31
+ colorSchemes: Partial<
32
+ Record<
33
+ DesignSystemColorScheme,
34
+ DesignSystemTheme['colorSchemes'][DesignSystemColorScheme]
35
+ >
30
36
  > &
31
- Record<ApplicationColorScheme, Theme['colorSchemes'][ApplicationColorScheme]>;
37
+ RequiredDeep<
38
+ Record<ApplicationColorScheme, ApplicationTheme['colorSchemes'][ApplicationColorScheme]>
39
+ >;
32
40
  };
33
41
  };
34
42
 
35
- export interface ColorSchemeContextValue<DesignSystemColorScheme extends string> {
36
- allColorSchemes: DesignSystemColorScheme[];
37
- colorScheme: DesignSystemColorScheme | undefined;
38
- setColorScheme: React.Dispatch<React.SetStateAction<DesignSystemColorScheme | undefined>>;
43
+ export interface ColorSchemeContextValue<SupportedColorScheme extends string>
44
+ extends Result<SupportedColorScheme> {
45
+ allColorSchemes: SupportedColorScheme[];
39
46
  }
40
47
 
41
48
  export default function createCssVarsProvider<
42
- ThemeInput extends {
43
- colorSchemes: Record<DesignSystemColorScheme | ApplicationColorScheme, any>;
49
+ DesignSystemThemeInput extends {
50
+ colorSchemes: Record<DesignSystemColorScheme, any>;
44
51
  },
45
52
  DesignSystemColorScheme extends string,
53
+ ApplicationThemeInput extends {
54
+ colorSchemes: Record<ApplicationColorScheme, any>;
55
+ } = never,
46
56
  ApplicationColorScheme extends string = never,
47
- >(
48
- ThemeContext: React.Context<BuildCssVarsTheme<ThemeInput> | undefined>,
49
- options: {
50
- theme: Omit<ThemeInput, 'colorSchemes'> & {
51
- colorSchemes: Record<
52
- DesignSystemColorScheme,
53
- ThemeInput['colorSchemes'][DesignSystemColorScheme]
54
- > &
55
- Partial<
56
- Record<
57
- ApplicationColorScheme,
58
- ThemeInput['colorSchemes'][DesignSystemColorScheme | ApplicationColorScheme]
59
- >
60
- >;
61
- };
62
- defaultColorScheme: DesignSystemColorScheme;
63
- prefix?: string;
64
- },
65
- ): {
57
+ >(options: {
58
+ /**
59
+ * Design system default theme
60
+ */
61
+ theme: DesignSystemThemeInput;
62
+ /**
63
+ * Design system default color scheme
64
+ */
65
+ defaultColorScheme:
66
+ | DesignSystemColorScheme
67
+ | { light: DesignSystemColorScheme; dark: DesignSystemColorScheme };
68
+ /**
69
+ * Design system default mode
70
+ * @default 'light'
71
+ */
72
+ defaultMode?: Mode;
73
+ /**
74
+ * CSS variable prefix
75
+ * @default ''
76
+ */
77
+ prefix?: string;
78
+ /**
79
+ * A function to determine if the key, value should be attached as CSS Variable
80
+ * `keys` is an array that represents the object path keys.
81
+ * Ex, if the theme is { foo: { bar: 'var(--test)' } }
82
+ * then, keys = ['foo', 'bar']
83
+ * value = 'var(--test)'
84
+ */
85
+ shouldSkipGeneratingVar?: (keys: string[], value: string | number) => boolean;
86
+ }): {
66
87
  CssVarsProvider: (
67
88
  props: React.PropsWithChildren<
68
89
  {
69
- defaultColorScheme?: DesignSystemColorScheme | ApplicationColorScheme;
70
- storageKey?: string;
90
+ /**
91
+ * Application default mode (overrides design system `defaultMode` if specified)
92
+ */
93
+ defaultMode?: Mode;
94
+ /**
95
+ * Application default colorScheme (overrides design system `defaultColorScheme` if specified)
96
+ */
97
+ defaultColorScheme?:
98
+ | DesignSystemColorScheme
99
+ | ApplicationColorScheme
100
+ | {
101
+ light: DesignSystemColorScheme | ApplicationColorScheme;
102
+ dark: DesignSystemColorScheme | ApplicationColorScheme;
103
+ };
104
+ /**
105
+ * localStorage key used to store application `mode`
106
+ * @default 'mui-mode'
107
+ */
108
+ modeStorageKey?: string;
109
+ /**
110
+ * DOM attribute for applying color scheme
111
+ * @default 'data-mui-color-scheme'
112
+ */
71
113
  attribute?: string;
114
+ /**
115
+ * CSS variable prefix (overrides design system `prefix` if specified)
116
+ */
72
117
  prefix?: string;
73
- } & DecideTheme<ThemeInput, DesignSystemColorScheme, ApplicationColorScheme>
118
+ } & DecideTheme<
119
+ DesignSystemThemeInput,
120
+ DesignSystemColorScheme,
121
+ ApplicationThemeInput,
122
+ ApplicationColorScheme
123
+ >
74
124
  >,
75
125
  ) => React.ReactElement;
76
126
  useColorScheme: () => ColorSchemeContextValue<DesignSystemColorScheme | ApplicationColorScheme>;
@@ -21,8 +21,12 @@ var _styledEngine = require("@mui/styled-engine");
21
21
 
22
22
  var _cssVarsParser = _interopRequireDefault(require("./cssVarsParser"));
23
23
 
24
+ var _ThemeProvider = _interopRequireDefault(require("../ThemeProvider"));
25
+
24
26
  var _getInitColorSchemeScript = _interopRequireWildcard(require("./getInitColorSchemeScript"));
25
27
 
28
+ var _useCurrentColorScheme = _interopRequireDefault(require("./useCurrentColorScheme"));
29
+
26
30
  var _jsxRuntime = require("react/jsx-runtime");
27
31
 
28
32
  const _excluded = ["colorSchemes"],
@@ -32,33 +36,16 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
32
36
 
33
37
  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; }
34
38
 
35
- const resolveMode = (key, fallback, supportedColorSchemes) => {
36
- if (typeof window === 'undefined') {
37
- return undefined;
38
- }
39
-
40
- let value;
41
-
42
- try {
43
- value = localStorage.getItem(key) || undefined;
44
-
45
- if (!supportedColorSchemes.includes(value)) {
46
- value = undefined;
47
- }
48
- } catch (e) {// Unsupported
49
- }
50
-
51
- return value || fallback;
52
- };
53
-
54
- function createCssVarsProvider(ThemeContext, options) {
39
+ function createCssVarsProvider(options) {
55
40
  const {
56
41
  theme: baseTheme = {},
42
+ defaultMode: desisgnSystemMode = 'light',
57
43
  defaultColorScheme: designSystemColorScheme,
58
- prefix: designSystemPrefix = ''
44
+ prefix: designSystemPrefix = '',
45
+ shouldSkipGeneratingVar
59
46
  } = options;
60
47
 
61
- if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
48
+ 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]) {
62
49
  console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
63
50
  }
64
51
 
@@ -78,8 +65,9 @@ function createCssVarsProvider(ThemeContext, options) {
78
65
  children,
79
66
  theme: themeProp = {},
80
67
  prefix = designSystemPrefix,
81
- storageKey = _getInitColorSchemeScript.DEFAULT_STORAGE_KEY,
68
+ modeStorageKey = _getInitColorSchemeScript.DEFAULT_MODE_STORAGE_KEY,
82
69
  attribute = _getInitColorSchemeScript.DEFAULT_ATTRIBUTE,
70
+ defaultMode = desisgnSystemMode,
83
71
  defaultColorScheme = designSystemColorScheme
84
72
  }) {
85
73
  const {
@@ -93,16 +81,47 @@ function createCssVarsProvider(ThemeContext, options) {
93
81
  let mergedTheme = (0, _utils.deepmerge)(restBaseTheme, restThemeProp);
94
82
  const colorSchemes = (0, _utils.deepmerge)(baseColorSchemes, colorSchemesProp);
95
83
  const allColorSchemes = Object.keys(colorSchemes);
96
- const joinedColorSchemes = allColorSchemes.join(',');
97
- const [colorScheme, setColorScheme] = React.useState(() => resolveMode(storageKey, defaultColorScheme, allColorSchemes));
98
- const resolvedColorScheme = colorScheme || defaultColorScheme;
84
+ const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
85
+ const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
86
+ const {
87
+ mode,
88
+ setMode,
89
+ lightColorScheme,
90
+ darkColorScheme,
91
+ colorScheme,
92
+ setColorScheme
93
+ } = (0, _useCurrentColorScheme.default)({
94
+ supportedColorSchemes: allColorSchemes,
95
+ defaultLightColorScheme,
96
+ defaultDarkColorScheme,
97
+ modeStorageKey,
98
+ defaultMode
99
+ });
100
+
101
+ const resolvedColorScheme = (() => {
102
+ if (!colorScheme) {
103
+ // This scope occurs on the server
104
+ if (defaultMode === 'dark') {
105
+ return defaultDarkColorScheme;
106
+ } // use light color scheme, if default mode is 'light' | 'auto'
107
+
108
+
109
+ return defaultLightColorScheme;
110
+ }
111
+
112
+ return colorScheme;
113
+ })();
114
+
99
115
  const {
100
116
  css: rootCss,
101
117
  vars: rootVars
102
118
  } = (0, _cssVarsParser.default)(mergedTheme, {
103
- prefix
119
+ prefix,
120
+ basePrefix: designSystemPrefix,
121
+ shouldSkipGeneratingVar
104
122
  });
105
123
  mergedTheme = (0, _extends2.default)({}, mergedTheme, colorSchemes[resolvedColorScheme], {
124
+ colorSchemes,
106
125
  vars: rootVars
107
126
  });
108
127
  const styleSheet = {};
@@ -111,15 +130,26 @@ function createCssVarsProvider(ThemeContext, options) {
111
130
  css,
112
131
  vars
113
132
  } = (0, _cssVarsParser.default)(scheme, {
114
- prefix
133
+ prefix,
134
+ basePrefix: designSystemPrefix,
135
+ shouldSkipGeneratingVar
115
136
  });
137
+ mergedTheme.vars = (0, _utils.deepmerge)(mergedTheme.vars, vars);
116
138
 
117
- if (key === resolvedColorScheme) {
118
- mergedTheme.vars = (0, _extends2.default)({}, mergedTheme.vars, vars);
119
- }
139
+ const resolvedDefaultColorScheme = (() => {
140
+ if (typeof defaultColorScheme === 'string') {
141
+ return defaultColorScheme;
142
+ }
143
+
144
+ if (defaultMode === 'dark') {
145
+ return defaultColorScheme.dark;
146
+ }
120
147
 
121
- if (key === defaultColorScheme) {
122
- styleSheet[':root'] = (0, _utils.deepmerge)(rootCss, css);
148
+ return defaultColorScheme.light;
149
+ })();
150
+
151
+ if (key === resolvedDefaultColorScheme) {
152
+ styleSheet[':root'] = css;
123
153
  } else {
124
154
  styleSheet[`[${attribute}="${key}"]`] = css;
125
155
  }
@@ -127,41 +157,26 @@ function createCssVarsProvider(ThemeContext, options) {
127
157
  React.useEffect(() => {
128
158
  if (colorScheme) {
129
159
  document.body.setAttribute(attribute, colorScheme);
130
- localStorage.setItem(storageKey, colorScheme);
131
- }
132
- }, [colorScheme, attribute, storageKey]); // local storage modified in the context of another document
133
-
134
- React.useEffect(() => {
135
- const handleStorage = event => {
136
- const storageColorScheme = event.newValue;
137
-
138
- if (event.key === storageKey && joinedColorSchemes.match(storageColorScheme)) {
139
- if (storageColorScheme) {
140
- setColorScheme(storageColorScheme);
141
- }
142
- }
143
- };
144
-
145
- window.addEventListener('storage', handleStorage);
146
- return () => window.removeEventListener('storage', handleStorage);
147
- }, [setColorScheme, storageKey, joinedColorSchemes]);
148
- const wrappedSetColorScheme = React.useCallback(val => {
149
- if (typeof val === 'string' && !allColorSchemes.includes(val)) {
150
- console.error(`\`${val}\` does not exist in \`theme.colorSchemes\`.`);
151
- } else {
152
- setColorScheme(val);
153
160
  }
154
- }, [setColorScheme, allColorSchemes]);
161
+ }, [colorScheme, attribute]);
155
162
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(ColorSchemeContext.Provider, {
156
163
  value: {
164
+ mode,
165
+ setMode,
166
+ lightColorScheme,
167
+ darkColorScheme,
157
168
  colorScheme,
158
- setColorScheme: wrappedSetColorScheme,
169
+ setColorScheme,
159
170
  allColorSchemes
160
171
  },
161
172
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
173
+ styles: {
174
+ ':root': rootCss
175
+ }
176
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
162
177
  styles: styleSheet
163
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(ThemeContext.Provider, {
164
- value: mergedTheme,
178
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ThemeProvider.default, {
179
+ theme: mergedTheme,
165
180
  children: children
166
181
  })]
167
182
  });
@@ -181,17 +196,22 @@ function createCssVarsProvider(ThemeContext, options) {
181
196
  /**
182
197
  * The initial color scheme used.
183
198
  */
184
- defaultColorScheme: _propTypes.default.string,
199
+ defaultColorScheme: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]),
185
200
 
186
201
  /**
187
- * css variable prefix
202
+ * The initial mode used.
188
203
  */
189
- prefix: _propTypes.default.string,
204
+ defaultMode: _propTypes.default.string,
190
205
 
191
206
  /**
192
207
  * The key in the local storage used to store current color scheme.
193
208
  */
194
- storageKey: _propTypes.default.string,
209
+ modeStorageKey: _propTypes.default.string,
210
+
211
+ /**
212
+ * css variable prefix
213
+ */
214
+ prefix: _propTypes.default.string,
195
215
 
196
216
  /**
197
217
  * The calculated theme object that will be passed through context.
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,57 +1,68 @@
1
- declare type NestedRecord<V = any> = {
2
- [k: string | number]: NestedRecord<V> | V;
3
- };
4
- /**
5
- * This function create an object from keys, value and then assign to target
6
- *
7
- * @param {Object} obj : the target object to be assigned
8
- * @param {string[]} keys
9
- * @param {string | number} value
10
- *
11
- * @example
12
- * const source = {}
13
- * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
14
- * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
15
- *
16
- * @example
17
- * const source = { palette: { primary: 'var(--palette-primary)' } }
18
- * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
19
- * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
20
- */
21
- export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value) => void;
22
- /**
23
- *
24
- * @param {Object} obj : source object
25
- * @param {Function} callback : a function that will be called when
26
- * - the deepest key in source object is reached
27
- * - the value of the deepest key is NOT `undefined` | `null`
28
- *
29
- * @example
30
- * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
31
- * // ['palette', 'primary', 'main'] '#000000'
32
- */
33
- export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value) => void) => void;
34
- /**
35
- * a function that parse theme and return { css, vars }
36
- *
37
- * @param {Object} theme
38
- * @param {{ prefix?: string }} options
39
- * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
40
- *
41
- * @example
42
- * const { css, vars } = parser({
43
- * fontSize: 12,
44
- * lineHeight: 1.2,
45
- * palette: { primary: { 500: '#000000' } }
46
- * })
47
- *
48
- * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
49
- * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
50
- */
51
- export default function cssVarsParser(obj: Record<string, any>, options?: {
52
- prefix?: string;
53
- }): {
54
- css: NestedRecord<string>;
55
- vars: NestedRecord<string>;
56
- };
57
- export {};
1
+ declare type NestedRecord<V = any> = {
2
+ [k: string | number]: NestedRecord<V> | V;
3
+ };
4
+ /**
5
+ * This function create an object from keys, value and then assign to target
6
+ *
7
+ * @param {Object} obj : the target object to be assigned
8
+ * @param {string[]} keys
9
+ * @param {string | number} value
10
+ *
11
+ * @example
12
+ * const source = {}
13
+ * assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
14
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
15
+ *
16
+ * @example
17
+ * const source = { palette: { primary: 'var(--palette-primary)' } }
18
+ * assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
19
+ * console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
20
+ */
21
+ export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value) => void;
22
+ /**
23
+ *
24
+ * @param {Object} obj : source object
25
+ * @param {Function} callback : a function that will be called when
26
+ * - the deepest key in source object is reached
27
+ * - the value of the deepest key is NOT `undefined` | `null`
28
+ *
29
+ * @example
30
+ * walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
31
+ * // ['palette', 'primary', 'main'] '#000000'
32
+ */
33
+ export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value, scope: Record<string, string | number>) => void) => void;
34
+ /**
35
+ * a function that parse theme and return { css, vars }
36
+ *
37
+ * @param {Object} theme
38
+ * @param {{
39
+ * prefix?: string,
40
+ * basePrefix?: string,
41
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
42
+ * }} options.
43
+ * `basePrefix`: defined by design system.
44
+ * `prefix`: defined by application
45
+ *
46
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
47
+ *
48
+ * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
49
+ *
50
+ * @example
51
+ * const { css, vars } = parser({
52
+ * fontSize: 12,
53
+ * lineHeight: 1.2,
54
+ * palette: { primary: { 500: '#000000' } }
55
+ * })
56
+ *
57
+ * console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
58
+ * console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
59
+ */
60
+ export default function cssVarsParser(theme: Record<string, any>, options?: {
61
+ prefix?: string;
62
+ basePrefix?: string;
63
+ shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
64
+ }): {
65
+ css: NestedRecord<string>;
66
+ vars: NestedRecord<string>;
67
+ };
68
+ export {};
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
@@ -7,6 +9,8 @@ exports.assignNestedKeys = void 0;
7
9
  exports.default = cssVarsParser;
8
10
  exports.walkObjectDeep = void 0;
9
11
 
12
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
+
10
14
  /**
11
15
  * This function create an object from keys, value and then assign to target
12
16
  *
@@ -62,7 +66,7 @@ const walkObjectDeep = (obj, callback) => {
62
66
  if (typeof value === 'object' && Object.keys(value).length > 0) {
63
67
  recurse(value, [...parentKeys, key]);
64
68
  } else {
65
- callback([...parentKeys, key], value);
69
+ callback([...parentKeys, key], value, object);
66
70
  }
67
71
  }
68
72
  });
@@ -89,7 +93,16 @@ const getCssValue = (keys, value) => {
89
93
  * a function that parse theme and return { css, vars }
90
94
  *
91
95
  * @param {Object} theme
92
- * @param {{ prefix?: string }} options
96
+ * @param {{
97
+ * prefix?: string,
98
+ * basePrefix?: string,
99
+ * shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
100
+ * }} options.
101
+ * `basePrefix`: defined by design system.
102
+ * `prefix`: defined by application
103
+ *
104
+ * This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
105
+ *
93
106
  * @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
94
107
  *
95
108
  * @example
@@ -104,19 +117,36 @@ const getCssValue = (keys, value) => {
104
117
  */
105
118
 
106
119
 
107
- function cssVarsParser(obj, options) {
120
+ function cssVarsParser(theme, options) {
121
+ const clonedTheme = (0, _extends2.default)({}, theme);
122
+ delete clonedTheme.vars; // remove 'vars' from the structure
123
+
108
124
  const {
109
- prefix
125
+ prefix,
126
+ basePrefix = '',
127
+ shouldSkipGeneratingVar
110
128
  } = options || {};
111
129
  const css = {};
112
130
  const vars = {};
113
- walkObjectDeep(obj, (keys, value) => {
114
- if (typeof value === 'string' || typeof value === 'number') {
115
- const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
116
- Object.assign(css, {
117
- [cssVar]: getCssValue(keys, value)
118
- });
119
- assignNestedKeys(vars, keys, `var(${cssVar})`);
131
+ walkObjectDeep(clonedTheme, (keys, val, scope) => {
132
+ if (typeof val === 'string' || typeof val === 'number') {
133
+ let value = val;
134
+
135
+ if (typeof value === 'string' && value.startsWith('var')) {
136
+ // replace the value of the `scope` object with the prefix or remove basePrefix from the value
137
+ value = prefix ? value.replace(basePrefix, prefix) : value.replace(`${basePrefix}-`, ''); // scope is the deepest object in the tree, keys is the theme path keys
138
+
139
+ scope[keys.slice(-1)[0]] = value;
140
+ }
141
+
142
+ if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, value)) {
143
+ // only create css & var if `shouldSkipGeneratingVar` return false
144
+ const cssVar = `--${prefix ? `${prefix}-` : ''}${keys.join('-')}`;
145
+ Object.assign(css, {
146
+ [cssVar]: getCssValue(keys, value)
147
+ });
148
+ assignNestedKeys(vars, keys, `var(${cssVar})`);
149
+ }
120
150
  }
121
151
  });
122
152
  return {
@@ -1,7 +1,12 @@
1
- /// <reference types="react" />
2
- export declare const DEFAULT_STORAGE_KEY = "mui-color-scheme";
3
- export declare const DEFAULT_ATTRIBUTE = "data-mui-color-scheme";
4
- export default function getInitColorSchemeScript(options?: {
5
- storageKey?: string;
6
- attribute?: string;
7
- }): JSX.Element;
1
+ /// <reference types="react" />
2
+ export declare const DEFAULT_MODE_STORAGE_KEY = "mui-mode";
3
+ export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "mui-color-scheme";
4
+ export declare const DEFAULT_ATTRIBUTE = "data-mui-color-scheme";
5
+ export default function getInitColorSchemeScript(options?: {
6
+ enableSystem?: boolean;
7
+ defaultLightColorScheme?: string;
8
+ defaultDarkColorScheme?: string;
9
+ modeStorageKey?: string;
10
+ colorSchemeStorageKey?: string;
11
+ attribute?: string;
12
+ }): JSX.Element;