@mui/system 5.0.6 → 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 +107 -0
- package/LICENSE +21 -21
- package/breakpoints.js +41 -8
- package/createBox.d.ts +5 -1
- package/createBox.js +5 -3
- package/createTheme/createBreakpoints.js +2 -2
- package/cssVars/createCssVarsProvider.d.ts +88 -38
- package/cssVars/createCssVarsProvider.js +83 -61
- package/cssVars/cssVarsParser.d.ts +14 -3
- package/cssVars/cssVarsParser.js +41 -11
- package/cssVars/getInitColorSchemeScript.d.ts +7 -2
- package/cssVars/getInitColorSchemeScript.js +27 -5
- package/cssVars/useCurrentColorScheme.d.ts +50 -0
- package/cssVars/useCurrentColorScheme.js +235 -0
- package/esm/breakpoints.js +39 -8
- package/esm/createBox.js +5 -3
- package/esm/createTheme/createBreakpoints.js +2 -2
- package/esm/cssVars/createCssVarsProvider.js +82 -63
- package/esm/cssVars/cssVarsParser.js +40 -11
- package/esm/cssVars/getInitColorSchemeScript.js +24 -3
- package/esm/cssVars/useCurrentColorScheme.js +217 -0
- package/esm/styleFunctionSx/extendSxProp.js +20 -1
- package/esm/styleFunctionSx/styleFunctionSx.js +45 -35
- package/index.js +1 -1
- package/legacy/breakpoints.js +39 -8
- package/legacy/createBox.js +6 -3
- package/legacy/createTheme/createBreakpoints.js +2 -2
- package/legacy/cssVars/createCssVarsProvider.js +83 -70
- package/legacy/cssVars/cssVarsParser.js +37 -9
- package/legacy/cssVars/getInitColorSchemeScript.js +13 -4
- package/legacy/cssVars/useCurrentColorScheme.js +231 -0
- package/legacy/index.js +1 -1
- 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/createTheme/createBreakpoints.js +2 -2
- package/modern/cssVars/createCssVarsProvider.js +82 -63
- package/modern/cssVars/cssVarsParser.js +40 -11
- package/modern/cssVars/getInitColorSchemeScript.js +24 -3
- package/modern/cssVars/useCurrentColorScheme.js +217 -0
- package/modern/index.js +1 -1
- package/modern/styleFunctionSx/extendSxProp.js +20 -1
- package/modern/styleFunctionSx/styleFunctionSx.js +45 -35
- package/package.json +7 -7
- package/styleFunctionSx/extendSxProp.js +21 -1
- package/styleFunctionSx/styleFunctionSx.d.ts +2 -1
- package/styleFunctionSx/styleFunctionSx.js +46 -36
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -0
|
@@ -1,43 +1,29 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
2
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
3
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
4
5
|
import { formatMuiErrorMessage as _formatMuiErrorMessage } from "@mui/utils";
|
|
5
6
|
import * as React from 'react';
|
|
6
7
|
import PropTypes from 'prop-types';
|
|
7
8
|
import { GlobalStyles } from '@mui/styled-engine';
|
|
8
9
|
import { deepmerge } from '@mui/utils';
|
|
9
10
|
import cssVarsParser from './cssVarsParser';
|
|
10
|
-
import
|
|
11
|
+
import ThemeProvider from '../ThemeProvider';
|
|
12
|
+
import getInitColorSchemeScript, { DEFAULT_ATTRIBUTE, DEFAULT_MODE_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
13
|
+
import useCurrentColorScheme from './useCurrentColorScheme';
|
|
11
14
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
15
|
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) {
|
|
16
|
+
export default function createCssVarsProvider(options) {
|
|
34
17
|
var _options$theme = options.theme,
|
|
35
18
|
baseTheme = _options$theme === void 0 ? {} : _options$theme,
|
|
19
|
+
_options$defaultMode = options.defaultMode,
|
|
20
|
+
desisgnSystemMode = _options$defaultMode === void 0 ? 'light' : _options$defaultMode,
|
|
36
21
|
designSystemColorScheme = options.defaultColorScheme,
|
|
37
22
|
_options$prefix = options.prefix,
|
|
38
|
-
designSystemPrefix = _options$prefix === void 0 ? '' : _options$prefix
|
|
23
|
+
designSystemPrefix = _options$prefix === void 0 ? '' : _options$prefix,
|
|
24
|
+
shouldSkipGeneratingVar = options.shouldSkipGeneratingVar;
|
|
39
25
|
|
|
40
|
-
if (!baseTheme.colorSchemes || !baseTheme.colorSchemes[designSystemColorScheme]) {
|
|
26
|
+
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]) {
|
|
41
27
|
console.error("MUI: `".concat(designSystemColorScheme, "` does not exist in `theme.colorSchemes`."));
|
|
42
28
|
}
|
|
43
29
|
|
|
@@ -59,10 +45,12 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
59
45
|
themeProp = _ref$theme === void 0 ? {} : _ref$theme,
|
|
60
46
|
_ref$prefix = _ref.prefix,
|
|
61
47
|
prefix = _ref$prefix === void 0 ? designSystemPrefix : _ref$prefix,
|
|
62
|
-
_ref$
|
|
63
|
-
|
|
48
|
+
_ref$modeStorageKey = _ref.modeStorageKey,
|
|
49
|
+
modeStorageKey = _ref$modeStorageKey === void 0 ? DEFAULT_MODE_STORAGE_KEY : _ref$modeStorageKey,
|
|
64
50
|
_ref$attribute = _ref.attribute,
|
|
65
51
|
attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute,
|
|
52
|
+
_ref$defaultMode = _ref.defaultMode,
|
|
53
|
+
defaultMode = _ref$defaultMode === void 0 ? desisgnSystemMode : _ref$defaultMode,
|
|
66
54
|
_ref$defaultColorSche = _ref.defaultColorScheme,
|
|
67
55
|
defaultColorScheme = _ref$defaultColorSche === void 0 ? designSystemColorScheme : _ref$defaultColorSche;
|
|
68
56
|
|
|
@@ -77,18 +65,41 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
77
65
|
var mergedTheme = deepmerge(restBaseTheme, restThemeProp);
|
|
78
66
|
var colorSchemes = deepmerge(baseColorSchemes, colorSchemesProp);
|
|
79
67
|
var allColorSchemes = Object.keys(colorSchemes);
|
|
80
|
-
var
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
68
|
+
var defaultLightColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.light;
|
|
69
|
+
var defaultDarkColorScheme = typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
|
|
70
|
+
|
|
71
|
+
var _useCurrentColorSchem = useCurrentColorScheme({
|
|
72
|
+
supportedColorSchemes: allColorSchemes,
|
|
73
|
+
defaultLightColorScheme: defaultLightColorScheme,
|
|
74
|
+
defaultDarkColorScheme: defaultDarkColorScheme,
|
|
75
|
+
modeStorageKey: modeStorageKey,
|
|
76
|
+
defaultMode: defaultMode
|
|
84
77
|
}),
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
mode = _useCurrentColorSchem.mode,
|
|
79
|
+
setMode = _useCurrentColorSchem.setMode,
|
|
80
|
+
lightColorScheme = _useCurrentColorSchem.lightColorScheme,
|
|
81
|
+
darkColorScheme = _useCurrentColorSchem.darkColorScheme,
|
|
82
|
+
colorScheme = _useCurrentColorSchem.colorScheme,
|
|
83
|
+
setColorScheme = _useCurrentColorSchem.setColorScheme;
|
|
84
|
+
|
|
85
|
+
var resolvedColorScheme = function () {
|
|
86
|
+
if (!colorScheme) {
|
|
87
|
+
// This scope occurs on the server
|
|
88
|
+
if (defaultMode === 'dark') {
|
|
89
|
+
return defaultDarkColorScheme;
|
|
90
|
+
} // use light color scheme, if default mode is 'light' | 'auto'
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
return defaultLightColorScheme;
|
|
94
|
+
}
|
|
87
95
|
|
|
88
|
-
|
|
96
|
+
return colorScheme;
|
|
97
|
+
}();
|
|
89
98
|
|
|
90
99
|
var _cssVarsParser = cssVarsParser(mergedTheme, {
|
|
91
|
-
prefix: prefix
|
|
100
|
+
prefix: prefix,
|
|
101
|
+
basePrefix: designSystemPrefix,
|
|
102
|
+
shouldSkipGeneratingVar: shouldSkipGeneratingVar
|
|
92
103
|
}),
|
|
93
104
|
rootCss = _cssVarsParser.css,
|
|
94
105
|
rootVars = _cssVarsParser.vars;
|
|
@@ -103,7 +114,9 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
103
114
|
scheme = _ref3[1];
|
|
104
115
|
|
|
105
116
|
var _cssVarsParser2 = cssVarsParser(scheme, {
|
|
106
|
-
prefix: prefix
|
|
117
|
+
prefix: prefix,
|
|
118
|
+
basePrefix: designSystemPrefix,
|
|
119
|
+
shouldSkipGeneratingVar: shouldSkipGeneratingVar
|
|
107
120
|
}),
|
|
108
121
|
css = _cssVarsParser2.css,
|
|
109
122
|
vars = _cssVarsParser2.vars;
|
|
@@ -112,8 +125,20 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
112
125
|
mergedTheme.vars = _extends({}, mergedTheme.vars, vars);
|
|
113
126
|
}
|
|
114
127
|
|
|
115
|
-
|
|
116
|
-
|
|
128
|
+
var resolvedDefaultColorScheme = function () {
|
|
129
|
+
if (typeof defaultColorScheme === 'string') {
|
|
130
|
+
return defaultColorScheme;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (defaultMode === 'dark') {
|
|
134
|
+
return defaultColorScheme.dark;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return defaultColorScheme.light;
|
|
138
|
+
}();
|
|
139
|
+
|
|
140
|
+
if (key === resolvedDefaultColorScheme) {
|
|
141
|
+
styleSheet[':root'] = css;
|
|
117
142
|
} else {
|
|
118
143
|
styleSheet["[".concat(attribute, "=\"").concat(key, "\"]")] = css;
|
|
119
144
|
}
|
|
@@ -121,43 +146,26 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
121
146
|
React.useEffect(function () {
|
|
122
147
|
if (colorScheme) {
|
|
123
148
|
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
149
|
}
|
|
150
|
-
}, [
|
|
150
|
+
}, [colorScheme, attribute]);
|
|
151
151
|
return /*#__PURE__*/_jsxs(ColorSchemeContext.Provider, {
|
|
152
152
|
value: {
|
|
153
|
+
mode: mode,
|
|
154
|
+
setMode: setMode,
|
|
155
|
+
lightColorScheme: lightColorScheme,
|
|
156
|
+
darkColorScheme: darkColorScheme,
|
|
153
157
|
colorScheme: colorScheme,
|
|
154
|
-
setColorScheme:
|
|
158
|
+
setColorScheme: setColorScheme,
|
|
155
159
|
allColorSchemes: allColorSchemes
|
|
156
160
|
},
|
|
157
161
|
children: [/*#__PURE__*/_jsx(GlobalStyles, {
|
|
162
|
+
styles: {
|
|
163
|
+
':root': rootCss
|
|
164
|
+
}
|
|
165
|
+
}), /*#__PURE__*/_jsx(GlobalStyles, {
|
|
158
166
|
styles: styleSheet
|
|
159
|
-
}), /*#__PURE__*/_jsx(
|
|
160
|
-
|
|
167
|
+
}), /*#__PURE__*/_jsx(ThemeProvider, {
|
|
168
|
+
theme: mergedTheme,
|
|
161
169
|
children: children
|
|
162
170
|
})]
|
|
163
171
|
});
|
|
@@ -177,17 +185,22 @@ export default function createCssVarsProvider(ThemeContext, options) {
|
|
|
177
185
|
/**
|
|
178
186
|
* The initial color scheme used.
|
|
179
187
|
*/
|
|
180
|
-
defaultColorScheme: PropTypes.string,
|
|
188
|
+
defaultColorScheme: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
181
189
|
|
|
182
190
|
/**
|
|
183
|
-
*
|
|
191
|
+
* The initial mode used.
|
|
184
192
|
*/
|
|
185
|
-
|
|
193
|
+
defaultMode: PropTypes.string,
|
|
186
194
|
|
|
187
195
|
/**
|
|
188
196
|
* The key in the local storage used to store current color scheme.
|
|
189
197
|
*/
|
|
190
|
-
|
|
198
|
+
modeStorageKey: PropTypes.string,
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* css variable prefix
|
|
202
|
+
*/
|
|
203
|
+
prefix: PropTypes.string,
|
|
191
204
|
|
|
192
205
|
/**
|
|
193
206
|
* The calculated theme object that will be passed through context.
|
|
@@ -61,7 +61,7 @@ export var walkObjectDeep = function walkObjectDeep(obj, callback) {
|
|
|
61
61
|
if (_typeof(value) === 'object' && Object.keys(value).length > 0) {
|
|
62
62
|
recurse(value, [].concat(_toConsumableArray(parentKeys), [key]));
|
|
63
63
|
} else {
|
|
64
|
-
callback([].concat(_toConsumableArray(parentKeys), [key]), value);
|
|
64
|
+
callback([].concat(_toConsumableArray(parentKeys), [key]), value, object);
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
});
|
|
@@ -88,7 +88,16 @@ var getCssValue = function getCssValue(keys, value) {
|
|
|
88
88
|
* a function that parse theme and return { css, vars }
|
|
89
89
|
*
|
|
90
90
|
* @param {Object} theme
|
|
91
|
-
* @param {{
|
|
91
|
+
* @param {{
|
|
92
|
+
* prefix?: string,
|
|
93
|
+
* basePrefix?: string,
|
|
94
|
+
* shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
|
|
95
|
+
* }} options.
|
|
96
|
+
* `basePrefix`: defined by design system.
|
|
97
|
+
* `prefix`: defined by application
|
|
98
|
+
*
|
|
99
|
+
* This function also mutate the string value of theme input by replacing `basePrefix` (if existed) with `prefix`
|
|
100
|
+
*
|
|
92
101
|
* @returns {{ css: Object, vars: Object }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme)
|
|
93
102
|
*
|
|
94
103
|
* @example
|
|
@@ -103,19 +112,38 @@ var getCssValue = function getCssValue(keys, value) {
|
|
|
103
112
|
*/
|
|
104
113
|
|
|
105
114
|
|
|
106
|
-
export default function cssVarsParser(
|
|
115
|
+
export default function cssVarsParser(theme, options) {
|
|
116
|
+
var clonedTheme = _extends({}, theme);
|
|
117
|
+
|
|
118
|
+
delete clonedTheme.vars; // remove 'vars' from the structure
|
|
119
|
+
|
|
107
120
|
var _ref3 = options || {},
|
|
108
|
-
prefix = _ref3.prefix
|
|
121
|
+
prefix = _ref3.prefix,
|
|
122
|
+
_ref3$basePrefix = _ref3.basePrefix,
|
|
123
|
+
basePrefix = _ref3$basePrefix === void 0 ? '' : _ref3$basePrefix,
|
|
124
|
+
shouldSkipGeneratingVar = _ref3.shouldSkipGeneratingVar;
|
|
109
125
|
|
|
110
126
|
var css = {};
|
|
111
127
|
var vars = {};
|
|
112
|
-
walkObjectDeep(
|
|
113
|
-
if (typeof
|
|
114
|
-
var
|
|
128
|
+
walkObjectDeep(clonedTheme, function (keys, val, scope) {
|
|
129
|
+
if (typeof val === 'string' || typeof val === 'number') {
|
|
130
|
+
var _value = val;
|
|
115
131
|
|
|
116
|
-
|
|
132
|
+
if (typeof _value === 'string' && _value.startsWith('var')) {
|
|
133
|
+
// replace the value of the `scope` object with the prefix or remove basePrefix from the value
|
|
134
|
+
_value = prefix ? _value.replace(basePrefix, prefix) : _value.replace("".concat(basePrefix, "-"), ''); // scope is the deepest object in the tree, keys is the theme path keys
|
|
117
135
|
|
|
118
|
-
|
|
136
|
+
scope[keys.slice(-1)[0]] = _value;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!shouldSkipGeneratingVar || shouldSkipGeneratingVar && !shouldSkipGeneratingVar(keys, _value)) {
|
|
140
|
+
// only create css & var if `shouldSkipGeneratingVar` return false
|
|
141
|
+
var cssVar = "--".concat(prefix ? "".concat(prefix, "-") : '').concat(keys.join('-'));
|
|
142
|
+
|
|
143
|
+
_extends(css, _defineProperty({}, cssVar, getCssValue(keys, _value)));
|
|
144
|
+
|
|
145
|
+
assignNestedKeys(vars, keys, "var(".concat(cssVar, ")"));
|
|
146
|
+
}
|
|
119
147
|
}
|
|
120
148
|
});
|
|
121
149
|
return {
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
export var
|
|
3
|
+
export var DEFAULT_MODE_STORAGE_KEY = 'mui-mode';
|
|
4
|
+
export var DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'mui-color-scheme';
|
|
4
5
|
export var DEFAULT_ATTRIBUTE = 'data-mui-color-scheme';
|
|
5
6
|
export default function getInitColorSchemeScript(options) {
|
|
6
7
|
var _ref = options || {},
|
|
7
|
-
_ref$
|
|
8
|
-
|
|
8
|
+
_ref$defaultMode = _ref.defaultMode,
|
|
9
|
+
defaultMode = _ref$defaultMode === void 0 ? 'light' : _ref$defaultMode,
|
|
10
|
+
_ref$defaultLightColo = _ref.defaultLightColorScheme,
|
|
11
|
+
defaultLightColorScheme = _ref$defaultLightColo === void 0 ? 'light' : _ref$defaultLightColo,
|
|
12
|
+
_ref$defaultDarkColor = _ref.defaultDarkColorScheme,
|
|
13
|
+
defaultDarkColorScheme = _ref$defaultDarkColor === void 0 ? 'dark' : _ref$defaultDarkColor,
|
|
14
|
+
_ref$modeStorageKey = _ref.modeStorageKey,
|
|
15
|
+
modeStorageKey = _ref$modeStorageKey === void 0 ? DEFAULT_MODE_STORAGE_KEY : _ref$modeStorageKey,
|
|
16
|
+
_ref$colorSchemeStora = _ref.colorSchemeStorageKey,
|
|
17
|
+
colorSchemeStorageKey = _ref$colorSchemeStora === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _ref$colorSchemeStora,
|
|
9
18
|
_ref$attribute = _ref.attribute,
|
|
10
19
|
attribute = _ref$attribute === void 0 ? DEFAULT_ATTRIBUTE : _ref$attribute;
|
|
11
20
|
|
|
12
21
|
return /*#__PURE__*/_jsx("script", {
|
|
13
22
|
// eslint-disable-next-line react/no-danger
|
|
14
23
|
dangerouslySetInnerHTML: {
|
|
15
|
-
__html: "(function() { try {\n var colorScheme = localStorage.getItem('".concat(
|
|
24
|
+
__html: "(function() { try {\n var mode = localStorage.getItem('".concat(modeStorageKey, "');\n var colorScheme = '';\n if (mode === 'system' || (!mode && ").concat(defaultMode, " === 'system')) {\n // handle system mode\n var mql = window.matchMedia('(prefers-color-scheme: dark)');\n if (mql.matches) {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-dark') || ").concat(defaultLightColorScheme, ";\n } else {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || ").concat(defaultDarkColorScheme, ";\n }\n }\n if (mode === 'light') {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-light') || ").concat(defaultLightColorScheme, ";\n }\n if (mode === 'dark') {\n colorScheme = localStorage.getItem('").concat(colorSchemeStorageKey, "-dark') || ").concat(defaultDarkColorScheme, ";\n }\n if (colorScheme) {\n document.body.setAttribute('").concat(attribute, "', colorScheme);\n }\n } catch (e) {} })();")
|
|
16
25
|
}
|
|
17
26
|
});
|
|
18
27
|
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { DEFAULT_MODE_STORAGE_KEY, DEFAULT_COLOR_SCHEME_STORAGE_KEY } from './getInitColorSchemeScript';
|
|
4
|
+
export function getSystemMode(mode) {
|
|
5
|
+
if (typeof window !== 'undefined' && mode === 'system') {
|
|
6
|
+
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
7
|
+
|
|
8
|
+
if (mql.matches) {
|
|
9
|
+
return 'dark';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return 'light';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function processState(state, callback) {
|
|
19
|
+
if (state.mode === 'light' || state.mode === 'system' && state.systemMode === 'light') {
|
|
20
|
+
return callback('light');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (state.mode === 'dark' || state.mode === 'system' && state.systemMode === 'dark') {
|
|
24
|
+
return callback('dark');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getColorScheme(state) {
|
|
31
|
+
return processState(state, function (mode) {
|
|
32
|
+
if (mode === 'light') {
|
|
33
|
+
return state.lightColorScheme;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (mode === 'dark') {
|
|
37
|
+
return state.darkColorScheme;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return undefined;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function resolveValue(key, defaultValue) {
|
|
45
|
+
if (typeof window === 'undefined') {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var value;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
value = localStorage.getItem(key) || undefined;
|
|
53
|
+
} catch (e) {// Unsupported
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return value || defaultValue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default function useCurrentColorScheme(options) {
|
|
60
|
+
var _options$defaultMode = options.defaultMode,
|
|
61
|
+
defaultMode = _options$defaultMode === void 0 ? 'light' : _options$defaultMode,
|
|
62
|
+
defaultLightColorScheme = options.defaultLightColorScheme,
|
|
63
|
+
defaultDarkColorScheme = options.defaultDarkColorScheme,
|
|
64
|
+
_options$supportedCol = options.supportedColorSchemes,
|
|
65
|
+
supportedColorSchemes = _options$supportedCol === void 0 ? [] : _options$supportedCol,
|
|
66
|
+
_options$modeStorageK = options.modeStorageKey,
|
|
67
|
+
modeStorageKey = _options$modeStorageK === void 0 ? DEFAULT_MODE_STORAGE_KEY : _options$modeStorageK,
|
|
68
|
+
_options$colorSchemeS = options.colorSchemeStorageKey,
|
|
69
|
+
colorSchemeStorageKey = _options$colorSchemeS === void 0 ? DEFAULT_COLOR_SCHEME_STORAGE_KEY : _options$colorSchemeS;
|
|
70
|
+
var joinedColorSchemes = supportedColorSchemes.join(',');
|
|
71
|
+
|
|
72
|
+
var _React$useState = React.useState(function () {
|
|
73
|
+
var initialMode = resolveValue(modeStorageKey, defaultMode);
|
|
74
|
+
return {
|
|
75
|
+
mode: initialMode,
|
|
76
|
+
systemMode: getSystemMode(initialMode),
|
|
77
|
+
lightColorScheme: resolveValue("".concat(colorSchemeStorageKey, "-light")) || defaultLightColorScheme,
|
|
78
|
+
darkColorScheme: resolveValue("".concat(colorSchemeStorageKey, "-dark")) || defaultDarkColorScheme
|
|
79
|
+
};
|
|
80
|
+
}),
|
|
81
|
+
state = _React$useState[0],
|
|
82
|
+
setState = _React$useState[1];
|
|
83
|
+
|
|
84
|
+
var colorScheme = getColorScheme(state);
|
|
85
|
+
var setMode = React.useCallback(function (mode) {
|
|
86
|
+
setState(function (currentState) {
|
|
87
|
+
var newMode = !mode ? defaultMode : mode;
|
|
88
|
+
|
|
89
|
+
if (typeof localStorage !== 'undefined') {
|
|
90
|
+
localStorage.setItem(modeStorageKey, newMode);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return _extends({}, currentState, {
|
|
94
|
+
mode: newMode,
|
|
95
|
+
systemMode: getSystemMode(newMode)
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}, [modeStorageKey, defaultMode]);
|
|
99
|
+
var setColorScheme = React.useCallback(function (value) {
|
|
100
|
+
if (!value || typeof value === 'string') {
|
|
101
|
+
if (value && !supportedColorSchemes.includes(value)) {
|
|
102
|
+
console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
|
|
103
|
+
} else {
|
|
104
|
+
setState(function (currentState) {
|
|
105
|
+
var newState = _extends({}, currentState);
|
|
106
|
+
|
|
107
|
+
if (!value) {
|
|
108
|
+
// reset to default color scheme
|
|
109
|
+
newState.lightColorScheme = defaultLightColorScheme;
|
|
110
|
+
newState.darkColorScheme = defaultDarkColorScheme;
|
|
111
|
+
return newState;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
processState(currentState, function (mode) {
|
|
115
|
+
localStorage.setItem("".concat(colorSchemeStorageKey, "-").concat(mode), value);
|
|
116
|
+
|
|
117
|
+
if (mode === 'light') {
|
|
118
|
+
newState.lightColorScheme = value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (mode === 'dark') {
|
|
122
|
+
newState.darkColorScheme = value;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return newState;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
} else if (value.light && !supportedColorSchemes.includes(value.light) || value.dark && !supportedColorSchemes.includes(value.dark)) {
|
|
129
|
+
console.error("`".concat(value, "` does not exist in `theme.colorSchemes`."));
|
|
130
|
+
} else {
|
|
131
|
+
setState(function (currentState) {
|
|
132
|
+
var newState = _extends({}, currentState);
|
|
133
|
+
|
|
134
|
+
if (value.light || value.light === null) {
|
|
135
|
+
newState.lightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (value.dark || value.dark === null) {
|
|
139
|
+
newState.darkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return newState;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (value.light) {
|
|
146
|
+
localStorage.setItem("".concat(colorSchemeStorageKey, "-light"), value.light);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (value.dark) {
|
|
150
|
+
localStorage.setItem("".concat(colorSchemeStorageKey, "-dark"), value.dark);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}, [colorSchemeStorageKey, supportedColorSchemes, defaultLightColorScheme, defaultDarkColorScheme]);
|
|
154
|
+
var handleMediaQuery = React.useCallback(function (e) {
|
|
155
|
+
if (state.mode === 'system') {
|
|
156
|
+
setState(function (currentState) {
|
|
157
|
+
return _extends({}, currentState, {
|
|
158
|
+
systemMode: e.matches ? 'dark' : 'light'
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}, [state.mode]); // Ref hack to avoid adding handleMediaQuery as a dep
|
|
163
|
+
|
|
164
|
+
var mediaListener = React.useRef(handleMediaQuery);
|
|
165
|
+
mediaListener.current = handleMediaQuery;
|
|
166
|
+
React.useEffect(function () {
|
|
167
|
+
var handler = function handler() {
|
|
168
|
+
return mediaListener.current.apply(mediaListener, arguments);
|
|
169
|
+
}; // Always listen to System preference
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
var media = window.matchMedia('(prefers-color-scheme: dark)'); // Intentionally use deprecated listener methods to support iOS & old browsers
|
|
173
|
+
|
|
174
|
+
media.addListener(handler);
|
|
175
|
+
handler(media);
|
|
176
|
+
return function () {
|
|
177
|
+
return media.removeListener(handler);
|
|
178
|
+
};
|
|
179
|
+
}, []); // Save mode, lightColorScheme & darkColorScheme to localStorage
|
|
180
|
+
|
|
181
|
+
React.useEffect(function () {
|
|
182
|
+
if (state.mode) {
|
|
183
|
+
localStorage.setItem(modeStorageKey, state.mode);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
processState(state, function (mode) {
|
|
187
|
+
if (mode === 'light') {
|
|
188
|
+
localStorage.setItem("".concat(colorSchemeStorageKey, "-light"), state.lightColorScheme);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (mode === 'dark') {
|
|
192
|
+
localStorage.setItem("".concat(colorSchemeStorageKey, "-dark"), state.darkColorScheme);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
|
|
196
|
+
|
|
197
|
+
React.useEffect(function () {
|
|
198
|
+
var handleStorage = function handleStorage(event) {
|
|
199
|
+
var value = event.newValue;
|
|
200
|
+
|
|
201
|
+
if (typeof event.key === 'string' && event.key.startsWith(colorSchemeStorageKey) && (!value || joinedColorSchemes.match(value))) {
|
|
202
|
+
// If the key is deleted, value will be null then reset color scheme to the default one.
|
|
203
|
+
if (event.key.endsWith('light')) {
|
|
204
|
+
setColorScheme({
|
|
205
|
+
light: value
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (event.key.endsWith('dark')) {
|
|
210
|
+
setColorScheme({
|
|
211
|
+
dark: value
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (event.key === modeStorageKey && (!value || ['light', 'dark', 'system'].includes(value))) {
|
|
217
|
+
setMode(value || defaultMode);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
window.addEventListener('storage', handleStorage);
|
|
222
|
+
return function () {
|
|
223
|
+
return window.removeEventListener('storage', handleStorage);
|
|
224
|
+
};
|
|
225
|
+
}, [setColorScheme, setMode, modeStorageKey, colorSchemeStorageKey, joinedColorSchemes, defaultMode]);
|
|
226
|
+
return _extends({}, state, {
|
|
227
|
+
colorScheme: colorScheme,
|
|
228
|
+
setMode: setMode,
|
|
229
|
+
setColorScheme: setColorScheme
|
|
230
|
+
});
|
|
231
|
+
}
|
package/legacy/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
+
import { isPlainObject } from '@mui/utils';
|
|
3
5
|
import { propToStyleFunction } from '../getThemeValue';
|
|
4
6
|
|
|
5
7
|
var splitProps = function splitProps(props) {
|
|
@@ -25,7 +27,25 @@ export default function extendSxProp(props) {
|
|
|
25
27
|
systemProps = _splitProps.systemProps,
|
|
26
28
|
otherProps = _splitProps.otherProps;
|
|
27
29
|
|
|
30
|
+
var finalSx;
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(inSx)) {
|
|
33
|
+
finalSx = [systemProps].concat(_toConsumableArray(inSx));
|
|
34
|
+
} else if (typeof inSx === 'function') {
|
|
35
|
+
finalSx = function finalSx() {
|
|
36
|
+
var result = inSx.apply(void 0, arguments);
|
|
37
|
+
|
|
38
|
+
if (!isPlainObject(result)) {
|
|
39
|
+
return systemProps;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return _extends({}, systemProps, result);
|
|
43
|
+
};
|
|
44
|
+
} else {
|
|
45
|
+
finalSx = _extends({}, systemProps, inSx);
|
|
46
|
+
}
|
|
47
|
+
|
|
28
48
|
return _extends({}, otherProps, {
|
|
29
|
-
sx:
|
|
49
|
+
sx: finalSx
|
|
30
50
|
});
|
|
31
51
|
}
|