@mui/system 5.0.4 → 5.0.5
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/CHANGELOG.md +79 -0
- package/createStyled.d.ts +16 -190
- package/cssVars/createCssVarsProvider.d.ts +81 -0
- package/cssVars/createCssVarsProvider.js +206 -0
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
- package/cssVars/cssVarsParser.d.ts +57 -0
- package/cssVars/cssVarsParser.js +126 -0
- package/cssVars/getInitColorSchemeScript.d.ts +7 -0
- package/cssVars/getInitColorSchemeScript.js +38 -0
- package/cssVars/index.d.ts +2 -0
- package/cssVars/index.js +15 -0
- package/cssVars/package.json +6 -0
- package/esm/cssVars/createCssVarsProvider.js +188 -0
- package/esm/cssVars/cssVarsParser.js +112 -0
- package/esm/cssVars/getInitColorSchemeScript.js +21 -0
- package/esm/cssVars/index.js +1 -0
- package/esm/index.js +2 -1
- package/esm/styleFunctionSx/styleFunctionSx.js +6 -6
- package/index.d.ts +6 -0
- package/index.js +11 -2
- package/legacy/cssVars/createCssVarsProvider.js +202 -0
- package/legacy/cssVars/cssVarsParser.js +125 -0
- package/legacy/cssVars/getInitColorSchemeScript.js +18 -0
- package/legacy/cssVars/index.js +1 -0
- package/legacy/index.js +3 -2
- package/legacy/styleFunctionSx/styleFunctionSx.js +6 -6
- package/modern/cssVars/createCssVarsProvider.js +188 -0
- package/modern/cssVars/cssVarsParser.js +112 -0
- package/modern/cssVars/getInitColorSchemeScript.js +21 -0
- package/modern/cssVars/index.js +1 -0
- package/modern/index.js +3 -2
- package/modern/styleFunctionSx/styleFunctionSx.js +6 -6
- package/package.json +2 -3
- package/style.d.ts +1 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +6 -1
- package/styleFunctionSx/styleFunctionSx.js +6 -6
|
@@ -0,0 +1,202 @@
|
|
|
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 { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import PropTypes from 'prop-types';
|
|
7
|
+
import { GlobalStyles } from '@mui/styled-engine';
|
|
8
|
+
import { deepmerge } from '@mui/utils';
|
|
9
|
+
import cssVarsParser from './cssVarsParser';
|
|
10
|
+
import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
11
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
+
|
|
14
|
+
var resolveMode = function resolveMode(key, fallback, supportedColorSchemes) {
|
|
15
|
+
if (typeof window === 'undefined') {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var value;
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
value = localStorage.getItem(key) || undefined;
|
|
23
|
+
|
|
24
|
+
if (!supportedColorSchemes.includes(value)) {
|
|
25
|
+
value = undefined;
|
|
26
|
+
}
|
|
27
|
+
} catch (e) {// Unsupported
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return value || fallback;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default function createCssVarsProvider(ThemeContext, options) {
|
|
34
|
+
var _options$theme = options.theme,
|
|
35
|
+
baseTheme = _options$theme === void 0 ? {} : _options$theme,
|
|
36
|
+
designSystemColorScheme = options.defaultColorScheme,
|
|
37
|
+
_options$prefix = options.prefix,
|
|
38
|
+
designSystemPrefix = _options$prefix === void 0 ? '' : _options$prefix;
|
|
39
|
+
|
|
40
|
+
if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
|
|
41
|
+
console.error("MUI: `".concat(designSystemColorScheme, "` does not exist in `theme.colorSchemes`."));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
45
|
+
|
|
46
|
+
var useColorScheme = function useColorScheme() {
|
|
47
|
+
var value = React.useContext(ColorSchemeContext);
|
|
48
|
+
|
|
49
|
+
if (!value) {
|
|
50
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "MUI: `useColorScheme` must be called under <CssVarsProvider />" : _formatMuiErrorMessage(19));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return value;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function CssVarsProvider(_ref) {
|
|
57
|
+
var children = _ref.children,
|
|
58
|
+
_ref$theme = _ref.theme,
|
|
59
|
+
themeProp = _ref$theme === void 0 ? {} : _ref$theme,
|
|
60
|
+
_ref$prefix = _ref.prefix,
|
|
61
|
+
prefix = _ref$prefix === void 0 ? designSystemPrefix : _ref$prefix,
|
|
62
|
+
_ref$storageKey = _ref.storageKey,
|
|
63
|
+
storageKey = _ref$storageKey === void 0 ? DEFAULT_STORAGE_KEY : _ref$storageKey,
|
|
64
|
+
_ref$attribute = _ref.attribute,
|
|
65
|
+
attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute,
|
|
66
|
+
_ref$defaultColorSche = _ref.defaultColorScheme,
|
|
67
|
+
defaultColorScheme = _ref$defaultColorSche === void 0 ? designSystemColorScheme : _ref$defaultColorSche;
|
|
68
|
+
|
|
69
|
+
var _baseTheme$colorSchem = baseTheme.colorSchemes,
|
|
70
|
+
baseColorSchemes = _baseTheme$colorSchem === void 0 ? {} : _baseTheme$colorSchem,
|
|
71
|
+
restBaseTheme = _objectWithoutProperties(baseTheme, ["colorSchemes"]);
|
|
72
|
+
|
|
73
|
+
var _themeProp$colorSchem = themeProp.colorSchemes,
|
|
74
|
+
colorSchemesProp = _themeProp$colorSchem === void 0 ? {} : _themeProp$colorSchem,
|
|
75
|
+
restThemeProp = _objectWithoutProperties(themeProp, ["colorSchemes"]);
|
|
76
|
+
|
|
77
|
+
var mergedTheme = deepmerge(restBaseTheme, restThemeProp);
|
|
78
|
+
var colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
|
|
79
|
+
var allColorSchemes = Object.keys(colorSchemes);
|
|
80
|
+
var joinedColorSchemes = allColorSchemes.join(',');
|
|
81
|
+
|
|
82
|
+
var _React$useState = React.useState(function () {
|
|
83
|
+
return resolveMode(storageKey, defaultColorScheme, allColorSchemes);
|
|
84
|
+
}),
|
|
85
|
+
colorScheme = _React$useState[0],
|
|
86
|
+
setColorScheme = _React$useState[1];
|
|
87
|
+
|
|
88
|
+
var resolvedColorScheme = colorScheme || defaultColorScheme;
|
|
89
|
+
|
|
90
|
+
var _cssVarsParser = cssVarsParser(mergedTheme, {
|
|
91
|
+
prefix: prefix
|
|
92
|
+
}),
|
|
93
|
+
rootCss = _cssVarsParser.css,
|
|
94
|
+
rootVars = _cssVarsParser.vars;
|
|
95
|
+
|
|
96
|
+
mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
|
|
97
|
+
vars: rootVars
|
|
98
|
+
});
|
|
99
|
+
var styleSheet = {};
|
|
100
|
+
Object.entries(colorSchemes).forEach(function (_ref2) {
|
|
101
|
+
var _ref3 = _slicedToArray(_ref2, 2),
|
|
102
|
+
key = _ref3[0],
|
|
103
|
+
scheme = _ref3[1];
|
|
104
|
+
|
|
105
|
+
var _cssVarsParser2 = cssVarsParser(scheme, {
|
|
106
|
+
prefix: prefix
|
|
107
|
+
}),
|
|
108
|
+
css = _cssVarsParser2.css,
|
|
109
|
+
vars = _cssVarsParser2.vars;
|
|
110
|
+
|
|
111
|
+
if (key === resolvedColorScheme) {
|
|
112
|
+
mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (key === defaultColorScheme) {
|
|
116
|
+
styleSheet[':root'] = deepmerge(rootCss, css);
|
|
117
|
+
} else {
|
|
118
|
+
styleSheet["[".concat(attribute, "=\"").concat(key, "\"]")] = css;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
React.useEffect(function () {
|
|
122
|
+
if (colorScheme) {
|
|
123
|
+
document.body.setAttribute(attribute, colorScheme);
|
|
124
|
+
localStorage.setItem(storageKey, colorScheme);
|
|
125
|
+
}
|
|
126
|
+
}, [colorScheme, attribute, storageKey]); // local storage modified in the context of another document
|
|
127
|
+
|
|
128
|
+
React.useEffect(function () {
|
|
129
|
+
var handleStorage = function handleStorage(event) {
|
|
130
|
+
var storageColorScheme = event.newValue;
|
|
131
|
+
|
|
132
|
+
if (event.key === storageKey && joinedColorSchemes.match(storageColorScheme)) {
|
|
133
|
+
if (storageColorScheme) {
|
|
134
|
+
setColorScheme(storageColorScheme);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
window.addEventListener('storage', handleStorage);
|
|
140
|
+
return function () {
|
|
141
|
+
return window.removeEventListener('storage', handleStorage);
|
|
142
|
+
};
|
|
143
|
+
}, [setColorScheme, storageKey, joinedColorSchemes]);
|
|
144
|
+
var wrappedSetColorScheme = React.useCallback(function (val) {
|
|
145
|
+
if (typeof val === 'string' && !allColorSchemes.includes(val)) {
|
|
146
|
+
console.error("`".concat(val, "` does not exist in `theme.colorSchemes`."));
|
|
147
|
+
} else {
|
|
148
|
+
setColorScheme(val);
|
|
149
|
+
}
|
|
150
|
+
}, [setColorScheme, allColorSchemes]);
|
|
151
|
+
return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
|
|
152
|
+
value: {
|
|
153
|
+
colorScheme: colorScheme,
|
|
154
|
+
setColorScheme: wrappedSetColorScheme,
|
|
155
|
+
allColorSchemes: allColorSchemes
|
|
156
|
+
},
|
|
157
|
+
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
158
|
+
styles: styleSheet
|
|
159
|
+
}), /*#__PURE__*/_jsx(ThemeContext.Provider, {
|
|
160
|
+
value: mergedTheme,
|
|
161
|
+
children: children
|
|
162
|
+
})]
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
167
|
+
/**
|
|
168
|
+
* The body attribute name to attach colorScheme.
|
|
169
|
+
*/
|
|
170
|
+
attribute: PropTypes.string,
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Your component tree.
|
|
174
|
+
*/
|
|
175
|
+
children: PropTypes.node,
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The initial color scheme used.
|
|
179
|
+
*/
|
|
180
|
+
defaultColorScheme: PropTypes.string,
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* css variable prefix
|
|
184
|
+
*/
|
|
185
|
+
prefix: PropTypes.string,
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The key in the local storage used to store current color scheme.
|
|
189
|
+
*/
|
|
190
|
+
storageKey: PropTypes.string,
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The calculated theme object that will be passed through context.
|
|
194
|
+
*/
|
|
195
|
+
theme: PropTypes.object
|
|
196
|
+
} : void 0;
|
|
197
|
+
return {
|
|
198
|
+
CssVarsProvider: CssVarsProvider,
|
|
199
|
+
useColorScheme: useColorScheme,
|
|
200
|
+
getInitColorSchemeScript: getInitColorSchemeScript
|
|
201
|
+
};
|
|
202
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
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);
|
|
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 {{ prefix?: string }} options
|
|
92
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* const { css, vars } = parser({
|
|
96
|
+
* fontSize: 12,
|
|
97
|
+
* lineHeight: 1.2,
|
|
98
|
+
* palette: { primary: { 500: '#000000' } }
|
|
99
|
+
* })
|
|
100
|
+
*
|
|
101
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
102
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
export default function cssVarsParser(obj, options) {
|
|
107
|
+
var _ref3 = options || {},
|
|
108
|
+
prefix = _ref3.prefix;
|
|
109
|
+
|
|
110
|
+
var css = {};
|
|
111
|
+
var vars = {};
|
|
112
|
+
walkObjectDeep(obj, function (keys, value) {
|
|
113
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
114
|
+
var cssVar = "--".concat(prefix ? "".concat(prefix, "-") : '').concat(keys.join('-'));
|
|
115
|
+
|
|
116
|
+
_extends(css, _defineProperty({}, cssVar, getCssValue(keys, value)));
|
|
117
|
+
|
|
118
|
+
assignNestedKeys(vars, keys, "var(".concat(cssVar, ")"));
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
css: css,
|
|
123
|
+
vars: vars
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
export var DEFAULT_STORAGE_KEY = 'mui-color-scheme';
|
|
4
|
+
export var DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
5
|
+
export default function getInitColorSchemeScript(options) {
|
|
6
|
+
var _ref = options || {},
|
|
7
|
+
_ref$storageKey = _ref.storageKey,
|
|
8
|
+
storageKey = _ref$storageKey === void 0 ? DEFAULT_STORAGE_KEY : _ref$storageKey,
|
|
9
|
+
_ref$attribute = _ref.attribute,
|
|
10
|
+
attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute;
|
|
11
|
+
|
|
12
|
+
return /*#__PURE__*/_jsx("script", {
|
|
13
|
+
// eslint-disable-next-line react/no-danger
|
|
14
|
+
dangerouslySetInnerHTML: {
|
|
15
|
+
__html: "(function() { try {\n var colorScheme = localStorage.getItem('".concat(storageKey, "');\n if (colorScheme) {\n document.body.setAttribute('").concat(attribute, "', colorScheme);\n }\n } catch (e) {} })();")
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './createCssVarsProvider';
|
package/legacy/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @license MUI v5.0.
|
|
1
|
+
/** @license MUI v5.0.5
|
|
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.
|
|
@@ -41,4 +41,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
|
|
|
41
41
|
export { default as useTheme } from './useTheme';
|
|
42
42
|
export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
|
|
43
43
|
export * from './colorManipulator';
|
|
44
|
-
export { default as ThemeProvider } from './ThemeProvider';
|
|
44
|
+
export { default as ThemeProvider } from './ThemeProvider';
|
|
45
|
+
export { default as unstable_createCssVarsProvider } from './cssVars/createCssVarsProvider';
|
|
@@ -32,11 +32,11 @@ function styleFunctionSx(props) {
|
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
return styles(theme);
|
|
37
|
-
}
|
|
35
|
+
var stylesObject = styles;
|
|
38
36
|
|
|
39
|
-
if (
|
|
37
|
+
if (typeof styles === 'function') {
|
|
38
|
+
stylesObject = styles(theme);
|
|
39
|
+
} else if (_typeof(styles) !== 'object') {
|
|
40
40
|
// value
|
|
41
41
|
return styles;
|
|
42
42
|
}
|
|
@@ -44,8 +44,8 @@ function styleFunctionSx(props) {
|
|
|
44
44
|
var emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
|
|
45
45
|
var breakpointsKeys = Object.keys(emptyBreakpoints);
|
|
46
46
|
var css = emptyBreakpoints;
|
|
47
|
-
Object.keys(
|
|
48
|
-
var value = callIfFn(
|
|
47
|
+
Object.keys(stylesObject).forEach(function (styleKey) {
|
|
48
|
+
var value = callIfFn(stylesObject[styleKey], theme);
|
|
49
49
|
|
|
50
50
|
if (_typeof(value) === 'object') {
|
|
51
51
|
if (propToStyleFunction[styleKey]) {
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
|
+
import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
|
4
|
+
const _excluded = ["colorSchemes"],
|
|
5
|
+
_excluded2 = ["colorSchemes"];
|
|
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 getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
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) {
|
|
35
|
+
const {
|
|
36
|
+
theme: baseTheme = {},
|
|
37
|
+
defaultColorScheme: designSystemColorScheme,
|
|
38
|
+
prefix: designSystemPrefix = ''
|
|
39
|
+
} = options;
|
|
40
|
+
|
|
41
|
+
if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
|
|
42
|
+
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
46
|
+
|
|
47
|
+
const useColorScheme = () => {
|
|
48
|
+
const value = React.useContext(ColorSchemeContext);
|
|
49
|
+
|
|
50
|
+
if (!value) {
|
|
51
|
+
throw new Error(process.env.NODE_ENV !== "production" ? `MUI: \`useColorScheme\` must be called under <CssVarsProvider />` : _formatMuiErrorMessage(19));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function CssVarsProvider({
|
|
58
|
+
children,
|
|
59
|
+
theme: themeProp = {},
|
|
60
|
+
prefix = designSystemPrefix,
|
|
61
|
+
storageKey = DEFAULT_STORAGE_KEY,
|
|
62
|
+
attribute = DEFAULT_ATTRIBUTE,
|
|
63
|
+
defaultColorScheme = designSystemColorScheme
|
|
64
|
+
}) {
|
|
65
|
+
const {
|
|
66
|
+
colorSchemes: baseColorSchemes = {}
|
|
67
|
+
} = baseTheme,
|
|
68
|
+
restBaseTheme = _objectWithoutPropertiesLoose(baseTheme, _excluded);
|
|
69
|
+
|
|
70
|
+
const {
|
|
71
|
+
colorSchemes: colorSchemesProp = {}
|
|
72
|
+
} = themeProp,
|
|
73
|
+
restThemeProp = _objectWithoutPropertiesLoose(themeProp, _excluded2);
|
|
74
|
+
|
|
75
|
+
let mergedTheme = deepmerge(restBaseTheme, restThemeProp);
|
|
76
|
+
const colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
|
|
77
|
+
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;
|
|
81
|
+
const {
|
|
82
|
+
css: rootCss,
|
|
83
|
+
vars: rootVars
|
|
84
|
+
} = cssVarsParser(mergedTheme, {
|
|
85
|
+
prefix
|
|
86
|
+
});
|
|
87
|
+
mergedTheme = _extends({}, mergedTheme, colorSchemes[resolvedColorScheme], {
|
|
88
|
+
vars: rootVars
|
|
89
|
+
});
|
|
90
|
+
const styleSheet = {};
|
|
91
|
+
Object.entries(colorSchemes).forEach(([key, scheme]) => {
|
|
92
|
+
const {
|
|
93
|
+
css,
|
|
94
|
+
vars
|
|
95
|
+
} = cssVarsParser(scheme, {
|
|
96
|
+
prefix
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (key === resolvedColorScheme) {
|
|
100
|
+
mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (key === defaultColorScheme) {
|
|
104
|
+
styleSheet[':root'] = deepmerge(rootCss, css);
|
|
105
|
+
} else {
|
|
106
|
+
styleSheet[`[${attribute}="${key}"]`] = css;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
React.useEffect(() => {
|
|
110
|
+
if (colorScheme) {
|
|
111
|
+
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
|
+
}
|
|
136
|
+
}, [setColorScheme, allColorSchemes]);
|
|
137
|
+
return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
|
|
138
|
+
value: {
|
|
139
|
+
colorScheme,
|
|
140
|
+
setColorScheme: wrappedSetColorScheme,
|
|
141
|
+
allColorSchemes
|
|
142
|
+
},
|
|
143
|
+
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
144
|
+
styles: styleSheet
|
|
145
|
+
}), /*#__PURE__*/_jsx(ThemeContext.Provider, {
|
|
146
|
+
value: mergedTheme,
|
|
147
|
+
children: children
|
|
148
|
+
})]
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
153
|
+
/**
|
|
154
|
+
* The body attribute name to attach colorScheme.
|
|
155
|
+
*/
|
|
156
|
+
attribute: PropTypes.string,
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Your component tree.
|
|
160
|
+
*/
|
|
161
|
+
children: PropTypes.node,
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The initial color scheme used.
|
|
165
|
+
*/
|
|
166
|
+
defaultColorScheme: PropTypes.string,
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* css variable prefix
|
|
170
|
+
*/
|
|
171
|
+
prefix: PropTypes.string,
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The key in the local storage used to store current color scheme.
|
|
175
|
+
*/
|
|
176
|
+
storageKey: PropTypes.string,
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The calculated theme object that will be passed through context.
|
|
180
|
+
*/
|
|
181
|
+
theme: PropTypes.object
|
|
182
|
+
} : void 0;
|
|
183
|
+
return {
|
|
184
|
+
CssVarsProvider,
|
|
185
|
+
useColorScheme,
|
|
186
|
+
getInitColorSchemeScript
|
|
187
|
+
};
|
|
188
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function create an object from keys, value and then assign to target
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} obj : the target object to be assigned
|
|
5
|
+
* @param {string[]} keys
|
|
6
|
+
* @param {string | number} value
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const source = {}
|
|
10
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
11
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
15
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
16
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
17
|
+
*/
|
|
18
|
+
export const assignNestedKeys = (obj, keys, value) => {
|
|
19
|
+
let temp = obj;
|
|
20
|
+
keys.forEach((k, index) => {
|
|
21
|
+
if (index === keys.length - 1) {
|
|
22
|
+
if (temp && typeof temp === 'object') {
|
|
23
|
+
temp[k] = value;
|
|
24
|
+
}
|
|
25
|
+
} else if (temp && typeof temp === 'object') {
|
|
26
|
+
if (!temp[k]) {
|
|
27
|
+
temp[k] = {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
temp = temp[k];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} obj : source object
|
|
37
|
+
* @param {Function} callback : a function that will be called when
|
|
38
|
+
* - the deepest key in source object is reached
|
|
39
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
43
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
export const walkObjectDeep = (obj, callback) => {
|
|
47
|
+
function recurse(object, parentKeys = []) {
|
|
48
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
49
|
+
if (value !== undefined && value !== null) {
|
|
50
|
+
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
51
|
+
recurse(value, [...parentKeys, key]);
|
|
52
|
+
} else {
|
|
53
|
+
callback([...parentKeys, key], value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
recurse(obj);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const getCssValue = (keys, value) => {
|
|
63
|
+
if (typeof value === 'number') {
|
|
64
|
+
if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
|
|
65
|
+
// css property that are unitless
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return `${value}px`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return value;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* a function that parse theme and return { css, vars }
|
|
76
|
+
*
|
|
77
|
+
* @param {Object} theme
|
|
78
|
+
* @param {{ prefix?: string }} options
|
|
79
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const { css, vars } = parser({
|
|
83
|
+
* fontSize: 12,
|
|
84
|
+
* lineHeight: 1.2,
|
|
85
|
+
* palette: { primary: { 500: '#000000' } }
|
|
86
|
+
* })
|
|
87
|
+
*
|
|
88
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
89
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
export default function cssVarsParser(obj, options) {
|
|
94
|
+
const {
|
|
95
|
+
prefix
|
|
96
|
+
} = options || {};
|
|
97
|
+
const css = {};
|
|
98
|
+
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})`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
css,
|
|
110
|
+
vars
|
|
111
|
+
};
|
|
112
|
+
}
|