@mui/system 5.0.3 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +274 -0
- package/borders.js +1 -1
- package/breakpoints.js +45 -11
- package/colorManipulator.js +8 -8
- package/createBox.d.ts +5 -1
- package/createBox.js +5 -3
- package/createStyled.d.ts +16 -190
- package/createStyled.js +6 -2
- package/createTheme/createBreakpoints.d.ts +53 -6
- package/createTheme/createBreakpoints.js +3 -3
- package/cssVars/createCssVarsProvider.d.ts +131 -0
- package/cssVars/createCssVarsProvider.js +228 -0
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -0
- package/cssVars/cssVarsParser.d.ts +68 -0
- package/cssVars/cssVarsParser.js +156 -0
- package/cssVars/getInitColorSchemeScript.d.ts +12 -0
- package/cssVars/getInitColorSchemeScript.js +60 -0
- package/cssVars/index.d.ts +2 -0
- package/cssVars/index.js +15 -0
- package/cssVars/package.json +6 -0
- package/cssVars/useCurrentColorScheme.d.ts +50 -0
- package/cssVars/useCurrentColorScheme.js +235 -0
- package/display.js +1 -1
- package/esm/breakpoints.js +39 -8
- package/esm/createBox.js +5 -3
- package/esm/createStyled.js +5 -1
- package/esm/createTheme/createBreakpoints.js +2 -2
- package/esm/cssVars/createCssVarsProvider.js +207 -0
- package/esm/cssVars/cssVarsParser.js +141 -0
- package/esm/cssVars/getInitColorSchemeScript.js +42 -0
- package/esm/cssVars/index.js +1 -0
- package/esm/cssVars/useCurrentColorScheme.js +217 -0
- package/esm/index.js +2 -1
- package/esm/styleFunctionSx/extendSxProp.js +20 -1
- package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
- package/flexbox.js +1 -1
- package/getThemeValue.js +1 -1
- package/grid.js +1 -1
- package/index.d.ts +6 -0
- package/index.js +77 -68
- package/legacy/breakpoints.js +39 -8
- package/legacy/createBox.js +6 -3
- package/legacy/createStyled.js +5 -1
- package/legacy/createTheme/createBreakpoints.js +2 -2
- package/legacy/cssVars/createCssVarsProvider.js +215 -0
- package/legacy/cssVars/cssVarsParser.js +153 -0
- package/legacy/cssVars/getInitColorSchemeScript.js +27 -0
- package/legacy/cssVars/index.js +1 -0
- package/legacy/cssVars/useCurrentColorScheme.js +231 -0
- package/legacy/index.js +3 -2
- package/legacy/styleFunctionSx/extendSxProp.js +21 -1
- package/legacy/styleFunctionSx/styleFunctionSx.js +44 -34
- package/modern/breakpoints.js +39 -8
- package/modern/createBox.js +5 -3
- package/modern/createStyled.js +5 -1
- package/modern/createTheme/createBreakpoints.js +2 -2
- package/modern/cssVars/createCssVarsProvider.js +207 -0
- package/modern/cssVars/cssVarsParser.js +141 -0
- package/modern/cssVars/getInitColorSchemeScript.js +42 -0
- package/modern/cssVars/index.js +1 -0
- package/modern/cssVars/useCurrentColorScheme.js +217 -0
- package/modern/index.js +3 -2
- package/modern/styleFunctionSx/extendSxProp.js +20 -1
- package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
- package/package.json +8 -8
- package/palette.js +1 -1
- package/positions.js +1 -1
- package/sizing.js +1 -1
- package/spacing.js +3 -3
- package/style.d.ts +2 -2
- package/style.js +1 -1
- package/styleFunctionSx/extendSxProp.js +21 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +7 -1
- package/styleFunctionSx/styleFunctionSx.js +46 -36
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
- package/typography.js +1 -1
- package/useTheme.js +1 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = createCssVarsProvider;
|
|
9
|
+
|
|
10
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
|
+
|
|
12
|
+
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
13
|
+
|
|
14
|
+
var _utils = require("@mui/utils");
|
|
15
|
+
|
|
16
|
+
var React = _interopRequireWildcard(require("react"));
|
|
17
|
+
|
|
18
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
19
|
+
|
|
20
|
+
var _styledEngine = require("@mui/styled-engine");
|
|
21
|
+
|
|
22
|
+
var _cssVarsParser = _interopRequireDefault(require("./cssVarsParser"));
|
|
23
|
+
|
|
24
|
+
var _ThemeProvider = _interopRequireDefault(require("../ThemeProvider"));
|
|
25
|
+
|
|
26
|
+
var _getInitColorSchemeScript = _interopRequireWildcard(require("./getInitColorSchemeScript"));
|
|
27
|
+
|
|
28
|
+
var _useCurrentColorScheme = _interopRequireDefault(require("./useCurrentColorScheme"));
|
|
29
|
+
|
|
30
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
31
|
+
|
|
32
|
+
const _excluded = ["colorSchemes"],
|
|
33
|
+
_excluded2 = ["colorSchemes"];
|
|
34
|
+
|
|
35
|
+
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); }
|
|
36
|
+
|
|
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; }
|
|
38
|
+
|
|
39
|
+
function createCssVarsProvider(options) {
|
|
40
|
+
const {
|
|
41
|
+
theme: baseTheme = {},
|
|
42
|
+
defaultMode: desisgnSystemMode = 'light',
|
|
43
|
+
defaultColorScheme: designSystemColorScheme,
|
|
44
|
+
prefix: designSystemPrefix = '',
|
|
45
|
+
shouldSkipGeneratingVar
|
|
46
|
+
} = options;
|
|
47
|
+
|
|
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]) {
|
|
49
|
+
console.error(`MUI: \`${designSystemColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ColorSchemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
53
|
+
|
|
54
|
+
const useColorScheme = () => {
|
|
55
|
+
const value = React.useContext(ColorSchemeContext);
|
|
56
|
+
|
|
57
|
+
if (!value) {
|
|
58
|
+
throw new Error(process.env.NODE_ENV !== "production" ? `MUI: \`useColorScheme\` must be called under <CssVarsProvider />` : (0, _utils.formatMuiErrorMessage)(19));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return value;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function CssVarsProvider({
|
|
65
|
+
children,
|
|
66
|
+
theme: themeProp = {},
|
|
67
|
+
prefix = designSystemPrefix,
|
|
68
|
+
modeStorageKey = _getInitColorSchemeScript.DEFAULT_MODE_STORAGE_KEY,
|
|
69
|
+
attribute = _getInitColorSchemeScript.DEFAULT_ATTRIBUTE,
|
|
70
|
+
defaultMode = desisgnSystemMode,
|
|
71
|
+
defaultColorScheme = designSystemColorScheme
|
|
72
|
+
}) {
|
|
73
|
+
const {
|
|
74
|
+
colorSchemes: baseColorSchemes = {}
|
|
75
|
+
} = baseTheme,
|
|
76
|
+
restBaseTheme = (0, _objectWithoutPropertiesLoose2.default)(baseTheme, _excluded);
|
|
77
|
+
const {
|
|
78
|
+
colorSchemes: colorSchemesProp = {}
|
|
79
|
+
} = themeProp,
|
|
80
|
+
restThemeProp = (0, _objectWithoutPropertiesLoose2.default)(themeProp, _excluded2);
|
|
81
|
+
let mergedTheme = (0, _utils.deepmerge)(restBaseTheme, restThemeProp);
|
|
82
|
+
const colorSchemes = (0, _utils.deepmerge)(baseColorSchemes, colorSchemesProp);
|
|
83
|
+
const allColorSchemes = Object.keys(colorSchemes);
|
|
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
|
+
|
|
115
|
+
const {
|
|
116
|
+
css: rootCss,
|
|
117
|
+
vars: rootVars
|
|
118
|
+
} = (0, _cssVarsParser.default)(mergedTheme, {
|
|
119
|
+
prefix,
|
|
120
|
+
basePrefix: designSystemPrefix,
|
|
121
|
+
shouldSkipGeneratingVar
|
|
122
|
+
});
|
|
123
|
+
mergedTheme = (0, _extends2.default)({}, mergedTheme, colorSchemes[resolvedColorScheme], {
|
|
124
|
+
vars: rootVars
|
|
125
|
+
});
|
|
126
|
+
const styleSheet = {};
|
|
127
|
+
Object.entries(colorSchemes).forEach(([key, scheme]) => {
|
|
128
|
+
const {
|
|
129
|
+
css,
|
|
130
|
+
vars
|
|
131
|
+
} = (0, _cssVarsParser.default)(scheme, {
|
|
132
|
+
prefix,
|
|
133
|
+
basePrefix: designSystemPrefix,
|
|
134
|
+
shouldSkipGeneratingVar
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (key === resolvedColorScheme) {
|
|
138
|
+
mergedTheme.vars = (0, _extends2.default)({}, mergedTheme.vars, vars);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const resolvedDefaultColorScheme = (() => {
|
|
142
|
+
if (typeof defaultColorScheme === 'string') {
|
|
143
|
+
return defaultColorScheme;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (defaultMode === 'dark') {
|
|
147
|
+
return defaultColorScheme.dark;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return defaultColorScheme.light;
|
|
151
|
+
})();
|
|
152
|
+
|
|
153
|
+
if (key === resolvedDefaultColorScheme) {
|
|
154
|
+
styleSheet[':root'] = css;
|
|
155
|
+
} else {
|
|
156
|
+
styleSheet[`[${attribute}="${key}"]`] = css;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
React.useEffect(() => {
|
|
160
|
+
if (colorScheme) {
|
|
161
|
+
document.body.setAttribute(attribute, colorScheme);
|
|
162
|
+
}
|
|
163
|
+
}, [colorScheme, attribute]);
|
|
164
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(ColorSchemeContext.Provider, {
|
|
165
|
+
value: {
|
|
166
|
+
mode,
|
|
167
|
+
setMode,
|
|
168
|
+
lightColorScheme,
|
|
169
|
+
darkColorScheme,
|
|
170
|
+
colorScheme,
|
|
171
|
+
setColorScheme,
|
|
172
|
+
allColorSchemes
|
|
173
|
+
},
|
|
174
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
|
|
175
|
+
styles: {
|
|
176
|
+
':root': rootCss
|
|
177
|
+
}
|
|
178
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledEngine.GlobalStyles, {
|
|
179
|
+
styles: styleSheet
|
|
180
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ThemeProvider.default, {
|
|
181
|
+
theme: mergedTheme,
|
|
182
|
+
children: children
|
|
183
|
+
})]
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
process.env.NODE_ENV !== "production" ? CssVarsProvider.propTypes = {
|
|
188
|
+
/**
|
|
189
|
+
* The body attribute name to attach colorScheme.
|
|
190
|
+
*/
|
|
191
|
+
attribute: _propTypes.default.string,
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Your component tree.
|
|
195
|
+
*/
|
|
196
|
+
children: _propTypes.default.node,
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The initial color scheme used.
|
|
200
|
+
*/
|
|
201
|
+
defaultColorScheme: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]),
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The initial mode used.
|
|
205
|
+
*/
|
|
206
|
+
defaultMode: _propTypes.default.string,
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The key in the local storage used to store current color scheme.
|
|
210
|
+
*/
|
|
211
|
+
modeStorageKey: _propTypes.default.string,
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* css variable prefix
|
|
215
|
+
*/
|
|
216
|
+
prefix: _propTypes.default.string,
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The calculated theme object that will be passed through context.
|
|
220
|
+
*/
|
|
221
|
+
theme: _propTypes.default.object
|
|
222
|
+
} : void 0;
|
|
223
|
+
return {
|
|
224
|
+
CssVarsProvider,
|
|
225
|
+
useColorScheme,
|
|
226
|
+
getInitColorSchemeScript: _getInitColorSchemeScript.default
|
|
227
|
+
};
|
|
228
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +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, 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 {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.assignNestedKeys = void 0;
|
|
9
|
+
exports.default = cssVarsParser;
|
|
10
|
+
exports.walkObjectDeep = void 0;
|
|
11
|
+
|
|
12
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* This function create an object from keys, value and then assign to target
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} obj : the target object to be assigned
|
|
18
|
+
* @param {string[]} keys
|
|
19
|
+
* @param {string | number} value
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const source = {}
|
|
23
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
24
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
28
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
29
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
30
|
+
*/
|
|
31
|
+
const assignNestedKeys = (obj, keys, value) => {
|
|
32
|
+
let temp = obj;
|
|
33
|
+
keys.forEach((k, index) => {
|
|
34
|
+
if (index === keys.length - 1) {
|
|
35
|
+
if (temp && typeof temp === 'object') {
|
|
36
|
+
temp[k] = value;
|
|
37
|
+
}
|
|
38
|
+
} else if (temp && typeof temp === 'object') {
|
|
39
|
+
if (!temp[k]) {
|
|
40
|
+
temp[k] = {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
temp = temp[k];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param {Object} obj : source object
|
|
50
|
+
* @param {Function} callback : a function that will be called when
|
|
51
|
+
* - the deepest key in source object is reached
|
|
52
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
56
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
exports.assignNestedKeys = assignNestedKeys;
|
|
61
|
+
|
|
62
|
+
const walkObjectDeep = (obj, callback) => {
|
|
63
|
+
function recurse(object, parentKeys = []) {
|
|
64
|
+
Object.entries(object).forEach(([key, value]) => {
|
|
65
|
+
if (value !== undefined && value !== null) {
|
|
66
|
+
if (typeof value === 'object' && Object.keys(value).length > 0) {
|
|
67
|
+
recurse(value, [...parentKeys, key]);
|
|
68
|
+
} else {
|
|
69
|
+
callback([...parentKeys, key], value, object);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
recurse(obj);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
exports.walkObjectDeep = walkObjectDeep;
|
|
79
|
+
|
|
80
|
+
const getCssValue = (keys, value) => {
|
|
81
|
+
if (typeof value === 'number') {
|
|
82
|
+
if (['lineHeight', 'fontWeight', 'opacity', 'zIndex'].some(prop => keys.includes(prop))) {
|
|
83
|
+
// css property that are unitless
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return `${value}px`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return value;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* a function that parse theme and return { css, vars }
|
|
94
|
+
*
|
|
95
|
+
* @param {Object} theme
|
|
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
|
+
*
|
|
106
|
+
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const { css, vars } = parser({
|
|
110
|
+
* fontSize: 12,
|
|
111
|
+
* lineHeight: 1.2,
|
|
112
|
+
* palette: { primary: { 500: '#000000' } }
|
|
113
|
+
* })
|
|
114
|
+
*
|
|
115
|
+
* console.log(css) // { '--fontSize': '12px', '--lineHeight': 1.2, '--palette-primary-500': '#000000' }
|
|
116
|
+
* console.log(vars) // { fontSize: '--fontSize', lineHeight: '--lineHeight', palette: { primary: { 500: 'var(--palette-primary-500)' } } }
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
function cssVarsParser(theme, options) {
|
|
121
|
+
const clonedTheme = (0, _extends2.default)({}, theme);
|
|
122
|
+
delete clonedTheme.vars; // remove 'vars' from the structure
|
|
123
|
+
|
|
124
|
+
const {
|
|
125
|
+
prefix,
|
|
126
|
+
basePrefix = '',
|
|
127
|
+
shouldSkipGeneratingVar
|
|
128
|
+
} = options || {};
|
|
129
|
+
const css = {};
|
|
130
|
+
const vars = {};
|
|
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
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
css,
|
|
154
|
+
vars
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,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 default function getInitColorSchemeScript(options?: {
|
|
6
|
+
defaultMode?: 'light' | 'dark' | 'system';
|
|
7
|
+
defaultLightColorScheme?: string;
|
|
8
|
+
defaultDarkColorScheme?: string;
|
|
9
|
+
modeStorageKey?: string;
|
|
10
|
+
colorSchemeStorageKey?: string;
|
|
11
|
+
attribute?: string;
|
|
12
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DEFAULT_MODE_STORAGE_KEY = exports.DEFAULT_COLOR_SCHEME_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_MODE_STORAGE_KEY = 'mui-mode';
|
|
18
|
+
exports.DEFAULT_MODE_STORAGE_KEY = DEFAULT_MODE_STORAGE_KEY;
|
|
19
|
+
const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
|
|
20
|
+
exports.DEFAULT_COLOR_SCHEME_STORAGE_KEY = DEFAULT_COLOR_SCHEME_STORAGE_KEY;
|
|
21
|
+
const DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
22
|
+
exports.DEFAULT_ATTRIBUTE = DEFAULT_ATTRIBUTE;
|
|
23
|
+
|
|
24
|
+
function getInitColorSchemeScript(options) {
|
|
25
|
+
const {
|
|
26
|
+
defaultMode = 'light',
|
|
27
|
+
defaultLightColorScheme = 'light',
|
|
28
|
+
defaultDarkColorScheme = 'dark',
|
|
29
|
+
modeStorageKey = DEFAULT_MODE_STORAGE_KEY,
|
|
30
|
+
colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY,
|
|
31
|
+
attribute = DEFAULT_ATTRIBUTE
|
|
32
|
+
} = options || {};
|
|
33
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", {
|
|
34
|
+
// eslint-disable-next-line react/no-danger
|
|
35
|
+
dangerouslySetInnerHTML: {
|
|
36
|
+
__html: `(function() { try {
|
|
37
|
+
var mode = localStorage.getItem('${modeStorageKey}');
|
|
38
|
+
var colorScheme = '';
|
|
39
|
+
if (mode === 'system' || (!mode && ${defaultMode} === 'system')) {
|
|
40
|
+
// handle system mode
|
|
41
|
+
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
42
|
+
if (mql.matches) {
|
|
43
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultLightColorScheme};
|
|
44
|
+
} else {
|
|
45
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultDarkColorScheme};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (mode === 'light') {
|
|
49
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-light') || ${defaultLightColorScheme};
|
|
50
|
+
}
|
|
51
|
+
if (mode === 'dark') {
|
|
52
|
+
colorScheme = localStorage.getItem('${colorSchemeStorageKey}-dark') || ${defaultDarkColorScheme};
|
|
53
|
+
}
|
|
54
|
+
if (colorScheme) {
|
|
55
|
+
document.body.setAttribute('${attribute}', colorScheme);
|
|
56
|
+
}
|
|
57
|
+
} catch (e) {} })();`
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
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,50 @@
|
|
|
1
|
+
export declare type Mode = 'light' | 'dark' | 'system';
|
|
2
|
+
export declare type SystemMode = Exclude<Mode, 'system'>;
|
|
3
|
+
export interface State<SupportedColorScheme extends string> {
|
|
4
|
+
/**
|
|
5
|
+
* User selected mode.
|
|
6
|
+
* Note: on the server, mode is always undefined
|
|
7
|
+
*/
|
|
8
|
+
mode: Mode | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Only valid if `mode: 'system'`, either 'light' | 'dark'.
|
|
11
|
+
*/
|
|
12
|
+
systemMode: SystemMode | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The color scheme for the light mode.
|
|
15
|
+
*/
|
|
16
|
+
lightColorScheme: SupportedColorScheme;
|
|
17
|
+
/**
|
|
18
|
+
* The color scheme for the dark mode.
|
|
19
|
+
*/
|
|
20
|
+
darkColorScheme: SupportedColorScheme;
|
|
21
|
+
}
|
|
22
|
+
export declare type Result<SupportedColorScheme extends string> = State<SupportedColorScheme> & {
|
|
23
|
+
/**
|
|
24
|
+
* The current application color scheme. It is always `undefined` on the server.
|
|
25
|
+
*/
|
|
26
|
+
colorScheme: SupportedColorScheme | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* `mode` is saved to internal state and localStorage
|
|
29
|
+
* If `mode` is null, it will be reset to the defaultMode
|
|
30
|
+
*/
|
|
31
|
+
setMode: (mode: Mode | null) => void;
|
|
32
|
+
/**
|
|
33
|
+
* `colorScheme` is saved to internal state and localStorage
|
|
34
|
+
* If `colorScheme` is null, it will be reset to the defaultColorScheme (light | dark)
|
|
35
|
+
*/
|
|
36
|
+
setColorScheme: (colorScheme: SupportedColorScheme | Partial<{
|
|
37
|
+
light: SupportedColorScheme | null;
|
|
38
|
+
dark: SupportedColorScheme | null;
|
|
39
|
+
}> | null) => void;
|
|
40
|
+
};
|
|
41
|
+
export declare function getSystemMode(mode: undefined | string): SystemMode | undefined;
|
|
42
|
+
export declare function getColorScheme<SupportedColorScheme extends string>(state: State<SupportedColorScheme>): SupportedColorScheme | undefined;
|
|
43
|
+
export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: {
|
|
44
|
+
defaultLightColorScheme: SupportedColorScheme;
|
|
45
|
+
defaultDarkColorScheme: SupportedColorScheme;
|
|
46
|
+
supportedColorSchemes: Array<SupportedColorScheme>;
|
|
47
|
+
defaultMode?: Mode;
|
|
48
|
+
modeStorageKey?: string;
|
|
49
|
+
colorSchemeStorageKey?: string;
|
|
50
|
+
}): Result<SupportedColorScheme>;
|