@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,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.assignNestedKeys = void 0;
|
|
7
|
+
exports.default = cssVarsParser;
|
|
8
|
+
exports.walkObjectDeep = void 0;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This function create an object from keys, value and then assign to target
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} obj : the target object to be assigned
|
|
14
|
+
* @param {string[]} keys
|
|
15
|
+
* @param {string | number} value
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const source = {}
|
|
19
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
20
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
24
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
25
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
26
|
+
*/
|
|
27
|
+
const assignNestedKeys = (obj, keys, value) => {
|
|
28
|
+
let temp = obj;
|
|
29
|
+
keys.forEach((k, index) => {
|
|
30
|
+
if (index === keys.length - 1) {
|
|
31
|
+
if (temp && typeof temp === 'object') {
|
|
32
|
+
temp[k] = value;
|
|
33
|
+
}
|
|
34
|
+
} else if (temp && typeof temp === 'object') {
|
|
35
|
+
if (!temp[k]) {
|
|
36
|
+
temp[k] = {};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
temp = temp[k];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param {Object} obj : source object
|
|
46
|
+
* @param {Function} callback : a function that will be called when
|
|
47
|
+
* - the deepest key in source object is reached
|
|
48
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
52
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
exports.assignNestedKeys = assignNestedKeys;
|
|
57
|
+
|
|
58
|
+
const walkObjectDeep = (obj, callback) => {
|
|
59
|
+
function recurse(object, parentKeys = []) {
|
|
60
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
61
|
+
if (value !== undefined && value !== null) {
|
|
62
|
+
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
63
|
+
recurse(value, [...parentKeys, key]);
|
|
64
|
+
} else {
|
|
65
|
+
callback([...parentKeys, key], value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
recurse(obj);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
exports.walkObjectDeep = walkObjectDeep;
|
|
75
|
+
|
|
76
|
+
const getCssValue = (keys, value) => {
|
|
77
|
+
if (typeof value === 'number') {
|
|
78
|
+
if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
|
|
79
|
+
// css property that are unitless
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return `${value}px`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return value;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* a function that parse theme and return { css, vars }
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} theme
|
|
92
|
+
* @param {{ prefix?: string }} options
|
|
93
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const { css, vars } = parser({
|
|
97
|
+
* fontSize: 12,
|
|
98
|
+
* lineHeight: 1.2,
|
|
99
|
+
* palette: { primary: { 500: '#000000' } }
|
|
100
|
+
* })
|
|
101
|
+
*
|
|
102
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
103
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
function cssVarsParser(obj, options) {
|
|
108
|
+
const {
|
|
109
|
+
prefix
|
|
110
|
+
} = options || {};
|
|
111
|
+
const css = {};
|
|
112
|
+
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})`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
css,
|
|
124
|
+
vars
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
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;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DEFAULT_STORAGE_KEY = exports.DEFAULT_ATTRIBUTE = void 0;
|
|
7
|
+
exports.default = getInitColorSchemeScript;
|
|
8
|
+
|
|
9
|
+
var React = _interopRequireWildcard(require("react"));
|
|
10
|
+
|
|
11
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
12
|
+
|
|
13
|
+
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); }
|
|
14
|
+
|
|
15
|
+
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; }
|
|
16
|
+
|
|
17
|
+
const DEFAULT_STORAGE_KEY = 'mui-color-scheme';
|
|
18
|
+
exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;
|
|
19
|
+
const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
20
|
+
exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
|
|
21
|
+
|
|
22
|
+
function getInitColorSchemeScript(options) {
|
|
23
|
+
const {
|
|
24
|
+
storageKey = DEFAULT_STORAGE_KEY,
|
|
25
|
+
attribute = DEFAULT_ATTRIBUTE
|
|
26
|
+
} = options || {};
|
|
27
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
|
|
28
|
+
// eslint-disable-next-line react/no-danger
|
|
29
|
+
dangerouslySetInnerHTML: {
|
|
30
|
+
__html: `(function() { try {
|
|
31
|
+
var colorScheme = localStorage.getItem('${storageKey}');
|
|
32
|
+
if (colorScheme) {
|
|
33
|
+
document.body.setAttribute('${attribute}', colorScheme);
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {} })();`
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
package/cssVars/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "default", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _createCssVarsProvider.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
var _createCssVarsProvider = _interopRequireDefault(require("./createCssVarsProvider"));
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
export const DEFAULT_STORAGE_KEY = 'mui-color-scheme';
|
|
4
|
+
export const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
5
|
+
export default function getInitColorSchemeScript(options) {
|
|
6
|
+
const {
|
|
7
|
+
storageKey = DEFAULT_STORAGE_KEY,
|
|
8
|
+
attribute = DEFAULT_ATTRIBUTE
|
|
9
|
+
} = options || {};
|
|
10
|
+
return /*#__PURE__*/_jsx("script", {
|
|
11
|
+
// eslint-disable-next-line react/no-danger
|
|
12
|
+
dangerouslySetInnerHTML: {
|
|
13
|
+
__html: `(function() { try {
|
|
14
|
+
var colorScheme = localStorage.getItem('${storageKey}');
|
|
15
|
+
if (colorScheme) {
|
|
16
|
+
document.body.setAttribute('${attribute}', colorScheme);
|
|
17
|
+
}
|
|
18
|
+
} catch (e) {} })();`
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './createCssVarsProvider';
|
package/esm/index.js
CHANGED
|
@@ -36,4 +36,5 @@ export { default as useThemeProps, getThemeProps } from './useThemeProps';
|
|
|
36
36
|
export { default as useTheme } from './useTheme';
|
|
37
37
|
export { default as useThemeWithoutDefault } from './useThemeWithoutDefault';
|
|
38
38
|
export * from './colorManipulator';
|
|
39
|
-
export { default as ThemeProvider } from './ThemeProvider';
|
|
39
|
+
export { default as ThemeProvider } from './ThemeProvider';
|
|
40
|
+
export { default as unstable_createCssVarsProvider } from './cssVars/createCssVarsProvider';
|
|
@@ -22,11 +22,11 @@ function styleFunctionSx(props) {
|
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
return styles(theme);
|
|
27
|
-
}
|
|
25
|
+
let stylesObject = styles;
|
|
28
26
|
|
|
29
|
-
if (typeof styles
|
|
27
|
+
if (typeof styles === 'function') {
|
|
28
|
+
stylesObject = styles(theme);
|
|
29
|
+
} else if (typeof styles !== 'object') {
|
|
30
30
|
// value
|
|
31
31
|
return styles;
|
|
32
32
|
}
|
|
@@ -34,8 +34,8 @@ function styleFunctionSx(props) {
|
|
|
34
34
|
const emptyBreakpoints = createEmptyBreakpointObject(theme.breakpoints);
|
|
35
35
|
const breakpointsKeys = Object.keys(emptyBreakpoints);
|
|
36
36
|
let css = emptyBreakpoints;
|
|
37
|
-
Object.keys(
|
|
38
|
-
const value = callIfFn(
|
|
37
|
+
Object.keys(stylesObject).forEach(styleKey => {
|
|
38
|
+
const value = callIfFn(stylesObject[styleKey], theme);
|
|
39
39
|
|
|
40
40
|
if (typeof value === 'object') {
|
|
41
41
|
if (propToStyleFunction[styleKey]) {
|
package/index.d.ts
CHANGED
|
@@ -107,6 +107,9 @@ export {
|
|
|
107
107
|
GlobalStyles,
|
|
108
108
|
GlobalStylesProps,
|
|
109
109
|
StyledEngineProvider,
|
|
110
|
+
Interpolation,
|
|
111
|
+
CSSInterpolation,
|
|
112
|
+
CSSObject,
|
|
110
113
|
} from '@mui/styled-engine';
|
|
111
114
|
|
|
112
115
|
export * from './style';
|
|
@@ -154,3 +157,6 @@ export * from './colorManipulator';
|
|
|
154
157
|
|
|
155
158
|
export { default as ThemeProvider } from './ThemeProvider';
|
|
156
159
|
export * from './ThemeProvider';
|
|
160
|
+
|
|
161
|
+
export { default as unstable_createCssVarsProvider } from './cssVars';
|
|
162
|
+
export * from './cssVars';
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @license MUI v5.0.
|
|
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.
|
|
@@ -47,7 +47,8 @@ var _exportNames = {
|
|
|
47
47
|
getThemeProps: true,
|
|
48
48
|
useTheme: true,
|
|
49
49
|
useThemeWithoutDefault: true,
|
|
50
|
-
ThemeProvider: true
|
|
50
|
+
ThemeProvider: true,
|
|
51
|
+
unstable_createCssVarsProvider: true
|
|
51
52
|
};
|
|
52
53
|
Object.defineProperty(exports, "Box", {
|
|
53
54
|
enumerable: true,
|
|
@@ -229,6 +230,12 @@ Object.defineProperty(exports, "typography", {
|
|
|
229
230
|
return _typography.default;
|
|
230
231
|
}
|
|
231
232
|
});
|
|
233
|
+
Object.defineProperty(exports, "unstable_createCssVarsProvider", {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
get: function () {
|
|
236
|
+
return _createCssVarsProvider.default;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
232
239
|
Object.defineProperty(exports, "unstable_extendSxProp", {
|
|
233
240
|
enumerable: true,
|
|
234
241
|
get: function () {
|
|
@@ -450,6 +457,8 @@ Object.keys(_colorManipulator).forEach(function (key) {
|
|
|
450
457
|
|
|
451
458
|
var _ThemeProvider = _interopRequireDefault(require("./ThemeProvider"));
|
|
452
459
|
|
|
460
|
+
var _createCssVarsProvider = _interopRequireDefault(require("./cssVars/createCssVarsProvider"));
|
|
461
|
+
|
|
453
462
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
454
463
|
|
|
455
464
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|