@mui/system 5.6.2 → 5.7.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.
- package/Box/Box.d.ts +18 -1
- package/Box/Box.js +26 -0
- package/Box/Box.spec.d.ts +1 -1
- package/CHANGELOG.md +225 -0
- package/ThemeProvider/ThemeProvider.d.ts +7 -1
- package/ThemeProvider/ThemeProvider.js +9 -2
- package/createBox.js +0 -26
- package/createBox.spec.d.ts +1 -1
- package/createStyled.js +3 -1
- package/createTheme/createSpacing.d.ts +10 -10
- package/cssVars/createCssVarsProvider.d.ts +25 -0
- package/cssVars/createCssVarsProvider.js +74 -57
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
- package/cssVars/createGetCssVar.d.ts +5 -5
- package/cssVars/cssVarsParser.d.ts +70 -70
- package/cssVars/cssVarsParser.js +11 -9
- package/cssVars/getInitColorSchemeScript.d.ts +40 -12
- package/cssVars/getInitColorSchemeScript.js +4 -3
- package/cssVars/index.d.ts +2 -2
- package/cssVars/useCurrentColorScheme.d.ts +53 -50
- package/cssVars/useCurrentColorScheme.js +17 -7
- package/esm/Box/Box.js +25 -0
- package/esm/ThemeProvider/ThemeProvider.js +9 -2
- package/esm/createBox.js +0 -25
- package/esm/createStyled.js +3 -1
- package/esm/cssVars/createCssVarsProvider.js +76 -57
- package/esm/cssVars/cssVarsParser.js +11 -9
- package/esm/cssVars/getInitColorSchemeScript.js +4 -3
- package/esm/cssVars/useCurrentColorScheme.js +17 -7
- package/esm/spacing.js +3 -1
- package/esm/style.js +7 -1
- package/index.js +1 -1
- package/index.spec.d.ts +1 -1
- package/legacy/Box/Box.js +25 -0
- package/legacy/ThemeProvider/ThemeProvider.js +9 -2
- package/legacy/createBox.js +0 -25
- package/legacy/createStyled.js +3 -1
- package/legacy/cssVars/createCssVarsProvider.js +83 -55
- package/legacy/cssVars/cssVarsParser.js +11 -7
- package/legacy/cssVars/getInitColorSchemeScript.js +6 -3
- package/legacy/cssVars/useCurrentColorScheme.js +20 -9
- package/legacy/index.js +1 -1
- package/legacy/spacing.js +3 -1
- package/legacy/style.js +5 -1
- package/modern/Box/Box.js +25 -0
- package/modern/ThemeProvider/ThemeProvider.js +9 -2
- package/modern/createBox.js +0 -25
- package/modern/createStyled.js +3 -1
- package/modern/cssVars/createCssVarsProvider.js +76 -55
- package/modern/cssVars/cssVarsParser.js +11 -9
- package/modern/cssVars/getInitColorSchemeScript.js +4 -3
- package/modern/cssVars/useCurrentColorScheme.js +17 -7
- package/modern/index.js +1 -1
- package/modern/spacing.js +1 -1
- package/modern/style.js +7 -1
- package/package.json +5 -5
- package/spacing.js +3 -1
- package/style.js +7 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +3 -1
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
|
@@ -20,10 +20,6 @@ var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
20
20
|
|
|
21
21
|
var _styledEngine = require("@mui/styled-engine");
|
|
22
22
|
|
|
23
|
-
var _createSpacing = _interopRequireDefault(require("../createTheme/createSpacing"));
|
|
24
|
-
|
|
25
|
-
var _createBreakpoints = _interopRequireDefault(require("../createTheme/createBreakpoints"));
|
|
26
|
-
|
|
27
23
|
var _cssVarsParser = _interopRequireDefault(require("./cssVarsParser"));
|
|
28
24
|
|
|
29
25
|
var _ThemeProvider = _interopRequireDefault(require("../ThemeProvider"));
|
|
@@ -36,9 +32,7 @@ var _createGetCssVar = _interopRequireDefault(require("./createGetCssVar"));
|
|
|
36
32
|
|
|
37
33
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
38
34
|
|
|
39
|
-
const _excluded = ["colorSchemes"]
|
|
40
|
-
_excluded2 = ["colorSchemes"],
|
|
41
|
-
_excluded3 = ["components"];
|
|
35
|
+
const _excluded = ["colorSchemes", "components"];
|
|
42
36
|
|
|
43
37
|
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); }
|
|
44
38
|
|
|
@@ -48,10 +42,8 @@ const DISABLE_CSS_TRANSITION = '*{-webkit-transition:none!important;-moz-transit
|
|
|
48
42
|
exports.DISABLE_CSS_TRANSITION = DISABLE_CSS_TRANSITION;
|
|
49
43
|
|
|
50
44
|
function createCssVarsProvider(options) {
|
|
51
|
-
var _baseTheme$breakpoint;
|
|
52
|
-
|
|
53
45
|
const {
|
|
54
|
-
theme:
|
|
46
|
+
theme: defaultTheme = {},
|
|
55
47
|
defaultMode: desisgnSystemMode = 'light',
|
|
56
48
|
defaultColorScheme: designSystemColorScheme,
|
|
57
49
|
disableTransitionOnChange: designSystemTransitionOnChange = false,
|
|
@@ -60,10 +52,8 @@ function createCssVarsProvider(options) {
|
|
|
60
52
|
shouldSkipGeneratingVar,
|
|
61
53
|
resolveTheme
|
|
62
54
|
} = options;
|
|
63
|
-
const systemSpacing = (0, _createSpacing.default)(baseTheme.spacing);
|
|
64
|
-
const systemBreakpoints = (0, _createBreakpoints.default)((_baseTheme$breakpoint = baseTheme.breakpoints) != null ? _baseTheme$breakpoint : {});
|
|
65
55
|
|
|
66
|
-
if (!
|
|
56
|
+
if (!defaultTheme.colorSchemes || typeof designSystemColorScheme === 'string' && !defaultTheme.colorSchemes[designSystemColorScheme] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.light] || typeof designSystemColorScheme === 'object' && !defaultTheme.colorSchemes[designSystemColorScheme == null ? void 0 : designSystemColorScheme.dark]) {
|
|
67
57
|
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
68
58
|
}
|
|
69
59
|
|
|
@@ -81,32 +71,26 @@ function createCssVarsProvider(options) {
|
|
|
81
71
|
|
|
82
72
|
function CssVarsProvider({
|
|
83
73
|
children,
|
|
84
|
-
theme: themeProp =
|
|
74
|
+
theme: themeProp = defaultTheme,
|
|
85
75
|
prefix = designSystemPrefix,
|
|
86
76
|
modeStorageKey = _getInitColorSchemeScript.DEFAULT_MODE_STORAGE_KEY,
|
|
77
|
+
colorSchemeStorageKey = _getInitColorSchemeScript.DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
87
78
|
attribute = _getInitColorSchemeScript.DEFAULT_ATTRIBUTE,
|
|
88
79
|
defaultMode = desisgnSystemMode,
|
|
89
80
|
defaultColorScheme = designSystemColorScheme,
|
|
90
81
|
disableTransitionOnChange = designSystemTransitionOnChange,
|
|
91
|
-
enableColorScheme = designSystemEnableColorScheme
|
|
82
|
+
enableColorScheme = designSystemEnableColorScheme,
|
|
83
|
+
storageWindow = typeof window === 'undefined' ? undefined : window,
|
|
84
|
+
documentNode = typeof document === 'undefined' ? undefined : document,
|
|
85
|
+
colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
|
|
86
|
+
colorSchemeSelector = ':root'
|
|
92
87
|
}) {
|
|
88
|
+
const hasMounted = React.useRef(false);
|
|
93
89
|
const {
|
|
94
|
-
colorSchemes
|
|
95
|
-
} = baseTheme,
|
|
96
|
-
restBaseTheme = (0, _objectWithoutPropertiesLoose2.default)(baseTheme, _excluded);
|
|
97
|
-
const {
|
|
98
|
-
colorSchemes: colorSchemesProp = {}
|
|
99
|
-
} = themeProp,
|
|
100
|
-
restThemeProp = (0, _objectWithoutPropertiesLoose2.default)(themeProp, _excluded2);
|
|
101
|
-
const hasMounted = React.useRef(false); // eslint-disable-next-line prefer-const
|
|
102
|
-
|
|
103
|
-
let _deepmerge = (0, _utils.deepmerge)(restBaseTheme, restThemeProp),
|
|
104
|
-
{
|
|
90
|
+
colorSchemes = {},
|
|
105
91
|
components = {}
|
|
106
|
-
} =
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const colorSchemes = (0, _utils.deepmerge)(baseColorSchemes, colorSchemesProp);
|
|
92
|
+
} = themeProp,
|
|
93
|
+
restThemeProp = (0, _objectWithoutPropertiesLoose2.default)(themeProp, _excluded);
|
|
110
94
|
const allColorSchemes = Object.keys(colorSchemes);
|
|
111
95
|
const defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
112
96
|
const defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
@@ -123,7 +107,9 @@ function createCssVarsProvider(options) {
|
|
|
123
107
|
defaultLightColorScheme,
|
|
124
108
|
defaultDarkColorScheme,
|
|
125
109
|
modeStorageKey,
|
|
126
|
-
|
|
110
|
+
colorSchemeStorageKey,
|
|
111
|
+
defaultMode,
|
|
112
|
+
storageWindow
|
|
127
113
|
});
|
|
128
114
|
|
|
129
115
|
const resolvedColorScheme = (() => {
|
|
@@ -140,22 +126,21 @@ function createCssVarsProvider(options) {
|
|
|
140
126
|
return colorScheme;
|
|
141
127
|
})();
|
|
142
128
|
|
|
129
|
+
let theme = restThemeProp;
|
|
143
130
|
const {
|
|
144
131
|
css: rootCss,
|
|
145
132
|
vars: rootVars,
|
|
146
133
|
parsedTheme
|
|
147
|
-
} = (0, _cssVarsParser.default)(
|
|
134
|
+
} = (0, _cssVarsParser.default)(theme, {
|
|
148
135
|
prefix,
|
|
149
136
|
basePrefix: designSystemPrefix,
|
|
150
137
|
shouldSkipGeneratingVar
|
|
151
138
|
});
|
|
152
|
-
|
|
139
|
+
theme = (0, _extends2.default)({}, parsedTheme, {
|
|
153
140
|
components,
|
|
154
141
|
colorSchemes,
|
|
155
142
|
prefix,
|
|
156
143
|
vars: rootVars,
|
|
157
|
-
spacing: themeProp.spacing ? (0, _createSpacing.default)(themeProp.spacing) : systemSpacing,
|
|
158
|
-
breakpoints: themeProp.breakpoints ? (0, _createBreakpoints.default)(themeProp.breakpoints) : systemBreakpoints,
|
|
159
144
|
getCssVar: (0, _createGetCssVar.default)(prefix)
|
|
160
145
|
});
|
|
161
146
|
const styleSheet = {};
|
|
@@ -169,10 +154,16 @@ function createCssVarsProvider(options) {
|
|
|
169
154
|
basePrefix: designSystemPrefix,
|
|
170
155
|
shouldSkipGeneratingVar
|
|
171
156
|
});
|
|
172
|
-
|
|
157
|
+
theme.vars = (0, _utils.deepmerge)(theme.vars, vars);
|
|
173
158
|
|
|
174
159
|
if (key === resolvedColorScheme) {
|
|
175
|
-
|
|
160
|
+
theme = (0, _extends2.default)({}, theme, parsedScheme);
|
|
161
|
+
|
|
162
|
+
if (theme.palette) {
|
|
163
|
+
// assign runtime mode & colorScheme
|
|
164
|
+
theme.palette.mode = mode;
|
|
165
|
+
theme.palette.colorScheme = resolvedColorScheme;
|
|
166
|
+
}
|
|
176
167
|
}
|
|
177
168
|
|
|
178
169
|
const resolvedDefaultColorScheme = (() => {
|
|
@@ -188,54 +179,54 @@ function createCssVarsProvider(options) {
|
|
|
188
179
|
})();
|
|
189
180
|
|
|
190
181
|
if (key === resolvedDefaultColorScheme) {
|
|
191
|
-
styleSheet[
|
|
182
|
+
styleSheet[colorSchemeSelector] = css;
|
|
192
183
|
} else {
|
|
193
|
-
styleSheet[
|
|
184
|
+
styleSheet[`${colorSchemeSelector === ':root' ? '' : colorSchemeSelector}[${attribute}="${key}"]`] = css;
|
|
194
185
|
}
|
|
195
186
|
});
|
|
196
187
|
React.useEffect(() => {
|
|
197
|
-
if (colorScheme) {
|
|
188
|
+
if (colorScheme && colorSchemeNode) {
|
|
198
189
|
// attaches attribute to <html> because the css variables are attached to :root (html)
|
|
199
|
-
|
|
190
|
+
colorSchemeNode.setAttribute(attribute, colorScheme);
|
|
200
191
|
}
|
|
201
|
-
}, [colorScheme, attribute]);
|
|
192
|
+
}, [colorScheme, attribute, colorSchemeNode]);
|
|
202
193
|
(0, _utils.unstable_useEnhancedEffect)(() => {
|
|
203
|
-
if (!mode || !enableColorScheme) {
|
|
194
|
+
if (!mode || !enableColorScheme || !colorSchemeNode) {
|
|
204
195
|
return undefined;
|
|
205
196
|
}
|
|
206
197
|
|
|
207
|
-
const priorColorScheme =
|
|
198
|
+
const priorColorScheme = colorSchemeNode.style.getPropertyValue('color-scheme'); // `color-scheme` tells browser to render built-in elements according to its value: `light` or `dark`
|
|
208
199
|
|
|
209
200
|
if (mode === 'system') {
|
|
210
|
-
|
|
201
|
+
colorSchemeNode.style.setProperty('color-scheme', systemMode);
|
|
211
202
|
} else {
|
|
212
|
-
|
|
203
|
+
colorSchemeNode.style.setProperty('color-scheme', mode);
|
|
213
204
|
}
|
|
214
205
|
|
|
215
206
|
return () => {
|
|
216
|
-
|
|
207
|
+
colorSchemeNode.style.setProperty('color-scheme', priorColorScheme);
|
|
217
208
|
};
|
|
218
|
-
}, [mode, systemMode, enableColorScheme]);
|
|
209
|
+
}, [mode, systemMode, enableColorScheme, colorSchemeNode]);
|
|
219
210
|
React.useEffect(() => {
|
|
220
211
|
let timer;
|
|
221
212
|
|
|
222
|
-
if (disableTransitionOnChange && hasMounted.current) {
|
|
213
|
+
if (disableTransitionOnChange && hasMounted.current && documentNode) {
|
|
223
214
|
// credit: https://github.com/pacocoursey/next-themes/blob/b5c2bad50de2d61ad7b52a9c5cdc801a78507d7a/index.tsx#L313
|
|
224
|
-
const css =
|
|
225
|
-
css.appendChild(
|
|
226
|
-
|
|
215
|
+
const css = documentNode.createElement('style');
|
|
216
|
+
css.appendChild(documentNode.createTextNode(DISABLE_CSS_TRANSITION));
|
|
217
|
+
documentNode.head.appendChild(css); // Force browser repaint
|
|
227
218
|
|
|
228
|
-
(() => window.getComputedStyle(
|
|
219
|
+
(() => window.getComputedStyle(documentNode.body))();
|
|
229
220
|
|
|
230
221
|
timer = setTimeout(() => {
|
|
231
|
-
|
|
222
|
+
documentNode.head.removeChild(css);
|
|
232
223
|
}, 1);
|
|
233
224
|
}
|
|
234
225
|
|
|
235
226
|
return () => {
|
|
236
227
|
clearTimeout(timer);
|
|
237
228
|
};
|
|
238
|
-
}, [colorScheme, disableTransitionOnChange]);
|
|
229
|
+
}, [colorScheme, disableTransitionOnChange, documentNode]);
|
|
239
230
|
React.useEffect(() => {
|
|
240
231
|
hasMounted.current = true;
|
|
241
232
|
return () => {
|
|
@@ -254,12 +245,12 @@ function createCssVarsProvider(options) {
|
|
|
254
245
|
},
|
|
255
246
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
|
|
256
247
|
styles: {
|
|
257
|
-
|
|
248
|
+
[colorSchemeSelector]: rootCss
|
|
258
249
|
}
|
|
259
250
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
|
|
260
251
|
styles: styleSheet
|
|
261
252
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ThemeProvider.default, {
|
|
262
|
-
theme: resolveTheme ? resolveTheme(
|
|
253
|
+
theme: resolveTheme ? resolveTheme(theme) : theme,
|
|
263
254
|
children: children
|
|
264
255
|
})]
|
|
265
256
|
});
|
|
@@ -276,6 +267,21 @@ function createCssVarsProvider(options) {
|
|
|
276
267
|
*/
|
|
277
268
|
children: _propTypes.default.node,
|
|
278
269
|
|
|
270
|
+
/**
|
|
271
|
+
* The node used to attach the color-scheme attribute
|
|
272
|
+
*/
|
|
273
|
+
colorSchemeNode: _propTypes.default.any,
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The CSS selector for attaching the generated custom properties
|
|
277
|
+
*/
|
|
278
|
+
colorSchemeSelector: _propTypes.default.string,
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* localStorage key used to store `colorScheme`
|
|
282
|
+
*/
|
|
283
|
+
colorSchemeStorageKey: _propTypes.default.string,
|
|
284
|
+
|
|
279
285
|
/**
|
|
280
286
|
* The initial color scheme used.
|
|
281
287
|
*/
|
|
@@ -291,6 +297,11 @@ function createCssVarsProvider(options) {
|
|
|
291
297
|
*/
|
|
292
298
|
disableTransitionOnChange: _propTypes.default.bool,
|
|
293
299
|
|
|
300
|
+
/**
|
|
301
|
+
* The document to attach the attribute to
|
|
302
|
+
*/
|
|
303
|
+
documentNode: _propTypes.default.any,
|
|
304
|
+
|
|
294
305
|
/**
|
|
295
306
|
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
|
|
296
307
|
*/
|
|
@@ -306,6 +317,12 @@ function createCssVarsProvider(options) {
|
|
|
306
317
|
*/
|
|
307
318
|
prefix: _propTypes.default.string,
|
|
308
319
|
|
|
320
|
+
/**
|
|
321
|
+
* The window that attaches the 'storage' event listener
|
|
322
|
+
* @default window
|
|
323
|
+
*/
|
|
324
|
+
storageWindow: _propTypes.default.any,
|
|
325
|
+
|
|
309
326
|
/**
|
|
310
327
|
* The calculated theme object that will be passed through context.
|
|
311
328
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The benefit of this function is to help developers get CSS var from theme without specifying the whole variable
|
|
3
|
-
* and they does not need to remember the prefix (defined once).
|
|
4
|
-
*/
|
|
5
|
-
export default function createGetCssVar<T extends string = string>(prefix?: string): <AdditionalVars extends string = never>(field: T | AdditionalVars, ...vars: (T | AdditionalVars)[]) => string;
|
|
1
|
+
/**
|
|
2
|
+
* The benefit of this function is to help developers get CSS var from theme without specifying the whole variable
|
|
3
|
+
* and they does not need to remember the prefix (defined once).
|
|
4
|
+
*/
|
|
5
|
+
export default function createGetCssVar<T extends string = string>(prefix?: string): <AdditionalVars extends string = never>(field: T | AdditionalVars, ...vars: (T | AdditionalVars)[]) => string;
|
|
@@ -1,70 +1,70 @@
|
|
|
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,
|
|
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
|
-
* the CSS variable value will be adjusted based on the provided `basePrefix` & `prefix` which can be found in `parsedTheme`.
|
|
47
|
-
*
|
|
48
|
-
* @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* const { css, vars, parsedTheme } = parser({
|
|
52
|
-
* fontSize: 12,
|
|
53
|
-
* lineHeight: 1.2,
|
|
54
|
-
* palette: { primary: { 500: 'var(--color)' } }
|
|
55
|
-
* }, { prefix: 'foo' })
|
|
56
|
-
*
|
|
57
|
-
* console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--foo-color)' }
|
|
58
|
-
* console.log(vars) // { fontSize: '--foo-fontSize', lineHeight: '--foo-lineHeight', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
|
|
59
|
-
* console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--foo-color)' } } }
|
|
60
|
-
*/
|
|
61
|
-
export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
|
|
62
|
-
prefix?: string;
|
|
63
|
-
basePrefix?: string;
|
|
64
|
-
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
65
|
-
}): {
|
|
66
|
-
css: NestedRecord<string>;
|
|
67
|
-
vars: NestedRecord<string>;
|
|
68
|
-
parsedTheme: T;
|
|
69
|
-
};
|
|
70
|
-
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, arrayKeys?: Array<string>) => 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, arrayKeys: Array<string>) => void, shouldSkipPaths?: ((keys: Array<string>) => boolean) | undefined) => 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
|
+
* the CSS variable value will be adjusted based on the provided `basePrefix` & `prefix` which can be found in `parsedTheme`.
|
|
47
|
+
*
|
|
48
|
+
* @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const { css, vars, parsedTheme } = parser({
|
|
52
|
+
* fontSize: 12,
|
|
53
|
+
* lineHeight: 1.2,
|
|
54
|
+
* palette: { primary: { 500: 'var(--color)' } }
|
|
55
|
+
* }, { prefix: 'foo' })
|
|
56
|
+
*
|
|
57
|
+
* console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--foo-color)' }
|
|
58
|
+
* console.log(vars) // { fontSize: '--foo-fontSize', lineHeight: '--foo-lineHeight', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
|
|
59
|
+
* console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--foo-color)' } } }
|
|
60
|
+
*/
|
|
61
|
+
export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
|
|
62
|
+
prefix?: string;
|
|
63
|
+
basePrefix?: string;
|
|
64
|
+
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
65
|
+
}): {
|
|
66
|
+
css: NestedRecord<string>;
|
|
67
|
+
vars: NestedRecord<string>;
|
|
68
|
+
parsedTheme: T;
|
|
69
|
+
};
|
|
70
|
+
export {};
|
package/cssVars/cssVarsParser.js
CHANGED
|
@@ -24,16 +24,18 @@ exports.walkObjectDeep = void 0;
|
|
|
24
24
|
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
25
25
|
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
26
26
|
*/
|
|
27
|
-
const assignNestedKeys = (obj, keys, value) => {
|
|
27
|
+
const assignNestedKeys = (obj, keys, value, arrayKeys = []) => {
|
|
28
28
|
let temp = obj;
|
|
29
29
|
keys.forEach((k, index) => {
|
|
30
30
|
if (index === keys.length - 1) {
|
|
31
|
-
if (temp
|
|
31
|
+
if (Array.isArray(temp)) {
|
|
32
|
+
temp[Number(k)] = value;
|
|
33
|
+
} else if (temp && typeof temp === 'object') {
|
|
32
34
|
temp[k] = value;
|
|
33
35
|
}
|
|
34
36
|
} else if (temp && typeof temp === 'object') {
|
|
35
37
|
if (!temp[k]) {
|
|
36
|
-
temp[k] = {};
|
|
38
|
+
temp[k] = arrayKeys.includes(k) ? [] : {};
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
temp = temp[k];
|
|
@@ -56,14 +58,14 @@ const assignNestedKeys = (obj, keys, value) => {
|
|
|
56
58
|
exports.assignNestedKeys = assignNestedKeys;
|
|
57
59
|
|
|
58
60
|
const walkObjectDeep = (obj, callback, shouldSkipPaths) => {
|
|
59
|
-
function recurse(object, parentKeys = []) {
|
|
61
|
+
function recurse(object, parentKeys = [], arrayKeys = []) {
|
|
60
62
|
Object.entries(object).forEach(([key, value]) => {
|
|
61
63
|
if (!shouldSkipPaths || shouldSkipPaths && !shouldSkipPaths([...parentKeys, key])) {
|
|
62
64
|
if (value !== undefined && value !== null) {
|
|
63
65
|
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
64
|
-
recurse(value, [...parentKeys, key]);
|
|
66
|
+
recurse(value, [...parentKeys, key], Array.isArray(value) ? [...arrayKeys, key] : arrayKeys);
|
|
65
67
|
} else {
|
|
66
|
-
callback([...parentKeys, key], value,
|
|
68
|
+
callback([...parentKeys, key], value, arrayKeys);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
}
|
|
@@ -132,7 +134,7 @@ function cssVarsParser(theme, options) {
|
|
|
132
134
|
const css = {};
|
|
133
135
|
const vars = {};
|
|
134
136
|
const parsedTheme = {};
|
|
135
|
-
walkObjectDeep(theme, (keys, value) => {
|
|
137
|
+
walkObjectDeep(theme, (keys, value, arrayKeys) => {
|
|
136
138
|
if (typeof value === 'string' || typeof value === 'number') {
|
|
137
139
|
if (typeof value === 'string' && value.match(/var\(\s*--/)) {
|
|
138
140
|
// for CSS variable, apply prefix or remove basePrefix from the variable
|
|
@@ -150,11 +152,11 @@ function cssVarsParser(theme, options) {
|
|
|
150
152
|
Object.assign(css, {
|
|
151
153
|
[cssVar]: getCssValue(keys, value)
|
|
152
154
|
});
|
|
153
|
-
assignNestedKeys(vars, keys, `var(${cssVar})
|
|
155
|
+
assignNestedKeys(vars, keys, `var(${cssVar})`, arrayKeys);
|
|
154
156
|
}
|
|
155
157
|
}
|
|
156
158
|
|
|
157
|
-
assignNestedKeys(parsedTheme, keys, value);
|
|
159
|
+
assignNestedKeys(parsedTheme, keys, value, arrayKeys);
|
|
158
160
|
}, keys => keys[0] === 'vars' // skip 'vars/*' paths
|
|
159
161
|
);
|
|
160
162
|
return {
|
|
@@ -1,12 +1,40 @@
|
|
|
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
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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 interface GetInitColorSchemeScriptOptions {
|
|
6
|
+
/**
|
|
7
|
+
* If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
enableSystem?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The default color scheme to be used on the light mode
|
|
13
|
+
*/
|
|
14
|
+
defaultLightColorScheme?: string;
|
|
15
|
+
/**
|
|
16
|
+
* The default color scheme to be used on the dark mode
|
|
17
|
+
*/
|
|
18
|
+
defaultDarkColorScheme?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The node (provided as string) used to attach the color-scheme attribute
|
|
21
|
+
* @default 'document.documentElement'
|
|
22
|
+
*/
|
|
23
|
+
colorSchemeNode?: string;
|
|
24
|
+
/**
|
|
25
|
+
* localStorage key used to store `mode`
|
|
26
|
+
* @default 'mui-mode'
|
|
27
|
+
*/
|
|
28
|
+
modeStorageKey?: string;
|
|
29
|
+
/**
|
|
30
|
+
* localStorage key used to store `colorScheme`
|
|
31
|
+
* @default 'mui-color-scheme'
|
|
32
|
+
*/
|
|
33
|
+
colorSchemeStorageKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* DOM attribute for applying color scheme
|
|
36
|
+
* @default 'data-mui-color-scheme'
|
|
37
|
+
*/
|
|
38
|
+
attribute?: string;
|
|
39
|
+
}
|
|
40
|
+
export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions): JSX.Element;
|
|
@@ -23,12 +23,13 @@ exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
|
|
|
23
23
|
|
|
24
24
|
function getInitColorSchemeScript(options) {
|
|
25
25
|
const {
|
|
26
|
-
enableSystem,
|
|
26
|
+
enableSystem = false,
|
|
27
27
|
defaultLightColorScheme = 'light',
|
|
28
28
|
defaultDarkColorScheme = 'dark',
|
|
29
29
|
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
30
30
|
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
31
|
-
attribute = DEFAULT_ATTRIBUTE
|
|
31
|
+
attribute = DEFAULT_ATTRIBUTE,
|
|
32
|
+
colorSchemeNode = 'document.documentElement'
|
|
32
33
|
} = options || {};
|
|
33
34
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
|
|
34
35
|
// eslint-disable-next-line react/no-danger
|
|
@@ -52,7 +53,7 @@ function getInitColorSchemeScript(options) {
|
|
|
52
53
|
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}';
|
|
53
54
|
}
|
|
54
55
|
if (colorScheme) {
|
|
55
|
-
|
|
56
|
+
${colorSchemeNode}.setAttribute('${attribute}', colorScheme);
|
|
56
57
|
}
|
|
57
58
|
} catch (e) {} })();`
|
|
58
59
|
}
|
package/cssVars/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default } from './createCssVarsProvider';
|
|
2
|
-
export type { CreateCssVarsProviderResult } from './createCssVarsProvider';
|
|
1
|
+
export { default } from './createCssVarsProvider';
|
|
2
|
+
export type { CreateCssVarsProviderResult } from './createCssVarsProvider';
|